Skip to content

Commit

Permalink
Merge pull request #50 from theohbrothers/enhancement/ci-add-unit-tests
Browse files Browse the repository at this point in the history
Enhancement (ci): Add unit tests
  • Loading branch information
leojonathanoh authored Oct 28, 2022
2 parents f8085b7 + 509afed commit c22be1e
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 32 deletions.
12 changes: 10 additions & 2 deletions .github/workflows/ci-master-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/.*

!/.env
!/.github
!/.gitignore
!/.vscode

index.htm
/index.htm
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"files.associations": {
"*.bats": "shellscript",
"*.env": "shellscript"
}
}
6 changes: 6 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh
set -eu
docker run --rm -it -v $(pwd):/code bats/bats:1.7.0 *.bats
53 changes: 27 additions & 26 deletions webize
Original file line number Diff line number Diff line change
Expand Up @@ -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 <dir> Generate a gallery index.htm'
echo ' clean <dir> 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
Expand Down Expand Up @@ -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
Expand All @@ -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}' )
Expand All @@ -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'
<html lang="en">
<head>
Expand Down Expand Up @@ -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
48 changes: 48 additions & 0 deletions webize.bats
Original file line number Diff line number Diff line change
@@ -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 ]
}

0 comments on commit c22be1e

Please sign in to comment.