3 The CLI tests are focused on testing the zstd CLI.
4 They are intended to be simple tests that the CLI and arguments work as advertised.
5 They are not intended to test the library, only the code in `programs/`.
6 The library will get incidental coverage, but if you find yourself trying to trigger a specific condition in the library, this is the wrong tool.
10 The test runner `run.py` will run tests against the in-tree build of `zstd` and `datagen` by default. Which means that `zstd` and `datagen` must be built.
12 The `zstd` binary used can be passed with `--zstd /path/to/zstd`.
13 Additionally, to run `zstd` through a tool like `valgrind` or `qemu`, set the `--exec-prefix 'valgrind -q'` flag.
15 Similarly, the `--datagen`, and `--zstdgrep` flags can be set to specify
16 the paths to their respective binaries. However, these tools do not use
19 Each test executes in its own scratch directory under `scratch/test/name`. E.g. `scratch/basic/help.sh/`. Normally these directories are removed after the test executes. However, the `--preserve` flag will preserve these directories after execution, and save the tests exit code, stdout, and stderr in the scratch directory to `exit`, `stderr`, and `stdout` respectively. This can be useful for debugging/editing a test and updating the expected output.
21 ### Running all the tests
23 By default the test runner `run.py` will run all the tests, and report the results.
30 ./run.py --zstd ../../build/programs/zstd --datagen ../../build/tests/datagen
33 ### Running specific tests
35 A set of test names can be passed to the test runner `run.py` to only execute those tests.
36 This can be useful for writing or debugging a test, especially with `--preserve`.
38 The test name can either be the path to the test file, or the test name, which is the path relative to the test directory.
43 ./run.py basic/help.sh
44 ./run.py --preserve basic/help.sh basic/version.sh
45 ./run.py --preserve --verbose basic/help.sh
48 ### Updating exact output
50 If a test is failing because a `.stderr.exact` or `.stdout.exact` no longer matches, you can re-run the tests with `--set-exact-output` and the correct output will be written.
54 ./run.py --set-exact-output
55 ./run.py basic/help.sh --set-exact-output
60 Test cases are arbitrary executables, and can be written in any language, but are generally shell scripts.
61 After the script executes, the exit code, stderr, and stdout are compared against the expectations.
63 Each test is run in a clean directory that the test can use for intermediate files. This directory will be cleaned up at the end of the test, unless `--preserve` is passed to the test runner. Additionally, the `setup` script can prepare the directory before the test runs.
65 ### Calling zstd, utilities, and environment variables
67 The `$PATH` for tests is prepended with the `bin/` sub-directory, which contains helper scripts for ease of testing.
68 The `zstd` binary will call the zstd binary specified by `run.py` with the correct `$EXEC_PREFIX`.
69 Similarly, `datagen`, `unzstd`, `zstdgrep`, `zstdcat`, etc, are provided.
71 Helper utilities like `cmp_size`, `println`, and `die` are provided here too. See their scripts for details.
73 Common shell script libraries are provided under `common/`, with helper variables and functions. They can be sourced with `source "$COMMON/library.sh`.
75 Lastly, environment variables are provided for testing, which can be listed when calling `run.py` with `--verbose`.
76 They are generally used by the helper scripts in `bin/` to coordinate everything.
80 When executing your `$TEST` executable, by default the exit code is expected to be `0`. However, you can provide an alternate expected exit code in a `$TEST.exit` file.
82 When executing your `$TEST` executable, by default the expected stderr and stdout are empty. However, you can override the default by providing one of three files:
84 * `$TEST.{stdout,stderr}.exact`
85 * `$TEST.{stdout,stderr}.glob`
86 * `$TEST.{stdout,stderr}.ignore`
88 If you provide a `.exact` file, the output is expected to exactly match, byte-for-byte.
90 If you provide a `.glob` file, the output is expected to match the expected file, where each line is interpreted as a glob syntax. Additionally, a line containing only `...` matches all lines until the next expected line matches.
92 If you provide a `.ignore` file, the output is ignored.
96 All these examples pass.
98 Exit 1, and change the expectation to be 1.
113 Check the stdout output exactly matches.
128 Check the stderr output using a glob.
134 head -c 10 < /dev/urandom | xxd >&2
137 random.sh.stderr.glob
139 00000000: * * * * * *
142 Multiple lines can be matched with ...
153 random-num-lines.sh.stdout.glob
162 #### Failing examples
164 Exit code is expected to be 0, but is 1.
174 Stdout is expected to be empty, but isn't.
183 Stderr is expected to be hello but is world.
192 hello.sh.stderr.exact
198 ### Setup & teardown scripts
200 Finally, test writing can be eased with setup and teardown scripts.
201 Each directory in the test directory is a test-suite consisting of all tests within that directory (but not sub-directories).
202 This test suite can come with 4 scripts to help test writing:
209 The `setup_once` and `teardown_once` are run once before and after all the tests in the suite respectively.
210 They operate in the scratch directory for the test suite, which is the parent directory of each scratch directory for each test case.
211 They can do work that is shared between tests to improve test efficiency.
212 For example, the `dictionaries/setup_once` script builds several dictionaries, for use in the `dictionaries` tests.
214 The `setup` and `teardown` scripts run before and after each test case respectively, in the test case's scratch directory.
215 These scripts can do work that is shared between test cases to make tests more succinct.
216 For example, the `dictionaries/setup` script copies the dictionaries built by the `dictionaries/setup_once` script into the test's scratch directory, to make them easier to use, and make sure they aren't accidentally modified.
224 # Create some files for testing with
233 zstd file file0 file1
236 dictionaries/setup_once
242 for i in $(seq 10); do
243 datagen -g1000 > files/$i
246 zstd --train -r files/ -o dicts/0
253 # Runs in the test case's scratch directory.
254 # The test suite's scratch directory that
255 # `setup_once` operates in is the parent directory.
256 cp -r ../files ../dicts .