Skip to content

Commit fe37068

Browse files
authored
Merge pull request #36 from olehermanse/testing
Added linting tests
2 parents f7df415 + f64432e commit fe37068

25 files changed

Lines changed: 194 additions & 0 deletions

.github/workflows/test.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,7 @@ jobs:
4747
run: |
4848
source .venv/bin/activate
4949
bash tests/run-format-tests.sh
50+
- name: Lint tests
51+
run: |
52+
source .venv/bin/activate
53+
bash tests/run-lint-tests.sh

tests/README.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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`).

tests/lint/001_hello_world.cf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
bundle agent main
2+
{
3+
reports:
4+
"Hello, world!";
5+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
"Hello, CFEngine"
3+
ifvarclass => "cfengine";
4+
^--------^
5+
Deprecation: Use 'if' instead of 'ifvarclass' at tests/lint/002_ifvarclass.x.cf:5:7
6+
FAIL: tests/lint/002_ifvarclass.x.cf (1 errors)
7+
Failure, 1 errors in total.

tests/lint/002_ifvarclass.x.cf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
bundle agent main
2+
{
3+
reports:
4+
"Hello, CFEngine"
5+
ifvarclass => "cfengine";
6+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
bundle agent main
2+
{
3+
vars:
4+
"x" string => "value";
5+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
{
3+
defaults:
4+
^-------^
5+
Deprecation: Promise type 'defaults' is deprecated at tests/lint/003_deprecated_promise_type.x.cf:3:3
6+
FAIL: tests/lint/003_deprecated_promise_type.x.cf (1 errors)
7+
Failure, 1 errors in total.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
bundle agent main
2+
{
3+
defaults:
4+
"x" string => "value";
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
bundle agent my_bundle
2+
{
3+
reports:
4+
"Hello";
5+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
bundle agent MyBundle
3+
^------^
4+
Convention: Bundle name should be lowercase at tests/lint/004_bundle_name_lowercase.x.cf:1:14
5+
FAIL: tests/lint/004_bundle_name_lowercase.x.cf (1 errors)
6+
Failure, 1 errors in total.

0 commit comments

Comments
 (0)