sstephenson/bats

Ask about variables inside bats files

Opened this issue · 2 comments

Hi, I have a question, I realized that you can't use variables in the same ways as bash files inside bats, for instance for the next file:

file=/home/file1
@test "Check $file for WARNING!!!" {
    run grep -qse "WARNING \!\!\!" $file
    [ ${status} -eq 0 ]
}
file=/home/file2
@test "Check $file for WARNING!!!" {
    run grep -qse "WARNING \!\!\!" $file
    [ ${status} -eq 0 ]
}

All the test is done using /home/file2, for both test, although in the first one I set /home/file1. To try to solve that issue I set different variables, as follow:

file1=/home/file1
@test "Check $file1 for WARNING!!!" {
    run grep -qse "WARNING \!\!\!" $file1
    [ ${status} -eq 0 ]
}
file2=/home/file2
@test "Check $file2 for WARNING!!!" {
    run grep -qse "WARNING \!\!\!" $file2
    [ ${status} -eq 0 ]
}

With this workaround works. But I found one situation where the error persist and I couldn't solve using variables, I have to hard code the filenames, the case is the following:

filetail=/etc/motd.tail
@test "Check ${filetail} for WARNING!!!" {
    run grep -qse "WARNING \!\!\!" ${filetail}
    [ ${status} -eq 0 ]
}
filemotd=/etc/motd
@test "Check ${filemotd} for WARNING!!!" {
    run grep -qse "WARNING \!\!\!" ${filemotd}
    [ ${status} -eq 0 ]
}

And the output is as follow:

 ✓ Check /etc/motd for WARNING!!!
 ✓ Check /etc/motd for WARNING!!!

Always, it is used the last valor of the variable, if I change the order, the output is:

 ✓ Check /etc/motd.tail for WARNING!!!
 ✓ Check /etc/motd.tail for WARNING!!!

I don't know if this behavior is by design, but I'd like to use variables, is there a way to solve that or it uses a workaround.

Regards

This is the expected behaviour. This has to do with how bats processes test files, and the explanation is the same as to your other question in #185.

Thank you for your answer, I understand your point, and it is clear, but I can't realize why this doesn't work:

filetail=/etc/motd.tail
@test "Check ${filetail} for WARNING!!!" {
    run grep -qse "WARNING \!\!\!" ${filetail}
    [ ${status} -eq 0 ]
}
filemotd=/etc/motd
@test "Check ${filemotd} for WARNING!!!" {
    run grep -qse "WARNING \!\!\!" ${filemotd}
    [ ${status} -eq 0 ]
}

Because this works with others contents but not with this in particular, I used in other cases, but with this particular content doesn't work.

Do you have any idea?

Regards