sstephenson / bats

Bash Automated Testing System
MIT License
7.13k stars 517 forks source link

WIP: Allow sourcing of helper files from BATS_LIB_PATH #216

Open ntnn opened 7 years ago

ntnn commented 7 years ago

The existing functionality of load() is preserved:

  1. Load helper files in the same directory the current testfile resides in
  2. Load helper files by absolute path

Additionally an environment variable BATS_LIB_PATH can be defined to be source helper files from.

If BATS_LIB_PATH is empty the following locations are used:

  1. $HOME/.bats/lib Allows users to manage helper libraries themselves, similar to python/ruby/...
  2. /usr/lib/bats Allows to install helper libraries via a package manager.

Example usage:

$ tree /usr/lib/bats
/usr/lib/bats
├── bats-support/
│   ├── error.bash
│   ├── lang.bash
│   └── output.bash
└── bats-support.bash

1 directory, 4 files
$ cat /usr/lib/bats/bats-support
source "$(dirname "${BASH_SOURCE[0]}")/bats-support/output.bash"
source "$(dirname "${BASH_SOURCE[0]}")/bats-support/error.bash"
source "$(dirname "${BASH_SOURCE[0]}")/bats-support/lang.bash"
$ cat example.bats
#!/usr/bin/env bats

load bats-support
@test "This will fail" {
    fail "This is an error message with error.bash loaded from /usr/lib/bats/error.bash"
}
$ bats example.bats
1..1
not ok 1 This will fail
# (from function `fail' in file /usr/lib/bats/bats-support/error.bash, line 40,
#  in test file example.bats, line 6)
#   `fail "This is an error message with error.bash loaded from /usr/lib/bats/error.bash"' failed
# This is an error message with error.bash loaded from /usr/lib/bats/error.bash

The bats-support here is different than the load.bash found in the repository.

Currently a loading file is required, which sources the files in the directory, unless the library is a single file. To work around this I'll add a mechanism which sources all files in a directory, if the directory exists but no loading file is found.

Also, the last line of the error message shows the wrong location for error.bash.

ntnn commented 7 years ago

@ztombol You've written three libraries for bats so far, what do you think of a load mechanism like this?

Additionally I've read quite some issues and comments about loading helper files, but none seem to have fruited into anything other than the current load(), which only sources in the same dir or absolute paths.

ntnn commented 7 years ago

Additionally I'd like to know how heavy posix compliance weighs in. Since many features of bats are based on non-posix functionality (e.g. set +-E/set +-T) it doesn't make sense to use the slower [ builtin vs. the [[ keyword.

If POSIX-compliance is a long term goal I'll gladly push that to the branch.