Skip to content

Commit

Permalink
Merge pull request #48 from automaticp/parser-tests-with-pytest
Browse files Browse the repository at this point in the history
Parser tests: replace shell scripts with pytest scripts
  • Loading branch information
nolanderc authored Nov 2, 2023
2 parents 5896114 + 4c51f97 commit f5694d1
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 95 deletions.
2 changes: 1 addition & 1 deletion justfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ watch *ARGS:
test:
zig build test --summary failures {{flags}}
just install
tests/run-all-tests.sh
pytest tests/

test-file *ARGS:
zig test {{flags}} "$@"
Expand Down
27 changes: 15 additions & 12 deletions tests/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# `glsl_analyzer/tests`

- [How do I run the tests?](#how-do-i-run-the-tests)
- [LSP tests](#lsp-tests)
- [How do I run the tests?](#how-do-i-run-the-tests)
- [How do I debug failing tests?](#how-do-i-debug-failing-tests)
- [How do I add more files for testing / write new test cases?](#how-do-i-add-more-files-for-testing--write-new-test-cases)
- [How tests and test input are structured](#how-tests-and-test-input-are-structured)
Expand All @@ -13,10 +13,8 @@
- [Expecting failure](#expecting-failure)
- [Parser tests](#parser-tests)

## LSP tests


### How do I run the tests?
## How do I run the tests?

**Requirements:** `python 3.10` and `requirements.txt`.

Expand All @@ -34,9 +32,18 @@ $ cd tests/ && pytest

`pytest` will automatically pick up tests from modules that have names like `test_*.py` and contain functions `test_*` and classes `Test*`. More patterns are available, see the `pytest` docs.

If you want to run individual test files, just specify them as arguments:

```sh
$ pytest tests/test_lsp.py
```

*Caution: do not run `pytest` with the `--runxfail` flag on these tests. We do not use `xfail` in its conventional form in our test suite; using this flag will make the tests fail in unpredictable ways.*


## LSP tests


### How do I debug failing tests?

The current test routines are set up to output the glsl file location that was targeted in the test:
Expand Down Expand Up @@ -259,17 +266,13 @@ We always expect failure in *strict mode*: if the test was expected to fail, but

## Parser tests

Parser tests are simple bash scripts that run the parser with `glsl_analyzer --parse-file` on a set of files. Currently we only test successful parsing on well-formed code.
Parser tests are simple pytest scripts that run the parser with `glsl_analyzer --parse-file` on a set of files. Currently we only test successful parsing on well-formed code and check that the `glsl_analyzer` did not output anything into `stderr`.

*The `glsl_analyzer` version you want to test must be available in your PATH.*

You can run the tests by executing
Run only the parser tests by executing

```sh
$ ./run-all-tests.sh
$ pytest test_parser.py
```

You can add more directories for testing by adding them to the `well_formed_dirs` in `run-parser-tests.sh`.

*This setup will most likely be rewritten in the near future.*
You can add more directories for testing by adding them to the `dir_with_test_files` fixture `params` in `test_parser.py`.

21 changes: 0 additions & 21 deletions tests/run-all-tests.sh

This file was deleted.

61 changes: 0 additions & 61 deletions tests/run-parser-tests-well-formed.sh

This file was deleted.

42 changes: 42 additions & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import subprocess
from pathlib import Path

import pytest
import pytest_subtests


base_directory = Path(__file__).parent.resolve()

glsl_extensions = (
".glsl", ".vert", ".frag", ".geom",
".comp", ".tesc", ".tese", ".rgen",
".rint", ".rahit", ".rchit", ".rmiss",
".rcall", ".mesh", ".task"
)


@pytest.fixture(
params=("glsl-samples/well-formed/glslang/",)
)
def dir_with_test_files(request) -> Path:
return base_directory / Path(request.param)


def test_parser_in_directory(
subtests: pytest_subtests.SubTests,
dir_with_test_files: Path
):

for file in dir_with_test_files.iterdir():
if file.suffix not in glsl_extensions:
continue

with subtests.test(msg=str(file)):
output = subprocess.run(
args=("glsl_analyzer", "--parse-file", str(file)),
capture_output=True,
text=True
)

assert output.returncode == 0 and len(output.stderr) == 0, \
output.stderr

0 comments on commit f5694d1

Please # to comment.