在不更改脚本的情况下对脚本进行测试。
scriptkeeperRun tests against your scripts without changing your scripts.
Automated tests help us write well-behaved applications, and that's great, but what about all those pesky little scripts we use in and around our applications (e.g. deploy scripts)? How do we test those?
scriptkeeper is a tool for people who wish to write tests for existing
scripts and/or use TDD to write new scripts. Because of its design,
scriptkeeper is language agnostic, since it mocks out syscalls. That means
you can test Bash scripts just as well as Python, Ruby, etc.
This tool is very experimental. It may give incorrect results and delete your files.
There are lots and lots of features still missing from scriptkeeper. If you
try it out, I'd be interested to hear which features you would want the most.
Feel free to open (or vote on)
issues.
You can download scriptkeeper binaries on the release
page. Download the binary,
make it executable (chmod +x scriptkeeper) and optionally put it somewhere in
your $PATH. All executables in there are for Linux and x86_64 cpus.
scriptkeeper allows you to write tests for scripts (or other executables) --
without the need to modify your executables.
Here's an example script ./build-image.sh:
#!/usr/bin/env bash
if [ -z "$(git status --porcelain)" ] ; then
commit=$(git rev-parse HEAD)
docker build --tag image_name:$commit .
else
exit 1
fiFor scriptkeeper to be able to test this script, you need to add a yaml file
in the same directory as the script that has the additional file extension
.test.yaml. So here's the matching test file
./build-image.sh.test.yaml:
tests:
# builds a docker image when git repo is clean
- steps:
- command: /usr/bin/git status --porcelain
stdout: ""
- command: /usr/bin/git rev-parse HEAD
stdout: "mock_commit_hash\n"
- /usr/bin/docker build --tag image_name:mock_commit_hash .
# aborts when git repo is not clean
- steps:
- command: /usr/bin/git status --porcelain
stdout: " M some-file"
exitcode: 1Now running scriptkeeper ./build-image.sh will tell you whether your script
./build-image.sh conforms to your tests in
./build-image.sh.test.yaml.
There are more example test cases in the tests/examples folder.
.test.yaml formatHere's all the fields that are available in the yaml declarations for the
tests: (? marks optional fields.)
…For convenience you can specify commands as a string directly. So this
steps:
- command: git add .
- command: git pushcan be written as
steps:
- git add .
- git pushMultiple tests can be specified using a YAML array:
# when given the 'push' argument, it pushes to the remote
- arguments: push
steps:
- git add .
- git push
# when given the 'pull' argument, it just pulls
- arguments: pull
steps:
- git pullYou can also put everything into a tests field:
tests:
# when given the 'push' argument, it pushes to the remote
- arguments: push
steps:
- git add .
- git push
# when given the 'pull' argument, it just pulls
- arguments: pull
steps:
- git pullThere is experimental support for recording tests. You can either record
tests by passing in the --record command line flag, or you can put so-called
holes into your tests:
tests:
- steps:
- _This will actually execute the sub-commands that your script performs, without mocking them out. And it will overwrite your tests file with the recorded version.
You can also start with a partial test and have scriptkeeper fill in the
specified holes:
tests:
- arguments: foo
steps:
- git add .
- _This allows for an iterative process to create a test:
scriptkeeper.stdout and stderr.docker (for OSX)You can run the tool inside docker, for example like this:
./build-docker-image.sh
./scriptkeeper-in-docker.sh Contributions, feature requests, bug reports, etc. are all welcome. Please consider the following guidelines when submitting:
You can use just to run the tests during development with:
just dev
or -- if you want to specify a pattern for which tests to run:
just dev PATTERN
To run all checks that would be run on CI, you can do:
just ci
This tool does not currently compile or run on OSX. In order to develop on a Mac you will need to run inside of docker. Luckily, we have set up a one-liner for you. This will run the tests continuously, within docker, when files change:
just build_docker_image
./test-watch-in-docker.shTo cut a new release:
Cargo.tomljust ci distribution_buildgit tag with the version number (no leading v)git push --tagsdistribution/scriptkeeper as a binary release to githubThere's a script distribution/smoke-test.sh that allows to smoke-test the
distribution executable in a bunch of different docker images. This can be used
to make sure that all dynamically linked dependencies are available on those
systems.
暂无开放 Issues,或尚未同步最近议题。