Skip to content

Commit

Permalink
Merge pull request #10 from bouk/quote-more-words
Browse files Browse the repository at this point in the history
Quote the word values in unset substitutions
  • Loading branch information
bouk authored Jan 21, 2021
2 parents 02f9ea6 + f0b5715 commit dab74d8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
8 changes: 4 additions & 4 deletions translate/translate.go
Original file line number Diff line number Diff line change
Expand Up @@ -659,19 +659,19 @@ func (t *Translator) paramExp(p *syntax.ParamExp, quoted bool) {
switch op := p.Exp.Op; op {
case syntax.AlternateUnsetOrNull:
t.printf(`(test -n "$%s" && echo `, param)
t.word(p.Exp.Word, false)
t.word(p.Exp.Word, true)
t.str(" || echo)")
case syntax.AlternateUnset:
t.printf(`(set -q %s && echo `, param)
t.word(p.Exp.Word, false)
t.word(p.Exp.Word, true)
t.str(" || echo)")
case syntax.DefaultUnsetOrNull:
t.printf(`(test -n "$%s" && echo "$%s" || echo `, param, param)
t.word(p.Exp.Word, false)
t.word(p.Exp.Word, true)
t.str(")")
case syntax.DefaultUnset:
t.printf(`(set -q %s && echo "$%s" || echo `, param, param)
t.word(p.Exp.Word, false)
t.word(p.Exp.Word, true)
t.str(")")
case syntax.RemSmallPrefix, syntax.RemLargePrefix, syntax.RemSmallSuffix, syntax.RemLargeSuffix: // a#a a##a a%a a%%a
isPath := strings.HasSuffix(param, "PATH")
Expand Down
23 changes: 16 additions & 7 deletions translate/translate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ end
name: "recursive translation",
in: `source /opt/source.sh`,
expected: `/bin/babelfish < /opt/source.sh | source
`,
},
{
name: "append to PATH",
in: `
export NIX_PATH="nixpkgs=/nix/var/nix/profiles/per-user/root/channels/nixos:nixos-config=/etc/nixos/configuration.nix"
export NIX_PATH="$HOME/.nix-defexpr/channels${NIX_PATH:+:$NIX_PATH}"`,
expected: `set -gx NIX_PATH 'nixpkgs=/nix/var/nix/profiles/per-user/root/channels/nixos:nixos-config=/etc/nixos/configuration.nix'
set -gx NIX_PATH "$HOME"'/.nix-defexpr/channels'(test -n "$NIX_PATH" && echo ':'"$NIX_PATH" || echo)
`,
},
}
Expand Down Expand Up @@ -219,10 +228,10 @@ function chruby_use
puts "export RUBY_VERSION=#{RUBY_VERSION};"
begin; require \'rubygems\'; puts "export GEM_ROOT=#{Gem.default_dir.inspect};"; rescue LoadError; end
'| psub) | string collect; or echo)
set -gx PATH (test -n "$GEM_ROOT" && echo $GEM_ROOT/bin: || echo)"$PATH"
set -gx PATH (test -n "$GEM_ROOT" && echo "$GEM_ROOT"'/bin:' || echo)"$PATH"
if test (id -ru) -ne 0
set -gx GEM_HOME "$HOME"'/.gem/'"$RUBY_ENGINE"'/'"$RUBY_VERSION"
set -gx GEM_PATH "$GEM_HOME"(test -n "$GEM_ROOT" && echo :$GEM_ROOT || echo)(test -n "$GEM_PATH" && echo :$GEM_PATH || echo)
set -gx GEM_PATH "$GEM_HOME"(test -n "$GEM_ROOT" && echo ':'"$GEM_ROOT" || echo)(test -n "$GEM_PATH" && echo ':'"$GEM_PATH" || echo)
set -gx PATH "$GEM_HOME"'/bin:'"$PATH"
end
true
Expand Down Expand Up @@ -353,16 +362,16 @@ if [ -z "$SSH_AUTH_SOCK" ]
set -gx SSH_AUTH_SOCK (/bin/gpgconf --list-dirs agent-ssh-socket | string collect; or echo)
end
if [ -d '/share/gsettings-schemas/name' ]
set -gx whatevs "$whatevs"(test -n "$whatevs" && echo : || echo)'/share/gsettings-schemas/name'
set -gx whatevs "$whatevs"(test -n "$whatevs" && echo ':' || echo)'/share/gsettings-schemas/name'
else if false
true
else
true
end
echo (set -q cool && echo a || echo)
echo (test -n "$cool" && echo a || echo)
echo (set -q cool && echo "$cool" || echo a)
echo (test -n "$cool" && echo "$cool" || echo a)
echo (set -q cool && echo 'a' || echo)
echo (test -n "$cool" && echo 'a' || echo)
echo (set -q cool && echo "$cool" || echo 'a')
echo (test -n "$cool" && echo "$cool" || echo 'a')
set -e ASPELL_CONF
for i in a b c
if [ -d "$i"'/lib/aspell' ]
Expand Down

0 comments on commit dab74d8

Please sign in to comment.