From c4f811a23d43fe81b31a0504fdcb29824b6a9069 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Wed, 8 Nov 2023 23:50:22 +0100 Subject: [PATCH] Fix commands printed when TMPDIR is empty --- bin/ruby-build | 5 ++--- test/output.bats | 27 +++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 test/output.bats diff --git a/bin/ruby-build b/bin/ruby-build index 7eb540c9..c3764dab 100755 --- a/bin/ruby-build +++ b/bin/ruby-build @@ -110,10 +110,9 @@ colorize() { print_command() { local arg - local tmpdir="${TMPDIR%/}" for arg; do - arg="${arg//$tmpdir\//\$TMPDIR/}" - arg="${arg//$HOME\//\$HOME/}" + [ "${#TMPDIR}" -le 1 ] || arg="${arg//$TMP\//\$TMPDIR/}" + [ "${#HOME}" -le 1 ] || arg="${arg//$HOME\//\$HOME/}" case "$arg" in *\'* | *\$* ) printf ' "%s"' "$arg" ;; diff --git a/test/output.bats b/test/output.bats new file mode 100644 index 00000000..2c50e881 --- /dev/null +++ b/test/output.bats @@ -0,0 +1,27 @@ +#!/usr/bin/env bats + +load test_helper + +@test "print_command" { + mkdir -p "$TMP" + + cat < "$TMP"/definition +print_command ./configure --prefix="\$PREFIX_PATH" --arg='with spaces' +EOF + # substitute $TMPDIR in command invocations + TMPDIR="/tmp/" run ruby-build "$TMP"/definition /tmp/path/to/prefix + assert_output " ./configure \"--prefix=\$TMPDIR/path/to/prefix\" '--arg=with spaces'" + # doesn't substitute TMPDIR if it didn't come from user's environment + TMPDIR="" run ruby-build "$TMP"/definition /tmp/path/to/prefix + assert_output " ./configure --prefix=/tmp/path/to/prefix '--arg=with spaces'" + + cat < "$TMP"/definition +print_command install --bindir="$TMP"/home/.local/bin +EOF + # substitute $HOME in command invocations + HOME="$TMP"/home TMPDIR="" run ruby-build "$TMP"/definition /tmp/path/to/prefix + assert_output " install \"--bindir=\$HOME/.local/bin\"" + # do not substitute $HOME if it's root path + HOME="/" TMPDIR="" run ruby-build "$TMP"/definition /tmp/path/to/prefix + assert_output " install --bindir=${TMP}/home/.local/bin" +}