Permalink
Browse files

Fix the behavior of 'readonly foo', caught by our own configure script!

test/gold.sh runs configure.  'test/gold.sh all' passes now.

Also add two related spec tests for 'local' behavior.

Test cleanup:

- Remove a test that was folded into here-doc.test.sh.
- Move spec/readonly.sh to gold/ and add it to the test/gold.sh driver.
  - We want to migrate all the shell scripts in the spec/ directory that
    aren't *.test.sh.
  • Loading branch information...
Andy Chu
Andy Chu committed Nov 8, 2017
1 parent 4080735 commit ee4d2b87be171a0f1b84489332887a6ffb948b5e
Showing with 51 additions and 30 deletions.
  1. +17 −11 core/cmd_exec.py
  2. +1 −1 core/state.py
  3. +1 −3 {spec → gold}/readonly.sh
  4. +0 −11 spec/09-here-doc-compound.sh
  5. +19 −0 spec/builtin-vars.test.sh
  6. +12 −3 test/gold.sh
  7. +1 −1 test/spec.sh
View
@@ -690,30 +690,36 @@ def _Dispatch(self, node, fork_external):
raise NotImplementedError(node.keyword)
for pair in node.pairs:
if pair.rhs:
# RHS can be a string or array.
val = self.ev.EvalWordToAny(pair.rhs)
assert isinstance(val, runtime.value), val
else:
# 'local x' is equivalent to local x=""
val = runtime.Str('')
if pair.op == assign_op.PlusEqual:
assert pair.rhs, pair.rhs # I don't think a+= is valid?
val = self.ev.EvalWordToAny(pair.rhs)
old_val, lval = expr_eval.EvalLhs(pair.lhs, self.arith_ev, self.mem,
self.exec_opts)
sig = (old_val.tag, val.tag)
if sig == (value_e.Str, value_e.Str):
if sig == (value_e.Undef, value_e.Str):
pass # val is RHS
elif sig == (value_e.Undef, value_e.StrArray):
pass # val is RHS
elif sig == (value_e.Str, value_e.Str):
val = runtime.Str(old_val.s + val.s)
elif sig == (value_e.Str, value_e.StrArray):
e_die("Can't append array to string")
elif sig == (value_e.StrArray, value_e.Str):
e_die("Can't append string to array")
elif sig == (value_e.StrArray, value_e.StrArray):
val = runtime.StrArray(old_val.strs + val.strs)
else:
else: # plain assignment
lval = self._EvalLhs(pair.lhs)
#log('ASSIGNING %s -> %s', lval, val)
# RHS can be a string or array.
if pair.rhs:
val = self.ev.EvalWordToAny(pair.rhs)
assert isinstance(val, runtime.value), val
else:
# e.g. 'readonly x' or 'local x'
val = None # only changing flags
self.mem.SetVar(lval, val, flags, lookup_mode)
# TODO: This should be eval of RHS, unlike bash!
View
@@ -410,7 +410,7 @@ def SetVar(self, lval, value, new_flags, lookup_mode):
cell.readonly = True
else:
if value is None:
value = runtime.Str('') # export foo, readonly foo
value = runtime.Undef() # export foo, readonly foo
cell = runtime.cell(value,
var_flags.Exported in new_flags ,
var_flags.ReadOnly in new_flags )
@@ -1,7 +1,4 @@
#!/usr/bin/env bash
#
# Usage:
# ./readonly.sh <function name>
set -o nounset
set -o pipefail
@@ -14,6 +11,7 @@ f1() {
readonly foo # not anymore
#foo=2 # would cause an exception
echo $foo
echo done

This file was deleted.

Oops, something went wrong.
View
@@ -218,3 +218,22 @@ echo "${a[@]}" len="${#a[@]}"
# stdout: x z len=2
# N-I dash status: 2
# N-I dash stdout-json: ""
### Use local twice
f() {
local foo=bar
local foo
echo $foo
}
f
# stdout: bar
### Local without variable is still unset!
set -o nounset
f() {
local foo
echo "[$foo]"
}
f
# status: 1
# OK dash status: 2
View
@@ -76,7 +76,7 @@ gen-module-init() {
}
wild() {
_compare test/wild.sh parse-usr-bin
_compare test/wild.sh all '^distro/usr-bin'
}
# NOTE: zsh behaves differently under sh and bin/osh! Looks like it is an
@@ -96,17 +96,26 @@ nix() {
_compare gold/nix.sh isElfSimpleWithStdin
}
readonly_() {
_compare gold/readonly.sh
}
all() {
readonly_
version-text
count
one-spec-test
html-summary
configure
no-op
gen-module-init
wild
startup-benchmark
glob
# This one takes a little long, but it's realistic.
wild
# There are slight differences in the number of syscalls reported. Not sure
# of the cause.
#startup-benchmark
}
"$@"
View
@@ -420,7 +420,7 @@ arith-context() {
}
array() {
sh-spec spec/array.test.sh --osh-failures-allowed 11 \
sh-spec spec/array.test.sh --osh-failures-allowed 10 \
$BASH $MKSH $OSH "$@"
}

0 comments on commit ee4d2b8

Please sign in to comment.