91 lines
2.4 KiB
YAML
91 lines
2.4 KiB
YAML
name: "Builtins: readonly"
|
|
cases:
|
|
- name: "making var readonly"
|
|
stdin: |
|
|
my_var="value"
|
|
readonly my_var
|
|
|
|
echo "Invoking declare -p..."
|
|
declare -p my_var
|
|
|
|
- name: "using readonly with value"
|
|
stdin: |
|
|
readonly my_var="my_value"
|
|
|
|
echo "Invoking declare -p..."
|
|
declare -p my_var
|
|
|
|
- name: "readonly array after append"
|
|
stdin: |
|
|
ARR=()
|
|
f() {
|
|
ARR+=("a" "b")
|
|
readonly ARR
|
|
echo "${#ARR[@]}"
|
|
}
|
|
f
|
|
|
|
- name: "Multiple readonly operands expand before assignments are applied"
|
|
stdin: |
|
|
unset first second
|
|
readonly first=one second=$first
|
|
declare -p first second
|
|
|
|
- name: "readonly on nameref makes nameref readonly"
|
|
known_failure: true
|
|
ignore_stderr: true
|
|
stdin: |
|
|
target="value"
|
|
declare -n ref=target
|
|
readonly ref
|
|
declare -p ref
|
|
declare -p target
|
|
# Changing what ref points to should fail
|
|
declare -n ref=other 2>/dev/null
|
|
echo "exit: $?"
|
|
# But target should still be writable
|
|
target="new_value"
|
|
echo "target: $target"
|
|
|
|
- name: "readonly target through nameref assignment"
|
|
known_failure: true
|
|
ignore_stderr: true
|
|
stdin: |
|
|
target="value"
|
|
readonly target
|
|
declare -n ref=target
|
|
ref="new_value" 2>/dev/null
|
|
echo "exit: $?"
|
|
echo "target: $target"
|
|
|
|
- name: "a failed readonly assignment inside a pipeline reports its status"
|
|
known_failure: true # TODO: a readonly assignment failing as a pipeline element reports status 0
|
|
stdin: |
|
|
readonly r=1
|
|
r=2 | cat
|
|
echo "pipeline: ${PIPESTATUS[0]}"
|
|
|
|
- name: "readonly -a with an assignment converts and types the variable"
|
|
stdin: |
|
|
e5=x
|
|
readonly -a e5=y
|
|
echo "value_status=$?"
|
|
declare -p e5
|
|
readonly -A na=([k]=v)
|
|
echo "assoc_status=$?"
|
|
declare -p na
|
|
|
|
- name: "readonly -f marks a function readonly rather than displaying it"
|
|
# brush routes `readonly -f` through the same display path as `declare -f`, so it prints the
|
|
# function's definition and never makes it readonly. A shell prints nothing, rejects a later
|
|
# redefinition, and reports a missing name as "readonly: NAME: not a function".
|
|
known_failure: true
|
|
ignore_stderr: true
|
|
stdin: |
|
|
g() { echo original; }
|
|
readonly -f g
|
|
echo "status: $?"
|
|
g() { echo redefined; }
|
|
g
|
|
readonly -f nosuchfn
|