diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ebabab77f8..1461c92a30 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -191,7 +191,7 @@ jobs: # The format check gives the same result everywhere; it runs on Linux # only, where it is fastest. - name: Run tests - run: node scripts/test.js ${{ runner.os == 'Linux' && '-all' || '-ounit -mocha -build -docstrings' }} + run: node scripts/test.js ${{ runner.os == 'Linux' && '-all' || '-ounit -mocha -projects -docstrings' }} - name: Check for diffs in tests folder run: git diff --ignore-cr-at-eol --exit-code tests @@ -230,13 +230,8 @@ jobs: if: ${{ !matrix.skip_build_system_integration_tests }} timeout-minutes: 20 run: | - . tests/build_tests/isolated/process_watchdog.sh - start_process_watchdog - bash tests/build_tests/isolated/check_integration_test_coverage.sh - bash tests/build_tests/isolated/check_config_acceptance.sh \ - packages/@rescript/${{ matrix.node-target }}/bin/rescript.exe - bash tests/build_tests/isolated/check_command_validation.sh \ - packages/@rescript/${{ matrix.node-target }}/bin/rescript.exe + make -C tests/build_tests snapshots \ + RESCRIPT="$PWD/packages/@rescript/${{ matrix.node-target }}/bin/rescript.exe" shell: bash - name: Run build system interactive output tests @@ -246,46 +241,41 @@ jobs: if: matrix.node-target == 'linux-arm64' || matrix.node-target == 'darwin-arm64' timeout-minutes: 15 run: | - bash tests/build_tests/isolated/check_interactive_output.sh \ - packages/@rescript/${{ matrix.node-target }}/bin/rescript.exe + make -C tests/build_tests interactive \ + RESCRIPT="$PWD/packages/@rescript/${{ matrix.node-target }}/bin/rescript.exe" shell: bash - name: Run build system verbose output tests if: ${{ !matrix.skip_build_system_integration_tests }} timeout-minutes: 10 run: | - bash tests/build_tests/isolated/check_verbose_output.sh \ - packages/@rescript/${{ matrix.node-target }}/bin/rescript.exe + make -C tests/build_tests verbose \ + RESCRIPT="$PWD/packages/@rescript/${{ matrix.node-target }}/bin/rescript.exe" shell: bash - name: Run build system unit tests timeout-minutes: 10 - run: | - opam exec -- dune build tests/build_tests/unit/build_system_tests_main.exe - # Run the executable directly so a stuck test leaves live progress; - # dune otherwise buffers the test output until the action exits. - opam exec -- ./_build/default/tests/build_tests/unit/build_system_tests_main.exe -runner sequential + run: opam exec -- make -C tests/build_tests unit shell: bash # The integration suite works in tests/build_tests/testrepo and the # isolated one in directories of its own, so they run side by side; each - # takes one to three and a half minutes. The integration suite fails when - # another process runs its executable as it starts, so it runs a copy. - # Its output is shown once both are done. + # takes one to three and a half minutes. The integration target runs a + # copy of the binaries, so the isolated suite's processes don't count as + # leftovers. Its output is shown once both are done. - name: Run build system integration and isolated tests if: ${{ !matrix.skip_build_system_integration_tests }} timeout-minutes: 30 run: | bin=packages/@rescript/${{ matrix.node-target }}/bin - rm -rf tmp/integration-bin mkdir -p tmp - cp -R "$bin" tmp/integration-bin integration_log=tmp/integration-tests.log - ./tests/build_tests/integration/suite.sh tmp/integration-bin/rescript.exe >"$integration_log" 2>&1 & + make -C tests/build_tests integration RESCRIPT="$PWD/$bin/rescript.exe" \ + >"$integration_log" 2>&1 & integration=$! isolated_status=0 - sh tests/build_tests/isolated/run.sh "$bin/rescript.exe" || isolated_status=$? + make -C tests/build_tests isolated RESCRIPT="$PWD/$bin/rescript.exe" || isolated_status=$? integration_status=0 wait "$integration" || integration_status=$? diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index b0ef28b11b..5ad2b515d2 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -296,12 +296,12 @@ node scripts/test.js -mocha **Run build, project and error tests:** -This will run the test projects in `tests/build_tests` (build system), +This will run the test projects in `tests/build_tests/projects` (build system), `tests/project_tests` (compiler end-to-end) and `tests/error_tests` (error message snapshots). ``` -node scripts/test.js -build +node scripts/test.js -projects ``` **Run ounit tests:** diff --git a/Makefile b/Makefile index a32df873b9..b2e86f0663 100644 --- a/Makefile +++ b/Makefile @@ -157,7 +157,7 @@ test-gentype: lib make -C tests/gentype_tests/stdlib-no-shims clean test test-build: lib - ./tests/build_tests/integration/suite.sh $(RESCRIPT_EXE) + $(MAKE) -C tests/build_tests test RESCRIPT=$(abspath $(RESCRIPT_EXE)) test-all: test test-gentype test-analysis test-tools test-build @@ -230,7 +230,7 @@ semantic-diff: # make coverage # run full test suite, generate report # make clean-coverage # remove coverage artifacts # -# Suites covered: the `scripts/test.js -all` suites (mocha/build/ounit/ +# Suites covered: the `scripts/test.js -all` suites (mocha/projects/ounit/ # docstrings), the syntax tests (syntax/, via the instrumented # res_parser), the gentype tests (compiler/gentype, via the instrumented # bsc) and the analysis + tools tests, including reanalyze (analysis/, via @@ -286,7 +286,7 @@ coverage-lib: coverage-prepare # Run the test suites that exercise the instrumented toolchain. Each suite # shells out to a binary that `coverage-build` instrumented: -# - `node scripts/test.js -all` drives bsc (mocha/build/ounit/docstrings). +# - `node scripts/test.js -all` drives bsc (mocha/projects/ounit/docstrings). # - the syntax tests drive the instrumented `res_parser` from # _build/install/default/bin, adding coverage for syntax/. # ROUNDTRIP_TEST=1 matches Linux CI and also exercises the printer paths. diff --git a/build_system/AGENTS.md b/build_system/AGENTS.md index df38f3fbd7..ec8ae2ea84 100644 --- a/build_system/AGENTS.md +++ b/build_system/AGENTS.md @@ -32,10 +32,8 @@ repository root. ## Testing -- Unit tests: `dune runtest tests/build_tests/unit`. -- Isolated end-to-end tests: - `sh tests/build_tests/isolated/run.sh "$PWD/packages/@rescript//bin/rescript.exe"`. -- Integration suite: `make test-build`. Run it for any change to build - behavior. +- All build system tests: `make test-build`. Run it for any change to build + behavior. Single suites: `make -C tests/build_tests unit`, `isolated`, + `snapshots`, `verbose`, `interactive` or `integration`. - Add a test for every bug fix. Prefer the integration suite in `tests/build_tests/integration`. Poll for observable conditions instead of sleeping. diff --git a/build_system/README.md b/build_system/README.md index d924683fdf..cb3cd46f68 100644 --- a/build_system/README.md +++ b/build_system/README.md @@ -182,9 +182,10 @@ Windows CI. ## Testing ```sh -dune runtest tests/build_tests/unit # unit tests -sh tests/build_tests/isolated/run.sh "$PWD/packages/@rescript//bin/rescript.exe" # isolated end-to-end tests -make test-build # integration suite in tests/build_tests/integration +make test-build # all build system tests +make -C tests/build_tests unit # unit tests +make -C tests/build_tests isolated # isolated end-to-end tests +make -C tests/build_tests integration # integration suite in tests/build_tests/integration ``` The scripts in `tests/build_tests/isolated/check_*.sh` check configuration diff --git a/changes/make-test-build-all.internal.md b/changes/make-test-build-all.internal.md new file mode 100644 index 0000000000..d7d2f2740e --- /dev/null +++ b/changes/make-test-build-all.internal.md @@ -0,0 +1 @@ +- `make test-build` runs all build system tests (unit, isolated and integration) through `tests/build_tests/Makefile`, whose targets CI calls; `scripts/test.js -build` is now `-projects`. diff --git a/scripts/test.js b/scripts/test.js index 820c364aa6..1a66e91b28 100644 --- a/scripts/test.js +++ b/scripts/test.js @@ -26,7 +26,7 @@ import { let ounitTest = false; let mochaTest = false; -let buildTest = false; +let projectTest = false; let formatTest = false; let runtimeDocstrings = false; @@ -38,8 +38,8 @@ if (process.argv.includes("-mocha")) { mochaTest = true; } -if (process.argv.includes("-build")) { - buildTest = true; +if (process.argv.includes("-projects")) { + projectTest = true; } if (process.argv.includes("-format")) { @@ -53,7 +53,7 @@ if (process.argv.includes("-docstrings")) { if (process.argv.includes("-all")) { ounitTest = true; mochaTest = true; - buildTest = true; + projectTest = true; formatTest = true; runtimeDocstrings = true; } @@ -153,7 +153,7 @@ if (mochaTest) { }); } -if (buildTest) { +if (projectTest) { // Each directory below these suites with an input.js is one test. const suites = [buildTestDir, projectTestDir, errorTestDir]; let hasError = false; diff --git a/tests/build_tests/Makefile b/tests/build_tests/Makefile new file mode 100644 index 0000000000..363718187c --- /dev/null +++ b/tests/build_tests/Makefile @@ -0,0 +1,53 @@ +SHELL = /bin/bash + +# The build system's tests (see README.md). `make test-build` at the +# repository root builds the toolchain and runtime and then runs `test`; CI +# runs the targets one at a time. RESCRIPT is the executable under test, by +# default the one in the platform package; pass an absolute path to override. + +ROOT := $(abspath ../..) +# Relative, so that a make that spells paths the MSYS way still gives node +# a path it can resolve. +RESCRIPT ?= $(shell node -p "require('../../cli/common/bins.cjs').rescript_exe") + +test: unit snapshots verbose interactive isolated integration + +# OUnit tests of the build system's modules. The executable runs directly, not +# through `dune test`, so a stuck test still shows its progress. +unit: + cd $(ROOT) && dune build tests/build_tests/unit/build_system_tests_main.exe + $(ROOT)/_build/default/tests/build_tests/unit/build_system_tests_main.exe -runner sequential + +# Output snapshots of configuration acceptance and command validation, and the +# check that suite.sh runs every integration script. On Windows CI they run +# under a watchdog that stops runaway processes. +snapshots: + . isolated/process_watchdog.sh && start_process_watchdog && \ + bash isolated/check_integration_test_coverage.sh && \ + bash isolated/check_config_acceptance.sh "$(RESCRIPT)" && \ + bash isolated/check_command_validation.sh "$(RESCRIPT)" + +verbose: + bash isolated/check_verbose_output.sh "$(RESCRIPT)" + +# Needs the `script` PTY, which Windows lacks. +interactive: + bash isolated/check_interactive_output.sh "$(RESCRIPT)" + +isolated: + sh isolated/run.sh "$(RESCRIPT)" + +# The suite fails when another process runs its executable as it starts, so it +# runs a copy of the binaries: the other suites, run alongside it with -j or by +# another make, use the original. The copy's path is always the same, so a +# watcher left by an interrupted earlier run still counts as a leftover, and +# the next run replaces it. +INTEGRATION_BIN := $(ROOT)/tmp/integration-bin + +integration: + rm -rf "$(INTEGRATION_BIN)" + mkdir -p "$(INTEGRATION_BIN)" + cp -R "$$(dirname "$(RESCRIPT)")"/. "$(INTEGRATION_BIN)" + ./integration/suite.sh "$(INTEGRATION_BIN)/$$(basename "$(RESCRIPT)")" + +.PHONY: test unit snapshots verbose interactive isolated integration diff --git a/tests/build_tests/README.md b/tests/build_tests/README.md index 7deac1ecf3..a7abdce786 100644 --- a/tests/build_tests/README.md +++ b/tests/build_tests/README.md @@ -25,10 +25,11 @@ which are not recommended for new projects. Run commands from the repository root: ```sh -make test-build # integration suite -dune runtest tests/build_tests/unit # unit tests -sh tests/build_tests/isolated/run.sh "$PWD/packages/@rescript//bin/rescript.exe" -node scripts/test.js -build # projects (with project_tests and error_tests) +make test-build # unit, isolated and integration suites +make -C tests/build_tests unit # unit tests +make -C tests/build_tests isolated # isolated/run.sh; also snapshots, verbose, interactive +make -C tests/build_tests integration # integration suite +node scripts/test.js -projects # projects (part of make test, with project_tests and error_tests) ``` The integration suite is [`integration/suite.sh`](integration/suite.sh). It diff --git a/tests/build_tests/integration/suite.sh b/tests/build_tests/integration/suite.sh index 81e10e2015..47066b8fbe 100755 --- a/tests/build_tests/integration/suite.sh +++ b/tests/build_tests/integration/suite.sh @@ -26,11 +26,27 @@ fi source ./utils.sh +# Processes that run the executable under test, directly or, for the +# cli/rescript.js script, through node. Processes that only name it among +# their other arguments, like this script and the make that started it, don't +# count. +# The ps of Git for Windows has no -o and lists only the program, without its +# arguments, so there the plain match is enough. grep finding nothing is the +# normal case and must not end the script under set -e. +executable_processes() { + if ps -eo pid=,args= >/dev/null 2>&1; then + ps -eo pid=,args= | awk -v exe="$RESCRIPT_EXECUTABLE" \ + '$2 == exe || ($2 ~ /(^|\/)node$/ && $3 == exe)' + else + ps aux | grep -F "$RESCRIPT_EXECUTABLE" | grep -v grep || true + fi +} + bold "Check for stale rescript processes" -STALE_PIDS=$(ps aux | grep "$RESCRIPT_EXECUTABLE" | grep -v grep | awk '{print $2}') -if [ -n "$STALE_PIDS" ]; then +STALE_PROCESSES=$(executable_processes) +if [ -n "$STALE_PROCESSES" ]; then error "Found stale rescript processes using this executable:" - ps aux | grep "$RESCRIPT_EXECUTABLE" | grep -v grep + echo "$STALE_PROCESSES" exit 1 fi success "No stale rescript processes found"