xpd rules test
Use xpd rules test when you write your own rules. It checks the valid and invalid examples of each
project rule, and the snapshots of their issues and fixes. A snapshot is a saved file of the
issues and fixes that a rule gives on its invalid examples, such as
rule-tests/__snapshots__/team-no-print-snapshot.yml.
A test file has the ast-grep fields id, valid and invalid. A valid example must give no
issue from its rule, and an invalid example must give one. A case can set path, so that the
rule's files and ignores globs and the language detection see that file name. The default
engine runs each example through the same scanner as xpd check, with import gates, value
proofs and version bounds. --engine structural runs ast-grep's own test runner, which checks
plain rules without them.
xpd rules test [OPTIONS]Options
Section titled “Options”| Option | Default | What it does |
|---|---|---|
--engine <ENGINE> |
xpd |
Run domain checks with xpd, or raw structural checks with ast-grep. Values: xpd, structural. |
-c, --config <CONFIG> |
The project file to read instead of the nearest sgconfig.yml. | |
-t, --test-dir <TEST_DIR> |
Read the test cases in this folder instead of the testConfigs of sgconfig.yml. |
|
--snapshot-dir <SNAPSHOT_DIR> |
__snapshots__ |
The snapshot folder inside the test folder. |
-f, --filter <FILTER> |
Test only the rules whose id matches this regular expression. | |
--skip-snapshot-tests |
Check only the valid and invalid examples, and no snapshot. Conflicts with --update-all, --interactive. |
|
-U, --update-all |
Accept every changed snapshot and write it. | |
-i, --interactive |
Ask about each changed snapshot before writing it. Conflicts with --format. |
|
--format <FORMAT> |
text |
Print text, or JSON with each case's id, source and status and the failed count. Values: text, json. |
Examples
Section titled “Examples”xpd rules new makes the project, the rule and an empty test file.
$ xpd rules new project --pack team --yes --format json | jq -c .item"project"$ xpd rules new rule team/no-print --pattern 'print($X)' --message 'Use the project logger.' --with-test --yes --format json | jq -c .id"team/no-print"With this test file, the first run fails because no snapshot exists yet. xpd rules test -U then writes
the snapshot.
id: team/no-printvalid: - "log(1)"invalid: - "import team\nprint(1)"$ xpd rules testteam/no-print: passedteam/no-print: snapshot mismatch2 cases, 1 failed$ xpd rules test -Uteam/no-print: passedteam/no-print: updated2 cases, 0 failed--format json prints each case with its id, source and status, and the failed count.
Exit status
Section titled “Exit status”| Exit code | Meaning |
|---|---|
| 0 | Every example and snapshot passed, or was updated |
| 1 | An example or a snapshot failed |
| 2 | The command could not run, for example because no case matched |