Skip to content

Commit 4737a3e

Browse files
committed
Local+isolated test runner and documentation update
1 parent d9a2e4e commit 4737a3e

2 files changed

Lines changed: 59 additions & 6 deletions

File tree

docs/test.rst

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,46 @@ To execute the unit tests, please run the ``run`` script:
2626
2727
The ``run`` script will automatically install if it is not already present, and will then run all tests found under the ``test`` directory, including subdirectories.
2828

29-
To run only a subset of the tests, you can provide the name of the test subdirectory that you want to run, e.g. like this for the tests in the ``test/themes`` directory:
29+
To run only a subset of the tests, you can provide a directory or a specific test file:
3030

3131
.. code-block:: bash
3232
33-
# If you are in the root `.bash_it` directory:
33+
# Run all tests in a directory:
3434
test/run test/themes
3535
36-
By default, the tests run in single-threaded mode.
37-
If you want to speed up the test execution, you can install the `GNU ``parallel`` tool <https://www.gnu.org/software/parallel/>`_\ , which is supported by Bats.
38-
When using ``parallel``\ , the ``test/run`` script will use a number of threads in parallel, depending on the available CPU cores of your system.
39-
This can speed up test execution significantly.
36+
# Run a single test file:
37+
test/run test/completion/op.completion.bats
38+
39+
# Run multiple specific files:
40+
test/run test/completion/op.completion.bats test/completion/herdr.completion.bats
41+
42+
The tests always run in single-threaded mode (``TEST_JOBS=1``). Parallel execution
43+
via GNU ``parallel`` was previously supported but caused TAP plan count mismatches
44+
when combined with the ``--tap`` flag, producing false failures. Single-threaded
45+
mode is reliable and is what CI enforces.
46+
47+
Local Runs and Isolation
48+
~~~~~~~~~~~~~~~~~~~~~~~~
49+
50+
Running ``test/run`` directly on your machine works, but it inherits your shell's
51+
``PATH``. Any tool installed locally (e.g. ``op``, ``herdr``, ``docker``) will be
52+
visible to the tests, which can cause tests to behave differently than they do on CI,
53+
where the runner is a clean environment with only a known set of packages installed.
54+
55+
To reproduce CI conditions exactly, use ``test/run-local``, which builds a minimal
56+
Docker image and runs the tests inside it:
57+
58+
.. code-block:: bash
59+
60+
# Run all tests in a clean container:
61+
test/run-local
62+
63+
# Run a subset:
64+
test/run-local test/completion/op.completion.bats
65+
66+
The image is built on the first run and cached afterwards, so subsequent runs are
67+
fast. The trade-off compared to running ``test/run`` directly is that the first run
68+
takes longer and Docker must be installed.
4069

4170
Writing Tests
4271
-------------

test/run-local

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
IMAGE="bash-it-local-test-runner"
5+
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
6+
7+
# Build the image — idempotent, Docker cache makes subsequent runs instant
8+
docker build --tag "${IMAGE}" - << 'DOCKERFILE'
9+
FROM ubuntu:24.04
10+
RUN apt-get update -q \
11+
&& apt-get install -qy git locales curl iproute2 \
12+
&& locale-gen en_US.UTF-8 \
13+
&& rm -rf /var/lib/apt/lists/*
14+
ENV LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8
15+
DOCKERFILE
16+
17+
# Mirror CI exactly: TEST_JOBS=1 avoids --tap + --jobs TAP count mismatches.
18+
# Pass any arguments through to test/run (e.g. test/completion/op.completion.bats)
19+
exec docker run --rm \
20+
--volume "${REPO_ROOT}:/workspace" \
21+
--workdir /workspace \
22+
--env TEST_JOBS=1 \
23+
"${IMAGE}" \
24+
bash test/run "$@"

0 commit comments

Comments
 (0)