bach-sh / bach Goto Github PK
View Code? Open in Web Editor NEWBach Testing Framework
Home Page: https://bach.sh
License: Other
Bach Testing Framework
Home Page: https://bach.sh
License: Other
This is the introductory example in the docs:
#!/usr/bin/env bash
set -euo pipefail
source bach.sh
test-rm-rf() {
# Write your test case
project_log_path=/tmp/project/logs
sudo rm -rf "$project_log_ptah/" # Typo here!
}
test-rm-rf-assert() {
# Verify your test case
sudo rm -rf / # This is the actual command to run on your host!
# DO NOT PANIC! By using Bach Testing Framework it won't actually run.
}
test-rm-your-dot-git() {
# Mock `find` command with certain parameters, will output two directories
@mock find ~ -type d -name .git === @stdout ~/src/your-awesome-project/.git \
~/src/code/.git
# Do it, remove all .git directories
find ~ -type d -name .git | xargs -- rm -rf
}
test-rm-your-dot-git-assert() {
# Verify the actual command
rm -rf ~/src/your-awesome-project/.git ~/src/code/.git
}
I understand that you are trying to make the point that the command is safe when you use bach, but I can just see someone who isn't very experienced in shell scripting commenting out the set -e
line to try to get it to work for them.. Or getting the source
patch wrong by accident.
Only one or two lines in this script need to fail, and the user's machine is potentially destroyed.
I respectfully suggest that you try to make it a bit safer. One idea would be to consider removing sudo
. It seems like you could make the same point without that.
While writing a test on a bash script that calls Python, I'm getting an error when trying to @mock python3.9
.
I've reduced this to the minimum example using a.b
:
test-dot() {
@mock a.b === @stdout TEST
a.b
}
test-dot-assert() {
TEST
}
This gives:
/home/james/bin/bach.sh: line 381: ${mock_exec_a.b_9705119_SEQ:-0}: bad substitution
--- actual-stdout.txt 2021-05-16 21:23:48.912484029 +0100
+++ expected-stdout.txt 2021-05-16 21:23:48.936483778 +0100
@@ -1 +1,2 @@
-# Exit code: 1
+TEST
+# Exit code: 0
I've not been able to solve the problem by using single or double quotes: @mock 'python.39'
or "python3.9"
.
Is there something I'm doing wrong? Or some tweak I need to make to the test? Or some bug in bach?
Really cool project, love the approach and simplicity.
As I understand it, when you run a test, all commands are mocked, including those found on the path, by default.
This is great 99% of the time, but there is a case where I do want the original command to be fully run within the script I am testing.
Here is my situation.
I am testing some docker script automation. I used @mock to mock the docker scripts with fixtures pulled from real execution of docker. Now, I want to test that piping those fixtures from the mocked docker into my script's grep
and jq
produces the expected results. In this case, I need the script being tested to actually run grep
and jq
.
I know I can execute @real grep
or even @grep
within a test, but I cannot wire the script subject I am testing to include @real
at the front of all its binary calls. Basically, I want a configurably mixed mode of mocks and real command executions.
I tried creating a function grep
and function jq
that executes @real grep $@
but this does not seem to work, as the piping of stdin and stdout and processing of args does not work exactly right. @unmock
only reverses a mock, it does not seem to make an executable 'available' to be run within the target script.
Is there a technique that can be used to allow scripts being tested to actually execute certain commands using PATH and no mock interactions?
I used @assert-fail
to assert exit code 1 When something run fail, but it does't work.
This is my example:
source bach.sh
test-something-fail(){
run_something_fail
@assert-fail
}
run_something_fail(){
exit 1
}
And I got the error:
1..1
not ok 1 - something fail
# -----
# All tests: 1, failed: 1, skipped: 0
So, what's wrong with this? what should I do?
Hi, after the fix 71471d0,
@mock command -v example === @echo "Example"
works perfectly, but I have seen it doesn't work when used in a condition as so:
test-script () {
@mock command -v example === @echo "Example"
if [ -n "$(command -v example)" ]
echo "Works!"
fi
}
test-script-assert() {
echo "Works!"
}
It does work when using command example
, without -v
bach.sh: line 15: declare: -g: invalid option
declare: usage: declare [-afFirtx] [-p] [name[=value] ...]
I am running Git Bash (version 4.4) in Windows. source bach.sh
within the shell works fine, sometimes it gives
bash: BACH_OS_NAME: readonly variable
However, when try to source this file within a .sh
file, I get
bach.sh: line 33: `@out': not a valid identifier
How to resolve this? Thank you!
As an FYI, uname
on my OS gives MINGW64_NT-10.0-22621
.
Given that I would much interested in using this bash testing framework, I would also be willing to assist in the process of testing it on Windows (assuming it hasn't already been done to some extent). 也可以用中文跟我私下沟通。
Since 447edb6
there is a visualization for "no arguments passed".
Some tests written against this library before this commit now fail with things like:
Making those tests pass again is annoying, because the descriptor that is to be pushed
into the verification is using a lot of escape sequences.
The only way I found to make those test pass is to
BACH_NO_ARGUMENTS_SIGN=$'\x1b\x5b31m\u2205\x1b\x5b0m'
pushd" "$BACH_NO_ARGUMENTS_SIGN
Maybe I'm missing something, but this I don't find ergonomic. Copy&pasting the
sign from terminal into the editor did not work due to the "red" escape sequence
not being copied.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.