Skip to content
This repository has been archived by the owner on Apr 29, 2021. It is now read-only.

Ask about variables inside bats files #184

Open
mvalin opened this issue Oct 11, 2016 · 2 comments
Open

Ask about variables inside bats files #184

mvalin opened this issue Oct 11, 2016 · 2 comments

Comments

@mvalin
Copy link

mvalin commented Oct 11, 2016

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

@ztombol
Copy link

ztombol commented Oct 11, 2016

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.

@mvalin
Copy link
Author

mvalin commented Oct 13, 2016

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

jasonkarns pushed a commit to jasonkarns/bats that referenced this issue Apr 28, 2020
This rewrites the old logic a bit by using `type -p`, `readlink -m`  and
on boxes where we don't have coreutils like `readlink` we use `realpath`
to figure out the correct installation directory of bats.

Also fixes GH#182

Signed-off-by: Wesley Schwengle <wesley@schwengle.net>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants