BATS Temporary Test Directory

I’m trying to create a test directory in the setup_file() function and delete it in the teardown_file() function. However, the variable that contains the name of the directory created is empty in the teardown_file function. As I understand from the documentation variables declared in the setup_file are still available in the teardown_file function. I’m currently doing:

setup_file() {
declare -rx TEST_DIRECTORY_NAME="${0##*/}-${RANDOM}"


Is there something else I need to do or should I approach it differently?

Hi @JohnMS,
declare is declaring local variables by default when used inside of a function (see the quickdoc on declare or help declare in a terminal). Make that declare -rxg ... and it should work for you.
(I’m not an expert writing bats-core tests, so there might be better approaches to handle the directory.)

1 Like

That did the trick. I don’t know if its the right way either, but it worked. :slight_smile: