|
| 1 | +# Tests |
| 2 | + |
| 3 | +There are 4 types of tests: unit, shell, format, and lint. |
| 4 | +All test scripts should be run from the repository root. |
| 5 | + |
| 6 | +## Unit tests |
| 7 | + |
| 8 | +Python unit tests using pytest, located in `tests/unit/` as `test_*.py` files. |
| 9 | + |
| 10 | +```bash |
| 11 | +uv run pytest |
| 12 | +``` |
| 13 | + |
| 14 | +To add a new test, create a `tests/unit/test_<name>.py` file with test functions prefixed `test_`. |
| 15 | + |
| 16 | +## Shell tests |
| 17 | + |
| 18 | +Bash scripts in `tests/shell/` that exercise the CLI end-to-end. |
| 19 | +Each script is a standalone test that runs commands and relies on `set -e` to fail on errors. |
| 20 | + |
| 21 | +```bash |
| 22 | +bash tests/run-shell-tests.sh |
| 23 | +``` |
| 24 | + |
| 25 | +To add a new test, create a `tests/shell/<NNN>-<name>.sh` file. |
| 26 | + |
| 27 | +## Format tests |
| 28 | + |
| 29 | +Tests for `cfengine format`, located in `tests/format/`. |
| 30 | +Each test is a pair of files: |
| 31 | + |
| 32 | +- `<name>.input.cf` - the unformatted input |
| 33 | +- `<name>.expected.cf` - the expected formatted output |
| 34 | + |
| 35 | +The test runner formats each input file and diffs against the expected output. |
| 36 | + |
| 37 | +```bash |
| 38 | +bash tests/run-format-tests.sh |
| 39 | +``` |
| 40 | + |
| 41 | +To add a new test, create both an `.input.cf` and `.expected.cf` file with matching names. |
| 42 | + |
| 43 | +## Lint tests |
| 44 | + |
| 45 | +Tests for `cfengine lint`, located in `tests/lint/`. |
| 46 | +Each test is a `.cf` file. |
| 47 | +The file extension determines the expected outcome: |
| 48 | + |
| 49 | +- `<name>.cf` - expected to pass linting (exit code 0) |
| 50 | +- `<name>.x.cf` - expected to fail linting (non-zero exit code), must have a corresponding `<name>.output.txt` file containing the expected error output |
| 51 | + |
| 52 | +```bash |
| 53 | +bash tests/run-lint-tests.sh |
| 54 | +``` |
| 55 | + |
| 56 | +To add a passing test, create a `tests/lint/<NNN>_<name>.cf` file. |
| 57 | +To add a failing test, create both a `tests/lint/<NNN>_<name>.x.cf` file and a `tests/lint/<NNN>_<name>.output.txt` file with the expected lint output. |
| 58 | +The output file must match exactly, including the relative file path (e.g. `tests/lint/<NNN>_<name>.x.cf`). |
0 commit comments