Skip to content

Commit 254ca88

Browse files
authored
Add debugging output section to TESTS.md and adjust formatting (#768)
* Add debugging output section to TESTS.md and adjust formatting * Add debugging guidance to README and TESTS.md; create DEBUGGING_LOCALLY.md * Update debugging references in documentation for clarity and consistency * Improve documentation for debugging: standardize casing and clarify file descriptor reference
1 parent 90a9e53 commit 254ca88

5 files changed

Lines changed: 44 additions & 40 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
Exercism Exercises in Bash
66

7+
If you are solving exercises locally and need help with Bats debug output, see [Debugging with `bats`](https://exercism.org/docs/tracks/bash/debugging).
8+
79
## Contributing Guide
810

911
Please see the [contributing guide](https://github.com/exercism/bash/blob/master/CONTRIBUTING.md) for information.

docs/DEBUGGING_LOCALLY.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Debugging Locally
2+
3+
When you run Bash track tests with [Bats](https://github.com/bats-core/bats-core), Bats captures both STDOUT and STDERR for output assertions.
4+
5+
```exercism/caution
6+
This works locally with `bats`, but **not** in the Exercism online editor.
7+
```
8+
9+
If you want to print debug output without affecting the test result, write it to file descriptor (fd) 3.
10+
11+
```bash
12+
echo 'debug message' >&3
13+
```
14+
15+
Bats shows output written to fd 3 during the test run, but it does not include that output in assertions such as `assert_output`.
16+
17+
Example run:
18+
19+
```none
20+
$ bats hello_world.bats
21+
hello_world.bats
22+
✓ Say Hi!
23+
debug message
24+
1 test, 0 failures
25+
```
26+
27+
This is useful when you are running the tests locally.

docs/TESTS.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ cd /path/to/your/exercise_workspace/bash/whatever
3939
bats whatever.bats
4040
```
4141

42+
If you want to print debug output while running the tests locally, see [Debugging Locally](/docs/tracks/bash/debugging).
43+
4244
## Installing `bats-core`
4345

4446
You should be able to install it from your favorite package manager:
@@ -56,8 +58,8 @@ $ brew install bats-core
5658
🍺 /usr/local/Cellar/bats-core/1.1.0: 13 files, 55KB, built in 4 seconds
5759
```
5860

59-
* The legacy `bats` package also exists in the homebrew ecosystem.
60-
**_Do not install that by mistake_**: <u>install `bats-core`</u>.
61+
- The legacy `bats` package also exists in the homebrew ecosystem.
62+
**_Do not install that by mistake_**: <u>install `bats-core`</u>.
6163

6264
### For Linux
6365

@@ -124,7 +126,7 @@ annotations prepending other tests.
124126

125127
### Overriding skips
126128

127-
To run all tests, including the ones with `skip` annotations, you can set an environment variable `BATS_RUN_SKIPPED` to the value `true`.
129+
To run all tests, including the ones with `skip` annotations, you can set an environment variable `BATS_RUN_SKIPPED` to the value `true`.
128130
One way to set this just for the duration of running bats is:
129131

130132
```bash
@@ -153,7 +155,6 @@ Ownership was handed over in 2017: [sstephenson/bats#150 (comment)][bats-fork].
153155

154156
If you have the original sstephenson/bats installed (check with `bats -v` reporting a version number less than 1.0), then you should switch to bats-core; otherwise you may find yourself [experiencing unexplained test failures][legacy-failures].
155157

156-
157158
[exercism-cli]: https://exercism.org/docs/using/solving-exercises/working-locally
158159
[bats]: https://github.com/bats-core/bats-core
159160
[bats-assert]: https://github.com/bats-core/bats-assert

docs/config.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@
2121
"title": "Testing on the Bash track",
2222
"blurb": "Learn how to test your Bash exercises on Exercism"
2323
},
24+
{
25+
"uuid": "14a9f11d-ada7-4f5f-a9f5-2094b3e2e659",
26+
"slug": "debugging",
27+
"path": "docs/DEBUGGING_LOCALLY.md",
28+
"title": "Debugging with `bats`",
29+
"blurb": "Debugging your Bash code while testing with bats."
30+
},
2431
{
2532
"uuid": "c74e1d25-18f1-4212-a10c-05303a962b1c",
2633
"slug": "resources",

exercises/shared/.docs/tests.md

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@ Run the tests using the `bats` program.
77
bats hello_world.bats
88
```
99

10+
If you need to print debug output while running tests locally, see [Debugging Locally][debugging].
11+
1012
`bats` will need to be installed.
1113
See the [Testing on the Bash track][tests] page for instructions to install `bats` for your system.
1214

1315
[tests]: https://exercism.org/docs/tracks/bash/tests
16+
[debugging]: https://exercism.org/docs/tracks/bash/debugging
1417

1518
## Help for assert functions
1619

@@ -19,42 +22,6 @@ Help for the various `assert*` functions can be found there.
1922

2023
[bats-assert]: https://github.com/bats-core/bats-assert
2124

22-
## Debugging output
23-
24-
```exercism/caution
25-
This works locally with `bats`, but **not** in the Exercism online editor.
26-
```
27-
28-
When running tests, `bats` captures both stdout and stderr for comparison with the expected output.
29-
If you print debug messages to stdout (`echo`) or stderr (`>&2`), they will be included in the captured output and may cause the test to fail.
30-
31-
To print debug information without affecting the test results, `bats` provides file descriptor **3** for this purpose.
32-
Anything redirected to `>&3` will be shown during the test run but will not be included in the captured output used for assertions.
33-
34-
Example:
35-
36-
```bash
37-
#!/usr/bin/env bash
38-
39-
# This debug message will not interfere with test output comparison
40-
echo "debug message" >&3
41-
42-
# Normal program output (this is what your tests will see and compare)
43-
echo "Hello, World!"
44-
```
45-
46-
Example run:
47-
48-
```none
49-
$ bats hello_world.bats
50-
hello_world.bats
51-
✓ Say Hi!
52-
debug message
53-
1 test, 0 failures
54-
```
55-
56-
This allows you to see helpful debug output without affecting the tests.
57-
5825
## Skipped tests
5926

6027
Solving an exercise means making all its tests pass.

0 commit comments

Comments
 (0)