BATS - how to get a consistent UUID in my example?

130 Views Asked by At

This is a bats test script I wrote

    export UUID=$(openssl rand -hex 6)
    export TEST_NAME=SecretsManagerTest1-$UUID

    setup_file() {
       echo "test name is $TEST_NAME" > setup.txt
    }
    
    @test "addition using bc" {
       echo "test name is $TEST_NAME" > runtest.txt
    }

I want $TEST_NAME to have the same value in setup.txt and runtest.txt but they have different UUIDS(same prefix - SecretsManagerTest1)

How can I accomplish this?

1

There are 1 best solutions below

0
On

When bats runs a test suite, it executes a file multiple times, once per test, since each test has its own shell, and once for setup_file.

This in turn means, that the whole file will be source several times, executing the "free code" outside any functions each time.

If you want to retain the same UUID for all tests in a .bats file, you should move the UUID generation into setup_file. Note that setup_file variable that should be visible to the tests must be exported, since the tests run in a subshell.