diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..a6cc54a --- /dev/null +++ b/.clang-format @@ -0,0 +1 @@ +BasedOnStyle: GNU diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..d7f2880 --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,20 @@ +version: 2 +updates: + - package-ecosystem: github-actions + directory: / + schedule: + interval: weekly + cooldown: + default-days: 7 + groups: + actions: + patterns: ['*'] + - package-ecosystem: cargo + directories: [/imgconvert, /imgcrop, /pypaste] + schedule: + interval: weekly + cooldown: + default-days: 7 + groups: + rust: + patterns: ['*'] diff --git a/.github/workflows/analysis.yml b/.github/workflows/analysis.yml new file mode 100644 index 0000000..aee9959 --- /dev/null +++ b/.github/workflows/analysis.yml @@ -0,0 +1,40 @@ +name: Analysis + +on: + schedule: + - cron: '34 5 * * 4' + workflow_dispatch: + +permissions: + contents: read + +concurrency: + group: analysis-${{ github.ref }} + cancel-in-progress: false + +jobs: + codeql: + strategy: + fail-fast: false + matrix: + language: [actions, rust, c-cpp] + permissions: + contents: read + security-events: write + uses: cleisson-labs/.github/.github/workflows/codeql.yml@03f53ed9e71b2741c683c448a538f7a7b8aa1786 # v1.0.0 + with: + language: ${{ matrix.language }} + + rust-audit: + strategy: + fail-fast: false + matrix: + project: [imgconvert, imgcrop, pypaste] + uses: cleisson-labs/.github/.github/workflows/rust-audit.yml@03f53ed9e71b2741c683c448a538f7a7b8aa1786 # v1.0.0 + with: + working-directory: ${{ matrix.project }} + + external-links: + uses: cleisson-labs/.github/.github/workflows/docs.yml@03f53ed9e71b2741c683c448a538f7a7b8aa1786 # v1.0.0 + with: + external: true diff --git a/.github/workflows/scorecard.yml b/.github/workflows/scorecard.yml new file mode 100644 index 0000000..b33656b --- /dev/null +++ b/.github/workflows/scorecard.yml @@ -0,0 +1,34 @@ +name: Scorecard +on: + push: + branches: [main] + schedule: + - cron: '31 6 * * 4' +permissions: {} +cache-mode: none +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true +jobs: + scorecard: + runs-on: ubuntu-24.04 + timeout-minutes: 12 + permissions: + contents: read + id-token: write + security-events: write + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + - name: Analyze and publish the public result + uses: ossf/scorecard-action@23f22cd43608bae7a1f1fa2603d7314dee9538a5 # v2.6.0 + with: + results_file: scorecard.sarif + results_format: sarif + publish_results: true + fail_on_publish_error: true + - name: Upload code scanning results + uses: github/codeql-action/upload-sarif@915412ff23e8b7659517e607487dbeae018f2934 # v4.39.5 + with: + sarif_file: scorecard.sarif diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index b784775..76a9be7 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -1,32 +1,91 @@ -name: Run All Tests +name: CI on: - push: pull_request: + push: + branches: [main] + +permissions: + contents: read + +concurrency: + group: ci-${{ github.event.pull_request.number || github.ref }} + cancel-in-progress: ${{ github.event_name == 'pull_request' }} jobs: - test: - runs-on: ubuntu-latest + security: + uses: cleisson-labs/.github/.github/workflows/security.yml@03f53ed9e71b2741c683c448a538f7a7b8aa1786 # v1.0.0 - steps: - - name: Checkout repository - uses: actions/checkout@v4 + docs: + uses: cleisson-labs/.github/.github/workflows/docs.yml@03f53ed9e71b2741c683c448a538f7a7b8aa1786 # v1.0.0 - - name: Install Rust toolchain - uses: dtolnay/rust-toolchain@stable + all-tools: + runs-on: ubuntu-latest + timeout-minutes: 25 + env: + RUSTUP_TOOLCHAIN: stable + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: - components: rustfmt, clippy - - - name: Install C dependencies + persist-credentials: false + - name: Set up Rust + run: rustup toolchain install stable --profile minimal --component rustfmt --component clippy + - uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3 + with: + workspaces: | + imgconvert + imgcrop + pypaste + save-if: ${{ github.event_name == 'push' }} + cache-bin: false + - name: Install build and shell lint dependencies run: | sudo apt-get update - sudo apt-get install -y libcurl4-openssl-dev shellcheck - + sudo apt-get install -y --no-install-recommends libcurl4-openssl-dev shellcheck + - name: Verify committed dependency locks + run: | + for manifest in */Cargo.toml; do + cargo metadata --locked --format-version=1 --manifest-path "$manifest" > /dev/null + done - name: Run all lint checks run: ./.scripts/lint_all.sh - - name: Build all tools run: ./.scripts/build_all.sh - - name: Run all tests run: ./.scripts/test_all.sh + + c: + strategy: + fail-fast: false + matrix: + project: [raw_http, workpay] + uses: cleisson-labs/.github/.github/workflows/c.yml@03f53ed9e71b2741c683c448a538f7a7b8aa1786 # v1.0.0 + with: + working-directory: ${{ matrix.project }} + apt-packages: libcurl4-openssl-dev + coverage: true + sanitizers: true + + codeql: + strategy: + fail-fast: false + matrix: + language: [actions, rust, c-cpp] + permissions: + contents: read + security-events: write + uses: cleisson-labs/.github/.github/workflows/codeql.yml@03f53ed9e71b2741c683c448a538f7a7b8aa1786 # v1.0.0 + with: + language: ${{ matrix.language }} + + ci-ok: + if: always() + needs: [security, docs, codeql, all-tools, c] + runs-on: ubuntu-latest + timeout-minutes: 3 + permissions: {} + steps: + - name: Require every CI job to pass + env: + RESULTS: ${{ toJSON(needs) }} + run: jq -e 'length > 0 and all(.[]; .result == "success")' <<< "$RESULTS" diff --git a/.gitignore b/.gitignore index 6861e9e..3350d32 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,5 @@ ai_tasks/ +# Native test and coverage artifacts +*.dSYM/ +*.gcno +*.gcda diff --git a/.scripts/c-checks.mk b/.scripts/c-checks.mk new file mode 100644 index 0000000..a282674 --- /dev/null +++ b/.scripts/c-checks.mk @@ -0,0 +1,15 @@ +# Each C tool keeps its own sources, flags and test entry point. +.PHONY: analyze sanitize coverage + +analyze: + cppcheck --std=c11 --enable=warning,performance,portability --error-exitcode=1 \ + $(CPPFLAGS) src tests + +sanitize: + UBSAN_OPTIONS=halt_on_error=1 $(MAKE) test \ + CFLAGS="$(CFLAGS) -O1 -g -fsanitize=address,undefined -fno-omit-frame-pointer" + +coverage: + rm -f ./*.gcda ./*.gcno + $(MAKE) build test CFLAGS="$(CFLAGS) -O0 -g --coverage" + gcovr --root . --filter 'src/' --txt --print-summary diff --git a/README.md b/README.md index b757ab7..7050d17 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,9 @@ # CLI Tools Collection +[![CI](https://github.com/cleisson-labs/cli_tools/actions/workflows/tests.yml/badge.svg?branch=main)](https://github.com/cleisson-labs/cli_tools/actions/workflows/tests.yml) +[![License](https://img.shields.io/github/license/cleisson-labs/cli_tools)](LICENSE) +[![Coverage report](https://img.shields.io/badge/coverage-C%20report-blue)](https://github.com/cleisson-labs/cli_tools/actions/workflows/tests.yml) + A multi-language set of focused command-line tools for everyday workflows. ## Tools @@ -10,7 +14,7 @@ A multi-language set of focused command-line tools for everyday workflows. | `imgcrop` | Rust | Crops images to exact dimensions with optional proportion/framing controls. | `cd imgcrop && make build` | `cd imgcrop && make test` | | `pypaste` | Rust | Compacts Python scripts for terminal pasting and copies output to clipboard. | `cd pypaste && make build` | `cd pypaste && make test` | | `raw_http` | C (libcurl) | Sends raw HTTP requests from `.txt` files and prints/saves full responses. | `cd raw_http && make build` | `cd raw_http && make test` | -| `workpay` | C (libcurl) | Calculates earnings from worked time and converts currencies via AwesomeAPI. | `cd workpay && make build` | `N/A` (no `test` target in `Makefile`) | +| `workpay` | C (libcurl) | Calculates earnings from worked time and converts currencies via AwesomeAPI. | `cd workpay && make build` | `cd workpay && make test` | Detailed per-tool docs: - [imgconvert/README.md](imgconvert/README.md) @@ -45,6 +49,13 @@ Use the repository scripts from the repository root: ./.scripts/test_all.sh ``` +CI runs the same lint, build and test scripts on Linux and checks changed C +lines with clang-format's GNU style. For either C tool, +`make -C raw_http analyze` runs Cppcheck, `make -C raw_http sanitize CC=clang` +runs the tests with AddressSanitizer and UndefinedBehaviorSanitizer, and +`make -C raw_http coverage CC=gcc` prints a gcovr report. Replace `raw_http` +with `workpay` to check that tool. Coverage requires GCC and gcovr. + Install built binaries from a local clone: ```bash diff --git a/raw_http/Makefile b/raw_http/Makefile index 4cf3239..172c5b2 100644 --- a/raw_http/Makefile +++ b/raw_http/Makefile @@ -17,3 +17,5 @@ test: clean: @rm -f raw_http test_parse test_cli_output + +include ../.scripts/c-checks.mk diff --git a/workpay/Makefile b/workpay/Makefile index 9fb9b1d..68e3c5a 100644 --- a/workpay/Makefile +++ b/workpay/Makefile @@ -32,3 +32,5 @@ test: clean: @rm -f workpay test_parse test_money test_cli test_quota test_report_store test_report_query test_report_export + +include ../.scripts/c-checks.mk diff --git a/workpay/tests/test_quota.c b/workpay/tests/test_quota.c index 8604e63..dc0e756 100644 --- a/workpay/tests/test_quota.c +++ b/workpay/tests/test_quota.c @@ -14,8 +14,10 @@ get_today_date (char *output, size_t output_size) struct tm local_time; assert (now != (time_t)-1); - assert (localtime_r (&now, &local_time) != NULL); - assert (strftime (output, output_size, "%Y-%m-%d", &local_time) != 0); + struct tm *result = localtime_r (&now, &local_time); + assert (result != NULL); + size_t length = strftime (output, output_size, "%Y-%m-%d", &local_time); + assert (length != 0); } static void @@ -28,7 +30,8 @@ create_temp_usage_file_path (char *output, size_t output_size) fd = mkstemp (output); assert (fd != -1); close (fd); - assert (remove (output) == 0); + int cleanup_result = remove (output); + assert (cleanup_result == 0); } static void @@ -36,7 +39,8 @@ write_usage_file (const char *path, const char *date, int request_count) { FILE *file = fopen (path, "w"); assert (file != NULL); - assert (fprintf (file, "%s %d\n", date, request_count) > 0); + int written = fprintf (file, "%s %d\n", date, request_count); + assert (written > 0); fclose (file); } @@ -47,7 +51,8 @@ read_usage_count (const char *path, char *date_output, size_t date_output_size) int request_count = -1; assert (file != NULL); - assert (fscanf (file, "%15s %d", date_output, &request_count) == 2); + int fields = fscanf (file, "%15s %d", date_output, &request_count); + assert (fields == 2); fclose (file); (void)date_output_size; @@ -73,7 +78,8 @@ test_first_request_creates_usage_file (void) assert (request_count == 1); assert (unsetenv ("WORKPAY_USAGE_FILE") == 0); - assert (remove (path) == 0); + int cleanup_result = remove (path); + assert (cleanup_result == 0); } static void @@ -96,7 +102,8 @@ test_request_99_advances_to_100 (void) assert (request_count == 100); assert (unsetenv ("WORKPAY_USAGE_FILE") == 0); - assert (remove (path) == 0); + int cleanup_result = remove (path); + assert (cleanup_result == 0); } static void @@ -119,7 +126,8 @@ test_limit_reached_at_100 (void) assert (request_count == 100); assert (unsetenv ("WORKPAY_USAGE_FILE") == 0); - assert (remove (path) == 0); + int cleanup_result = remove (path); + assert (cleanup_result == 0); } int diff --git a/workpay/tests/test_report_export.c b/workpay/tests/test_report_export.c index 905ecd1..0feff0c 100644 --- a/workpay/tests/test_report_export.c +++ b/workpay/tests/test_report_export.c @@ -69,7 +69,8 @@ test_csv_export_writes_expected_header_and_rows (void) assert (strstr (content, "alpha") != NULL); assert (strstr (content, "beta") != NULL); - assert (remove (output_path) == 0); + int cleanup_result = remove (output_path); + assert (cleanup_result == 0); } static void @@ -91,7 +92,8 @@ test_json_export_writes_expected_fields (void) assert (strstr (content, "\"from_currency\": \"USD\"") != NULL); assert (strstr (content, "alpha \\\"quoted\\\"") != NULL); - assert (remove (output_path) == 0); + int cleanup_result = remove (output_path); + assert (cleanup_result == 0); } static void diff --git a/workpay/tests/test_report_store.c b/workpay/tests/test_report_store.c index ba354e6..4de6e37 100644 --- a/workpay/tests/test_report_store.c +++ b/workpay/tests/test_report_store.c @@ -15,7 +15,8 @@ create_temp_entries_path (char *output_path, size_t output_size) fd = mkstemp (output_path); assert (fd != -1); close (fd); - assert (remove (output_path) == 0); + int cleanup_result = remove (output_path); + assert (cleanup_result == 0); } static void @@ -55,7 +56,8 @@ test_append_and_load_roundtrip (void) file = fopen (path, "r"); assert (file != NULL); - assert (fgets (header_line, sizeof (header_line), file) != NULL); + char *header = fgets (header_line, sizeof (header_line), file); + assert (header != NULL); fclose (file); assert (strstr (header_line, REPORT_CSV_HEADER) == header_line); @@ -70,7 +72,8 @@ test_append_and_load_roundtrip (void) report_entry_list_free (&loaded); assert (unsetenv ("WORKPAY_ENTRIES_FILE") == 0); - assert (remove (path) == 0); + int cleanup_result = remove (path); + assert (cleanup_result == 0); } static void @@ -104,7 +107,8 @@ test_invalid_header_returns_parse_error (void) file = fopen (path, "w"); assert (file != NULL); - assert (fprintf (file, "bad,header\n") > 0); + int written = fprintf (file, "bad,header\n"); + assert (written > 0); fclose (file); assert (report_store_load_entries (&loaded, error, sizeof (error)) @@ -112,7 +116,8 @@ test_invalid_header_returns_parse_error (void) assert (strstr (error, "Invalid entries CSV header") != NULL); assert (unsetenv ("WORKPAY_ENTRIES_FILE") == 0); - assert (remove (path) == 0); + int cleanup_result = remove (path); + assert (cleanup_result == 0); } int