diff --git a/.github/workflows/test-linux.yml b/.github/workflows/test-linux.yml new file mode 100644 index 0000000..4e2e5d2 --- /dev/null +++ b/.github/workflows/test-linux.yml @@ -0,0 +1,30 @@ +--- +name: test-linux +on: + pull_request: + push: + branches: + - master + tags: + +jobs: + test: + runs-on: ubuntu-latest + strategy: + fail-fast: False + + name: "[test] [linux]" + steps: + # ------------------------------------------------------------ + # Setup + # ------------------------------------------------------------ + - name: Checkout repository + uses: actions/checkout@v2 + + # ------------------------------------------------------------ + # Tests: Behaviour + # ------------------------------------------------------------ + - name: test + shell: bash + run: | + make test diff --git a/.github/workflows/test-macos.yml b/.github/workflows/test-macos.yml new file mode 100644 index 0000000..df5493e --- /dev/null +++ b/.github/workflows/test-macos.yml @@ -0,0 +1,30 @@ +--- +name: test-macos +on: + pull_request: + push: + branches: + - master + tags: + +jobs: + test: + runs-on: macos-latest + strategy: + fail-fast: False + + name: "[test] [macos]" + steps: + # ------------------------------------------------------------ + # Setup + # ------------------------------------------------------------ + - name: Checkout repository + uses: actions/checkout@v2 + + # ------------------------------------------------------------ + # Tests: Behaviour + # ------------------------------------------------------------ + - name: test + shell: bash + run: | + make test diff --git a/.github/workflows/test-windows.yml b/.github/workflows/test-windows.yml new file mode 100644 index 0000000..911d841 --- /dev/null +++ b/.github/workflows/test-windows.yml @@ -0,0 +1,30 @@ +--- +name: test-windows +on: + pull_request: + push: + branches: + - master + tags: + +jobs: + test: + runs-on: windows-latest + strategy: + fail-fast: False + + name: "[test] [windows]" + steps: + # ------------------------------------------------------------ + # Setup + # ------------------------------------------------------------ + - name: Checkout repository + uses: actions/checkout@v2 + + # ------------------------------------------------------------ + # Tests: Behaviour + # ------------------------------------------------------------ + - name: test + shell: bash + run: | + make test diff --git a/Makefile b/Makefile index 9486515..5412d17 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ endif # ------------------------------------------------------------------------------------------------- # Default configuration # ------------------------------------------------------------------------------------------------- -.PHONY: help lint +.PHONY: help lint test FL_VERSION = 0.3 FL_IGNORES = .git/,.github/ @@ -16,6 +16,7 @@ FL_IGNORES = .git/,.github/ # ------------------------------------------------------------------------------------------------- help: @echo "lint Lint repository" + @echo "test Run integration tests" # ------------------------------------------------------------------------------------------------- @@ -43,3 +44,10 @@ _lint-shell: @echo "# Shellcheck" @echo "# -------------------------------------------------------------------- #" @docker run --rm $$(tty -s && echo "-it" || echo) -v $(PWD):/mnt -w /mnt koalaman/shellcheck:stable watcherd + + +# ------------------------------------------------------------------------------------------------- +# Test Targets +# ------------------------------------------------------------------------------------------------- +test: + ./test/01.sh diff --git a/README.md b/README.md index 3599ec0..fbd69e7 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,10 @@ # watcherd +![tag](https://img.shields.io/github/v/tag/devilbox/watcherd.svg?colorB=orange&sort=semver) [![linting](https://github.com/devilbox/watcherd/workflows/linting/badge.svg)](https://github.com/devilbox/watcherd/actions?query=workflow%3Alinting) +[![test-linux](https://github.com/devilbox/watcherd/workflows/test-linux/badge.svg)](https://github.com/devilbox/watcherd/actions?query=workflow%3Atest-linux) +[![test-macos](https://github.com/devilbox/watcherd/workflows/test-macos/badge.svg)](https://github.com/devilbox/watcherd/actions?query=workflow%3Atest-macos) +[![test-windows](https://github.com/devilbox/watcherd/workflows/test-windows/badge.svg)](https://github.com/devilbox/watcherd/actions?query=workflow%3Atest-windows) **[watcherd](https://github.com/devilbox/watcherp/blob/master/watcherd)** will look for directory changes (added and deleted directories) under the specified path (`-p`) and will execute specified commands or shell scripts (`-a`, `-d`) depending on the event. Once all events have happened during one round (`-i`), a trigger command can be executed (`-t`). diff --git a/test/01.sh b/test/01.sh new file mode 100755 index 0000000..3e9b257 --- /dev/null +++ b/test/01.sh @@ -0,0 +1,73 @@ +#!/usr/bin/env bash + +set -e +set -u +set -o pipefail + +SCRIPT_PATH="$( cd "$(dirname "$0")" && pwd -P )" + +BIN_PATH="${SCRIPT_PATH}/.." +DIR_PATH="${SCRIPT_PATH}/dirs" + + + +cleanup() { + rm -rf "${DIR_PATH}" || true + rm -rf "${SCRIPT_PATH}/01.actual" || true + rm -rf "${SCRIPT_PATH}/01.expected" || true +} + + +### +### 01. Clean and create test dirs +### +cleanup +mkdir -p "${DIR_PATH}/dir 1" +mkdir -p "${DIR_PATH}/dir 2" +mkdir -p "${DIR_PATH}/dir 3" +mkdir -p "${DIR_PATH}/dir 4" +mkdir -p "${DIR_PATH}/dir 4/subdir" +touch "${DIR_PATH}/file 1" +touch "${DIR_PATH}/file 2" + + +### +### 02. Setup expected +### +{ + echo "add: ./dir 1"; + echo "add: ./dir 2"; + echo "add: ./dir 3"; + echo "add: ./dir 4"; +} > "${SCRIPT_PATH}/01.expected" + + +### +### 03. Run watcherd +### +cd "${DIR_PATH}" +"${BIN_PATH}/watcherd" -p "." -a "echo 'add: %p'" -d "echo 'del: %p'" > "${SCRIPT_PATH}/01.actual" & +watch_pid="${!}" +echo "Started watcherd with pid: ${watch_pid}" +echo "Waiting 5 sec." +sleep 5 + + + +### +### 04 .Compare results and shutdown +### +echo "Diff results" +if ! diff "${SCRIPT_PATH}/01.actual" "${SCRIPT_PATH}/01.expected"; then + echo "[ERR] Results did not equal" + echo "Killing watcherd" + kill "${watch_pid}" || true + cleanup + exit 1 +fi + +echo "[OK] Results equal." +echo "Killing watcherd" +kill "${watch_pid}" || true +cleanup +exit 0 diff --git a/watcherd b/watcherd index f2350c1..f8b1c0b 100755 --- a/watcherd +++ b/watcherd @@ -25,11 +25,11 @@ IFS=$'\n' # Versioning MY_NAME="watcherd" -MY_DATE="2017-09-30" +MY_DATE="2020-12-11" MY_URL="https://github.com/devilbox/watcherd" MY_AUTHOR="cytopia " MY_GPGKEY="0xA02C56F0" -MY_VERSION="0.1" +MY_VERSION="1.0.2" MY_LICENSE="MIT" # Default settings @@ -100,10 +100,21 @@ function print_version() { function get_subdirs() { local path="${1}" # shellcheck disable=SC2016 - (find "${path}" || true) \ - | grep -Ev "^${path}\$" \ - | grep -Ev "^${path}/.+/" \ - | xargs -n1 sh -c 'if [ -d "${1}" ]; then echo "${1}"; fi' -- \ + #(find "${path}" -type d -print0 || true) \ + # | xargs -0 -n1 sh -c 'if [ -d "${1}" ]; then echo "${1}"; fi' -- \ + # | grep -Ev "^${path}\$" \ + # | grep -Ev "^${path}/.+/" \ + # | sort + path="${path%/}/" + (ls -1 -a "${path}" || true) \ + | tr '\r\n' '\000' \ + | tr '\n' '\000' \ + | tr '\r' '\000' \ + | xargs \ + -0 \ + -P"$(getconf _NPROCESSORS_ONLN)" \ + -n1 \ + sh -c "if [ -d \"${path}\${1}\" ] && [ \"\${1}\" != \".\" ] && [ \"\${1}\" != \"..\" ]; then echo \"${path}\${1}\"; fi" -- \ | sort }