It is common to test Go code that uses the exec package.
Tausch lets that code keep calling commands while replacing the command output with configured stubs.
Writing tools in the Unix/Linux world is a composition of many other tools. This is, in a lot of ways, the unix philosophy.
Though as we start to write these scripts/tools we quickly realise that verifying they work is not easy. You might ask yourself how can we test these?
If you have done some test-driven development, you might be wondering the same thing?
Now the world of writing testable scripts has come a long way from when we first learned about shell scripts.
For those interested, check out the following:
Though as your codebase starts getting bigger, you start to question was this scripting language the right choice? This is where you will get other solutions, such as:
All these projects are fine, though you might have some hard requirements or just preferences and need to use go. If this is you, then this project might help.
As this tool just stubs the output of a command, how do we make sure that we stay compatible with what we record?
Well this is a tough problem, though a problem regardless of this tool. As the tools you use will change in subtle (or not) and interesting ways.
This is known as dependency hell. Upgrading dependencies need to be verified for compatibility. Practices like semantic Versioning and to pin exact dependency versions, will likely help or you might say that is wishful thinking.
So managing the outputs needs a careful process. One ways it to have a runbook or find a way run it periodically to record the output.
Though as you might realise it does not matter if you manage to verify/tests lots of combinations, you will miss something. This is why making sure that whatever you build you need to have observability as a first class citizen.
So you might be asking yourself, why should I use this solution?
Some of these reasons I have encountered:
As this is a single binary and ties into the already defined cmd type in the defined stdlib.
Of course you might not want another dependency, if that is the case then just copy the code.
Tausch requires a Go toolchain compatible with the module’s go.mod directive.
Install the CLI:
go install github.com/alexfalkowski/tausch@latest
Create a config:
cmds:
- name: go version
stdout: "text:stubbed go version"
- name: go bob
stderr: "text:go bob: unknown command"
Run a command through Tausch:
tausch -config config.yml -- go version
[!TIP] Set
TAUSCH_CONFIGin tests when many commands should share the same config path.
The configuration is a YAML document with a top-level cmds list. Each command entry has a name and may set stdout, stderr, or exit_code.
[!WARNING] Unknown YAML keys are ignored, so a misspelled field does not fail config loading. Keep command names unique; if multiple entries have the same exact name, Tausch uses the first one in
cmds.
The stdout and stderr values use a kind:data format:
text:This is awesome
base64:VGhpcyBpcyBhd2Vzb21l
file:path
The supported kinds are:
text:<literal text> writes the text bytes as-is.base64:<base64-encoded bytes> decodes standard base64 and writes the bytes.file:<path> reads the file at the path and writes its bytes. Relative paths are resolved from the directory containing the config file.The configuration can look like:
cmds:
- name: go version
stdout: file:test/stdout/go_version.txt
- name: go bob
stderr: file:test/stderr/go_bob.txt
exit_code: 2
- name: grep needle file.txt
exit_code: 1
[!IMPORTANT] Configure at most one non-empty encoded output stream for each stub. By default, a non-empty
stdoutvalue exits0; otherwise, Tausch falls back tostderrand exits1. If both streams are empty or omitted andexit_codeis not set, the command exits1with no configured output. Setexit_codeto override the final status after any configured output is written successfully; valid values are0through255. To stub a successful command that writes zero bytes, usestdout: "text:". Configuring bothstdoutandstderrfor one command is rejected.
Payload values and file: paths are decoded only when the matching command is invoked, not when the YAML file is loaded. Unknown kinds or payloads without the kind:data separator make that invocation exit 1 with the decode error.
Capture real command output with shell redirection, then reference the recorded files from the config.
For stdout:
go version > test/stdout/go_version.txt
For stderr:
go bob 2> test/stderr/go_bob.txt
echo $? # copy the captured status (2 for this example) into exit_code
For a command where you want both streams in one file:
command &> path
Then configure the recorded output:
cmds:
- name: go version
stdout: file:test/stdout/go_version.txt
- name: go bob
stderr: file:test/stderr/go_bob.txt
exit_code: 2
To refresh a fixture, rerun the same redirect command:
go version > test/stdout/go_version.txt
[!CAUTION]
&>combines stdout and stderr. Use>or2>when the stub should prove which stream produced the output.
There are multiple ways you can use this.
The executable looks for configuration in a few places.
The executable reads the config path in this order:
-config - argument with a path.TAUSCH_CONFIG - from an env variable.tausch/config.yml appended - default config location. On Unix-like systems this is commonly $HOME/.config/tausch/config.yml, but the exact base directory comes from Go’s os.UserConfigDir.Using the library looks for the executable in this order:
$PATH - finds the tausch executable provided in the path.TAUSCH_PATH - path of the binary if tausch is not found on PATH.PATH lookup uses Go’s os/exec.LookPath("tausch"). If Go rejects a relative current-directory match such as PATH=., the wrapper falls back to TAUSCH_PATH; use an absolute directory on PATH or set TAUSCH_PATH in tests.
Pass a config and, after --, call your usual command. The command tokens after -- are joined with spaces and matched against a config entry’s name.
[!WARNING] Command matching is exact and case-sensitive. For example,
tausch -- go versionmatchesname: go version, but notname: Go Versionorname: go version extra.
Print command usage with any of the standard Go flag help forms:
tausch -h
tausch -help
tausch --help
Help output includes the invocation shape, config path resolution order, and available flags. These help invocations write usage to stderr and exit with status 1.
tausch -config test/configs/config.yml -- go version
tausch -config test/configs/config.yml -- go bob
To verify it caused an error:
echo $? # 1
Add the module dependency in the Go module that uses the wrapper:
go get github.com/alexfalkowski/tausch@latest
In your code you would use it just like you would the exec.
import (
"context"
"github.com/alexfalkowski/tausch/exec"
)
cmd := exec.CommandContext(context.Background(), "go", "version")
_ = cmd.Run()
The wrapper above invokes the tausch binary with -- go version, so the command name must be present in the active YAML config.
For library use, configure Tausch with TAUSCH_CONFIG or the default config location before running the command. CommandContext arguments are only the target command tokens; passing -config there would make it part of the stubbed command name instead of a Tausch CLI flag.
For a fresh checkout, initialize the shared build tooling and module dependencies first:
git submodule update --init
make dep
Build the CLI from the repository root:
make build
Run the repository checks after the binary exists:
make specs
Run the benchmark aggregate:
make benchmarks
Run one benchmark package:
make benchmark package=internal/io
Run count-bounded smoke fuzzing for all fuzz targets:
make fuzzes
Run one fuzz target:
make fuzz package=internal/io name=FuzzWriteText
The repository default is fuzztime=1000x, so fuzzing is bounded by run count
instead of wall-clock duration. Override it when needed:
make fuzz package=internal/io name=FuzzWriteText fuzztime=250x