Skip to content

Commit

Permalink
Allows multiple data files to be provided (#10)
Browse files Browse the repository at this point in the history
Uses confmerge to merge multiple data files for the j2cli.
  • Loading branch information
mike-carey authored Apr 26, 2023
1 parent 1e8050d commit c21abcf
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 15 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/validate-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ inputs:
debug:
description: 'If true, sets -x in the shell command'
required: false
default: false
default: 'false'

outputs:
file:
Expand Down
1 change: 1 addition & 0 deletions fixtures/data.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
---

message: hello world
secondary: bar
1 change: 1 addition & 0 deletions fixtures/expected.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ bar: foo
baz: baz
foobar: foo
empty: ""
secondary: "foo"
3 changes: 3 additions & 0 deletions fixtures/secondary.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---

secondary: foo
1 change: 1 addition & 0 deletions fixtures/template.yml.j2
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ bar: {{ foo | translate }}
baz: {{ env('FOOBAR') }}
foobar: {% if "foo" is foobar %}foo{% endif %}
empty: "{{ nothere }}"
secondary: "{{ secondary }}"
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
34 changes: 29 additions & 5 deletions src/render.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ function set-output() {
else
echo "[DEBUG] Output file not found, skipping outputs" >&2
fi

rm -rf "$TEMP_DIRECTORY"
}

function render() {
Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -77,7 +102,6 @@ function render() {
COMMAND+=("$TEMPLATE")
[ -z "$DATA" ] || COMMAND+=("$DATA")

which j2
echo "[DEBUG] ${COMMAND[*]}" >&2
"${COMMAND[@]}"
}
Expand Down
59 changes: 51 additions & 8 deletions tests/render.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
}

0 comments on commit c21abcf

Please sign in to comment.