From 7d907666723dfde8141f0e96cb69e0584106c841 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 5 Dec 2026 16:36:32 +0000 Subject: [PATCH] Gradle cn1Verify task, the twin of cn1:verify `mvn cn1:verify` checks the first screen in the hidden simulator (light and dark, portrait and landscape, largest text) and fails on errors; a Gradle project had no equivalent. cn1Verify starts the same simulator with CN1_VERIFY_DIR set to build/cn1-verify, on an MCP port of its own and without touching cn1Agent's port file, then reads report.json and fails on errors unless -Pcn1.verify.failOnError=false. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01SZMcbmyfhDCp3DQ3U1YvWh --- .../developer-guide/MCP-Headless-API.asciidoc | 2 +- .../java/com/codename1/gradle/AppSupport.java | 104 +++++++++++++++++- .../codename1/gradle/PluginHelpersTest.java | 13 +++ 3 files changed, 116 insertions(+), 3 deletions(-) diff --git a/docs/developer-guide/MCP-Headless-API.asciidoc b/docs/developer-guide/MCP-Headless-API.asciidoc index b4244025c6c..b7dcc955a5f 100644 --- a/docs/developer-guide/MCP-Headless-API.asciidoc +++ b/docs/developer-guide/MCP-Headless-API.asciidoc @@ -143,7 +143,7 @@ In the simulator, `sim_record_film` films an animation the same way: called righ The largest-text condition is skipped, and says so, when the application hasn't opted into larger text, because the setting would change nothing. The tool writes `report.json`, `report.html` and the screenshots into the directory, puts every setting back and leaves the clock as it found it. -`mvn cn1:verify` runs the same check without an agent: it starts the simulator hidden, verifies the first form, writes `common/target/cn1-verify/report.html` and fails the build when the report has an error. Pass `-Dcn1.verify.failOnError=false` to only write the report. To check the other screens, navigate to each one in `cn1:agent` and call `sim_verify` there. +`mvn cn1:verify` runs the same check without an agent: it starts the simulator hidden, verifies the first form, writes `common/target/cn1-verify/report.html` and fails the build when the report has an error. Pass `-Dcn1.verify.failOnError=false` to only write the report. In a Gradle project, `./gradlew cn1Verify` does the same, writes `build/cn1-verify/report.html` and takes `-Pcn1.verify.failOnError=false`; on Linux with no display, run it under `xvfb-run -a`. To check the other screens, navigate to each one in `cn1:agent` and call `sim_verify` there. === Reporting friction diff --git a/maven/codenameone-gradle-plugin/src/main/java/com/codename1/gradle/AppSupport.java b/maven/codenameone-gradle-plugin/src/main/java/com/codename1/gradle/AppSupport.java index a3034830d34..6e055cade11 100644 --- a/maven/codenameone-gradle-plugin/src/main/java/com/codename1/gradle/AppSupport.java +++ b/maven/codenameone-gradle-plugin/src/main/java/com/codename1/gradle/AppSupport.java @@ -585,6 +585,26 @@ static void apply(final Project project, final ProjectLayout layout, final Coden t.doLast(new DeletePortFile(agentPortFile)); }); + // cn1:verify's twin: the simulator hidden, the first screen checked in light and + // dark, portrait and landscape and at the largest text size, a report written to + // build/cn1-verify. Like cn1Agent it needs a display (xvfb-run -a on Linux). + registerSimulator(project, VerifyEnvironment.TASK, "Checks the app's first screen in the hidden simulator" + + " and writes build/cn1-verify/report.html; fails on errors unless" + + " -Pcn1.verify.failOnError=false", false, + layout, main, javase, simulator, prepare, css, ext, userProperties); + final File verifyDir = new File(layout.buildDir(), VerifyEnvironment.REPORT_DIR); + final Provider verifyFailOnError = project.getProviders() + .gradleProperty(VerifyEnvironment.FAIL_ON_ERROR_PROPERTY).map(Boolean::parseBoolean) + .orElse(Boolean.TRUE); + project.getTasks().named(VerifyEnvironment.TASK, JavaExec.class).configure( + t -> { + // The report, not the exit status, says what went wrong. + t.setIgnoreExitValue(true); + t.doFirst(new VerifyEnvironment(verifyDir)); + t.doLast(new VerifyReport(verifyDir, verifyFailOnError)); + t.getOutputs().upToDateWhen(x -> false); + }); + project.getTasks().register("generateNativeInterfaces", NativeInterfacesTask.class, t -> { nativeCommon(t, project, layout, ext, userProperties, main); @@ -1540,7 +1560,7 @@ public void execute(org.gradle.api.Task task) { task.getLogger().lifecycle("[cn1.mcp] agent mode: the simulator will serve MCP on 127.0.0.1:" + port); } - private static boolean portFree(int port) { + static boolean portFree(int port) { try (java.net.ServerSocket s = new java.net.ServerSocket(port, 1, java.net.InetAddress.getByName("127.0.0.1"))) { return s.getLocalPort() == port; @@ -1549,7 +1569,7 @@ private static boolean portFree(int port) { } } - private static int freePort() { + static int freePort() { try (java.net.ServerSocket s = new java.net.ServerSocket(0, 1, java.net.InetAddress.getByName("127.0.0.1"))) { return s.getLocalPort(); @@ -1559,6 +1579,86 @@ private static int freePort() { } } + /// Starts cn1Verify's simulator hidden, in agent mode on a port of its own (it + /// may run beside a cn1Agent session, whose port file it leaves alone), with + /// `CN1_VERIFY_DIR` set so the simulator verifies its first form, writes the + /// report there and exits. A previous report is deleted first, so a run that + /// writes none cannot be read as passing. + static final class VerifyEnvironment implements org.gradle.api.Action { + static final String TASK = "cn1Verify"; + static final String REPORT_DIR = "cn1-verify"; + static final String FAIL_ON_ERROR_PROPERTY = "cn1.verify.failOnError"; + private final File reportDir; + + VerifyEnvironment(File reportDir) { + this.reportDir = reportDir; + } + + @Override + public void execute(org.gradle.api.Task task) { + File json = new File(reportDir, "report.json"); + if (json.exists() && !json.delete()) { + throw new org.gradle.api.GradleException("Could not delete the previous " + json); + } + int port = AgentEnvironment.portFree(AgentEnvironment.DEFAULT_PORT) ? AgentEnvironment.DEFAULT_PORT + : AgentEnvironment.freePort(); + JavaExec exec = (JavaExec) task; + exec.environment("CN1_MCP_PORT", String.valueOf(port)); + exec.environment("CN1_AGENT_BACKGROUND", "1"); + exec.environment("CN1_SIMULATOR_HIDDEN", "1"); + exec.environment("CN1_VERIFY_DIR", reportDir.getAbsolutePath()); + } + } + + /// Reads the report cn1Verify's simulator wrote and fails the task on errors, + /// unless `-Pcn1.verify.failOnError=false`, as `cn1:verify` does. + static final class VerifyReport implements org.gradle.api.Action { + private final File reportDir; + private final Provider failOnError; + + VerifyReport(File reportDir, Provider failOnError) { + this.reportDir = reportDir; + this.failOnError = failOnError; + } + + @Override + public void execute(org.gradle.api.Task task) { + File json = new File(reportDir, "report.json"); + String report; + try { + report = new String(java.nio.file.Files.readAllBytes(json.toPath()), + java.nio.charset.StandardCharsets.UTF_8); + } catch (java.io.IOException ex) { + throw new org.gradle.api.GradleException("The simulator wrote no report (" + json + + "); see its output above.", ex); + } + String summary = summarize(report, new File(reportDir, "report.html"), failOnError.get()); + task.getLogger().lifecycle(VerifyEnvironment.TASK + ": " + summary); + } + + /// The line the task logs, or a GradleException when the report fails the build. + static String summarize(String report, File html, boolean failOnError) { + // A failure report is an object whose only key is "failed"; a clean report can + // hold the word anywhere in app text, so only its first key counts. + if (report.trim().matches("(?s)\\{\\s*\"failed\"\\s*:.*")) { + throw new org.gradle.api.GradleException(VerifyEnvironment.TASK + " could not run: " + report); + } + int errors = count(report, "errors"); + int warnings = count(report, "warnings"); + String summary = errors + " error(s), " + warnings + " warning(s); report: " + html; + if (errors > 0 && failOnError) { + throw new org.gradle.api.GradleException(VerifyEnvironment.TASK + " found " + summary); + } + return summary; + } + + static int count(String json, String key) { + java.util.regex.Matcher m = java.util.regex.Pattern.compile("\"" + key + "\"\\s*:\\s*(\\d+)") + .matcher(json); + return m.find() ? Integer.parseInt(m.group(1)) : 0; + } + } + /// Removes the bundle a previous browser build wrote, before the next one /// runs: a build that then writes none leaves none to be mistaken for its own. static final class DeleteStaleBundle implements org.gradle.api.Action { diff --git a/maven/codenameone-gradle-plugin/src/test/java/com/codename1/gradle/PluginHelpersTest.java b/maven/codenameone-gradle-plugin/src/test/java/com/codename1/gradle/PluginHelpersTest.java index 6c840db3bed..55dd3a737df 100644 --- a/maven/codenameone-gradle-plugin/src/test/java/com/codename1/gradle/PluginHelpersTest.java +++ b/maven/codenameone-gradle-plugin/src/test/java/com/codename1/gradle/PluginHelpersTest.java @@ -271,4 +271,17 @@ void eachPlatformHasItsOwnCn1libConfiguration() { assertEquals("cn1libIos", Cn1libs.configurationName("ios")); assertEquals("cn1libJavascript", Cn1libs.configurationName("javascript")); } + + @Test + void verifyReportSummarizesACleanRunAndFailsOnErrors() { + java.io.File html = new java.io.File("report.html"); + assertEquals("0 error(s), 2 warning(s); report: " + html, + AppSupport.VerifyReport.summarize("{\"errors\": 0, \"warnings\": 2, \"note\": \"failed\"}", html, true)); + assertEquals("3 error(s), 0 warning(s); report: " + html, + AppSupport.VerifyReport.summarize("{\"errors\": 3, \"warnings\": 0}", html, false)); + org.junit.jupiter.api.Assertions.assertThrows(org.gradle.api.GradleException.class, + () -> AppSupport.VerifyReport.summarize("{\"errors\": 3, \"warnings\": 0}", html, true)); + org.junit.jupiter.api.Assertions.assertThrows(org.gradle.api.GradleException.class, + () -> AppSupport.VerifyReport.summarize("{\"failed\": \"no form\"}", html, false)); + } }