From 1012452d79235ff26d20b3e83bcb40c62eedbee0 Mon Sep 17 00:00:00 2001 From: Jorge Bucaran Date: Thu, 7 Feb 2019 12:47:48 +0900 Subject: [PATCH] Fishtape 2 (#31) * Make test syntax compatible with fish_indent; #34 #27 * Run test files in parallel (not assertions, but files) * Remove `--pipe` in favor of any userland solution like: `"fish -c "fishtape *.fish | tap-nyan"` * Remove the built-in `test` array extensions --- .gitignore | 2 + .travis.yml | 7 +- LICENSE | 21 - LICENSE.md | 7 + README.md | 151 ++++--- completions/fishtape.fish | 5 - fishtape.fish | 584 ++++++++-------------------- man/man1/fishtape.1 | 183 --------- test/arrays.fish | 36 -- test/fixtures/meta.fish | 15 - test/helpers/select_tap_output.fish | 4 - test/meta.fish | 47 --- test/methods.fish | 16 - test/numbers.fish | 23 -- test/sandbox.fish | 19 - test/scope.fish | 27 -- test/strings.fish | 15 - test/test.fish | 1 + test/variables.fish | 31 -- 19 files changed, 244 insertions(+), 950 deletions(-) create mode 100644 .gitignore delete mode 100644 LICENSE create mode 100644 LICENSE.md delete mode 100644 completions/fishtape.fish delete mode 100644 man/man1/fishtape.1 delete mode 100644 test/arrays.fish delete mode 100644 test/fixtures/meta.fish delete mode 100644 test/helpers/select_tap_output.fish delete mode 100644 test/meta.fish delete mode 100644 test/methods.fish delete mode 100644 test/numbers.fish delete mode 100644 test/sandbox.fish delete mode 100644 test/scope.fish delete mode 100644 test/strings.fish create mode 100644 test/test.fish delete mode 100644 test/variables.fish diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..addff48 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +node_modules +examples diff --git a/.travis.yml b/.travis.yml index dc486ee..8009535 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,9 +1,8 @@ -dist: trusty sudo: required before_install: - - sudo add-apt-repository -y ppa:fish-shell/release-2 + - sudo add-apt-repository -y ppa:fish-shell/nightly-master - sudo apt-get update - sudo apt-get -y install fish script: - - curl https://git.io/fisher --create-dirs -sLo ~/.config/fish/functions/fisher.fish - - fish -c "fisher add jorgebucaran/fishtape .; fishtape test/*.fish" + - curl git.io/fisher --create-dirs -sLo ~/.config/fish/functions/fisher.fish + - fish -c "fisher add . && fishtape test/*.fish" diff --git a/LICENSE b/LICENSE deleted file mode 100644 index 1a6e9b2..0000000 --- a/LICENSE +++ /dev/null @@ -1,21 +0,0 @@ -The MIT License (MIT) - -Copyright (c) 2016 Jorge Bucaran - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..6ba7a0f --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,7 @@ +Copyright © Jorge Bucaran <> + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md index 3931df3..cf28adf 100644 --- a/README.md +++ b/README.md @@ -1,129 +1,124 @@ +> ✋ Psst! Migrating from V1 to V2? Check the [migration guide](https://github.com/fisherman/fishtape/issues/36) and happy upgrading! + # Fishtape -[![Build Status][travis-badge]][travis-link] +[![Build Status](https://img.shields.io/travis/jorgebucaran/fishtape.svg)](https://travis-ci.org/jorgebucaran/fishtape) +[![Releases](https://img.shields.io/github/release/jorgebucaran/fishtape.svg?label=latest)](https://github.com/jorgebucaran/fishtape/releases) + +Fishtape is a TAP-based test runner for the [fish shell](https://fishshell.com). -> ✋ Psst! Fishtape is currently being rewritten. Follow [this issue](https://github.com/fisherman/fishtape/issues/31) for updates and check back soon! +## Features -Fishtape is a [TAP] producing test runner for [fish]. It scans one or more *.fish* files and evaluates test blocks producing a TAP stream. +- Clean syntax derived from the [`test`](https://fishshell.com/docs/current/commands.html#test) builtin +- TAP output—easy to parse & human readable (see [reporting options](#reporting-options)) +- Isolated test environment—tests run concurrently in their own sub-shells ## Install -With [fisher]: +With [Fisher](https://github.com/jorgebucaran/fisher) (recommended): ```fish fisher add jorgebucaran/fishtape ``` -## Usage +
+Not using a package manager? -### Writing Tests +--- -Test files are *.fish* files with one or more test blocks. A test block consists of an optional description and any test expression supported by test(1). +Copy [`fishtape.fish`](fishtape.fish) to any directory on your function path. ```fish -test "current directory is home" - $HOME = $DIRNAME -end +set -q XDG_CONFIG_HOME; or set XDG_CONFIG_HOME ~/.config +curl https://git.io/fishtape.fish --create-dirs -sLo $XDG_CONFIG_HOME/fish/functions/fishtape.fish +``` -test "math still works" - 42 -eq (math 41 + 1) -end +To uninstall, remove the file. -test "test is a builtin" - "test" = (builtin -n) -end +
-test "no odds are evens" - 1 3 5 7 != ( - for i in (seq $n) - if test (math $i%2) = 0 - echo $i - end - end - ) -end -``` +### System Requirements -### Running Tests +- [fish](https://github.com/fish-shell/fish-shell) 2.0+ -Fishtape reads any given files, or the standard input if no files are given, and converts test blocks into valid Fish syntax which is then evaluated, producing a TAP stream. +## Usage -```fish -fishtape path/to/tests/*.fish -``` +Test files are `.fish` files with `@test` definitions. A test definition (or test case) consists of an optional description, followed by one or more operators and their arguments. You can use any operator supported by the [`test`](https://fishshell.com/docs/current/commands.html#test) builtin except for the `-a` and `-o` conditional operators. -## Setup and Teardown +```fish +@test "math works" (math 41 + 1) -eq 42 -Include a setup and teardown method in your test file with code that must be run before and after every test. +@test "extract basename" ( + string split -rm1 / /usr/local/bin/fish +)[-1] = "fish" -### `setup` +@test "test is a builtin" ( + contains -- test (builtin -n) +) $status -eq 0 +``` -Run before each test in the current file. Use setup to load fixtures and/or set up your environment. +Run `fishtape` with one or more test files to run your tests. ```fish -set path $DIRNAME/$TESTNAME +fishtape tests/*.fish +``` -function setup - mkdir -p $path -end +```diff +TAP version 13 +ok 1 - math works +ok 2 - extract basename +ok 3 - test is a builtin + +1..3 +# pass 3 +# ok ``` -### `teardown` +Test files run in the background in a sub-shell while individual test cases run sequentially. Test output is buffered (delivered in batches) until all jobs are complete. If all the test cases pass, `fishtape` exits with status `0`—else, it exits with status `1`. -Run after each test in the current file. Use teardown to clean up loaded resources, etc. +Buffered output means we can't write to stdout or stderr without running into race conditions. To print a TAP message use the special `@mesg` function. ```fish -function teardown - rm -rf $path -end +@mesg "This message is approved by fish—the friendly interactive shell" ``` -## Variables +### Setup and Teardown -The following variables are available inside a test file: +You can define special `setup` and `teardown` functions, which run before and after each test case, respectively. Use them to load fixtures, set up your environment, and clean up when you're done. -### `$FILENAME` - -Path to the running script. - -### `$DIRNAME` +```fish +function setup + set -g tmp (command mktemp -d /tmp/foo.XXXXX) + command mkdir -p $tmp +end -Directory name of the running script. +function teardown + command rm -rf $tmp +end -### `$TESTNAME` +@test "directory is empty" -z ( + pushd $tmp + command ls -1 | command awk '{ ++i } END { print i }' + popd +) +``` -Name of the running script. +### Special Variables -### `$TAP_VERSION` +The following variables are globally available for all test files: -TAP protocol version. +- `$filename` the name of the currently running test file -## Notes +## Reporting Options -### Line Buffered Output +TAP output is easy to parse for machines and still readable for humans. If you are looking for a more sophisticated reporting option, you can pipe `fishtape` to any TAP-compliant reporter, e.g. [tapjs/tap-mocha-reporter](https://github.com/tapjs/tap-mocha-reporter). -According to [fish-shell/#1396], redirections and pipes involving blocks are run serially, not in parallel. This causes fishtape to block the pipeline and buffer all of its output. To emit a line buffered stream use --pipe=*program*. +Redirections and pipes involving blocks are run serially in fish ([fish-shell/#1396](https://github.com/fish-shell/fish-shell/issues/1396)). This means we must run `fishtape` in a subshell for streaming support. ```fish -fishtape test.fish --pipe=tap-nyan +fish -c "fishtape test/*.fish" | tap-mocha-reporter nyan ``` -### Tests - -* Only one expression per test block is allowed. Use command substitutions to create complex test expressions. - -* Each test file is wrapped in `begin; end` blocks under the hood to protect your local scope. In addition, global and universal variables are restored before each test. - -* See [Awesome TAP] for a list of consumers / reporters, tools and other TAP resources. - -[travis-link]: https://travis-ci.org/fisherman/fishtape -[travis-badge]: https://img.shields.io/travis/fisherman/fishtape.svg -[slack-link]: https://fisherman-wharf.herokuapp.com/ -[slack-badge]: https://fisherman-wharf.herokuapp.com/badge.svg +## License -[TAP]: http://testanything.org/ -[fish]: https://github.com/fish-shell/fish-shell -[Awesome TAP]: https://github.com/sindresorhus/awesome-tap -[fisherman]: http://github.com/fisherman/fisherman -[issues]: https://github.com/fisherman/fishtape/issues -[fish-shell/#1396]: https://github.com/fish-shell/fish-shell/issues/1396 +[MIT](LICENSE.md) diff --git a/completions/fishtape.fish b/completions/fishtape.fish deleted file mode 100644 index 4568d22..0000000 --- a/completions/fishtape.fish +++ /dev/null @@ -1,5 +0,0 @@ -complete -c fishtape -s p -l pipe -d "= Pipe line buffered output into command" -complete -c fishtape -s d -l dry-run -d "Print preprocessed files to stdout" -complete -c fishtape -s q -l quiet -d "Enable quiet mode" -complete -c fishtape -s h -l help -d "Show help information" -complete -c fishtape -s v -l version -d "Show version information" diff --git a/fishtape.fish b/fishtape.fish index ae5ade9..da1813d 100644 --- a/fishtape.fish +++ b/fishtape.fish @@ -1,436 +1,168 @@ -function fishtape -d "TAP producer and test harness for fish" - if test -z "$argv" - fishtape -h - return 1 - end - - set -l fishtape_version 1.1.0 +set -g fishtape_version 2.0.0 - set -l files - set -l pipes - set -l print source - set -l error /dev/stderr +complete -xc fishtape -n __fish_use_subcommand -a --help -d "Show usage help" +complete -xc fishtape -n __fish_use_subcommand -a --version -d "$fishtape_version" - printf "%s\n" $argv | sed -E 's/(^--?[a-z]+)=?/\1 /' | while read -l 1 2 - switch "$1" - case --pipe - if test -z "$2" - read 2 +function fishtape -a cmd -d "TAP-based test runner" + if not isatty + if not contains -- $argv @{test,mesg} + set argv $argv - + end + end + switch "$cmd" + case @mesg + echo -e "$argv[2]\tmesg\t$argv[3..-1]" + case @test + if set -q argv[4] + set -l rest (printf "%s\n" $argv[-1..3] | command awk ' + { + args[i++] = $0 + } + END { + right = args[0] + left = is_binary(operator = args[1]) ? args[2] : "" + is = args[i = (left || (left == 0) ? 3 : 2)] == "!" ? "!" : "" + print\ + "- " args[is ? ++i : i] "\n"\ + left "\n" \ + (is ? is" " : "") operator "\n"\ + right "\n"\ + (is ? is"\n" : "") (""left ? left"\n" : "") operator "\n" right + } + function is_binary(s) { + return s ~ /^(!?=|-(eq|ne|gt|ge|lt|le))$/ + } + ') + if test $rest[5..-1] + echo -s {$argv[2],1,$rest[1]}\t + else + echo -s {$argv[2],0,$rest[1],$rest[2],$rest[3],$rest[4]}\t end - - set pipes $pipes $2 - - case -q --quiet - set error /dev/null - - case -n -d --dry-run - set print cat - - case -v --version - printf "fishtape v%s\n" $fishtape_version - return - - case -h --help - __fishtape_usage > /dev/stderr - return - - case -- - - - case -\* - printf "fishtape: '%s' is not a valid option.\n" $1 > /dev/stderr - fishtape -h > /dev/stderr - return 1 - - case \* - if test ! -e "$1" - printf "fishtape: '%s' is not a valid file name\n" $1 > $error + functions -q teardown; and teardown + else + echo -s {$argv[2],todo,$argv[3]\ \#\ TODO}\t + end + case {,-}-v{ersion,} + echo "fishtape version $fishtape_version" + case {,-}-h{elp,} "" + echo "usage: fishtape Run tests in " + echo " fishtape --help Show this help" + echo " fishtape --version Show the current version" + echo "examples:" + echo " fishtape 1) { - - # Runs n times per n+1 files. - # Close matching begin block corresponding to previous file. - # This means each file has private scope for local variables. - - once = 0 - printf("end\n") + end + command awk ' + FNR == 1 { + print (NR > 1 ? end()";" : "") "fish -c \'" + id++ } - - printf("begin\n") - printf("set -l FILENAME %s/%s\n", ENVIRON["PWD"], FILENAME) - - print locals - - if (!once++) { - print reset + !/^[[:space:]]*#/ && $0 { + gsub(/\'/, "\\\\\'") + sub(/\$filename/, f[split(FILENAME, f, "/")]) + sub(/^[[:space:]]*@mesg/, "fishtape @mesg " id) + sub(/^[[:space:]]*@test/, "functions -q setup; and setup; fishtape @test " id) + print } - } - - /^ *#|^ *$/ { next } - - /^ *test */ { - print setup - - indent = index($0, "test") - - $1 = "" - - # Remove inline comments, but allow `#` inside strings. - - sub("#[^\"]*$", "") - - printf("fishtape_test ") - - if ($0 == "" || $0 == " ") { - printf " \"\" " + END { + print end()wait() } - - printf("%s", $0) - - test++ - next - } - - test && /^ *end$/ && (indent == index($0, "end")) { - - # A single test block consists of a pair of test/end keywords - # with matching indentation. Allows you to write tests inside - # fish loops, conditionals, etc. - - test = 0 - - printf("\n") - print count - - printf("fishtape_restore_globals\n") - print teardown - - next - } - - { print } - - ' $files | fish -c "$print" 2>$error -end - -function __fishtape_usage - echo "Usage: fishtape [FILE ...] [(-d | --dry-run)] [--pipe COMMAND]" - echo " [(-q | --quiet)] [(-h | --help)] [(-v | --version)]" - echo -end - -function __fishtape@runtime - function fishtape_assert - set -l count (count $argv) - - if test $count -le 3 - switch "$argv[1]" - case -n - not test -z "$argv[2]" - case \* - test $argv - end - return - end - - for operator in = != - if contains --index -- $operator $argv - break - end - end | read -l index - - switch "$index" - case "" 1 $count - return 1 - end - - for item in $argv[1..(math $index - 1)] - if not contains -- $item $argv[(math $index + 1)..-1] - test $argv[$index] = "!=" - return - end - end - - test $argv[$index] = "=" - end - - function fishtape_cleanup - set --name | grep __fishtape | while read -l var - set -e $var - end - - return 0 - end - - function comment -d "Print a message without breaking the TAP output" - printf "# %s\n" $argv > /dev/stderr - end - - function fishtape_error -a info operator expected received - switch 1 - case (count $argv) - set -l IFS \t - read operator expected received - end - - printf "not ok %s" (math 1 + $__fishtape_count) - - if test -n "$info" - set info " $info" - end - - printf "%s\n" $info - - echo " ---" - echo " operator: $operator" - echo " expected: $expected" - echo " received: $received" - echo " ..." - - return 1 - end - - function fail -a msg -d "Generate a failing assertion with a message" - fishtape_error "$msg" fail success failure - set __fishtape_count (math $__fishtape_count + 1) - set __fishtape_fails (math $__fishtape_fails + 1) - end - - set -g TAP_VERSION 13 - set -g __fishtape_count 0 - set -g __fishtape_fails 0 - - for scope in --global --universal - for var in (set $scope --name) - switch $var - case _\* version umask status history COLUMNS FISH_VERSION LINES PWD SHLVL PATH TMUX TERM - case \* - set -l x u - set -qx $var; and set x x - set $scope __fishtape__"$x"_$var $$var - end - end - end - - printf "TAP version %s\n" $TAP_VERSION - - function pass -a msg -d "Generate a passing assertion with a message" - fishtape_test "$msg" -z "" - set __fishtape_count (math $__fishtape_count + 1) - end - - function fishtape_restore_globals - set read_only_vars "fish_pid" "hostname" - for scope in --global --universal - set $scope --name | sed -nE 's/^__fishtape_(.)_(.*)/\1 \2 &/p' | while read -l x var old_var - if not contains $var $read_only_vars - set -$x $scope $var $$old_var - end - end - end - - return 0 - end - - function fishtape_test -a info - set -e argv[1] - - if fishtape_assert $argv 2>/dev/null - printf "ok %s" (math 1 + $__fishtape_count) - - if test -n "$info" - set info " $info" - end - - printf "%s\n" $info - return - end - - switch "$argv[1]" - case ! - set nix ! - set -e argv[1] - end - - switch "$argv[1]" - case -{z,n,b,c,d,e,f,g,G,L,O,p,r,s,S,t,u,w,x} - printf "%s\t" "$nix$argv[1]" - - if test "$nix" = ! - set nix "not " - end - - switch "$argv[1]" - case -z - printf "%s\t" "$nix""a zero length string" - case -n - printf "%s\t" "$nix""a non-zero length string" - case -b - printf "%s\t" "$nix""a block device" - case -c - printf "%s\t" "$nix""a character device" - case -d - printf "%s\t" "$nix""a directory" - case -e - printf "%s\t" "$nix""a file" - case -f - printf "%s\t" "$nix""a regular file" - case -g - printf "%s\t" "$nix""a file with the set-group-ID bit set" - case -G - printf "%s\t" "$nix""a file with same group ID as the current user" - case -L - printf "%s\t" "$nix""a symbolic link" - case -O - printf "%s\t" "$nix""a file owned by the current user" - case -p - printf "%s\t" "$nix""a named pipe" - case -r - printf "%s\t" "$nix""a file marked as readable" - case -s - printf "%s\t" "$nix""a file size greater than zero" - case -S - printf "%s\t" "$nix""a socket" - case -t - printf "%s\t" "$nix""a terminal tty file descriptor" - case -u - printf "%s\t" "$nix""a file with the set-user-ID bit set" - case -w - printf "%s\t" "$nix""a file marked as writable" - case -x - printf "%s\t" "$nix""a file marked as executable" - - end - - set -e argv[1] - - if test -z "$argv[1]" - set argv[1] "\"\"" - end - - printf "%s\n" $argv[1] - - case \* - switch (count $argv) - case 0 1 - printf "fail\targv > 1\t$argv" - - case \* - if not set -q argv[3] - set argv[3] "" - end - - set -l expected "$argv[1]" - set -l operator "$argv[2]" - set -l received "$argv[3..-1]" - - switch "$operator" - case = != -{eq,ne,gt,ge,lt,le} - case \* - switch "$argv" - case \*" = "\* \*" != "\* - for op in = != - set operator $op - if contains -i -- $op $argv - break - end - end | read -l i - - set expected $argv[1..(math $i-1)] - set received $argv[(math $i+1)..-1] - - case \* - set operator "fail" - set expected "= or !=" - set received "$argv" - end - end - - printf "$operator\t"(printf "'%s' " $expected)"\t"(printf "'%s' " $received)"\n" - end - end | fishtape_error "$info" - end -end - -function __fishtape@count - or set __fishtape_fails (math $__fishtape_fails + 1) - set __fishtape_count (math $__fishtape_count + 1) -end - -function __fishtape@locals - set -l TESTNAME (basename $FILENAME .fish) - set -l DIRNAME (dirname $FILENAME) -end - -function __fishtape@reset - function setup -d "Run before all tests in the current block" - end - - function teardown -d "Run after all tests in the current block" - end -end - -function __fishtape@setup - if not setup - fishtape_error "setup fail" status 0 $status - fishtape_cleanup - exit 1 - end -end - -function __fishtape@teardown - if not teardown - fishtape_error "teardown fail" status 0 $status - fishtape_cleanup - exit 1 - end -end - -function __fishtape@total - if test $__fishtape_count -eq 0 - fishtape_cleanup - exit 1 - end - - printf "\n1..%s\n" $__fishtape_count - printf "# tests %s\n" $__fishtape_count - printf "# pass %s\n" (math $__fishtape_count - $__fishtape_fails) - - if test $__fishtape_fails -gt 0 - printf "# fail %s\n" $__fishtape_fails - fishtape_cleanup - exit 1 + function end() { + return "echo " id "\'&;set -l js $js " jobs(" -l") + } + function wait() { + return ";while for j in $js;contains -- $j "jobs()";and break;end;end" + } + function jobs(opt) { + return "(jobs"opt" | command awk \'/^[0-9]+\\\t/ { print $1 }\')" + } + ' $argv | fish -c source | command awk -F\t ' + BEGIN { + print "TAP version 13" + } + NF == 1 { + for (i = 0; i < count[$1]; i++) { + print\ + (mesg = batch[$1 i "mesg"])\ + ? mesg\ + : sub(/ok/, "ok " ++total, batch[$1 i])\ + ? batch[$1 i] ((error = batch[$1 i "error"]) && ++failed ? "\n" error : "")\ + : "" + todo = (batch[$1 i "todo"]) ? todo + 1 : todo + fflush() + } + } + NF > 1 { + id = $1 count[$1]++ + if ($2 == "mesg") { + batch[id $2] = "# " $3 + } else if ($2) { + batch[id] = batch[id $2] = "ok " $3 + } else { + batch[id] = "not ok " $3 + is = (split($5, ops, " ") && ops[1] == "!" && ($5 = ops[2])) ? "not " : "" + $4 = $4 ? $4\ + : $5 == "-n" \ + ? is "a zero length string" \ + : $5 == "-z" \ + ? is "a non-zero length string" \ + : $5 == "-b" \ + ? is "a block device" \ + : $5 == "-c" \ + ? is "a character device" \ + : $5 == "-d" \ + ? is "a directory" \ + : $5 == "-e" \ + ? is "an existing file" \ + : $5 == "-f" \ + ? is "a regular file" \ + : $5 == "-g" \ + ? is "a file with the set-group-ID bit set" \ + : $5 == "-G" \ + ? is "a file with same group ID as the current user" \ + : $5 == "-L" \ + ? is "a symbolic link" \ + : $5 == "-O" \ + ? is "a file owned by the current user" \ + : $5 == "-p" \ + ? is "a named pipe" \ + : $5 == "-r" \ + ? is "a file marked as readable" \ + : $5 == "-s" \ + ? is "a file of size greater than zero" \ + : $5 == "-S" \ + ? is "a socket" \ + : $5 == "-t" \ + ? is "a terminal tty file descriptor" \ + : $5 == "-u" \ + ? is "a file with the set-user-ID bit set" \ + : $5 == "-w" \ + ? is "a file marked as writable" \ + : $5 == "-x" \ + ? is "a file marked as executable" \ + : $4 + batch[id "error"] =\ + " ---\n"\ + " operator: "(is ? "!" : "")$5"\n"\ + " expected: "$4"\n"\ + " actual: "($6 == "" ? "\"\"" : $6)"\n"\ + " ..." + } + } + END { + print "\n1.." total + print "# pass " (total - failed - todo) + if (failed) print "# fail " failed + if (todo) print "# todo " todo + else if (!failed) print "# ok" + } + ' end - - fishtape_cleanup - printf "\n# ok\n" end diff --git a/man/man1/fishtape.1 b/man/man1/fishtape.1 deleted file mode 100644 index b26c753..0000000 --- a/man/man1/fishtape.1 +++ /dev/null @@ -1,183 +0,0 @@ -. -.TH "FISHTAPE" "1" "February 2016" "" "fishtape" -. -.SH "NAME" -\fBfishtape\fR \- TAP producer and test harness for Fish -. -.SH "SYNOPSIS" -\fBfishtape\fR [\fIfile\fR \.\.\.] -. -.br -\fBfishtape\fR [\fB\-\-pipe\fR=\fIutility\fR] -. -.br -\fBfishtape\fR [\fB\-\-help\fR] [\fB\-\-version\fR] -. -.br -. -.SH "DESCRIPTION" -\fBFishtape\fR is a TAP \fIhttp://testanything\.org/\fR producing test runner for fish\. It scans one or more \fI\.fish\fR files and evaluates test blocks producing a TAP stream\. -. -.SH "USAGE" -. -.SS "WRITING TESTS" -Test files are \fI\.fish\fR files with one or more test blocks\. A test block consists of an optional description and any test expression supported by test(1)\. -. -.IP "" 4 -. -.nf - -test "current directory is home" - $HOME = $DIRNAME -end - -test "math still works" - 42 \-eq (math 41 + 1) -end - -test "test is a builtin" - "test" = (builtin \-n) -end - -test "no odds are evens" - 1 3 5 7 != ( - for i in (seq $n) - if test (math $i%2) = 0 - echo $i - end - end - ) -end -. -.fi -. -.IP "" 0 -. -.SS "RUNNING TESTS" -Fishtape reads any given files, or the standard input if no files are given, and converts test blocks into valid Fish syntax which is then evaluated, producing a TAP stream\. -. -.IP "" 4 -. -.nf - -fishtape path/to/tests/*\.fish -. -.fi -. -.IP "" 0 -. -.SH "SETUP AND TEARDOWN" -Include a setup and teardown method in your test file with code that must be run before and after every test\. -. -.IP "\(bu" 4 -setup -. -.IP "" 0 -. -.P -Run before each test in the current file\. Use setup to load fixtures and/or set up your environment\. -. -.IP "" 4 -. -.nf - -```fish -set path $DIRNAME/$TESTNAME - -function setup - mkdir \-p $path -end -``` -. -.fi -. -.IP "" 0 -. -.IP "\(bu" 4 -teardown -. -.IP "" 0 -. -.P -Run after each test in the current file\. Use teardown to clean up loaded resources, etc\. -. -.IP "" 4 -. -.nf - -```fish -function teardown - rm \-rf $path -end -``` -. -.fi -. -.IP "" 0 -. -.SH "VARIABLES" -The following variables are available inside a test file: -. -.IP "\(bu" 4 -$FILENAME -. -.IP -Path to the running script\. -. -.IP "\(bu" 4 -$DIRNAME -. -.IP -Directory name of the running script\. -. -.IP "\(bu" 4 -$TEST -. -.IP -Name of the running script\. -. -.IP "\(bu" 4 -$TAP_VERSION -. -.IP -TAP protocol version\. -. -.IP "" 0 -. -.SH "NOTES" -. -.SS "LINE BUFFERED OUTPUT" -According to [fish\-shell/#1396][fish\-shell\-1396], redirections and pipes involving blocks are run serially, not in parallel\. This causes fishtape to block the pipeline and buffer all of its output\. To emit a line buffered stream use \-\-pipe=\fIprogram\fR\. -. -.IP "" 4 -. -.nf - -fishtape test\.fish \-\-pipe=tap\-nyan -. -.fi -. -.IP "" 0 -. -.SS "TESTS" -. -.IP "\(bu" 4 -Only one expression per test block is allowed\. Use command substitutions to create complex test expressions\. -. -.IP "\(bu" 4 -Each test file is wrapped in \fBbegin; end\fR blocks under the hood to protect your local scope\. In addition, global and universal variables are restored before each test\. -. -.IP "" 0 -. -.SH "AUTHORS" -Jorge Bucaran \fIj@bucaran\.me\fR\. -. -.SH "SEE ALSO" -. -.IP "\(bu" 4 -\fBtest(1)\fR -. -.IP "\(bu" 4 -\fBhelp expand\-command\-substitution\fR -. -.IP "" 0 diff --git a/test/arrays.fish b/test/arrays.fish deleted file mode 100644 index 4389be6..0000000 --- a/test/arrays.fish +++ /dev/null @@ -1,36 +0,0 @@ -set -l items foo bar baz quux -set -l alien norf - -for item in $items - set -l array $array $item - - test "arrays: ( $array ) in ( $items )" - $array = $items - end - - # The overloaded `=' yields true when all items on the left - # exist at least once on the right side. - - if not set -q array[(count $items)] - test "arrays: ( $items ) != ( $array )" - $items != $array - end - end -end - -test "arrays: $alien does not exist in $items" - $alien != $items -end - -test "arrays: list contains sentence" - "now or never" = a b "now or never" c d -end - -test "arrays: list contains multiple sentences" - "now or never" "just do it" "what's done, is done" = \ - a b "now or never" c d "just do it" e f "what's done, is done" g h -end - -test "arrays: list does not contain at least one of the given words" - word1 word2 != a b c word1 d e f -end diff --git a/test/fixtures/meta.fish b/test/fixtures/meta.fish deleted file mode 100644 index 9bad424..0000000 --- a/test/fixtures/meta.fish +++ /dev/null @@ -1,15 +0,0 @@ -test "one is zero" - 1 -eq 0 -end - -test "nothing is something" - -n "" -end - -test "inline comments" # sure - -z "" -end - -test # description is optional - -z "" -end diff --git a/test/helpers/select_tap_output.fish b/test/helpers/select_tap_output.fish deleted file mode 100644 index 0c542e2..0000000 --- a/test/helpers/select_tap_output.fish +++ /dev/null @@ -1,4 +0,0 @@ -function select_tap_output - # Ignore any non TAP output in the console. - sed -n '/^TAP version 13$/,$p' -end diff --git a/test/meta.fish b/test/meta.fish deleted file mode 100644 index 93fa911..0000000 --- a/test/meta.fish +++ /dev/null @@ -1,47 +0,0 @@ -source $DIRNAME/helpers/select_tap_output.fish - -set -l meta (fishtape $DIRNAME/fixtures/meta.fish | select_tap_output) - -test "$TESTNAME - display failed test yaml data" - (printf "%s\n" $meta \ - | sed -n '3,7p' \ - | xargs) = "--- operator: -eq expected: 1 received: 0 ..." -end - -test "$TESTNAME - display TAP version" - (printf "%s\n" $meta | sed -n '1p') = "TAP version $TAP_VERSION" -end - -test "$TESTNAME - display 1..count" - (printf "%s\n" $meta | sed -n '17p') = "1..4" -end - -test "$TESTNAME - allow # inline comments" # ok - (printf "%s\n" $meta | sed -n '14p' | xargs) = "ok 3 inline comments" -end - -test "$TESTNAME - ok test" - (printf "%s\n" $meta | sed -n '15p' | xargs) = "ok 4" -end - -test "$TESTNAME - display number of tests" - (printf "%s\n" $meta | sed -n '18p' | xargs) = "# tests 4" -end - -test "$TESTNAME - display number of passed tests" - (printf "%s\n" $meta | sed -n '19p' | xargs) = "# pass 2" -end - -test "$TESTNAME - fail test" - (printf "%s\n" $meta | sed -n '2p') = "not ok 1 one is zero" -end - -test "$TESTNAME - display number of failed tests" - (printf "%s\n" $meta | sed -n '20p' | xargs) = "# fail 2" -end - -test "$TESTNAME - -n argument expects argument" - (printf "%s\n" $meta \ - | sed -n '8,13p' \ - | xargs) = "not ok 2 nothing is something --- operator: -n expected: a non-zero length string received: ..." -end diff --git a/test/methods.fish b/test/methods.fish deleted file mode 100644 index 065a3b3..0000000 --- a/test/methods.fish +++ /dev/null @@ -1,16 +0,0 @@ -source $DIRNAME/helpers/select_tap_output.fish - -test "$TESTNAME: print a '#' TAP comment" ( - echo "comment foo 2>&1" | fishtape - | select_tap_output | xargs - ) = "TAP version $TAP_VERSION # foo" -end - -test "$TESTNAME: generate a passing assertion with pass" ( - echo "pass foo" | fishtape - | select_tap_output | xargs - ) = "TAP version $TAP_VERSION ok 1 foo 1..1 # tests 1 # pass 1 # ok" -end - -test "$TESTNAME: generate a failing assertion with fail" ( - echo "fail foo" | fishtape - | select_tap_output | xargs - ) = "TAP version $TAP_VERSION not ok 1 foo --- operator: fail expected: success received: failure ... 1..1 # tests 1 # pass 0 # fail 1" -end diff --git a/test/numbers.fish b/test/numbers.fish deleted file mode 100644 index 8609c89..0000000 --- a/test/numbers.fish +++ /dev/null @@ -1,23 +0,0 @@ -test "$TESTNAME: 1 equals 1" - 1 -eq 1 -end - -test "$TESTNAME: 1 not equals 0" - 1 -ne 0 -end - -test "$TESTNAME: 1 > 0" - 1 -gt 0 -end - -test "$TESTNAME: 1 >= 1" - 1 -ge 1 -end - -test "$TESTNAME: 1 < 2" - 1 -lt 2 -end - -test "$TESTNAME: -1 <= 0" - -1 -le 0 -end diff --git a/test/sandbox.fish b/test/sandbox.fish deleted file mode 100644 index 8ac0eba..0000000 --- a/test/sandbox.fish +++ /dev/null @@ -1,19 +0,0 @@ -set -l tally 0 -set -l proof false - -function setup -S - set proof true - set tally (math 1 + $tally) -end - -function teardown -S - pass "$TESTNAME: teardown is called after running tests" -end - -test "$TESTNAME: setup is called before running tests" - $proof = true -end - -test "$TESTNAME: setup is called once per *every* test" - $tally -eq 2 -end diff --git a/test/scope.fish b/test/scope.fish deleted file mode 100644 index 8f9cf78..0000000 --- a/test/scope.fish +++ /dev/null @@ -1,27 +0,0 @@ -set -g SHELL foo - -function setup - set -g USER foo -end - -function teardown -S - set -l msg "$TESTNAME: globals are restored before teardown" - - if test $USER != foo - pass $msg - else - fail $msg - end -end - -test "$TESTNAME: globals modified outside `setup` are set only once #1" - $SHELL = foo -end - -test "$TESTNAME: globals modified outside `setup` are set only once #2" - $SHELL != foo -end - -test "$TESTNAME: globals modified inside `setup` are set once per each test" - $USER = foo -end diff --git a/test/strings.fish b/test/strings.fish deleted file mode 100644 index 0ab51f8..0000000 --- a/test/strings.fish +++ /dev/null @@ -1,15 +0,0 @@ -test "$TESTNAME: = operator" - "foo bar" = "foo bar" -end - -test "$TESTNAME: != operator" - foo != bar -end - -test "$TESTNAME: -z operator" - -z "" -end - -test "$TESTNAME: -n operator" - -n "foo" -end diff --git a/test/test.fish b/test/test.fish new file mode 100644 index 0000000..188523e --- /dev/null +++ b/test/test.fish @@ -0,0 +1 @@ +@test "Add tests" diff --git a/test/variables.fish b/test/variables.fish deleted file mode 100644 index 1a002dd..0000000 --- a/test/variables.fish +++ /dev/null @@ -1,31 +0,0 @@ -test "$TESTNAME: TESTNAME var is defined" - (set -q TESTNAME) $status -eq 0 -end - -test "$TESTNAME: DIRNAME var is defined" - (set -q DIRNAME) $status -eq 0 -end - -test "$TESTNAME: FILENAME var is defined" - (set -q FILENAME) $status -eq 0 -end - -test "$TESTNAME: FILENAME is the path to the running test script" - -n (printf "%s" "$FILENAME" | grep variables) -end - -test "$TESTNAME: DIRNAME is the directory name FILENAME" - $DIRNAME = (dirname $FILENAME) -end - -test "$TESTNAME: TESTNAME is the name of the running script" - $TESTNAME = variables -end - -test "$TESTNAME: DIRNAME is a directory" - -d $DIRNAME -end - -test "$TESTNAME: FILENAME is a file" - -e $FILENAME -end