Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add command line tests #5

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@

jinja2

jinja2-strcase
62 changes: 62 additions & 0 deletions tests/fhtagn-0.1.0/.github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
on:
- "push"

name: "Run tests"

jobs:
nix:
name: "*nix - all awks"
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ ubuntu-20.04, ubuntu-22.04 ]
steps:
- uses: actions/checkout@v3

- uses: actions/cache@v3
id: cache-soft
with:
path: soft
key: ${{ matrix.os }}-${{ hashFiles('Makesurefile') }}-soft--all-2

- name: "run tests"
run: |
./makesure

macos:
name: "macOS"
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ macos-11, macos-12, macos-13 ]
steps:
- uses: actions/checkout@v3

- uses: actions/cache@v3
id: cache-soft
with:
path: soft
key: ${{ matrix.os }}-soft--macos

- name: "run tests"
run: |
./makesure tested_by_default_awk

win:
name: "Win"
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ windows-2019, windows-2022 ]
steps:
- uses: actions/checkout@v3

- uses: actions/cache@v3
id: cache-soft
with:
path: soft
key: ${{ matrix.os }}-soft--win

- name: "run tests"
run: |
& bash -e -c "./makesure tested_by_default_awk"
3 changes: 3 additions & 0 deletions tests/fhtagn-0.1.0/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/*.iml
/.idea/
/soft/
21 changes: 21 additions & 0 deletions tests/fhtagn-0.1.0/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Volodymyr Gubarkov

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.
188 changes: 188 additions & 0 deletions tests/fhtagn-0.1.0/Makesurefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
# vim: syntax=bash
@options timing

@define TUSH_REPO='https://github.com/adolfopa/tush'
@define GOAWK_VERSION='1.23.1'
@define GOAWK="goawk$GOAWK_VERSION"

@goal soft_folder_created @private
@reached_if [[ -d "soft" ]]
mkdir soft

@goal tush_installed @private
@depends_on soft_folder_created
@reached_if [[ -f "soft/tush/bin/tush-check" ]]
echo
echo "Fetching tush..."
echo

cd "soft"

if command -v wget >/dev/null
then
wget $TUSH_REPO/archive/master.tar.gz -O./tush.tar.gz
tar xzvf ./tush.tar.gz
rm ./tush.tar.gz
mv tush-master tush
elif command -v curl >/dev/null
then
curl -L $TUSH_REPO/archive/master.tar.gz -o ./tush.tar.gz
tar xzvf ./tush.tar.gz
rm ./tush.tar.gz
mv tush-master tush
else
git clone --depth 1 $TUSH_REPO.git
rm -r tush/.git
fi

@goal default
@depends_on tested

@goal tested
@depends_on tested_by_default_awk
@depends_on tested_by_bwk
@depends_on tested_by_gawk
@depends_on tested_by @args 'tush' 'mawk -f ./fhtagn.awk'
@depends_on tested_by @args 'fhtagn' 'mawk -f ./fhtagn.awk'
@depends_on tested_by_busybox_awk
#@depends_on tested_by_goawk

@goal tested_by_bwk
@depends_on installed_bwk
@depends_on tested_by @args 'tush' './soft/bwk -f ./fhtagn.awk'
@depends_on tested_by @args 'fhtagn' './soft/bwk -f ./fhtagn.awk'

@goal tested_by_gawk
@depends_on tested_by @args 'tush' 'gawk -f ./fhtagn.awk'
@depends_on tested_by @args 'fhtagn' 'gawk -f ./fhtagn.awk'

@goal tested_by_default_awk
@depends_on tested_by @args 'tush' './fhtagn.awk'
@depends_on tested_by @args 'fhtagn' './fhtagn.awk'

@goal tested_by_busybox_awk
@depends_on installed_busybox
@depends_on tested_by @args 'tush' './soft/busybox awk -f ./fhtagn.awk'
@depends_on tested_by @args 'fhtagn' './soft/busybox awk -f ./fhtagn.awk'

@goal tested_by_goawk
@depends_on installed_goawk
@depends_on tested_by @args 'tush' './soft/goawk1.23.1 -f ./fhtagn.awk'
@depends_on tested_by @args 'fhtagn' './soft/goawk1.23.1 -f ./fhtagn.awk'

@goal tested_by @params TOOL FHTAGN
@depends_on tush_installed
f=tests/fhtagn.tush

echo "Testing with $TOOL ($FHTAGN)..."
export FHTAGN

calc_temp_files() {
local tmp_count=$(find /tmp -maxdepth 1 -type f -name 'fhtagn.*' | wc -l)
local cnt
(( cnt = tmp_count ))
if [[ -d "/dev/shm" ]]
then
local shm_count=$(find /dev/shm -maxdepth 1 -type f -name 'fhtagn.*' | wc -l)
(( cnt += shm_count ))
fi
echo $cnt
}

test_file() {
if [[ $TOOL == "tush" ]]
then
export PATH="$PATH:$MYDIR/soft/tush/bin"
DIFF="diff --strip-trailing-cr" tush-check "$f"
else
local before_count=$(calc_temp_files)
./fhtagn.awk "$f"
local after_count=$(calc_temp_files)
if (( before_count != after_count ))
then
echo >&2 "!!! temp file not deleted !!!"
exit 1
fi
fi
}

if test_file
then
echo "TESTS PASSED : $f"
else
echo >&2 "!!! TESTS FAILED !!! : $f"
exit 1
fi

@goal installed_bwk @private
@reached_if [[ -f soft/bwk ]]
@depends_on soft_folder_created
echo
echo "Fetching BWK..."
echo

cd "soft"

wget https://github.com/onetrueawk/awk/archive/refs/heads/master.tar.gz -Obwk.tar.gz
tar xzvf bwk.tar.gz
rm bwk.tar.gz

echo
echo "Compile BWK..."
echo

cd "awk-master"

make

mv a.out ../bwk

cd ..
./bwk --version
rm -r awk-master

@goal installed_busybox @private
@reached_if [[ -x ./soft/busybox ]]
wget 'https://busybox.net/downloads/binaries/1.31.0-i686-uclibc/busybox' -O ./soft/busybox
chmod +x ./soft/busybox
echo "Installed: $(./soft/busybox | head -n 1)"

@goal installed_goawk @private
@reached_if [[ -f soft/$GOAWK ]]
@depends_on soft_folder_created
echo
echo "Fetching GoAWK $GOAWK_VERSION ..."
echo

cd "soft"

if [[ "$OSTYPE" == "linux-gnu"* ]]; then
os="linux"
elif [[ "$OSTYPE" == "darwin"* ]]; then
os="darwin"
else
>&2 echo "Unknown OS"
exit 1
fi

F=goawk_v${GOAWK_VERSION}_${os}_amd64.tar.gz
wget "https://github.com/benhoyt/goawk/releases/download/v$GOAWK_VERSION/$F"
tar xzvf "$F" goawk
rm "$F"

mv goawk $GOAWK
"./$GOAWK" --version

@goal update_readme
@doc 'updates the README.md with the current output of the tool'
awk '
!Off
Examples {
if (/^\$/) { Off=1; system(substr($0,2)) }
else if (/^```$/ && Off) { Off=0; print }
}
/## Usage/ { Examples=1 }
' README.md > README.md.1
mv README.md.1 README.md


86 changes: 86 additions & 0 deletions tests/fhtagn-0.1.0/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# fhtagn

[![Run tests](https://github.com/xonixx/fhtagn/actions/workflows/run-tests.yml/badge.svg)](https://github.com/xonixx/fhtagn/actions/workflows/run-tests.yml)

`fhtagn.awk` is a tiny CLI tool for literate testing for command-line programs.

File `tests.tush`:
```
$ command --that --should --execute correctly
| expected stdout output

$ command --that --will --cause error
@ expected stderr output
? expected-exit-code
```

Run the tests:
```shell
./fhtagn.awk tests.tush
```

In fact this is a re-implementation of [darius/tush](https://github.com/darius/tush), [adolfopa/tush](https://github.com/adolfopa/tush).
But simpler (single tiny AWK script) and faster, because:

- it uses `/dev/shm` where available instead of `/tmp`
- it compares the expected result with the actual in the code and only calls `diff` to show the difference if they don't match
- it doesn't create a sandbox folder for each test
- it doesn't use `mktemp` but rather generates random name in the code

## Usage

```
./fhtagn.awk f1.tush [ f2.tush [ f3.tush [...] ] ]
```
This will stop on the first error found.

Example:
```
$ ./fhtagn.awk tests/1.tush tests/2.tush tests/3.tush
tests/2.tush:10: $ echo "hello world"; echo "error msg" >&2; exit 7
--- expected
+++ actual
@@ -1,4 +1,4 @@
| hello world
-@ error msg 444
-? 8
+@ error msg
+? 7

```

### Fail at the end

Set `ALL=1` environment variable.

This runs all tests, collects all errors, and shows the stats at the end.
```
ALL=1 ./fhtagn.awk f1.tush [ f2.tush [ f3.tush [...] ] ]
```

Useful for running in CI.

Example:
```
$ ALL=1 ./fhtagn.awk tests/1.tush tests/2.tush tests/3.tush
tests/2.tush:10: $ echo "hello world"; echo "error msg" >&2; exit 7
--- expected
+++ actual
@@ -1,4 +1,4 @@
| hello world
-@ error msg 444
-? 8
+@ error msg
+? 7

tests/3.tush:4: $ echo bbb
--- expected
+++ actual
@@ -1,2 +1,2 @@
-| BBB
+| bbb

result=FAIL, failure=2, success=4, total=6, files=3
```


Loading