Skip to content

Commit 8d6872c

Browse files
authored
Invoke external programs using full paths (#13)
Fixes #12 Add nix tests
1 parent ae95409 commit 8d6872c

File tree

4 files changed

+66
-32
lines changed

4 files changed

+66
-32
lines changed

.github/workflows/test-suite.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ jobs:
1818
uses: cachix/install-nix-action@v30
1919
- name: Run tests
2020
run: |
21-
nix develop '.#ci' --command ./tests.bats
21+
nix develop '.#ci' --command bats tests.bats nix-tests.bats

bash-env-json

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,20 @@
2222
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2323
# SOFTWARE.
2424

25-
VERSION="0.9.1"
25+
VERSION="0.9.2a"
2626
USAGE="bash-env-json [--help] [--shellfns <comma-separated-function-names>] [path]"
2727

2828
shopt -s extglob
2929

30+
# external dependencies are ideally invoked via full paths,
31+
# so make it straightforward for the Nix flake to substitute for these
32+
function _env() { env "$@"; }
33+
function _jq() { jq "$@"; }
34+
function _mktemp() { mktemp "$@"; }
35+
function _rm() { rm "$@"; }
36+
function _sed() { sed "$@"; }
37+
function _touch() { touch "$@"; }
38+
3039
function capture() {
3140
local -n _capture_env="$1"
3241
local -n _capture_shellvars="$2"
@@ -37,12 +46,12 @@ function capture() {
3746
# environment variables
3847
while IFS='=' read -r -d '' _name _value; do
3948
_capture_env["$_name"]="${_value}"
40-
done < <(env -0)
49+
done < <(_env -0)
4150

4251
# shellvars
4352
for _name in $(
4453
set -o posix
45-
set | sed -n -e '/^[a-zA-Z_][a-zA-Z_0-9]*=/s/=.*$//p'
54+
set | _sed -n -e '/^[a-zA-Z_][a-zA-Z_0-9]*=/s/=.*$//p'
4655
set +o posix
4756
); do
4857
if test -v "$_name" -a ! "${_capture_env[$_name]+EXISTS}" -a ! "${_inhibit_shellvars[$_name]+EXISTS}"; then
@@ -52,9 +61,9 @@ function capture() {
5261
}
5362

5463
function emit_value() {
55-
# jq -R produces nothing on empty input, but we want ""
64+
# `jq -R` produces nothing on empty input, but we want ""
5665
if test -n "$1"; then
57-
echo -n "$1" | jq -R
66+
echo -n "$1" | _jq -R
5867
else
5968
echo -n '""'
6069
fi
@@ -82,17 +91,17 @@ function emit_error() {
8291
}
8392

8493
function emit_error_exit() {
85-
emit_error "$@" | jq
94+
emit_error "$@" | _jq
8695
exit 1
8796
}
8897

8998
function emit_help_exit() {
90-
emit_meta '{' '}' usage | jq
99+
emit_meta '{' '}' usage | _jq
91100
exit 0
92101
}
93102

94103
function emit_version_exit() {
95-
emit_meta '{' '}' | jq
104+
emit_meta '{' '}' | _jq
96105
exit 0
97106
}
98107

@@ -136,11 +145,11 @@ function eval_or_source() {
136145
local _source _path _error_file
137146
_path="$1"
138147

139-
_error_file=$(mktemp -u)
140-
touch "$_error_file"
148+
_error_file=$(_mktemp -u)
149+
_touch "$_error_file"
141150
# shellcheck disable=SC2094
142151
exec 3<"$_error_file" 4>"$_error_file"
143-
rm -f "$_error_file"
152+
_rm -f "$_error_file"
144153

145154
if test -n "$_path"; then
146155
# source from file if specified
@@ -161,19 +170,19 @@ function eval_or_source() {
161170
if ! eval "$_source" >/dev/null 2>&4; then
162171
exec 4>&-
163172
# discard error location, because it is this file not the one sourced
164-
emit_error_exit "$(sed -e 's/^.*line\s*[0-9]*:\s*//' <&3)"
173+
emit_error_exit "$(_sed -e 's/^.*line\s*[0-9]*:\s*//' <&3)"
165174
fi
166175
fi
167176
}
168177

169178
function invoke_safely() {
170179
local _fn="$1"
171180

172-
_error_file=$(mktemp -u)
173-
touch "$_error_file"
181+
_error_file=$(_mktemp -u)
182+
_touch "$_error_file"
174183
# shellcheck disable=SC2094
175184
exec 3<"$_error_file" 4>"$_error_file"
176-
rm -f "$_error_file"
185+
_rm -f "$_error_file"
177186

178187
"$_fn" >/dev/null 2>&4 || {
179188
exec 4>&-
@@ -233,14 +242,14 @@ function main() {
233242
capture _env_current _shellvars_current
234243

235244
# accumulate result in a file until we know we are error-free
236-
_result_file=$(mktemp -u)
237-
touch "$_result_file"
245+
_result_file=$(_mktemp -u)
246+
_touch "$_result_file"
238247
# shellcheck disable=SC2094
239248
exec 5<"$_result_file" 6>"$_result_file"
240-
rm -f "$_result_file"
249+
_rm -f "$_result_file"
241250

242-
emit "{" env _env_previous _env_current >&6
243-
emit "," shellvars _shellvars_previous _shellvars_current >&6
251+
emit "{" "env" _env_previous _env_current >&6
252+
emit "," "shellvars" _shellvars_previous _shellvars_current >&6
244253

245254
test "${#_shellfn_names[@]}" -gt 0 && {
246255
echo ",\"fn\":{" >&6
@@ -253,8 +262,8 @@ function main() {
253262
capture _env_current _shellvars_current
254263

255264
echo "$_fn_comma\"$_fn\":" >&6
256-
emit "{" env _env_previous _env_current >&6
257-
emit "," shellvars _shellvars_previous _shellvars_current >&6
265+
emit "{" "env" _env_previous _env_current >&6
266+
emit "," "shellvars" _shellvars_previous _shellvars_current >&6
258267
echo "}" >&6
259268
_fn_comma=","
260269
done
@@ -266,7 +275,7 @@ function main() {
266275
emit_meta ',' '}' >&6
267276
exec 6>&-
268277

269-
jq <&5
278+
_jq <&5
270279
}
271280

272281
function bad_usage() {

flake.nix

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
description = "Nix package for bash-env";
2+
description = "Nix package for bash-env-json";
33

44
inputs = {
55
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
@@ -17,18 +17,28 @@
1717
let
1818
inherit (pkgs) bash coreutils gnused jq makeWrapper writeShellScriptBin;
1919
inherit (pkgs.lib) makeBinPath;
20+
21+
substFullPaths = program_package:
22+
let replaceList = pkgs.lib.attrsets.mapAttrsToList (name: pkg: { from = " ${name} "; to = " ${pkg}/bin/${name} "; }) program_package; in
23+
builtins.replaceStrings (map (x: x.from) replaceList) (map (x: x.to) replaceList);
24+
2025
in
21-
(writeShellScriptBin "bash-env-json" (builtins.readFile ./bash-env-json)).overrideAttrs (old: {
22-
buildInputs = [ bash jq makeWrapper ];
26+
(writeShellScriptBin "bash-env-json"
27+
(substFullPaths
28+
{
29+
env = pkgs.coreutils;
30+
jq = pkgs.jq;
31+
mktemp = pkgs.coreutils;
32+
rm = pkgs.coreutils;
33+
sed = pkgs.gnused;
34+
touch = pkgs.coreutils;
35+
}
36+
(builtins.readFile ./bash-env-json))).overrideAttrs (old: {
37+
buildInputs = [ bash ];
2338
buildCommand =
2439
''
2540
${old.buildCommand}
2641
patchShebangs $out
27-
wrapProgram $out/bin/bash-env-json --prefix PATH : ${makeBinPath [
28-
coreutils
29-
gnused
30-
jq
31-
]}
3242
'';
3343
});
3444
in

nix-tests.bats

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env bats
2+
#
3+
# simple tests for the Nix packaging
4+
5+
@test "simple" {
6+
actual=$(echo 'export SOME_VARIABLE=some_value' | bash-env-json | jq -r '.env.SOME_VARIABLE')
7+
expected='some_value'
8+
test "$actual" == "$expected"
9+
}
10+
11+
@test "path" {
12+
actual=$(echo 'export PATH=/oops' | bash-env-json | jq -r '.env.PATH')
13+
expected='/oops'
14+
test "$actual" == "$expected"
15+
}

0 commit comments

Comments
 (0)