From 509afedf3b938a4b478001b772ad0ef1f9f5c7bc Mon Sep 17 00:00:00 2001 From: Leonard Jonathan Oh Date: Fri, 28 Oct 2022 04:33:28 +0000 Subject: [PATCH] Enhancement (ci): Add unit tests This fixes a few bugs like matching file extensions, `clean` subcommand behavior, and adds support for `busybox`. --- .github/workflows/ci-master-pr.yml | 12 +++++-- .gitignore | 3 +- .vscode/settings.json | 6 ++++ .vscode/tasks.json | 6 ++++ README.md | 6 ++-- test.sh | 3 ++ webize | 53 +++++++++++++++--------------- webize.bats | 48 +++++++++++++++++++++++++++ 8 files changed, 105 insertions(+), 32 deletions(-) create mode 100644 .vscode/settings.json create mode 100755 test.sh create mode 100644 webize.bats diff --git a/.github/workflows/ci-master-pr.yml b/.github/workflows/ci-master-pr.yml index 96b7650..42e4cd7 100644 --- a/.github/workflows/ci-master-pr.yml +++ b/.github/workflows/ci-master-pr.yml @@ -11,8 +11,16 @@ on: - master jobs: + test: + runs-on: ubuntu-latest + container: + image: bats/bats:1.7.0 + steps: + - uses: actions/checkout@v3 + - run: bats *.bats + update-draft-release: - # needs: [test-powershell-5-1-windows-2016, test-powershell-5-1-windows-2019, test-powershell-6-0, test-powershell-6-1, test-powershell-6-1, test-powershell-7-0, test-powershell-7-1, test-powershell-7-2] + needs: [test] if: github.ref == 'refs/heads/master' runs-on: ubuntu-latest steps: @@ -22,7 +30,7 @@ jobs: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} publish-draft-release: - # needs: [test-powershell-5-1-windows-2016, test-powershell-5-1-windows-2019, test-powershell-6-0, test-powershell-6-1, test-powershell-6-1, test-powershell-7-0, test-powershell-7-1, test-powershell-7-2] + needs: [test] if: startsWith(github.ref, 'refs/tags/') runs-on: ubuntu-latest steps: diff --git a/.gitignore b/.gitignore index 711c6a0..d02b23d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,7 @@ /.* - !/.env !/.github !/.gitignore !/.vscode -index.htm +/index.htm diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..ca17fe1 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,6 @@ +{ + "files.associations": { + "*.bats": "shellscript", + "*.env": "shellscript" + } +} diff --git a/.vscode/tasks.json b/.vscode/tasks.json index c16e3f9..bb5f210 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -15,6 +15,12 @@ "command": "docker-compose -f docker-compose.traefik.yml up", "group": "build" }, + { + "label": "Test", + "type": "shell", + "command": "./test.sh", + "group": "build" + }, { "label": "./webize gallery .", "type": "shell", diff --git a/README.md b/README.md index 610b2d7..51bb470 100644 --- a/README.md +++ b/README.md @@ -73,8 +73,10 @@ Keyboard shortcuts: Available as [vscode tasks](.vscode/tasks.json). ```sh -cd /path/to/webize -# First, make some pictures available +# Test script +./test.sh + +# To test the gallery, make some pictures available cp -rf /path/to/pictures .pictures # Debug (Local browser) diff --git a/test.sh b/test.sh new file mode 100755 index 0000000..c64b4e8 --- /dev/null +++ b/test.sh @@ -0,0 +1,3 @@ +#!/bin/sh +set -eu +docker run --rm -it -v $(pwd):/code bats/bats:1.7.0 *.bats diff --git a/webize b/webize index 4f5be60..2f4f56c 100755 --- a/webize +++ b/webize @@ -3,22 +3,19 @@ set -u usage() { - echo 'Usage: ./webize.sh [OPTIONS]' - echo ' Subcommands:' - echo ' gallery Generate a gallery index.htm' - echo ' clean Removes all index.htm' + echo 'Usage: ./webize [OPTIONS]' + echo 'Subcommands:' + echo ' gallery Generate a gallery index.htm' + echo ' clean Removes index.htm' echo 'Examples: ' - echo ' # Generate a gallery index.htm for current directory and all descendent folders which contain images' - echo ' ./webize.sh gallery .' + echo ' # Generate a gallery index.htm for current directory and all descendent folders which contain images' + echo ' ./webize gallery .' echo - echo ' # Generate a gallery index.htm for /path/to/camera/roll and all descendent folders which contain images, and open them in firefox' - echo ' ./webize.sh gallery /path/to/camera/roll | xargs firefox' + echo ' # Generate a gallery index.htm for /path/to/camera/roll and all descendent folders which contain images, and open them in firefox' + echo ' ./webize gallery /path/to/camera/roll | xargs firefox' echo - echo ' # Clean all index.htm for current directory and all descendent folders which contain images' - echo ' ./webize.sh clean .' - echo - echo ' # Clean all index.htm for /path/to/camera/roll and all descendent folders which contain images' - echo ' ./webize.sh clean /path/to/camera/roll' + echo ' # Clean index.htm in current directory' + echo ' ./webize clean .' } # Exit if we got no options if [ $# -eq 0 ]; then usage; exit 1; fi @@ -73,7 +70,7 @@ if [ "$DIR" = '.' ]; then fi # Get directories with images -IMAGES_REGEX='\.gif|\.jpg|\.png|\.svg|\.webp' +IMAGES_REGEX='(\.gif|\.jpe?g|\.png|\.svg|\.webp)$' DIRS=$( find "$DIR" -type d | while read -r d; do if ls -p "$d" | grep -v '/' | grep -iE "$IMAGES_REGEX" > /dev/null; then @@ -86,20 +83,22 @@ if [ -z "$DIRS" ]; then exit 1 fi -# Generate index.htm in each directory +# Generate a index.htm in current directory if [ -n "$GALLERY" ]; then INDEX_HTM="index.htm" IMAGES_CSV=$( echo "$DIRS" | while read -r d; do - - # Get only files, ignore the first line of ls -al, then parse file attributes into a single CSV. E.g. -rwxr-xr-x 1 packer packer 22681 2021-08-27T07:36:45+0000 webize -> webize,22681,2021-08-27T07:36:45+0000 + # Get only files, ignore the first line of ls -al, then parse file attributes into a single CSV + # E.g.: + # -rwxr-xr-x 1 user user 22681 2021-08-27T07:36:45+0000 webize + # Becomes: + # webize,22681,2021-08-27T07:36:45+0000 if echo "${OSTYPE:-}" | grep -qE 'darwin'; then cd "$d" files=$( stat -l -t '%Y-%m-%dT%H:%M:%S%z' * ) else - files=$( ls -al --time-style='+%Y-%m-%dT%H:%M:%S%z' -p "$d" ) + files=$( ls -al --time-style='+%Y-%m-%dT%H:%M:%S%z' -p "$d" 2>/dev/null || ls -al --full-time -p "$d" | awk '{print $1,$2,$3,$4,$5,$6$7$8,$9}' ) fi - echo "$files" | grep -v '/' | grep -iE "$IMAGES_REGEX" | while read -r l; do size=$( echo "$l" | awk '{print $5}' ) dateiso=$( echo "$l" | awk '{print $6}' ) @@ -109,6 +108,10 @@ if [ -n "$GALLERY" ]; then done done ) + if [ -z "$IMAGES_CSV" ]; then + echo "Couldn't obtain images' metadata" + exit 1 + fi cat - > "$INDEX_HTM" <<'EOF' @@ -920,11 +923,9 @@ fi # Remove index.htm in each directory if [ -n "$CLEAN" ]; then - echo "$DIRS" | while read -r d; do - INDEX_HTM="$d/index.htm" - if [ -f "$INDEX_HTM" ]; then - rm -f "$INDEX_HTM" - echo "$INDEX_HTM" - fi - done + INDEX_HTM="$DIR/index.htm" + if [ -f "$INDEX_HTM" ]; then + rm -f "$INDEX_HTM" + echo "$INDEX_HTM" + fi fi diff --git a/webize.bats b/webize.bats new file mode 100644 index 0000000..6e7caba --- /dev/null +++ b/webize.bats @@ -0,0 +1,48 @@ +setup() { + mkdir -p "$BATS_FILE_TMPDIR/pictures" + touch "$BATS_FILE_TMPDIR/pictures/01.gif" + touch "$BATS_FILE_TMPDIR/pictures/02.GIF" + touch "$BATS_FILE_TMPDIR/pictures/03.jpg" + touch "$BATS_FILE_TMPDIR/pictures/04.jpeg" + touch "$BATS_FILE_TMPDIR/pictures/05.JPG" + touch "$BATS_FILE_TMPDIR/pictures/06.JPEG" + touch "$BATS_FILE_TMPDIR/pictures/07.png" + touch "$BATS_FILE_TMPDIR/pictures/08.PNG" + touch "$BATS_FILE_TMPDIR/pictures/09.svg" + touch "$BATS_FILE_TMPDIR/pictures/10.SVG" + touch "$BATS_FILE_TMPDIR/pictures/11.webp" + touch "$BATS_FILE_TMPDIR/pictures/12.WEBP" +} + +teardown() { + rm -rf "$BATS_FILE_TMPDIR/pictures" +} + +@test "Runs our script" { + output=$( "$BATS_TEST_DIRNAME/webize" --help ) + + echo "$output" | grep 'webize' +} + +@test "Exits if subcommand is missing" { + run "$BATS_TEST_DIRNAME/webize" + + [ "$status" = 1 ] +} + +@test "gallery - Generates index.htm with correct number of images" { + run "$BATS_TEST_DIRNAME/webize" gallery "$BATS_FILE_TMPDIR/pictures" + + [ "$status" = 0 ] + [ -f index.htm ] + [ -s index.htm ] + [ $( cat index.htm | grep 'const IMAGES_CSV' -A12 | grep -Ei '(\.gif|\.jpe?g|\.png|\.svg|\.webp)$' | wc -l ) = 12 ] +} + +@test "clean - Cleans index.htm" { + touch index.htm + run "$BATS_TEST_DIRNAME/webize" clean . + + [ "$status" = 0 ] + [ ! -f index.htm ] +}