sstephenson / bats

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

running bats on a specific test file says file does not exist #104

Open jasonkarns opened 9 years ago

jasonkarns commented 9 years ago

Running bats against a whole directory works fine. But when I run bats against a particular test file, it says the file doesn't exist.

$ bats test/
# succeeds
$ bats test/rbenv.bats
bats: /Users/<usr>/<code>/ruby-build/test
/Users/<usr>/<code>/ruby-build/test/rbenv.bats does not exist
$ head -1 test/rbenv.bats 
#!/usr/bin/env bats

I'm running bats 0.4.0 (installed via homebrew) on OSX 10.9.5

mislav commented 9 years ago

Strange. I just checked out bats 0.4.0:

$ ~/p/bats/bin/bats test/rbenv.bats
 ✓ install proper
 ✓ install rbenv local version by default
 ... (works as normal)

Do you use normal system bash? Can you try cloning latest bats from master and using that? Or brew upgrade bats --HEAD to do the same using Homebrew

jasonkarns commented 9 years ago

Same issue when installed from --HEAD with homebrew. I should also note that I can run individual test files when I'm in the test directory.

$ cd test/
$ bats rbenv.bats 
 ✓ blank invocation
 ✓ invalid command
 ✓ default RBENV_ROOT
 ✓ inherited RBENV_ROOT
 ✓ default RBENV_DIR
 ✓ inherited RBENV_DIR
 ✓ invalid RBENV_DIR
 ✓ adds its own libexec to PATH
 ✓ adds plugin bin dirs to PATH
 ✓ RBENV_HOOK_PATH preserves value from environment
 ✓ RBENV_HOOK_PATH includes rbenv built-in plugins

11 tests, 0 failures
jasonkarns commented 9 years ago

I'm using latest (non HEAD) bash from homebrew (4.3.33)

$ bash --version
GNU bash, version 4.3.33(1)-release (x86_64-apple-darwin13.4.0)
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
msabramo commented 9 years ago

I seem to be running into the same issue, except that when I run bats against a directory, it is not finding the test.

[marca@marca-mac2 ansible-heroku-output]$ ls -l test/bats
total 8
-rwxr-xr-x+ 1 marca  staff  125 Sep  8 07:03 echo_ascii.bats*

[marca@marca-mac2 ansible-heroku-output]$ bats test/bats/echo_ascii.bats
bats: /Users/marca/dev/git-repos/ansible-heroku-output/test/bats
/Users/marca/dev/git-repos/ansible-heroku-output/test/bats/echo_ascii.bats does not exist

[marca@marca-mac2 ansible-heroku-output]$ bats test/bats

0 tests, 0 failures

[marca@marca-mac2 ansible-heroku-output]$ bats test/bats/

0 tests, 0 failures

[marca@marca-mac2 ansible-heroku-output]$ ( cd test/bats && bats . )
 ✓ Test playbook with ASCII

1 test, 0 failures
msabramo commented 9 years ago

Here's the result of a run with xtrace enabled:

[marca@marca-mac2 ansible-heroku-output]$ pwd
/Users/marca/dev/git-repos/ansible-heroku-output

[marca@marca-mac2 ansible-heroku-output]$ bash -x /usr/local/bin/bats test/bats/echo_ascii.bats
...
+ for filename in '"${arguments[@]}"'
+ '[' -d test/bats/echo_ascii.bats ']'
++ expand_path test/bats/echo_ascii.bats
+++ dirname test/bats/echo_ascii.bats
++ cd test/bats
++ local dirname=/Users/marca/dev/git-repos/ansible-heroku-output/test/bats
++ cd /Users/marca/dev/git-repos/ansible-heroku-output
+++ basename test/bats/echo_ascii.bats
++ echo /Users/marca/dev/git-repos/ansible-heroku-output/test/bats/echo_ascii.bats
+ filenames["${#filenames[@]}"]='/Users/marca/dev/git-repos/ansible-heroku-output/test/bats
/Users/marca/dev/git-repos/ansible-heroku-output/test/bats/echo_ascii.bats'
+ '[' 1 -eq 1 ']'
+ command=bats-exec-test
+ '[' -n 1 ']'
+ extended_syntax_flag=-x
+ formatter=bats-format-tap-stream
+ set -o pipefail execfail
+ exec bats-exec-test -x '/Users/marca/dev/git-repos/ansible-heroku-output/test/bats
/Users/marca/dev/git-repos/ansible-heroku-output/test/bats/echo_ascii.bats'
+ bats-format-tap-stream
bats: /Users/marca/dev/git-repos/ansible-heroku-output/test/bats
/Users/marca/dev/git-repos/ansible-heroku-output/test/bats/echo_ascii.bats does not exist
Anvil commented 9 years ago

Notice the newline, in the error message. Do you have a CDPATH variable ? (echo "$CDPATH"). CDPATH makes the 'cd' built-in verbose.

msabramo commented 9 years ago

This part seems to be the crux of the error:

+ exec bats-exec-test -x '/Users/marca/dev/git-repos/ansible-heroku-output/test/bats
/Users/marca/dev/git-repos/ansible-heroku-output/test/bats/echo_ascii.bats'

It looks like it somehow got the impression that the filename it needs to pass to bats-exec-test is a two-word expression with the directory name and then the full path to the file.

If I had to guess I would say that it's trying to convert a relative path to an absolute path and doing it incorrectly. This is bolstered by the fact that a relative path does not work but an absolute path does work:

marca@marca-mac2:~/dev/git-repos/ansible-heroku-output$ bats test/bats/echo_ascii.bats
Running "bats-exec-test" on "/Users/marca/dev/git-repos/ansible-heroku-output/test/bats
/Users/marca/dev/git-repos/ansible-heroku-output/test/bats/echo_ascii.bats" ...
bats: /Users/marca/dev/git-repos/ansible-heroku-output/test/bats
/Users/marca/dev/git-repos/ansible-heroku-output/test/bats/echo_ascii.bats does not exist

marca@marca-mac2:~/dev/git-repos/ansible-heroku-output$ bats $(pwd)/test/bats/echo_ascii.bats
Running "bats-exec-test" on "/Users/marca/dev/git-repos/ansible-heroku-output/test/bats/echo_ascii.bats" ...
 ✓ Test playbook with ASCII

1 test, 0 failures
msabramo commented 9 years ago

Looks like @Anvil hit the nail on the head; it seems related to CDPATH.

marca@marca-mac2:~/dev/git-repos/ansible-heroku-output$ bats test/bats/echo_ascii.bats
Running "bats-exec-test" on "/Users/marca/dev/git-repos/ansible-heroku-output/test/bats
/Users/marca/dev/git-repos/ansible-heroku-output/test/bats/echo_ascii.bats" ...
bats: /Users/marca/dev/git-repos/ansible-heroku-output/test/bats
/Users/marca/dev/git-repos/ansible-heroku-output/test/bats/echo_ascii.bats does not exist

marca@marca-mac2:~/dev/git-repos/ansible-heroku-output$ echo $CDPATH
.:/Users/marca/dev/surveymonkey:/Users/marca/dev/git-repos:/Users/marca/dev/hg-repos

marca@marca-mac2:~/dev/git-repos/ansible-heroku-output$ unset CDPATH

marca@marca-mac2:~/dev/git-repos/ansible-heroku-output$ bats test/bats/echo_ascii.bats
Running "bats-exec-test" on "/Users/marca/dev/git-repos/ansible-heroku-output/test/bats/echo_ascii.bats" ...
 ✓ Test playbook with ASCII

1 test, 0 failures
Anvil commented 9 years ago

Try this patch : https://github.com/Anvil/bats/commit/80c86999eec8e176cbfc539b86db21b820e6f15d It does not prevent cd to be short-circuited by CDPATH, but cd stdout wont interfer anymore.

Anvil commented 9 years ago

unsetting CDPATH might interfer with user code, don't you think ?

msabramo commented 9 years ago

Yeah, I guess if someone is testing something that relies on CDPATH (probably rare), it could interfere.

I'll take a look at your patch in a bit.

msabramo commented 9 years ago

FWIW, I just found this:

https://bosker.wordpress.com/2012/02/12/bash-scripters-beware-of-the-cdpath/

msabramo commented 9 years ago

A related issue in rbenv, another project from @sstephenson:

https://github.com/sstephenson/rbenv/issues/316

Here's how it was fixed there (export -n CDPATH):

https://github.com/sstephenson/rbenv/commit/e3f72ebae20768079ca4b4425a364900f3f16fc6

msabramo commented 9 years ago

export -n CDPATH doesn't seem to work though.

msabramo commented 9 years ago

Here's another PR that unsets CDPATH but allows the option to prevent this by setting a variable: https://github.com/sstephenson/bats/pull/119

jasonkarns commented 9 years ago

As the OP, I can confirm that my issue was also caused by $CDPATH. I was exporting it in .bashrc but after reading https://bosker.wordpress.com/2012/02/12/bash-scripters-beware-of-the-cdpath/ (thanks @msabramo) I am no longer exporting it. This resolves the issue.

However, I think BATS should make every effort to still work even if $CDPATH is exported by the user so I'm not closing this issue (yet).

msabramo commented 9 years ago

@jasonkarns: I'm glad that helped! I agree with you that bats should make a reasonable effort to work despite an exported CDPATH. Did you try out #119?

jasonkarns commented 9 years ago

Works as advertised.

blueyed commented 8 years ago

See https://github.com/sstephenson/bats/pull/159 for another approach at fixing this, without unsetting CDPATH globally.