sstephenson / bats

Bash Automated Testing System
MIT License
7.12k stars 519 forks source link

all files are not sourced, but executed ? #146

Open hexadite-maxk opened 8 years ago

hexadite-maxk commented 8 years ago

I don't know if this is "by design", but this looks to me like a bug:

~$ bats --version
Bats 0.4.0
~$ cat > mylib.bash << _EOF_LIB
#!/usr/bin/env bash
if ! [[ "\${0}" =~ -bash$ ]]; then                                                                                                                                                                     
  echo "I have been executed"
else
  echo "I have been sourced"
fi
_EOF_LIB
~$ chmod +x mylib.bash 
~$ cat > test_mylib.bats << _EOF_TEST
#!/usr/bin/env bats
load mylib
source ${PWD}/mylib.bash
_EOF_TEST
~$ bats ./test_mylib.bats 
I have been executed
I have been executed
1..0
~$ ./mylib.bash 
I have been executed
~$ source ./mylib.bash                                                                                                
I have been sourced

I expected load to source the bash file. But apparently in the way I'm detecting being sourced - it is not being sourced.

Please help me.

ztombol commented 8 years ago

The load function is just a wrapper around source. mylib.bash is getting sourced, but your condition detecting it is wrong. The sourcing script is bats-exec-test.

What is that you are trying to do? What platform and what bash version?

_Edit:_ Removed incomplete list of paths where bats-exec-test could live. -- 2016-01-30 22:05 UTC

ztombol commented 8 years ago

@hexadite-maxk Did you figure out the solution? Was my explanation sufficient?