Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/developer-guide/MCP-Headless-API.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Boolean> 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);
Expand Down Expand Up @@ -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;
Expand All @@ -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();
Expand All @@ -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<org.gradle.api.Task> {
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<org.gradle.api.Task> {
private final File reportDir;
private final Provider<Boolean> failOnError;

VerifyReport(File reportDir, Provider<Boolean> 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<org.gradle.api.Task> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
Loading