jorgebucaran / fishtape

100% pure-Fish test runner
MIT License
346 stars 20 forks source link

Fishtape 2 #31

Closed jorgebucaran closed 5 years ago

jorgebucaran commented 5 years ago

Fishtape 2 will break the test syntax as we want to make it compatible with fish_indent #32, #34.

Summary

Here's the new test syntax I have in mind. See that test blocks are now gone. The test builtin has been "enhanced" to take an optional test description. If you already know how to use the shell's test builtin you already know how to create valid assertions in Fishtape 2.

@test "current directory is home" $PWD = $HOME

@test "math works" 42 -eq (math 41 + 1)

@test "test is a builtin" (contains -- test (builtin -n)) $status -eq 0

for odd in 1 3 5 7 9
    @test "$odd is not an even number" (
        contains -- $odd (seq 10 | awk '!($0%2)')
    ) $status -eq 0
end

@test "git_is_dirty shows changes not staged for commit" (
    pushd /path/to/some/dirty/git/repo
    git_is_dirty
    echo $status
    popd
) = 1

Notice that there is no reason you couldn't write the last test from above as follows (if you prefer):

pushd /path/to/some/dirty/git/repo
git_is_dirty
@test "git_is_dirty shows changes not staged for commit" $status = 1
popd

Issue History

edouard-lopez commented 5 years ago

Sound nice.

I will recommend to use the command substitution syntax test "description" ( … ) in order to isolate test from one another. The last example, with code outside, could have side effects on following test case. I also reckon the former allow one to use local variables but not the letter (I'm on mobile so didn't check this)

jorgebucaran commented 5 years ago

@edouard-lopez To isolate your test you have two options.

  1. Use a begin..end block:
begin
    pushd /path/to/some/dirty/git/repo
    git_is_dirty
    test "git_is_dirty shows changes not staged for commit" $status = 1
    popd
end
  1. Use command substitution:
test "git_is_dirty shows changes not staged for commit" (
    pushd /path/to/some/dirty/git/repo
    git_is_dirty
    echo $status
    popd
) = 1
jorgebucaran commented 5 years ago

Instead of overloading the test builtin I propose using a different syntax for creating tests, e.g. assert or @test. I prefer @test (to take advantage of syntax highlighting).

This helps make clear when you are writing a test or using the test builtin for something else.

if test "$version" = 3.0.0
    @test "Has some file" -f somefile
end
edouard-lopez commented 5 years ago

BATS, the test framework for bash use @test syntax too.

jorgebucaran commented 5 years ago

🎉 I'm done implementing everything. See the changelog: