bach-sh/bach

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

mattalxndr opened this issue · 3 comments

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.

By the way, I am enjoying working with this lib. My favorite part about it is being able to better visualize what steps are actually being carried out when I run a command.

It's also lead to a fair bit of clean up. 👍

Hi @mattalexx, thank you for your suggestions. I'v updated Readme and tests.

Thanks for such a quick reply @chaifeng