From c97ade43a9f45eb3feb9e1e72e07661bd01df0e6 Mon Sep 17 00:00:00 2001 From: Simon Guest Date: Wed, 1 Jan 2025 10:44:21 +1300 Subject: [PATCH 1/2] Fix multiline environment variables with jq -s When converting raw strings to JSON. Fixes #15 --- bash-env-json | 4 ++-- nix-tests.bats | 4 ++-- tests.bats | 7 +++++-- tests/multiline-string.env | 3 +++ tests/multiline-string.json | 6 ++++++ tests/multiline-string.setup.env | 1 + 6 files changed, 19 insertions(+), 6 deletions(-) create mode 100644 tests/multiline-string.env create mode 100644 tests/multiline-string.json create mode 100644 tests/multiline-string.setup.env diff --git a/bash-env-json b/bash-env-json index 0164ba9..dc83035 100755 --- a/bash-env-json +++ b/bash-env-json @@ -22,7 +22,7 @@ # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. -VERSION="0.10.0a1" +VERSION="0.10.0a2" USAGE="bash-env-json [--help] [--shellfns ] [path]" shopt -s extglob @@ -63,7 +63,7 @@ function capture() { function emit_value() { # `jq -R` produces nothing on empty input, but we want "" if test -n "$1"; then - echo -n "$1" | _jq -R + echo -n "$1" | _jq -Rs else echo -n '""' fi diff --git a/nix-tests.bats b/nix-tests.bats index 569aa10..4967da3 100755 --- a/nix-tests.bats +++ b/nix-tests.bats @@ -3,13 +3,13 @@ # simple tests for the Nix packaging @test "simple" { - actual=$(echo 'export SOME_VARIABLE=some_value' | bash-env-json | jq -r '.env.SOME_VARIABLE') + actual=$(echo 'export SOME_VARIABLE=some_value' | ./bash-env-json | jq -r '.env.SOME_VARIABLE') expected='some_value' test "$actual" == "$expected" } @test "path" { - actual=$(echo 'export PATH=/oops' | bash-env-json | jq -r '.env.PATH') + actual=$(echo 'export PATH=/oops' | ./bash-env-json | jq -r '.env.PATH') expected='/oops' test "$actual" == "$expected" } diff --git a/tests.bats b/tests.bats index 99ede72..243e390 100755 --- a/tests.bats +++ b/tests.bats @@ -13,7 +13,7 @@ function test_case() { echo "$@" # sort and remove `meta` before comparison with expected output - bash-env-json "$@" "$_test_file" | jq --sort-keys 'del(.meta)' | diff -w - "$_expected_output" + ./bash-env-json "$@" "$_test_file" | jq --sort-keys 'del(.meta)' | diff -w - "$_expected_output" } @test "empty" { @@ -40,11 +40,14 @@ function test_case() { test_case "Ming's menu of (merciless) monstrosities" } +@test "multiline-string" { + test_case multiline-string +} + @test "error" { test_case error } - @test "shell-function-error" { test_case shell-function-error --shellfns f } diff --git a/tests/multiline-string.env b/tests/multiline-string.env new file mode 100644 index 0000000..8560b37 --- /dev/null +++ b/tests/multiline-string.env @@ -0,0 +1,3 @@ +export MULTILINE="a +b +c" diff --git a/tests/multiline-string.json b/tests/multiline-string.json new file mode 100644 index 0000000..0c521cb --- /dev/null +++ b/tests/multiline-string.json @@ -0,0 +1,6 @@ +{ + "env": { + "MULTILINE": "a\nb\nc" + }, + "shellvars": { } +} diff --git a/tests/multiline-string.setup.env b/tests/multiline-string.setup.env new file mode 100644 index 0000000..632b83d --- /dev/null +++ b/tests/multiline-string.setup.env @@ -0,0 +1 @@ +unset MULTILINE From 97c28b1fc81032aee648fae998dd4b53ff5e9c4d Mon Sep 17 00:00:00 2001 From: Simon Guest Date: Wed, 1 Jan 2025 10:55:44 +1300 Subject: [PATCH 2/2] Unbreak the Nix tests --- nix-tests.bats | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nix-tests.bats b/nix-tests.bats index 4967da3..569aa10 100755 --- a/nix-tests.bats +++ b/nix-tests.bats @@ -3,13 +3,13 @@ # simple tests for the Nix packaging @test "simple" { - actual=$(echo 'export SOME_VARIABLE=some_value' | ./bash-env-json | jq -r '.env.SOME_VARIABLE') + actual=$(echo 'export SOME_VARIABLE=some_value' | bash-env-json | jq -r '.env.SOME_VARIABLE') expected='some_value' test "$actual" == "$expected" } @test "path" { - actual=$(echo 'export PATH=/oops' | ./bash-env-json | jq -r '.env.PATH') + actual=$(echo 'export PATH=/oops' | bash-env-json | jq -r '.env.PATH') expected='/oops' test "$actual" == "$expected" }