diff --git a/.cigen/config.yml b/.cigen/config.yml index 247252c..0e77d29 100644 --- a/.cigen/config.yml +++ b/.cigen/config.yml @@ -18,6 +18,7 @@ input_groups: - "tests/**/*.rs" - "tests/**/*.yml" - "tests/**/*.trycmd" + - "tests/cmd/**" - "integration_tests/**/*" # tests/json_schema.rs reads the committed schemas and checks every config against them - "schemas/v1/**" diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index a9e1cec..406e730 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -33,6 +33,9 @@ open an issue first so we can agree on the approach. `CIGEN_UPDATE_GOLDEN=1 cargo test --test golden_fixtures` and review the diff before committing. - **JSON schemas.** `schemas/v1/` is generated from the config types in `src/schema/`. Refresh it with `CIGEN_UPDATE_GOLDEN=1 cargo test --test json_schema`. +- **CLI snapshots.** `tests/cmd/*.trycmd` hold the exact output of cigen commands, run by `tests/cli.rs`. After an + intended change to help text or output, refresh them with `TRYCMD=overwrite cargo test --test cli -- cli_coverage` + and review the diff before committing. - **cigen's own CI.** `.github/workflows/` is generated from `.cigen/`, so never edit it by hand. Change `.cigen/`, then run `cargo build --workspace && cargo run -- generate --config .cigen` (the build matters: `cargo run` alone doesn't rebuild the provider binaries) and commit both. diff --git a/internal_docs/STATUS.md b/internal_docs/STATUS.md index bee69dc..98fd6b6 100644 --- a/internal_docs/STATUS.md +++ b/internal_docs/STATUS.md @@ -150,6 +150,9 @@ What the code does today, checked against the code in December 2026. Where cigen input or a needed job changes, and for a matrix entry. - `tests/expand.rs`: expanding every config in the repository gives a `cigen.yml` that loads to the same config and expands to itself; the JSON form is the same document; an invalid config isn't expanded. +- `tests/cli.rs` runs the trycmd snapshots in `tests/cmd/*.trycmd`: every command's help, `validate` on a broken + config (text, JSON and `generate`'s matching errors), and `validate`, `explain`, `hash --job` and `expand` on a + small project. Refresh them with `TRYCMD=overwrite cargo test --test cli -- cli_coverage` and review the diff. - `tests/derived_inputs.rs`: changing a script a step runs, a command the job uses, a toolchain pin or the `package.json` script that `npm run test` runs (the root's or a sub-package's) makes the job run in mockci, and changing a used command, a named service, a top-level provider block, a local action, a lockfile or the `packageManager` pin changes the job's hash; an unused command or service doesn't. diff --git a/tests/cli.rs b/tests/cli.rs index 3dd9bf3..f0e110f 100644 --- a/tests/cli.rs +++ b/tests/cli.rs @@ -15,9 +15,18 @@ fn cli_coverage() { // Stable env cases.env("CIGEN_SKIP_CIRCLECI_CLI", "1"); - // Note: paths in outputs will be absolute to the CI runner; acceptable for snapshots. - - cases.case("tests/cmd/**/*.trycmd"); + cases.env( + "CIGEN_PLUGIN_DIR", + support::plugin_dir().display().to_string(), + ); + // Inside a git hook, an inherited GIT_DIR would point `cigen hash` at this repository. + // trycmd can't unset a variable, so point it nowhere: git fails and patterns are matched + // on the filesystem, as they are in the sandboxes, which aren't repositories. + let no_git = support::repo_root().join("target/no-git-here"); + cases.env("GIT_DIR", no_git.display().to_string()); + // Refresh the snapshots after an intended change with + // `TRYCMD=overwrite cargo test --test cli -- cli_coverage`, and review the diff. + cases.case("tests/cmd/*.trycmd"); } #[test] diff --git a/tests/cmd/generate_inline.trycmd b/tests/cmd/generate_inline.trycmd deleted file mode 100644 index 46bb451..0000000 --- a/tests/cmd/generate_inline.trycmd +++ /dev/null @@ -1,7 +0,0 @@ -$ cd integration_tests/circleci_node_simple_inline -$ cigen generate -? status = 0 -Generating CI configuration from: -Generating all workflows -Skipping CircleCI CLI validation due to CIGEN_SKIP_CIRCLECI_CLI environment variable -✅ Generated circleci configuration for all workflows to diff --git a/tests/cmd/help.trycmd b/tests/cmd/help.trycmd index a570438..57a5ec6 100644 --- a/tests/cmd/help.trycmd +++ b/tests/cmd/help.trycmd @@ -1,4 +1,113 @@ +The commands and their options. + +```console $ cigen --help -? status = 0 -A CLI tool that generates CI pipeline configurations from templates +A CLI tool that generates CI pipeline configurations + Usage: cigen [OPTIONS] [COMMAND] + +Commands: + generate Generate CI configuration (default command) + hash Compute hashes for file patterns or jobs + explain Show what a job's hash covers: its inputs, the files they match, and the hash + expand Print the whole config as one cigen.yml document, the way the loader sees it + validate Check the config and list every problem in it + help Print this message or the help of the given subcommand(s) + +Options: + -v, --verbose... Enable verbose output (use -vv for debug output) + -h, --help Print help + -V, --version Print version + +``` + +```console +$ cigen validate --help +Check the config and list every problem in it + +Usage: cigen validate [OPTIONS] + +Options: + -c, --config The config: a cigen.yml file or a .cigen/ directory (found in the current directory by default) + --format Output format [default: text] [possible values: text, json] + -v, --verbose... Enable verbose output (use -vv for debug output) + -h, --help Print help + +``` + +```console +$ cigen explain --help +Show what a job's hash covers: its inputs, the files they match, and the hash + +Usage: cigen explain [OPTIONS] + +Arguments: + The job's id + +Options: + -c, --config The config: a cigen.yml file or a .cigen/ directory (found in --base-dir by default) + --base-dir The project root, where input patterns and step paths are looked up [default: .] + --format Output format [default: text] [possible values: text, json] + --matrix A matrix entry as JSON, for a job that runs GitHub's own matrix: the hash is that entry's, as its CI hash step computes it + -v, --verbose... Enable verbose output (use -vv for debug output) + -h, --help Print help + +``` + +```console +$ cigen expand --help +Print the whole config as one cigen.yml document, the way the loader sees it + +Usage: cigen expand [OPTIONS] + +Options: + -c, --config The config: a cigen.yml file or a .cigen/ directory (found in the current directory by default) + --format Output format. YAML output is a cigen.yml that loads to the same config (comments aren't kept) [default: yaml] [possible values: yaml, json] + -v, --verbose... Enable verbose output (use -vv for debug output) + -h, --help Print help + +``` + +```console +$ cigen hash --help +Compute hashes for file patterns or jobs + +Usage: cigen hash [OPTIONS] + +Options: + -p, --pattern + Glob patterns to include when computing the hash (relative to --base-dir) + --job + Optional job identifier to hash using the loaded cigen config + --config + The config: a cigen.yml file or a .cigen/ directory (found in --base-dir by default) + --output + Optional name for the output value (written to $GITHUB_OUTPUT when set) + --base-dir + Base directory the patterns are evaluated relative to (defaults to current dir) [default: .] + --cache + Optional cache file path to persist per-file hashes + -v, --verbose... + Enable verbose output (use -vv for debug output) + --modules-digest + The digest of the job's module expansions when its CI config was generated. It goes into the hash, so the hash covers the steps that config runs + --matrix + The job's matrix entry as JSON (`${{ toJSON(matrix) }}`), for a job that runs GitHub's own matrix. It goes into the hash, so each entry has its own hash and pass marker + -h, --help + Print help + +``` + +```console +$ cigen generate --help +Generate CI configuration (default command) + +Usage: cigen generate [OPTIONS] + +Options: + -c, --config Path to .cigen directory or cigen.yml file + -o, --output Output directory for generated files (default: .) + -v, --verbose... Enable verbose output (use -vv for debug output) + -h, --help Print help + +``` diff --git a/tests/cmd/invalid_config.trycmd b/tests/cmd/invalid_config.trycmd deleted file mode 100644 index ca36f4e..0000000 --- a/tests/cmd/invalid_config.trycmd +++ /dev/null @@ -1,4 +0,0 @@ -$ cigen generate --config does-not-exist -? status = 1 -Generating CI configuration from: -Error: Failed to load configuration diff --git a/tests/cmd/project.in/.cigen/commands/setup.yml b/tests/cmd/project.in/.cigen/commands/setup.yml new file mode 100644 index 0000000..24829b2 --- /dev/null +++ b/tests/cmd/project.in/.cigen/commands/setup.yml @@ -0,0 +1,2 @@ +steps: + - run: echo setting up diff --git a/tests/cmd/project.in/.cigen/config.yml b/tests/cmd/project.in/.cigen/config.yml new file mode 100644 index 0000000..e94bbb8 --- /dev/null +++ b/tests/cmd/project.in/.cigen/config.yml @@ -0,0 +1,3 @@ +provider: github +input_groups: + rust: ["src/**/*.rs", "Cargo.toml"] diff --git a/tests/cmd/project.in/.cigen/workflows/ci.yml b/tests/cmd/project.in/.cigen/workflows/ci.yml new file mode 100644 index 0000000..53e897f --- /dev/null +++ b/tests/cmd/project.in/.cigen/workflows/ci.yml @@ -0,0 +1,4 @@ +github: + on: + push: + branches: [main] diff --git a/tests/cmd/project.in/.cigen/workflows/ci/jobs/test.yml b/tests/cmd/project.in/.cigen/workflows/ci/jobs/test.yml new file mode 100644 index 0000000..a695c77 --- /dev/null +++ b/tests/cmd/project.in/.cigen/workflows/ci/jobs/test.yml @@ -0,0 +1,5 @@ +image: rust:latest +inputs: ["@rust"] +steps: + - setup + - run: sh scripts/check.sh diff --git a/tests/cmd/project.in/Cargo.lock b/tests/cmd/project.in/Cargo.lock new file mode 100644 index 0000000..17c6700 --- /dev/null +++ b/tests/cmd/project.in/Cargo.lock @@ -0,0 +1 @@ +# lock diff --git a/tests/cmd/project.in/Cargo.toml b/tests/cmd/project.in/Cargo.toml new file mode 100644 index 0000000..f76ed9f --- /dev/null +++ b/tests/cmd/project.in/Cargo.toml @@ -0,0 +1,2 @@ +[package] +name = "demo" diff --git a/tests/cmd/project.in/scripts/check.sh b/tests/cmd/project.in/scripts/check.sh new file mode 100644 index 0000000..e8117c6 --- /dev/null +++ b/tests/cmd/project.in/scripts/check.sh @@ -0,0 +1 @@ +echo ok diff --git a/tests/cmd/project.in/src/lib.rs b/tests/cmd/project.in/src/lib.rs new file mode 100644 index 0000000..8655f8e --- /dev/null +++ b/tests/cmd/project.in/src/lib.rs @@ -0,0 +1 @@ +pub fn one() {} diff --git a/tests/cmd/project.out/Cargo.toml b/tests/cmd/project.out/Cargo.toml new file mode 100644 index 0000000..f76ed9f --- /dev/null +++ b/tests/cmd/project.out/Cargo.toml @@ -0,0 +1,2 @@ +[package] +name = "demo" diff --git a/tests/cmd/project.trycmd b/tests/cmd/project.trycmd new file mode 100644 index 0000000..1da8640 --- /dev/null +++ b/tests/cmd/project.trycmd @@ -0,0 +1,191 @@ +A small project (`project.in/`): one job with an input group, a command and a script. + +```console +$ cigen validate +.cigen is valid: 1 job(s) in 1 workflow(s). + +``` + +What the job's hash covers. The hash is the one `cigen hash --job test` prints. + +```console +$ cigen explain test +Job `test` in workflow `ci` + +Declared inputs: + @rust + Cargo.toml (1 file) + Cargo.toml + src/**/*.rs (1 file) + src/lib.rs + +Derived inputs: + command:setup + Cargo.lock (lockfile) + scripts/check.sh (named in a step) + **/package.json (every package.json, no files) + +Also hashed: + cigen [..] + the job's definition (workflows.ci.jobs.test) + the settings of workflow `ci` + +Hash: [..] + +``` + +```console +$ cigen hash --job test +[..] + +``` + +```console +$ cigen explain test --format json +{ + "job": "test", + "workflow": "ci", + "inputs": [ + { + "entry": "@rust", + "patterns": [ + { + "pattern": "Cargo.toml", + "files": [ + "Cargo.toml" + ] + }, + { + "pattern": "src/**/*.rs", + "files": [ + "src/lib.rs" + ] + } + ] + } + ], + "derived": { + "config": [ + "command:setup" + ], + "files": [ + { + "path": "Cargo.lock", + "reason": "lockfile" + }, + { + "path": "scripts/check.sh", + "reason": "named in a step" + } + ], + "patterns": [ + { + "pattern": "**/package.json", + "reason": "every package.json", + "files": [] + } + ] + }, + "needs": [], + "also_hashed": [ + "cigen [..]", + "the job's definition (workflows.ci.jobs.test)", + "the settings of workflow `ci`" + ], + "runs_matrix": false, + "matrix_entry": null, + "hash": "[..]" +} + +``` + +```console +$ cigen explain tset +? failed +error: there's no job named `tset` + help: did you mean `test`? + +The config has 1 problem. `cigen validate --format json` lists them as data. + +``` + +The `.cigen/` directory as one `cigen.yml`: + +```console +$ cigen expand +provider: github +input_groups: + rust: + - src/**/*.rs + - Cargo.toml +commands: + setup: + steps: + - run: echo setting up +workflows: + ci: + jobs: + test: + image: rust:latest + inputs: + - '@rust' + steps: + - setup + - run: sh scripts/check.sh + github: + on: + push: + branches: + - main + +``` + +```console +$ cigen expand --format json +{ + "commands": { + "setup": { + "steps": [ + { + "run": "echo setting up" + } + ] + } + }, + "input_groups": { + "rust": [ + "src/**/*.rs", + "Cargo.toml" + ] + }, + "provider": "github", + "workflows": { + "ci": { + "github": { + "on": { + "push": { + "branches": [ + "main" + ] + } + } + }, + "jobs": { + "test": { + "image": "rust:latest", + "inputs": [ + "@rust" + ], + "steps": [ + "setup", + { + "run": "sh scripts/check.sh" + } + ] + } + } + } + } +} + +``` diff --git a/tests/cmd/validate.in/.cigen/config.yml b/tests/cmd/validate.in/.cigen/config.yml new file mode 100644 index 0000000..b5b8ddf --- /dev/null +++ b/tests/cmd/validate.in/.cigen/config.yml @@ -0,0 +1,3 @@ +provider: github +input_groups: + rust: ["src/**"] diff --git a/tests/cmd/validate.in/.cigen/workflows/ci/jobs/build.yml b/tests/cmd/validate.in/.cigen/workflows/ci/jobs/build.yml new file mode 100644 index 0000000..f6e5401 --- /dev/null +++ b/tests/cmd/validate.in/.cigen/workflows/ci/jobs/build.yml @@ -0,0 +1,2 @@ +steps: + - run: cargo build diff --git a/tests/cmd/validate.in/.cigen/workflows/ci/jobs/lint.yml b/tests/cmd/validate.in/.cigen/workflows/ci/jobs/lint.yml new file mode 100644 index 0000000..2a57849 --- /dev/null +++ b/tests/cmd/validate.in/.cigen/workflows/ci/jobs/lint.yml @@ -0,0 +1,2 @@ +stpes: + - run: cargo clippy diff --git a/tests/cmd/validate.in/.cigen/workflows/ci/jobs/test.yml b/tests/cmd/validate.in/.cigen/workflows/ci/jobs/test.yml new file mode 100644 index 0000000..e69b8f2 --- /dev/null +++ b/tests/cmd/validate.in/.cigen/workflows/ci/jobs/test.yml @@ -0,0 +1,4 @@ +needs: [lint, biuld] +inputs: ["@rusty"] +steps: + - run: cargo test diff --git a/tests/cmd/validate.trycmd b/tests/cmd/validate.trycmd new file mode 100644 index 0000000..5a82617 --- /dev/null +++ b/tests/cmd/validate.trycmd @@ -0,0 +1,96 @@ +`cigen validate` on a config with three mistakes (`validate.in/`): every problem, with its file, key +path and fix. + +```console +$ cigen validate +? failed +error: .cigen/workflows/ci/jobs/lint.yml (at workflows.ci.jobs.lint.stpes): unknown key `stpes` + help: did you mean `steps`? +error: .cigen/workflows/ci/jobs/test.yml (at workflows.ci.jobs.test.inputs): job `test` uses `@rusty`, which isn't defined + help: did you mean `@rust`? +error: .cigen/workflows/ci/jobs/test.yml (at workflows.ci.jobs.test.needs): job `test` needs `biuld`, which isn't a job + help: did you mean `build`? + +The config has 3 problems. `cigen validate --format json` lists them as data. + +``` + +The same errors as data: + +```console +$ cigen validate --format json +? failed +{ + "valid": false, + "config": ".cigen", + "errors": [ + { + "file": ".cigen/workflows/ci/jobs/lint.yml", + "line": null, + "path": [ + "workflows", + "ci", + "jobs", + "lint", + "stpes" + ], + "message": "unknown key `stpes`", + "suggestion": "did you mean `steps`?" + }, + { + "file": ".cigen/workflows/ci/jobs/test.yml", + "line": null, + "path": [ + "workflows", + "ci", + "jobs", + "test", + "inputs" + ], + "message": "job `test` uses `@rusty`, which isn't defined", + "suggestion": "did you mean `@rust`?" + }, + { + "file": ".cigen/workflows/ci/jobs/test.yml", + "line": null, + "path": [ + "workflows", + "ci", + "jobs", + "test", + "needs" + ], + "message": "job `test` needs `biuld`, which isn't a job", + "suggestion": "did you mean `build`?" + } + ] +} + +``` + +`generate` reports them the same way, and so does any other command that loads the config. + +```console +$ cigen generate +? failed +Loading config from: .cigen +error: .cigen/workflows/ci/jobs/lint.yml (at workflows.ci.jobs.lint.stpes): unknown key `stpes` + help: did you mean `steps`? +error: .cigen/workflows/ci/jobs/test.yml (at workflows.ci.jobs.test.inputs): job `test` uses `@rusty`, which isn't defined + help: did you mean `@rust`? +error: .cigen/workflows/ci/jobs/test.yml (at workflows.ci.jobs.test.needs): job `test` needs `biuld`, which isn't a job + help: did you mean `build`? + +The config has 3 problems. `cigen validate --format json` lists them as data. + +``` + +```console +$ cigen validate --config does-not-exist.yml +? failed +error: does-not-exist.yml: config not found + help: Pass the path of a cigen.yml file or a .cigen/ directory. + +The config has 1 problem. `cigen validate --format json` lists them as data. + +```