diff --git a/README.md b/README.md index 6883120..0258693 100644 --- a/README.md +++ b/README.md @@ -102,6 +102,18 @@ copy = true # copy result to clipboard notify = true # show desktop notification with the copied value ``` +## ๐Ÿงช Testing + +The project includes a comprehensive test suite that verifies color conversion, configuration loading, and system integrations (clipboard/notifications) using mocks. + +To run the tests: + +```bash +./tests/run_tests.sh +``` + +The test script creates a temporary isolated environment, so it won't affect your system configuration or active clipboard. + ## ๐Ÿค Credits The `wl-colorpicker-plasma` integration is based on the original work by [SASUPERNOVA](https://github.com/SASUPERNOVA/wl-colorpicker-plasma). diff --git a/tests/run_tests.sh b/tests/run_tests.sh new file mode 100755 index 0000000..32946f1 --- /dev/null +++ b/tests/run_tests.sh @@ -0,0 +1,149 @@ +#!/bin/bash +set -euo pipefail + +# โ”€โ”€ Setup โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ +# Using a static-ish path since we can't use command substitution in cat +TEST_DIR="/tmp/color-tool-tests-repro" +BIN_DIR="$TEST_DIR/bin" +rm -rf "$TEST_DIR" +mkdir -p "$BIN_DIR" + +# Mock notify-send +cat <<'MOCK' > "$BIN_DIR/notify-send" +#!/bin/bash +echo "NOTIFY: $*" >> "/tmp/color-tool-tests-repro/notify.log" +MOCK + +# Mock wl-copy +cat <<'MOCK' > "$BIN_DIR/wl-copy" +#!/bin/bash +cat > "/tmp/color-tool-tests-repro/clipboard.txt" +MOCK + +# Mock python3 +cat <<'MOCK' > "$BIN_DIR/python3" +#!/bin/bash +if [[ "$*" == *wl-colorpicker*.py ]]; then + echo "#aabbcc" + exit 0 +fi +exec /usr/bin/python3 "$@" +MOCK + +chmod +x "$BIN_DIR"/* + +# Symlink essential tools +ln -s /usr/bin/bash "$BIN_DIR/bash" +ln -s /usr/bin/cat "$BIN_DIR/cat" +ln -s /usr/bin/printf "$BIN_DIR/printf" +ln -s /usr/bin/echo "$BIN_DIR/echo" +ln -s /usr/bin/sed "$BIN_DIR/sed" +ln -s /usr/bin/grep "$BIN_DIR/grep" +ln -s /usr/bin/awk "$BIN_DIR/awk" +ln -s /usr/bin/jq "$BIN_DIR/jq" +ln -s /usr/bin/readlink "$BIN_DIR/readlink" +ln -s /usr/bin/dirname "$BIN_DIR/dirname" +ln -s /usr/bin/mkdir "$BIN_DIR/mkdir" +ln -s /usr/bin/mktemp "$BIN_DIR/mktemp" +ln -s /usr/bin/chmod "$BIN_DIR/chmod" +ln -s /usr/bin/rm "$BIN_DIR/rm" +ln -s /usr/bin/curl "$BIN_DIR/curl" + +export PATH="$BIN_DIR" +export HOME="$TEST_DIR" +export WAYLAND_DISPLAY=wayland-0 +export XDG_CURRENT_DESKTOP=KDE + +# Resolve the absolute path of color-tool +COLOR_TOOL="$(/usr/bin/readlink -f ./color-tool)" + +# โ”€โ”€ Test Helpers โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ +total=0 +passed=0 + +it() { + local label="$1" + total=$((total + 1)) + printf "Test: %s... " "$label" +} + +assert_contains() { + local haystack="$1" + local needle="$2" + if [[ "$haystack" == *"$needle"* ]]; then + echo "PASS" + passed=$((passed + 1)) + else + echo "FAIL" + echo " Expected to find: $needle" + echo " Actual output: $haystack" + return 1 + fi +} + +# โ”€โ”€ Tests โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ + +# 1. Basic Conversion +it "converts hex to rgb" +output=$("$COLOR_TOOL" "#ffffff" --output rgb --no-copy --no-notify) +assert_contains "$output" "rgb(255, 255, 255)" + +# 2. Multiple Formats +it "handles multiple formats" +output=$("$COLOR_TOOL" "#000000" --output hex,rgba --no-copy --no-notify) +assert_contains "$output" "#000000 rgba(0, 0, 0, 1.0)" + +# 3. Clipboard Integration +it "copies to clipboard (mocked)" +rm -f "$TEST_DIR/clipboard.txt" +"$COLOR_TOOL" "#ff0000" --copy --no-notify >/dev/null +if [[ -f "$TEST_DIR/clipboard.txt" ]]; then + assert_contains "$(cat "$TEST_DIR/clipboard.txt")" "#ff0000" +else + echo "FAIL (clipboard.txt not created)" +fi + +# 4. Notification Integration +it "sends notifications (mocked)" +rm -f "$TEST_DIR/notify.log" +"$COLOR_TOOL" "#00ff00" --notify --no-copy >/dev/null +if [[ -f "$TEST_DIR/notify.log" ]]; then + assert_contains "$(cat "$TEST_DIR/notify.log")" "NOTIFY: -i color-picker color-tool #00ff00" +else + echo "FAIL (notify.log not created)" +fi + +# 5. Color Picker (Mocked) +it "launches color picker and processes result" +output=$("$COLOR_TOOL" --pick --no-copy --no-notify) +assert_contains "$output" "#aabbcc" + +# 6. Config Loading +it "loads defaults from config.toml" +mkdir -p "$HOME/.config/color-tool" +cat <<'CONF' > "$HOME/.config/color-tool/config.toml" +[defaults] +output = "rgba" +CONF +output=$("$COLOR_TOOL" "#ffffff" --no-copy --no-notify) +assert_contains "$output" "rgba(255, 255, 255, 1.0)" + +# 7. Error Notification +it "notifies on missing clipboard utility" +rm -f "$BIN_DIR/wl-copy" +rm -f "$TEST_DIR/notify.log" +"$COLOR_TOOL" "#123456" --copy --notify 2>/dev/null >/dev/null +if [[ -f "$TEST_DIR/notify.log" ]]; then + assert_contains "$(cat "$TEST_DIR/notify.log")" "NOTIFY: -u normal -i dialog-warning color-tool Missing clipboard utility" +else + echo "FAIL (notify.log not created)" +fi + +# โ”€โ”€ Cleanup โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ +echo "---------------------------------------" +echo "Result: $passed/$total tests passed." +rm -rf "$TEST_DIR" + +if [[ $passed -ne $total ]]; then + exit 1 +fi