Skip to content
This repository has been archived by the owner on Apr 29, 2021. It is now read-only.

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

Open
jasonkarns opened this issue Jun 2, 2015 · 19 comments · May be fixed by #159
Open

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

jasonkarns opened this issue Jun 2, 2015 · 19 comments · May be fixed by #159

Comments

@jasonkarns
Copy link

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
Copy link
Collaborator

mislav commented Jun 5, 2015

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
Copy link
Author

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
Copy link
Author

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
Copy link

msabramo commented Sep 8, 2015

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
Copy link

msabramo commented Sep 8, 2015

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
Copy link

Anvil commented Sep 8, 2015

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

@msabramo
Copy link

msabramo commented Sep 8, 2015

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
Copy link

msabramo commented Sep 8, 2015

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
Copy link

Anvil commented Sep 8, 2015

Try this patch : Anvil@80c8699
It does not prevent cd to be short-circuited by CDPATH, but cd stdout wont interfer anymore.

msabramo added a commit to msabramo/bats that referenced this issue Sep 8, 2015
@Anvil
Copy link

Anvil commented Sep 8, 2015

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

@msabramo
Copy link

msabramo commented Sep 8, 2015

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
Copy link

msabramo commented Sep 8, 2015

@msabramo
Copy link

msabramo commented Sep 8, 2015

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

rbenv/rbenv#316

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

rbenv/rbenv@e3f72eb

@msabramo
Copy link

msabramo commented Sep 8, 2015

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

msabramo added a commit to msabramo/bats that referenced this issue Sep 8, 2015
but allow suppressing this behavior in case someone is testing something
that relies on CDPATH.

Fixes: sstephenson#104
@msabramo
Copy link

msabramo commented Sep 8, 2015

Here's another PR that unsets CDPATH but allows the option to prevent this by setting a variable: #119

@jasonkarns
Copy link
Author

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
Copy link

msabramo commented Sep 8, 2015

@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
Copy link
Author

Works as advertised.

blueyed added a commit to blueyed/bats that referenced this issue Apr 4, 2016
While not recommended [1] `CDPATH` might be exported and would be used
for e.g. `cd test` instead of using `./test`.
E.g. Vim will make use of it (and therefore it needs to be exported, if
you do not want to configure it twice).

Fixes sstephenson#104.

This supersedes sstephenson#119: there is
no need to unset/change CDPATH globally.

1: https://bosker.wordpress.com/2012/02/12/bash-scripters-beware-of-the-cdpath/
@blueyed blueyed linked a pull request Apr 4, 2016 that will close this issue
@blueyed
Copy link

blueyed commented Apr 4, 2016

See #159 for another approach at fixing this, without unsetting CDPATH globally.

blueyed added a commit to blueyed/bats that referenced this issue Nov 22, 2016
While not recommended [1] `CDPATH` might be exported and would be used
for e.g. `cd test` instead of using `./test`.
E.g. Vim will make use of it (and therefore it needs to be exported, if
you do not want to configure it twice).

Fixes sstephenson#104.

This supersedes sstephenson#119: there is
no need to unset/change CDPATH globally.

1: https://bosker.wordpress.com/2012/02/12/bash-scripters-beware-of-the-cdpath/
blueyed added a commit to blueyed/bats that referenced this issue Nov 22, 2016
While not recommended [1], `CDPATH` might be exported and would be used
for e.g. `cd test` instead of using `./test`.
E.g. Vim will make use of it (and therefore it needs to be exported, if
you do not want to configure it twice).

Fixes sstephenson#104.

This supersedes sstephenson#119: there is
no need to unset/change CDPATH globally.

1: https://bosker.wordpress.com/2012/02/12/bash-scripters-beware-of-the-cdpath/
yarikoptic pushed a commit to neurodebian/bats that referenced this issue Aug 6, 2019
Per @jasonkarns's comment in sstephenson#104, it makes more sense to highlight
Homebrew versus macOS, since Homebrew isn't the only method available on
macOS.
yarikoptic pushed a commit to neurodebian/bats that referenced this issue Aug 6, 2019
README: Update installation for macOS, npm
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants