-
Notifications
You must be signed in to change notification settings - Fork 9
Added linting tests #36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f3d8362
Added new testing framework for linting tests
olehermanse a88b7ef
Moved unit tests into subdirectory
olehermanse 52e6335
Added linting tests to GH Actions
olehermanse a4c314f
Added REAME about the different tests
olehermanse f64432e
Added more linting tests
olehermanse File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| # Tests | ||
|
|
||
| There are 4 types of tests: unit, shell, format, and lint. | ||
| All test scripts should be run from the repository root. | ||
|
|
||
| ## Unit tests | ||
|
|
||
| Python unit tests using pytest, located in `tests/unit/` as `test_*.py` files. | ||
|
|
||
| ```bash | ||
| uv run pytest | ||
| ``` | ||
|
|
||
| To add a new test, create a `tests/unit/test_<name>.py` file with test functions prefixed `test_`. | ||
|
|
||
| ## Shell tests | ||
|
|
||
| Bash scripts in `tests/shell/` that exercise the CLI end-to-end. | ||
| Each script is a standalone test that runs commands and relies on `set -e` to fail on errors. | ||
|
|
||
| ```bash | ||
| bash tests/run-shell-tests.sh | ||
| ``` | ||
|
|
||
| To add a new test, create a `tests/shell/<NNN>-<name>.sh` file. | ||
|
|
||
| ## Format tests | ||
|
|
||
| Tests for `cfengine format`, located in `tests/format/`. | ||
| Each test is a pair of files: | ||
|
|
||
| - `<name>.input.cf` - the unformatted input | ||
| - `<name>.expected.cf` - the expected formatted output | ||
|
|
||
| The test runner formats each input file and diffs against the expected output. | ||
|
|
||
| ```bash | ||
| bash tests/run-format-tests.sh | ||
| ``` | ||
|
|
||
| To add a new test, create both an `.input.cf` and `.expected.cf` file with matching names. | ||
|
|
||
| ## Lint tests | ||
|
|
||
| Tests for `cfengine lint`, located in `tests/lint/`. | ||
| Each test is a `.cf` file. | ||
| The file extension determines the expected outcome: | ||
|
|
||
| - `<name>.cf` - expected to pass linting (exit code 0) | ||
| - `<name>.x.cf` - expected to fail linting (non-zero exit code), must have a corresponding `<name>.output.txt` file containing the expected error output | ||
|
|
||
| ```bash | ||
| bash tests/run-lint-tests.sh | ||
| ``` | ||
|
|
||
| To add a passing test, create a `tests/lint/<NNN>_<name>.cf` file. | ||
| 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. | ||
| The output file must match exactly, including the relative file path (e.g. `tests/lint/<NNN>_<name>.x.cf`). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| bundle agent main | ||
| { | ||
| reports: | ||
| "Hello, world!"; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
|
|
||
| "Hello, CFEngine" | ||
| ifvarclass => "cfengine"; | ||
| ^--------^ | ||
| Deprecation: Use 'if' instead of 'ifvarclass' at tests/lint/002_ifvarclass.x.cf:5:7 | ||
| FAIL: tests/lint/002_ifvarclass.x.cf (1 errors) | ||
| Failure, 1 errors in total. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| bundle agent main | ||
| { | ||
| reports: | ||
| "Hello, CFEngine" | ||
| ifvarclass => "cfengine"; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| bundle agent main | ||
| { | ||
| vars: | ||
| "x" string => "value"; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
|
|
||
| { | ||
| defaults: | ||
| ^-------^ | ||
| Deprecation: Promise type 'defaults' is deprecated at tests/lint/003_deprecated_promise_type.x.cf:3:3 | ||
| FAIL: tests/lint/003_deprecated_promise_type.x.cf (1 errors) | ||
| Failure, 1 errors in total. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| bundle agent main | ||
| { | ||
| defaults: | ||
| "x" string => "value"; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| bundle agent my_bundle | ||
| { | ||
| reports: | ||
| "Hello"; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
|
|
||
| bundle agent MyBundle | ||
| ^------^ | ||
| Convention: Bundle name should be lowercase at tests/lint/004_bundle_name_lowercase.x.cf:1:14 | ||
| FAIL: tests/lint/004_bundle_name_lowercase.x.cf (1 errors) | ||
| Failure, 1 errors in total. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| bundle agent MyBundle | ||
| { | ||
| reports: | ||
| "Hello"; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| bundle common my_bundle | ||
| { | ||
| vars: | ||
| "x" string => "value"; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
|
|
||
| bundle notavalidtype my_bundle | ||
| ^-----------^ | ||
| Error: Bundle type must be one of (agent, common, monitor, server, edit_line, edit_xml), not 'notavalidtype' at tests/lint/005_bundle_type.x.cf:1:8 | ||
| FAIL: tests/lint/005_bundle_type.x.cf (1 errors) | ||
| Failure, 1 errors in total. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| bundle notavalidtype my_bundle | ||
| { | ||
| reports: | ||
| "Hello"; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| bundle agent main | ||
| { | ||
| reports: | ||
| "Valid syntax"; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
|
|
||
| { | ||
| reports | ||
| ^-----^ | ||
| Error: Syntax error at tests/lint/006_syntax_error.x.cf:3:3 | ||
| FAIL: tests/lint/006_syntax_error.x.cf (1 errors) | ||
| Failure, 1 errors in total. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| bundle agent main | ||
| { | ||
| reports | ||
| "Missing colon after promise type"; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| Error: Empty policy file 'tests/lint/007_empty_file.x.cf' | ||
| FAIL: tests/lint/007_empty_file.x.cf (1 errors) | ||
| Failure, 1 errors in total. |
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| #/usr/bin/env bash | ||
|
|
||
| set -e | ||
| # set -x | ||
|
|
||
| echo "These tests expect cfengine CLI to be installed globally or in venv" | ||
|
|
||
| echo "Looking for CFEngine CLI:" | ||
| which cfengine | ||
|
|
||
| echo "Check that test files are in expected location:" | ||
| ls -al tests/lint/*.cf | ||
|
|
||
| mkdir -p tmp | ||
olehermanse marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| echo "Run lint tests:" | ||
| for file in tests/lint/*.cf; do | ||
| if echo "$file" | grep -q '\.x\.cf$'; then | ||
| # File ends with .x.cf, we expect it to: | ||
| # - Fail (non-zero exit code) | ||
| # - Output the correct error message | ||
|
|
||
| expected="$(echo $file | sed 's/\.x\.cf$/.output.txt/')" | ||
| if [ ! -f "$expected" ]; then | ||
| echo "FAIL: Missing expected output file: $expected" | ||
| exit 1 | ||
| fi | ||
| output="tmp/$(basename $file .x.cf).lint-output.txt" | ||
| if cfengine lint "$file" > "$output" 2>&1; then | ||
| echo "FAIL: $file - expected lint failure but got success" | ||
| exit 1 | ||
| fi | ||
| diff -u "$expected" "$output" | ||
| echo "OK (expected failure): $file" | ||
| else | ||
| # Expect success | ||
| if ! cfengine lint "$file"; then | ||
| echo "FAIL: $file - expected lint success but got failure" | ||
| exit 1 | ||
| fi | ||
| echo "OK: $file" | ||
| fi | ||
| done | ||
|
|
||
| echo "All lint tests successful!" | ||
Empty file.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.