sstephenson / bats

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

reading from STDIN / defining variables #84

Closed amagura closed 9 years ago

amagura commented 9 years ago

Bats can't read from stdin, which makes it impossible to run a command with an isolated rcfile:

#!/bin/bash
startdir="$(dirname "$0")"

bats <<EOF
@test "this better be true" {
  cmd --rcfile="$startdir"/rc # FIXME doesn't work
  [ 1 -eq 1 ]
}
EOF

Instead of reading from STDIN, bats prints out its help usage:

bats <<EOF
@test
EOF
# OUTPUT:
Bats 0.4.0
Usage: bats [-c] [-p | -t] <test> [<test> ...]

A work around would be to allow users to specify variables that can be used globally within the bats file, however, bats should still probably be able to read from stdin regardless.

bats -v startdir="$(dirname "$0")" test.bats
sstephenson commented 9 years ago
  1. Correct, Bats currently doesn't read tests from stdin.
  2. What is an "isolated rcfile"?
  3. It sounds like you just want to pass an environment variable to a test case.

test.bats:

@test "environment variable" {
  [ "$VAR" = "hello" ]
}
$ bats test.bats
 ✗ environment variable
   (in test file test.bats, line 2)
     `[ "$VAR" = "hello" ]' failed

1 test, 1 failure
$ VAR=hello bats test.bats
 ✓ environment variable

1 test, 0 failures
amagura commented 9 years ago

Um... an rcfile is a run control file that some applications use to setup their environment ahead of time.. y'know like ~/.bashrc. An isolated one would just be one that only contains what's absolutely necessary in order for whatever test you're running to work; it's best to use an isolated one since using your ~/.FILErc may make the test non-portable.

An environment variable might work, but when I tried to use one... it didn't seem to bleed through into the testing environment, which is probably a good sign... since if the environment could do that then it could potentially mess up certain kinds of tests.