From 322440e045d1dc779d5734da4f56d13ef2a87dfe Mon Sep 17 00:00:00 2001 From: Aliaksandr Adziareika Date: Wed, 12 Nov 2025 15:50:25 +0100 Subject: [PATCH] Add api-github-gh.sh CLI unit-tests Fix CLI documentation links Exclude docs from triggering the workflows --- .github/workflows/test-bash-framework.yml | 8 +- .github/workflows/test-bash-scripts.yml | 8 +- docs/scripts/shell/bash/CONVENTIONS.md | 1 + .../shell/bash/api-github-gh/README.md | 4 + .../shell/bash/packaging-format/API.md | 4 +- .../shell/bash/packaging-format/README.md | 5 +- .../scripts/shell/bash/platform-format/API.md | 4 +- .../shell/bash/platform-format/README.md | 5 +- docs/scripts/shell/bash/version-format/API.md | 2 +- .../shell/bash/version-format/README.md | 5 +- .../bash/suites/test-api-github-gh-cli.sh | 240 ++++++++++++++++++ 11 files changed, 271 insertions(+), 15 deletions(-) create mode 100644 tests/shell/bash/suites/test-api-github-gh-cli.sh diff --git a/.github/workflows/test-bash-framework.yml b/.github/workflows/test-bash-framework.yml index 6745a10..4e49f0d 100644 --- a/.github/workflows/test-bash-framework.yml +++ b/.github/workflows/test-bash-framework.yml @@ -3,9 +3,13 @@ name: Test Bash Framework on: push: branches: [main] - paths: [ 'scripts-demo/**', 'tests/shell/bash/**' ] + paths: + - 'scripts-demo/**/*.sh' + - 'tests/shell/bash/**/*.sh' pull_request: - paths: [ 'scripts-demo/**', 'tests/shell/bash/**' ] + paths: + - 'scripts-demo/**/*.sh' + - 'tests/shell/bash/**/*.sh' workflow_dispatch: jobs: diff --git a/.github/workflows/test-bash-scripts.yml b/.github/workflows/test-bash-scripts.yml index c5c3b4f..29f5675 100644 --- a/.github/workflows/test-bash-scripts.yml +++ b/.github/workflows/test-bash-scripts.yml @@ -3,9 +3,13 @@ name: Test Bash Scripts on: push: branches: [main] - paths: [ 'scripts/**', 'tests/shell/bash/**' ] + paths: + - 'scripts/**/*.sh' + - 'tests/shell/bash/**/*.sh' pull_request: - paths: [ 'scripts/**', 'tests/shell/bash/**' ] + paths: + - 'scripts/**/*.sh' + - 'tests/shell/bash/**/*.sh' workflow_dispatch: jobs: diff --git a/docs/scripts/shell/bash/CONVENTIONS.md b/docs/scripts/shell/bash/CONVENTIONS.md index 68fff3c..5faf439 100644 --- a/docs/scripts/shell/bash/CONVENTIONS.md +++ b/docs/scripts/shell/bash/CONVENTIONS.md @@ -390,3 +390,4 @@ Following these conventions provides: - [platform-format.sh](platform-format/README.md) - Domain module example - [packaging-format.sh](packaging-format/README.md) - Domain module example - [api-github-gh.sh](api-github-gh/README.md) - API wrapper module example +- [README.md](./../../../../README.md) - Actions overview \ No newline at end of file diff --git a/docs/scripts/shell/bash/api-github-gh/README.md b/docs/scripts/shell/bash/api-github-gh/README.md index 8f2156c..72ded06 100644 --- a/docs/scripts/shell/bash/api-github-gh/README.md +++ b/docs/scripts/shell/bash/api-github-gh/README.md @@ -122,3 +122,7 @@ Tested on: - [GitHub CLI Documentation](https://cli.github.com/manual/) - [GitHub REST API](https://docs.github.com/en/rest) - [Naming Conventions](../CONVENTIONS.md) +- [version-format.sh](../version-format/README.md) - Version string utilities +- [platform-format.sh](../platform-format/README.md) - Platform detection utilities +- [packaging-format.sh](../packaging-format/README.md) - Package format utilities +- [README.md](./../../../../../README.md) - Actions overview diff --git a/docs/scripts/shell/bash/packaging-format/API.md b/docs/scripts/shell/bash/packaging-format/API.md index 42ac154..e802f20 100644 --- a/docs/scripts/shell/bash/packaging-format/API.md +++ b/docs/scripts/shell/bash/packaging-format/API.md @@ -216,5 +216,5 @@ All error messages are written to stderr with `[ERROR]` prefix: ## See Also -- [README.md](README.md) - Module overview and quick start -- [CONVENTIONS.md](../CONVENTIONS.md) - Bash scripting conventions +- [README.md](./README.md) - Module overview and quick start +- **[CONVENTIONS.md](./../CONVENTIONS.md)** - Common naming conventions for OpenDAQ bash scripts. diff --git a/docs/scripts/shell/bash/packaging-format/README.md b/docs/scripts/shell/bash/packaging-format/README.md index 7cb845f..48aa64c 100644 --- a/docs/scripts/shell/bash/packaging-format/README.md +++ b/docs/scripts/shell/bash/packaging-format/README.md @@ -217,5 +217,6 @@ type daq_packaging_detect_from_os ## See Also -- [API.md](API.md) - Complete API reference -- [CONVENTIONS.md](../CONVENTIONS.md) - Script naming conventions +- [API.md](./API.md) - Complete API reference +- [CONVENTIONS.md](./../CONVENTIONS.md) - Common naming conventions for OpenDAQ bash scripts. +- [README.md](./../../../../../README.md) - Actions overview diff --git a/docs/scripts/shell/bash/platform-format/API.md b/docs/scripts/shell/bash/platform-format/API.md index 1cfa012..d00e6a5 100644 --- a/docs/scripts/shell/bash/platform-format/API.md +++ b/docs/scripts/shell/bash/platform-format/API.md @@ -888,5 +888,5 @@ echo " Architecture: $os_arch" ## See Also -- [README.md](README.md) - Quick start and CLI usage -- [CONVENTIONS.md](../CONVENTIONS.md) - Common naming conventions for OpenDAQ bash scripts. +- [README.md](./README.md) - Quick start and CLI usage +- [CONVENTIONS.md](./../CONVENTIONS.md) - Common naming conventions for OpenDAQ bash scripts. diff --git a/docs/scripts/shell/bash/platform-format/README.md b/docs/scripts/shell/bash/platform-format/README.md index ed62fff..d85b0c8 100644 --- a/docs/scripts/shell/bash/platform-format/README.md +++ b/docs/scripts/shell/bash/platform-format/README.md @@ -450,5 +450,6 @@ Error: Cannot detect Linux distribution: /etc/os-release not found ## See Also -- [API.md](API.md) - Complete API reference for library usage -- [CONVENTIONS.md](../CONVENTIONS.md) - Common naming conventions for OpenDAQ bash scripts. +- [API.md](./API.md) - Complete API reference for library usage +- [CONVENTIONS.md](./../CONVENTIONS.md) - Common naming conventions for OpenDAQ bash scripts. +- [README.md](./../../../../../README.md) - Actions overview diff --git a/docs/scripts/shell/bash/version-format/API.md b/docs/scripts/shell/bash/version-format/API.md index dd31609..0ca425a 100644 --- a/docs/scripts/shell/bash/version-format/API.md +++ b/docs/scripts/shell/bash/version-format/API.md @@ -472,5 +472,5 @@ fi ## See Also -- [README.md](README.md) - Version format utility description +- [README.md](./README.md) - Version format utility description - [CONVENTIONS.md](../CONVENTIONS.md) - Common naming conventions for OpenDAQ bash scripts. diff --git a/docs/scripts/shell/bash/version-format/README.md b/docs/scripts/shell/bash/version-format/README.md index 3617e6e..da0d872 100644 --- a/docs/scripts/shell/bash/version-format/README.md +++ b/docs/scripts/shell/bash/version-format/README.md @@ -202,5 +202,6 @@ chmod +x version-format.sh ## 📚 API Documentation -- **[API.md](API.md)** - Complete function reference for script integration -- **[CONVENTIONS.md](../CONVENTIONS.md)** - Common naming conventions for OpenDAQ bash scripts. +- [API.md](./API.md) - Complete function reference for script integration +- [CONVENTIONS.md](./../CONVENTIONS.md) - Common naming conventions for OpenDAQ bash scripts. +- [README.md](./../../../../../README.md) - Actions overview \ No newline at end of file diff --git a/tests/shell/bash/suites/test-api-github-gh-cli.sh b/tests/shell/bash/suites/test-api-github-gh-cli.sh new file mode 100644 index 0000000..5888e36 --- /dev/null +++ b/tests/shell/bash/suites/test-api-github-gh-cli.sh @@ -0,0 +1,240 @@ +#!/usr/bin/env bash +# test-api-github-gh-cli.sh - CLI tests for api-github-gh.sh +# Tests by calling the script as a separate process (real CLI testing) + +# Path to script under test +SCRIPT_PATH="${__DAQ_TESTS_SCRIPTS_DIR}/api-github-gh.sh" + +# Setup function called before each test +test_setup() { + # Create temp directory for mocks + MOCK_DIR="$__DAQ_TESTS_SCRIPTS_DIR/tmp/gh-api-test-$$" + mkdir -p "$MOCK_DIR" + + # Create mock gh script + cat > "$MOCK_DIR/gh" << 'EOF' +#!/usr/bin/env bash +case "$1" in + "auth") + [[ "$2" == "status" ]] && exit 0 + ;; + "api") + # Return fake JSON with releases + echo '[{"tag_name": "v1.2.3"}, {"tag_name": "v1.2.2"}, {"tag_name": "v1.2.1"}]' + exit 0 + ;; +esac +exit 1 +EOF + chmod +x "$MOCK_DIR/gh" + + # Create mock jq script + cat > "$MOCK_DIR/jq" << 'EOF' +#!/usr/bin/env bash +case "$*" in + *"tag_name"*) + # Handle different jq queries + if echo "$*" | grep -q "\\[0\\]"; then + echo "v1.2.3" + else + echo "v1.2.3" + echo "v1.2.2" + echo "v1.2.1" + fi + ;; + *) echo "mock-output" ;; +esac +exit 0 +EOF + chmod +x "$MOCK_DIR/jq" + + # Prepend mock directory to PATH + export PATH="$MOCK_DIR:$PATH" +} + +# Teardown function called after each test +test_teardown() { + # Remove mock directory + rm -rf "$MOCK_DIR" +} + +test-cli-help-flag() { + local output + output=$("$SCRIPT_PATH" --help 2>&1) + local exit_code=$? + + daq_assert_success $exit_code "Help should succeed" || return 1 + daq_assert_contains "Usage:" "$output" "Should show usage" || return 1 + + return 0 +} + +test-cli-no-args() { + local output + output=$("$SCRIPT_PATH" 2>&1) + local exit_code=$? + + daq_assert_failure $exit_code "Should fail without args" || return 1 + daq_assert_contains "Repository not specified" "$output" "Should mention missing repo" || return 1 + + return 0 +} + +test-cli-help-shorthand() { + local output + output=$("$SCRIPT_PATH" -h 2>&1) + local exit_code=$? + + daq_assert_success $exit_code "Help shorthand should succeed" || return 1 + daq_assert_contains "Usage:" "$output" "Should show usage" || return 1 + + return 0 +} + +test-cli-unknown-option() { + local output + output=$("$SCRIPT_PATH" owner/repo --unknown 2>&1) + local exit_code=$? + + daq_assert_failure $exit_code "Should fail with unknown option" || return 1 + daq_assert_contains "Unknown option" "$output" "Should mention unknown option" || return 1 + + return 0 +} + +test-cli-version-default() { + local output + output=$("$SCRIPT_PATH" owner/repo 2>&1) + local exit_code=$? + + daq_assert_success $exit_code "Should get latest version" || return 1 + daq_assert_contains "v1.2.3" "$output" "Should output version" || return 1 + + return 0 +} + +test-cli-version-explicit() { + local output + output=$("$SCRIPT_PATH" owner/repo --version v1.0.0 2>&1) + local exit_code=$? + + daq_assert_success $exit_code "Should check explicit version" || return 1 + + return 0 +} + +test-cli-version-missing-value() { + local output + output=$("$SCRIPT_PATH" owner/repo --version 2>&1) + local exit_code=$? + + daq_assert_failure $exit_code "Should fail with missing value" || return 1 + daq_assert_contains "requires an argument" "$output" "Should mention missing argument" || return 1 + + return 0 +} + +test-cli-list-versions() { + local output + output=$("$SCRIPT_PATH" owner/repo --list-versions 2>&1) + local exit_code=$? + + daq_assert_success $exit_code "Should list versions" || return 1 + daq_assert_contains "v1.2" "$output" "Should contain version" || return 1 + + return 0 +} + +test-cli-list-versions-limit() { + local output + output=$("$SCRIPT_PATH" owner/repo --list-versions --limit 5 2>&1) + local exit_code=$? + + daq_assert_success $exit_code "Should list versions with limit" || return 1 + + return 0 +} + +test-cli-limit-numeric() { + local output + output=$("$SCRIPT_PATH" owner/repo --list-versions --limit 10 2>&1) + local exit_code=$? + + daq_assert_success $exit_code "Should accept numeric limit" || return 1 + + return 0 +} + +test-cli-limit-all() { + local output + output=$("$SCRIPT_PATH" owner/repo --list-versions --limit all 2>&1) + local exit_code=$? + + daq_assert_success $exit_code "Should accept 'all' limit" || return 1 + + return 0 +} + +test-cli-limit-missing-value() { + local output + output=$("$SCRIPT_PATH" owner/repo --list-versions --limit 2>&1) + local exit_code=$? + + daq_assert_failure $exit_code "Should fail with missing value" || return 1 + daq_assert_contains "requires an argument" "$output" "Should mention missing argument" || return 1 + + return 0 +} + +test-cli-verbose() { + local output + output=$("$SCRIPT_PATH" owner/repo --verbose 2>&1) + local exit_code=$? + + daq_assert_success $exit_code "Verbose should work" || return 1 + + return 0 +} + +test-cli-default-action() { + local output + output=$("$SCRIPT_PATH" owner/repo 2>&1) + local exit_code=$? + + daq_assert_success $exit_code "Default action should work" || return 1 + daq_assert_contains "v1.2.3" "$output" "Should get latest version by default" || return 1 + + return 0 +} + +test-cli-repo-valid() { + local output + output=$("$SCRIPT_PATH" owner/repo 2>&1) + local exit_code=$? + + daq_assert_success $exit_code "Valid repo format should work" || return 1 + + return 0 +} + +test-cli-repo-invalid() { + local output + output=$("$SCRIPT_PATH" invalid-repo 2>&1) + local exit_code=$? + + daq_assert_failure $exit_code "Invalid repo format should fail" || return 1 + daq_assert_contains "Invalid repository format" "$output" "Should mention invalid format" || return 1 + + return 0 +} + +test-cli-repo-missing() { + local output + output=$("$SCRIPT_PATH" --list-versions 2>&1) + local exit_code=$? + + daq_assert_failure $exit_code "Missing repo should fail" || return 1 + daq_assert_contains "Repository not specified" "$output" "Should mention missing repo" || return 1 + + return 0 +}