Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"[[: not found" error when sourcing files #162

Open
brandizzi opened this issue Aug 15, 2022 · 0 comments
Open

"[[: not found" error when sourcing files #162

brandizzi opened this issue Aug 15, 2022 · 0 comments
Labels

Comments

@brandizzi
Copy link

brandizzi commented Aug 15, 2022

When sourcing a file into a shunit2 test, Bash's [[ is not recognized.

Consider the file above, called example.sh:

#!/usr/bin/env bash
# example.sh
if [[ "$1" == "OK" ]]
then
  echo Everything is OK
else
  echo Something is not OK
fi

Now, let's call it from inside a shunit2 test script, called here test.sh:

#!/bin/bash
# test.sh
test_ok() {
  ./example.sh OK
}
test_not_ok() {
  ./example.sh NOK
}

This works:

$ shunit2 test.sh 
test_ok
Everything is OK
test_not_ok
Something is not OK

Ran 2 tests.

OK

Now, if I source example.sh into test-sourced.sh, like this...

$ cat test.sh 
#!/bin/bash
# test-sourced.sh
test_ok() {
  source ./example.sh OK
}
test_not_ok() {
  source ./example.sh NOK
}

...now the tests fail because source is not found.

$ shunit2 test-sourced.sh 
test_ok
/home/adam/bin/shunit2: 4: eval: source: not found
shunit2:ERROR test_ok() returned non-zero return code.
test_not_ok
/home/adam/bin/shunit2: 7: eval: source: not found
shunit2:ERROR test_not_ok() returned non-zero return code.

Ran 2 tests.

FAILED (failures=2)

If I use . instead of source, as in the test-dot.sh file below

#!/bin/bash

test_ok() {
  . ./example.sh OK
}
test_not_ok() {
  . ./example.sh NOK
}

...it fails because [[ is "not found":

$ shunit2 ./test.sh 
test_ok
/home/adam/bin/shunit2: 2: ./example.sh: [[: not found
Something is not OK
test_not_ok
/home/adam/bin/shunit2: 2: ./example.sh: [[: not found
Something is not OK

Ran 2 tests.

OK

I confess I don't understand why it is happening. I suppose it is a bug, but I'm not dead sure it is a mistake on my side. Do you see what is going on?

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants