From c21abcfd5a513b9639150cc949dab262b994eaa5 Mon Sep 17 00:00:00 2001 From: Mike Carey <32496966+mike-carey@users.noreply.github.com> Date: Wed, 26 Apr 2023 12:03:27 -0500 Subject: [PATCH] Allows multiple data files to be provided (#10) Uses confmerge to merge multiple data files for the j2cli. --- .github/workflows/validate-pr.yml | 4 ++- action.yml | 2 +- fixtures/data.yml | 1 + fixtures/expected.yml | 1 + fixtures/secondary.yml | 3 ++ fixtures/template.yml.j2 | 1 + requirements.txt | 2 ++ src/render.sh | 34 +++++++++++++++--- tests/render.bats | 59 ++++++++++++++++++++++++++----- 9 files changed, 92 insertions(+), 15 deletions(-) create mode 100644 fixtures/secondary.yml diff --git a/.github/workflows/validate-pr.yml b/.github/workflows/validate-pr.yml index f78731d..5881212 100644 --- a/.github/workflows/validate-pr.yml +++ b/.github/workflows/validate-pr.yml @@ -46,7 +46,9 @@ jobs: - uses: ./ with: template: fixtures/template.yml.j2 - data: fixtures/data.yml + data: | + fixtures/data.yml + fixtures/secondary.yml format: yaml undefined: 'true' customize: fixtures/customize.py diff --git a/action.yml b/action.yml index 002830e..bde4e67 100644 --- a/action.yml +++ b/action.yml @@ -49,7 +49,7 @@ inputs: debug: description: 'If true, sets -x in the shell command' required: false - default: false + default: 'false' outputs: file: diff --git a/fixtures/data.yml b/fixtures/data.yml index a0a02dc..d6a83f9 100644 --- a/fixtures/data.yml +++ b/fixtures/data.yml @@ -1,3 +1,4 @@ --- message: hello world +secondary: bar diff --git a/fixtures/expected.yml b/fixtures/expected.yml index 249bb14..2ff3d86 100644 --- a/fixtures/expected.yml +++ b/fixtures/expected.yml @@ -6,3 +6,4 @@ bar: foo baz: baz foobar: foo empty: "" +secondary: "foo" diff --git a/fixtures/secondary.yml b/fixtures/secondary.yml new file mode 100644 index 0000000..59ad4e1 --- /dev/null +++ b/fixtures/secondary.yml @@ -0,0 +1,3 @@ +--- + +secondary: foo diff --git a/fixtures/template.yml.j2 b/fixtures/template.yml.j2 index 1e2087c..92b47d8 100644 --- a/fixtures/template.yml.j2 +++ b/fixtures/template.yml.j2 @@ -6,3 +6,4 @@ bar: {{ foo | translate }} baz: {{ env('FOOBAR') }} foobar: {% if "foo" is foobar %}foo{% endif %} empty: "{{ nothere }}" +secondary: "{{ secondary }}" diff --git a/requirements.txt b/requirements.txt index 48a257c..4038c28 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,3 +2,5 @@ j2cli==0.3.10 Jinja2==3.0.2 MarkupSafe==2.0.1 PyYAML==6.0 +# TODO - Replace with a release when it comes out +git+https://github.com/aisbergg/python-confmerge@06ec81b41043cc76d5fc60085daf3cfed2b610b1 diff --git a/src/render.sh b/src/render.sh index 4adf369..5e39d4e 100755 --- a/src/render.sh +++ b/src/render.sh @@ -14,6 +14,8 @@ function set-output() { else echo "[DEBUG] Output file not found, skipping outputs" >&2 fi + + rm -rf "$TEMP_DIRECTORY" } function render() { @@ -30,13 +32,10 @@ function render() { return 1 } - for var in Template Data Filters Tests Customize; do + for var in Template Filters Tests Customize; do VAR="${var^^}" val="${!VAR}" if [ -n "$val" ] && ! [ -f "$val" ]; then - whoami - ls -al "$(dirname "$val")" - ls -al "$val" echo "[ERROR] $var file not found: $val" >&2 return 2 fi @@ -47,9 +46,35 @@ function render() { return 3 } + TEMP_DIRECTORY="$(mktemp -d)" + export TEMP_DIRECTORY + + if [ -n "$DATA" ]; then + local ext='' + local data_files=() + while read -r file; do + [ -n "$file" ] || continue + [ -n "$ext" ] || ext="${file##*.}" + [ -f "$file" ] || { + echo "[ERROR] Data file not found: $file" >&2 + return 2 + } + data_files+=("$file") + done <<< "$DATA" + + if [ ${#data_files[@]} -gt 1 ]; then + DATA="$TEMP_DIRECTORY/data.$ext" + echo "[DEBUG] Merging ${data_files[*]} to $DATA" >&2 + confmerge "${data_files[@]}" "$DATA" + fi + fi + # Main trap set-output EXIT + echo "[DEBUG] $(j2 --version)" >&2 + echo "[DEBUG] $(confmerge --version)" >&2 + local COMMAND=(j2 -o "$OUTPUT") [ "$UNDEFINED" != "true" ] || COMMAND+=(--undefined) @@ -77,7 +102,6 @@ function render() { COMMAND+=("$TEMPLATE") [ -z "$DATA" ] || COMMAND+=("$DATA") - which j2 echo "[DEBUG] ${COMMAND[*]}" >&2 "${COMMAND[@]}" } diff --git a/tests/render.bats b/tests/render.bats index c6fabe1..1430ea5 100644 --- a/tests/render.bats +++ b/tests/render.bats @@ -3,18 +3,33 @@ # shellcheck source=../render.sh source "$BATS_TEST_DIRNAME/../src/render.sh" +function j2() { + echo "$*" >> "$J2_CMD_FILE" + env >> "$J2_ENV_FILE" +} + +function confmerge() { + echo "$*" >> "$CONFMERGE_CMD_FILE" +} + +function mktemp() { + mkdir -p "$TEST_TEMP_DIRECTORY" + echo "$TEST_TEMP_DIRECTORY" +} + function setup() { export J2_CMD_FILE="$BATS_TEST_TMPDIR/j2.cmd" export J2_ENV_FILE="$BATS_TEST_TMPDIR/j2.env" - # Inject a mock j2 cli - mkdir -p "$BATS_TEST_TMPDIR/bin" - { - echo 'echo "$*" > "'$J2_CMD_FILE'"' - echo 'env > "'$J2_ENV_FILE'"' - } > "$BATS_TEST_TMPDIR/bin/j2" - chmod +x "$BATS_TEST_TMPDIR/bin/j2" - export PATH="$BATS_TEST_TMPDIR/bin:$PATH" + export CONFMERGE_CMD_FILE="$BATS_TEST_TMPDIR/confmerge.cmd" + + export TEST_TEMP_DIRECTORY="$BATS_TEST_TMPDIR/tmp" + + # Use mock j2 and confmerge commands + export -f \ + j2 \ + confmerge \ + mktemp # Template is required (per action.yml) TEMPLATE="$BATS_TEST_TMPDIR/template.j2" @@ -141,3 +156,31 @@ function teardown() { grep -q -- "--undefined" "$J2_CMD_FILE" grep -qE "$TEMPLATE $DATA\$" "$J2_CMD_FILE" } + +@test "it should not call confmerge when there is a single data file" { + export DATA="$BATS_TEST_TMPDIR/data.yml" + + touch "$DATA" + + run render + + [ "$status" -eq 0 ] + [ -f "$CONFMERGE_CMD_FILE" ] # --version is called on it + [ "$(< "$CONFMERGE_CMD_FILE")" = "--version" ] +} + +@test "it should merge files when multiple data files are provided" { + DATA_FILE_1="$BATS_TEST_TMPDIR/data.yml" + DATA_FILE_2="$BATS_TEST_TMPDIR/secondary.yml" + + export DATA="$DATA_FILE_1 +$DATA_FILE_2" + + touch "$DATA_FILE_1" + touch "$DATA_FILE_2" + + run render + + grep -qE "$DATA_FILE_1 $DATA_FILE_2 $TEST_TEMP_DIRECTORY/data.yml\$" "$CONFMERGE_CMD_FILE" + grep -qE "$TEMPLATE $TEST_TEMP_DIRECTORY/data.yml\$" "$J2_CMD_FILE" +}