Skip to content

specs-sh/assert

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

52 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Mac (BASH 3.2) BASH 4.0 BASH 4.4 BASH 5.0

☑️ assert & refute

Download the latest version or install via:

curl https://assert.specs.sh/install.sh | bash
source "assert"
source "refute.sh"

assert and refute are tiny building blocks for shell testing.

assert [ -f somefile ]
# Expected to succeed, but failed: $ [ -f somefile ]

refute [ -f i-exist ]
# Expected to fail, but succeeded: $ [ -f i-exist ]

Return or Exit

The default behavior of a failing assert is to exit.

assert [ -f somefile ] # <--- this will exit 1 on failure

The default behavior of a failing refute is to exit.

refute [ -f i-exist ] # <--- this will exit 1 on failure

Why Exit?

If you want to return 1 for a failing assertion, BASH has you covered:

testFileExists() {
  [ -f "$filename" ] # <--- this will return 0 or 1
}

Or:

testFileExists() {
  test -f "$filename" # <--- this will return 0 or 1
}

Or simply run any command:

testCommand() {
  ls some/dir # <--- this will return 1 if dir doesn't exist
}

assert and refute exist to provide an alternative which will exit.

This ensures no other commands in your test will run after an assertion failure.

Return

To configure assert and/or refute to return on failure instead:

ASSERT_FAIL=return
REFUTE_FAIL=return