Skip to content
Open
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
17 changes: 17 additions & 0 deletions docs/guides/develop-a-renderer.md
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,23 @@ These checks work on any renderer that marks entities with `data-entity-id` and
units with `data-projections-unit`, which the SDK's interaction helpers add for
you.

`projections check` runs the same checks on the projection's working tree, its
comparison with `HEAD` and every scenario in the renderer package's
`scenarios.json`. While you work on the renderer, narrow it to what you're
fixing: `--scenario <id>` checks one scenario (repeat it for more), and naming
only `scenarios.json` scenarios skips extracting and comparing, so a run takes
seconds; `--only <check>` runs one check by its ID (`opens-fitted`,
`neutral-endpoints`, …). When a scenario fails, the report ends its step with
the command that reruns just its failed checks:

```text
fail Renderer checks, scenarios.json core-edit: 27 passed, 1 failed, 4 skipped
- Neutral endpoints: At Before, "core" is emphasized: …
Rerun just this: projections check -p turn-cost --scenario core-edit --only neutral-endpoints --trust
```

The full run stays the default, and it's what CI should run.

## 4. Test it in CI

You can run the exact same checks in Playwright inside the real host frame using
Expand Down
2 changes: 2 additions & 0 deletions docs/reference/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ projections check [OPTIONS]
| `--png <FILE>` | Also write a PNG of the (one) projection's working tree, drawn by its renderer. |
| `--from <FILE>` | Check this extractor output (what the extractor writes to stdout) for the one projection instead of extracting the working tree; nothing runs, so hand-made extractions can be drawn and checked. |
| `--no-render` | Skip the renderer checks, which need Chrome, Chromium or Edge. |
| `--scenario <ID>` | Run the renderer checks on this scenario only, for the one projection: `working-tree`, `head` (HEAD → working tree) or a scenario of its renderer package's scenarios.json. Naming only scenarios.json ones skips extracting and comparing, for a fast loop on renderer changes. Repeat it for several. Default: every scenario. Repeatable. |
| `--only <CHECK>` | Run only this renderer check, by ID (`opens-fitted`, `neutral-endpoints`, …). Repeat it for several. Default: every check. Repeatable. |
| `--browser <PATH>` | Chrome, Chromium or Edge executable. Default: PROJECTIONS_BROWSER, else found on PATH or in its usual install location. |
| `--timeout <SECONDS>` | Seconds the renderer checks may take on each scenario. Default: `120`. |
| `--update` | Write or rewrite the snapshots of comparator tests (never their expectations). |
Expand Down
16 changes: 16 additions & 0 deletions packages/cli/src/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ pub struct Options {
pub ids: Vec<String>,
/// Run the renderer checks in a headless browser.
pub render: bool,
/// The scenarios to run the renderer checks on, by ID; empty for every one.
pub scenarios: Vec<String>,
/// The renderer checks to run, by ID; empty for every one.
pub only: Vec<String>,
/// The browser executable, when not found automatically.
pub browser: Option<PathBuf>,
/// Write a PNG of the (single) projection's working tree here.
Expand All @@ -51,6 +55,14 @@ pub struct Options {
pub strict_determinism: bool,
}

impl Options {
/// Whether only renderer scenarios are checked, none of them the working tree's or HEAD's, so nothing is
/// extracted.
fn renderer_only(&self) -> bool {
!self.scenarios.is_empty() && !self.scenarios.iter().any(|id| id == "working-tree" || id == "head")
}
}

/// One check's outcome: `pass`, `fail`, `warn` or `skip`.
fn step(id: &str, title: &str, status: &str, message: impl Into<String>) -> Value {
json!({"id": id, "title": title, "status": status, "message": message.into()})
Expand Down Expand Up @@ -89,6 +101,10 @@ pub async fn check(runner: &Runner, options: Options) -> Result<Value> {
options.from.is_none() || selected.len() + broken.len() == 1,
"--from checks one projection's extraction: select it with -p"
);
ensure!(
options.scenarios.is_empty() || selected.len() + broken.len() == 1,
"--scenario names one projection's scenarios: select it with -p"
);
Comment thread
agorischek marked this conversation as resolved.
Resolved
let checker = Checker {
runner,
has_head: runner.resolve(&root, "HEAD").is_ok(),
Expand Down
43 changes: 26 additions & 17 deletions packages/cli/src/check/projection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ use anyhow::Result;
use serde_json::{Value, json};
use std::path::{Path, PathBuf};

/// Why a narrowed run skips a step.
const NARROWED: &str = "--scenario names only renderer scenarios, so nothing is extracted";

/// What every projection's check shares.
pub(super) struct Checker<'a> {
pub(super) runner: &'a Runner,
Expand All @@ -29,7 +32,8 @@ pub(super) struct Checker<'a> {
pub(super) struct Rendered<'a> {
pub(super) id: &'a str,
pub(super) definition: &'a Value,
pub(super) document: &'a Value,
/// The working tree's extraction; none when only renderer scenarios are checked.
pub(super) document: Option<&'a Value>,
pub(super) comparison: Option<&'a Value>,
pub(super) bundle: Option<PathBuf>,
}
Expand All @@ -40,7 +44,14 @@ impl Checker<'_> {
let id = definition["id"].as_str().unwrap_or_default().to_owned();
let options = self.options;
let mut steps = vec![step("definition", "Definition", "pass", "Valid")];
let extraction = self.extract(&id, &mut steps);
// Only renderer scenarios are checked: nothing is extracted, compared or tested.
let narrowed = options.renderer_only();
let extraction = if narrowed {
steps.push(step("extract", self.source(), "skip", NARROWED));
None
} else {
self.extract(&id, &mut steps)
};
if let (Some(document), None) = (&extraction, &options.from) {
steps.push(determinism_step(self.runner, &id, definition, document, options.strict_determinism));
if let Some(step) = scope_step(self.runner, &id) {
Expand All @@ -62,13 +73,11 @@ impl Checker<'_> {
steps.push(parity_step(self.runner, &id, reference));
}
// Tests use their own fixtures, so they run even when extraction failed.
steps.push(crate::comparator_tests::check_step(
&self.root,
self.runner,
definition,
options.update,
options.coverage,
));
steps.push(if narrowed {
step(crate::comparator_tests::STEP, "Comparator tests", "skip", NARROWED)
} else {
crate::comparator_tests::check_step(&self.root, self.runner, definition, options.update, options.coverage)
});
let (renderer, bundle) = renderer_step(&self.root, definition);
let renderer_failed = renderer["status"] == "fail";
steps.push(renderer);
Expand All @@ -79,22 +88,22 @@ impl Checker<'_> {
let skipped = |message: String| step("render", "Renderer checks", "skip", message);
match (&extraction, &self.executable) {
(_, None) => steps.push(skipped("--no-render".into())),
(None, _) => steps.push(skipped("Nothing extracted to render".into())),
(Some(_), _) if renderer_failed => {
(None, _) if !narrowed => steps.push(skipped("Nothing extracted to render".into())),
(_, _) if renderer_failed => {
steps.push(skipped("The renderer bundle isn't available".into()));
}
(Some(_), _) if bundle.is_some() && self.renderer_withheld(definition) => steps.push(step(
(_, _) if bundle.is_some() && self.renderer_withheld(definition) => steps.push(step(
"render",
"Renderer checks",
"fail",
"The custom renderer is repository code that differs from the default branch's, so it runs only with --trust: pass --trust (in VS Code, trust the workspace), or --no-render to skip its checks",
)),
(Some(_), Some(Err(error))) => steps.push(skipped(format!("{error:#}. Pass --browser, or --no-render to skip them"))),
(Some(document), Some(Ok(executable))) => {
(_, Some(Err(error))) => steps.push(skipped(format!("{error:#}. Pass --browser, or --no-render to skip them"))),
(document, Some(Ok(executable))) => {
let target = Rendered {
id: &id,
definition,
document,
document: document.as_ref(),
comparison: comparison.as_ref(),
bundle,
};
Expand Down Expand Up @@ -233,10 +242,10 @@ impl Rendered<'_> {
}

/// The scenarios file the check writes, and a custom renderer's own.
pub(super) fn sources(&self, root: &Path, written: PathBuf) -> Vec<PathBuf> {
pub(super) fn sources(&self, root: &Path, written: Option<PathBuf>) -> Vec<PathBuf> {
// A custom renderer's own scenarios are checked too: they
// exercise comparisons the working tree may not have.
let mut sources = vec![written];
let mut sources: Vec<PathBuf> = written.into_iter().collect();
if let Some(file) = crate::dev_server::package(root, self.definition)
.map(|package| package.join("scenarios.json"))
.filter(|file| file.is_file())
Expand Down
Loading