GithubHelp home page GithubHelp logo

bach-sh / bach Goto Github PK

View Code? Open in Web Editor NEW
547.0 13.0 23.0 352 KB

Bach Testing Framework

Home Page: https://bach.sh

License: Other

Shell 100.00%
bash bash-scripting unit-testing unit-testing-framework testing framework shell shell-script shell-scripting testing-framework

bach's People

Contributors

chaifeng avatar guoyiz23 avatar hyperupcall avatar mgalos avatar mihaigalos avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

bach's Issues

The `sudo rm -rf /` example from the docs is dangerous

This is the introductory example in the docs:

A complete example

#!/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
}

https://bach.sh/

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.

Unable to mock executable when filename contains dot

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?

'unmock' binaries?

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?

How to assert exit code 1 ?

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?

Fix for @mock command -v doesn't work for conditions

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

Getting "line 33: `@out': not a valid identifier"

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). 也可以用中文跟我私下沟通。

"No Arguments" descriptor is annoying to use

Since 447edb6
there is a visualization for "no arguments passed".

Some tests written against this library before this commit now fail with things like:
Bildschirmfoto 2022-04-21 um 19 26 55

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.