Skip to content

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

Open
@brandizzi

Description

@brandizzi

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions