Skip to content

Commit

Permalink
fix-anchors: implement testing (#108)
Browse files Browse the repository at this point in the history
- Fix exploding anchors
- implement much-needed testing
  • Loading branch information
emmeowzing authored Dec 21, 2023
1 parent f0060f7 commit a8eb761
Show file tree
Hide file tree
Showing 17 changed files with 204 additions and 15 deletions.
4 changes: 2 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ orbs:
orb-tools: circleci/[email protected]
circleci-cli: circleci/[email protected]
# 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/[email protected]


Expand Down
3 changes: 2 additions & 1 deletion .circleci/scripts.ignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# Include scripts' changes under src/
!src/scripts/
!src/scripts/
!src/tests/
18 changes: 16 additions & 2 deletions .circleci/scripts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
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$'
67 changes: 66 additions & 1 deletion .circleci/src.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,62 @@ version: 2.1
orbs:
orb-tools: circleci/[email protected]
circleci-cli: circleci/[email protected]
general: bjd2385/[email protected]
general: premiscale/[email protected]
# bats: circleci/[email protected]


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:
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# These are supported funding model platforms

ko_fi: bjd2385
ko_fi: emmeowzing
22 changes: 17 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions .shellcheckrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
external-sources=true
exclude=SC2288,SC2001,SC2148,SC2153
2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
nodejs 20.10.0
nodejs 18.9.0
yarn 1.22.19
shellcheck 0.9.0
2 changes: 1 addition & 1 deletion src/examples/example-pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ usage:


orbs:
dynamic: bjd2385/dynamic-continuation@3.7.0
dynamic: bjd2385/dynamic-continuation@3.8.0


workflows:
Expand Down
Empty file modified src/scripts/filter.sh
100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion src/scripts/reduce.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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"
yq -Mr eval-all 'explode(.) as $item ireduce ( {}; . * $item )' $(cat "$SH_MODULES_FILTERED" | xargs) | tee "$SH_CONTINUE_CONFIG"
Empty file removed src/tests/.placeholder
Empty file.
56 changes: 56 additions & 0 deletions src/tests/anchors-105.bats
Original file line number Diff line number Diff line change
@@ -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?
10 changes: 10 additions & 0 deletions src/tests/data/105/example-1.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
jobs:
contexts: &contexts
- circleci3
- circleci4

workflows:
example-1:
jobs:
- job1:
context: *contexts
11 changes: 11 additions & 0 deletions src/tests/data/105/example-2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
jobs:
contexts: &contexts
- circleci3
- circleci4


workflows:
example-2:
jobs:
- job1:
context: *contexts
17 changes: 17 additions & 0 deletions src/tests/data/105/expected-result.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
jobs:
contexts:
- circleci3
- circleci4
workflows:
example-1:
jobs:
- job1:
context:
- circleci3
- circleci4
example-2:
jobs:
- job1:
context:
- circleci3
- circleci4
1 change: 1 addition & 0 deletions src/tests/force-all-93.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#! /usr/bin/env bats

0 comments on commit a8eb761

Please sign in to comment.