From a8eb76190404633dc48096f41b01a241a1fbdbe7 Mon Sep 17 00:00:00 2001 From: Emma Doyle Date: Wed, 20 Dec 2023 20:37:26 -0500 Subject: [PATCH] fix-anchors: implement testing (#108) - Fix exploding anchors - implement much-needed testing --- .circleci/config.yml | 4 +- .circleci/scripts.ignore | 3 +- .circleci/scripts.yml | 18 ++++++- .circleci/src.yml | 67 +++++++++++++++++++++++++- .github/FUNDING.yml | 2 +- .pre-commit-config.yaml | 22 +++++++-- .shellcheckrc | 2 + .tool-versions | 2 +- src/examples/example-pipeline.yml | 2 +- src/scripts/filter.sh | 0 src/scripts/reduce.sh | 2 +- src/tests/.placeholder | 0 src/tests/anchors-105.bats | 56 +++++++++++++++++++++ src/tests/data/105/example-1.yml | 10 ++++ src/tests/data/105/example-2.yml | 11 +++++ src/tests/data/105/expected-result.yml | 17 +++++++ src/tests/force-all-93.bats | 1 + 17 files changed, 204 insertions(+), 15 deletions(-) create mode 100644 .shellcheckrc mode change 100644 => 100755 src/scripts/filter.sh mode change 100644 => 100755 src/scripts/reduce.sh delete mode 100644 src/tests/.placeholder create mode 100755 src/tests/anchors-105.bats create mode 100644 src/tests/data/105/example-1.yml create mode 100644 src/tests/data/105/example-2.yml create mode 100644 src/tests/data/105/expected-result.yml create mode 100755 src/tests/force-all-93.bats diff --git a/.circleci/config.yml b/.circleci/config.yml index 038afd7..e6159f7 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -7,8 +7,8 @@ orbs: orb-tools: circleci/orb-tools@11.6.1 circleci-cli: circleci/circleci-cli@0.1.9 # dynamic: bjd2385/dynamic-continuation@dev:alpha - dynamic: bjd2385/dynamic-continuation@3.7.1 - general: bjd2385/general@0.7.8 + dynamic: bjd2385/dynamic-continuation@3.8.0 + general: premiscale/general@1.0.9 slack: circleci/slack@4.12.5 diff --git a/.circleci/scripts.ignore b/.circleci/scripts.ignore index cf28f11..e043021 100644 --- a/.circleci/scripts.ignore +++ b/.circleci/scripts.ignore @@ -1,2 +1,3 @@ # Include scripts' changes under src/ -!src/scripts/ \ No newline at end of file +!src/scripts/ +!src/tests/ \ No newline at end of file diff --git a/.circleci/scripts.yml b/.circleci/scripts.yml index 1a498b8..3e8af18 100644 --- a/.circleci/scripts.yml +++ b/.circleci/scripts.yml @@ -2,11 +2,25 @@ version: 2.1 orbs: - shellcheck: circleci/shellcheck@3.1.1 + shellcheck: circleci/shellcheck@3.2.0 workflows: scripts: jobs: - shellcheck/check: - exclude: SC2148 \ No newline at end of file + name: shellcheck bash [<< matrix.dir >>] + matrix: + parameters: + dir: + - scripts + - src/scripts + alias: shellcheck bash + shell: bash + pattern: '^.*.sh$' + exclude: SC2148 + + - shellcheck/check: + name: shellcheck bats + shell: bats + pattern: '^.*.bats$' \ No newline at end of file diff --git a/.circleci/src.yml b/.circleci/src.yml index 29a652d..44e7279 100644 --- a/.circleci/src.yml +++ b/.circleci/src.yml @@ -4,7 +4,62 @@ version: 2.1 orbs: orb-tools: circleci/orb-tools@11.6.1 circleci-cli: circleci/circleci-cli@0.1.9 - general: bjd2385/general@0.7.5 + general: premiscale/general@1.0.9 + # bats: circleci/bats@1.1.0 + + +executors: + default: + docker: + - image: cimg/base:2023.12 + + +jobs: + bats-tests: + parameters: + yq-version: + description: |+ + Version of yq to install. + + https://github.com/mikefarah/yq/releases + type: string + default: 4.40.5 + tests-directory: + description: Directory in which bats tests reside. + type: string + default: tests/ + formatter: + description: Bats formatter. + type: string + default: tap + resource-class: + description: Resource class to execute as. + type: enum + default: small + enum: + - small + - medium + - large + - 2xlarge + executor: default + resource_class: << parameters.resource-class >> + steps: + - checkout + - run: + name: Install bats + command: |+ + sudo apt update + sudo apt install -y bats + - run: + name: Install yq + command: |+ + wget https://github.com/mikefarah/yq/releases/download/v<< parameters.yq-version >>/yq_linux_amd64 -o yq + sudo install yq /usr/bin/yq + rm yq + - run: + name: Bats tests + command: |+ + bats --formatter << parameters.formatter >> --timing --recursive << parameters.tests-directory >> workflows: @@ -18,10 +73,20 @@ workflows: max_command_length: 256 resource_class: small + # - bats/run: + # path: ./src/tests + # save_test_results: true + # # setup-steps: + + - bats-tests: + tests-directory: ./src/tests/ + - general/orb-pack: requires: - orb-tools/lint - orb-tools/review + # - bats/run + - bats-tests - orb-tools/publish: name: publish development orb diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml index cf991f9..f3000b1 100644 --- a/.github/FUNDING.yml +++ b/.github/FUNDING.yml @@ -1,3 +1,3 @@ # These are supported funding model platforms -ko_fi: bjd2385 +ko_fi: emmeowzing \ No newline at end of file diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index d69418e..b29b5db 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -5,19 +5,31 @@ repos: hooks: - id: check-added-large-files args: [--maxkb=10000, --enforce-all] - - id: check-executables-have-shebangs + # - id: check-executables-have-shebangs - id: check-shebang-scripts-are-executable - id: mixed-line-ending - id: trailing-whitespace - - repo: https://github.com/jumanjihouse/pre-commit-hooks - rev: 3.0.0 + - repo: local hooks: - - id: shellcheck + - id: shellcheck-bash + language: script + name: shellcheck bash scripts + entry: /usr/bin/env shellcheck args: - -x + - --shell=bash + files: '^.*.sh$' + - id: shellcheck-bats + language: script + entry: /usr/bin/env shellcheck + name: shellcheck bats tests + args: + - -x + - --shell=bats + files: '^.*.bats$' - repo: https://github.com/premiscale/pre-commit-hooks rev: v0.0.9 hooks: - - id: msg-issue-prefix + - id: msg-issue-prefix \ No newline at end of file diff --git a/.shellcheckrc b/.shellcheckrc new file mode 100644 index 0000000..d562971 --- /dev/null +++ b/.shellcheckrc @@ -0,0 +1,2 @@ +external-sources=true +exclude=SC2288,SC2001,SC2148,SC2153 \ No newline at end of file diff --git a/.tool-versions b/.tool-versions index eb4ab86..bcae538 100644 --- a/.tool-versions +++ b/.tool-versions @@ -1,3 +1,3 @@ -nodejs 20.10.0 +nodejs 18.9.0 yarn 1.22.19 shellcheck 0.9.0 \ No newline at end of file diff --git a/src/examples/example-pipeline.yml b/src/examples/example-pipeline.yml index 43051a0..348b0b2 100644 --- a/src/examples/example-pipeline.yml +++ b/src/examples/example-pipeline.yml @@ -7,7 +7,7 @@ usage: orbs: - dynamic: bjd2385/dynamic-continuation@3.7.0 + dynamic: bjd2385/dynamic-continuation@3.8.0 workflows: diff --git a/src/scripts/filter.sh b/src/scripts/filter.sh old mode 100644 new mode 100755 diff --git a/src/scripts/reduce.sh b/src/scripts/reduce.sh old mode 100644 new mode 100755 index d0c87ed..0f65abc --- a/src/scripts/reduce.sh +++ b/src/scripts/reduce.sh @@ -19,4 +19,4 @@ awk "{ }" "$SH_MODULES_FILTERED" > /tmp/"$CIRCLE_WORKFLOW_ID.txt" mv /tmp/"$CIRCLE_WORKFLOW_ID.txt" "$SH_MODULES_FILTERED" -yq eval-all '. as $item ireduce ( {}; . * $item )' $(cat "$SH_MODULES_FILTERED" | xargs) | tee "$SH_CONTINUE_CONFIG" \ No newline at end of file +yq -Mr eval-all 'explode(.) as $item ireduce ( {}; . * $item )' $(cat "$SH_MODULES_FILTERED" | xargs) | tee "$SH_CONTINUE_CONFIG" \ No newline at end of file diff --git a/src/tests/.placeholder b/src/tests/.placeholder deleted file mode 100644 index e69de29..0000000 diff --git a/src/tests/anchors-105.bats b/src/tests/anchors-105.bats new file mode 100755 index 0000000..a92170b --- /dev/null +++ b/src/tests/anchors-105.bats @@ -0,0 +1,56 @@ +#! /usr/bin/env bats +# Compare the results of reduction to the expected outcome. + + +export ISSUE_NUMBER=105 +export SH_CONTINUE_CONFIG=/tmp/continue-config.yml +export SH_MODULES_FILTERED=/tmp/modules-filtered.txt +export SH_ROOT_CONFIG=app +export EXPECTED_CONFIG=src/tests/data/"$ISSUE_NUMBER"/expected-result.yml + + +## +# Set up test environment. +setup() +{ + printf "example-1\\nexample-2" > "$SH_MODULES_FILTERED" + + # Copy our test data -> /.circleci/ + cp src/tests/data/"$ISSUE_NUMBER"/example-{1,2}.yml .circleci/ +} + + +## +# Clean up test environment. +clean() +{ + rm "${SH_MODULES_FILTERED:?}" + rm .circleci/example-{1,2}.yml +} + + +## +# Show diff output. +_diff() +{ + diff -d -r -y <(yq -rM "$SH_CONTINUE_CONFIG") <(yq -rM "$EXPECTED_CONFIG") +} + + +@test reduce_exploded_yaml_anchors { + setup + + ./src/scripts/reduce.sh + + if [ "$(yq -rM '.' "$SH_CONTINUE_CONFIG")" != "$(yq -rM '.' "$EXPECTED_CONFIG")" ]; then + _diff + printf "\\n" + clean + return 1 + fi + + clean +} + + +# TODO: write another test that checks that it purposely fails w/o the explode? \ No newline at end of file diff --git a/src/tests/data/105/example-1.yml b/src/tests/data/105/example-1.yml new file mode 100644 index 0000000..a4671a6 --- /dev/null +++ b/src/tests/data/105/example-1.yml @@ -0,0 +1,10 @@ +jobs: + contexts: &contexts + - circleci3 + - circleci4 + +workflows: + example-1: + jobs: + - job1: + context: *contexts \ No newline at end of file diff --git a/src/tests/data/105/example-2.yml b/src/tests/data/105/example-2.yml new file mode 100644 index 0000000..94b3312 --- /dev/null +++ b/src/tests/data/105/example-2.yml @@ -0,0 +1,11 @@ +jobs: + contexts: &contexts + - circleci3 + - circleci4 + + +workflows: + example-2: + jobs: + - job1: + context: *contexts \ No newline at end of file diff --git a/src/tests/data/105/expected-result.yml b/src/tests/data/105/expected-result.yml new file mode 100644 index 0000000..a050b81 --- /dev/null +++ b/src/tests/data/105/expected-result.yml @@ -0,0 +1,17 @@ +jobs: + contexts: + - circleci3 + - circleci4 +workflows: + example-1: + jobs: + - job1: + context: + - circleci3 + - circleci4 + example-2: + jobs: + - job1: + context: + - circleci3 + - circleci4 \ No newline at end of file diff --git a/src/tests/force-all-93.bats b/src/tests/force-all-93.bats new file mode 100755 index 0000000..a298217 --- /dev/null +++ b/src/tests/force-all-93.bats @@ -0,0 +1 @@ +#! /usr/bin/env bats \ No newline at end of file