Skip to content

Commit

Permalink
Support failed not (!) tests (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattmc3 authored Jan 22, 2021
1 parent b954372 commit 4e62025
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ Tests are defined with the `@test` function. Each test begins with a description
@<a href=#writing-tests>test</a> <i>description</i> [<i>actual</i>] <a href=https://fishshell.com/docs/current/cmds/test.html#operators-for-files-and-directories>operator</a> <i>expected</i>
</pre>

> Operators to combine expressions are not currently supported: `!`, `-a`, `-o`.
> Operators to combine expressions are not currently supported: `-a`, `-o`.
Sometimes you need to test the exit status of running one or more commands and for that, you use command substitutions. Just make sure to suppress stdout to avoid cluttering your `test` expression.

Expand Down
12 changes: 9 additions & 3 deletions functions/fishtape.fish
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,19 @@ function fishtape --description "Test scripts, functions, and plugins in Fish"
string replace --filter --regex -- "\s+called on line (\d+) of file (.+)" '$2:$1' |
read --local at

if test $argv[1] = "!"
set operator "! "
set negation "not "
set --erase argv[1]
end

if set --query argv[3]
set operator $argv[2]
set operator "$operator"$argv[2]
set expected (string escape -- $argv[3])
set actual (string escape -- $argv[1])
else
set operator $argv[1]
set expected $expectations[(contains --index -- $operator $operators)]
set operator "$operator"$argv[1]
set expected "$negation"$expectations[(contains --index -- $argv[1] $operators)]
set actual (string escape -- $argv[2])
end

Expand Down
2 changes: 2 additions & 0 deletions tests/files.fish
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ set temp (command mktemp -d)
builtin cd $temp

@test "a directory" -d $temp
@test "a non-existent directory" ! -d $temp.fake
@test "a regular file" (command touch file) -f file
@test "a non-existent regular file" ! -f file.fake
@test "nothing to see here" -z (read <file)

command rm -rf $temp

0 comments on commit 4e62025

Please sign in to comment.