Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shellspec fails when using nounset option #31

Closed
damienflament opened this issue Feb 3, 2020 · 1 comment · Fixed by #32
Closed

Shellspec fails when using nounset option #31

damienflament opened this issue Feb 3, 2020 · 1 comment · Fixed by #32
Labels
bug Something isn't working

Comments

@damienflament
Copy link
Contributor

I use Bash strict mode in my script and unit tests. When an unset parameter is expanded in my tested script (due to a bug), this cause an error in Shellspec core.

I added an example in the Shellspec specification to bring out this bug:

Context "When using Bash strict mode"
    Skip if "not on Bash" test "${BASH:-}"x = x

    set -o nounset

    fDescribe "Shellspec"
        It "runs an example which expands an unset parameter"
            expand_unset() {
                : "${UNSET_PARAMETER}"
            }

            When call expand_unset
            The status should be failure
        End
    End
End

The expected result would be:

$ shellspec --focus
Running: /bin/sh [bash 5.0.11(1)-release]
F

Examples:
  1) When using Bash strict mode Shellspec runs an example which expands an unset parameter
     1.1) Example aborted (exit status: 1)

            stderr:main: line 93: UNSET_PARAMETER: unbound variable
            
          # spec/bugs_spec.sh:4

Finished in ? seconds (user ? seconds, sys ? seconds)
1 example, 1 failure

Failure examples: (Listed here affect your suite's status)

shellspec spec/bugs_spec.sh:30 # 1) When using Bash strict mode Shellspec runs an example which expands an unset parameter FAILED

But it is:

$ shellspec --focus
Running: /bin/sh [bash 5.0.11(1)-release]
/usr/lib/shellspec/lib/core/outputs.sh: line 176: LF: unbound variable
Unexpected output to the stderr at line 29-37 in 'spec/bugs_spec.sh'


Finished in ? seconds (user ? seconds, sys ? seconds)
0 examples, 0 failures, aborted by an unexpected error

An unexpected error occurred or output to the stderr. [0] [1] [102]
Fatal error occurred, terminated with exit status 1.

And with debug output:

image

But using the current Shellspec specification does not bring up the bug, because the LF parameter is defined in the specification helper:

LF="$SHELLSPEC_LF" TAB="$SHELLSPEC_TAB"

Because this parameter is not defined in my specification helper, it causes the error.

@ko1nksm
Copy link
Member

ko1nksm commented Feb 4, 2020

Thanks! I used a variable that should not be used.

I think it works with this fix.

diff --git a/lib/core/outputs.sh b/lib/core/outputs.sh
index fd75ea6..bf28e2b 100644
--- a/lib/core/outputs.sh
+++ b/lib/core/outputs.sh
@@ -169,11 +169,11 @@ shellspec_output_SYNTAX_ERROR_PARAM_TYPE() {
 shellspec_output_ABORTED() {
   if [ -s "$SHELLSPEC_STDOUT_FILE" ]; then
     shellspec_readfile SHELLSPEC_STDOUT "$SHELLSPEC_STDOUT_FILE"
-    set -- "$1" "${2:-}stdout:${SHELLSPEC_STDOUT}${LF}"
+    set -- "$1" "${2:-}stdout:${SHELLSPEC_STDOUT}${SHELLSPEC_LF}"
   fi
   if [ -s "$SHELLSPEC_STDERR_FILE" ]; then
     shellspec_readfile SHELLSPEC_STDERR "$SHELLSPEC_STDERR_FILE"
-    set -- "$1" "${2:-}stderr:${SHELLSPEC_STDERR}${LF}"
+    set -- "$1" "${2:-}stderr:${SHELLSPEC_STDERR}${SHELLSPEC_LF}"
   fi
   shellspec_output_statement "tag:bad" "note:" "fail:y" \
     "message:Example aborted (exit status: $1)" "failure_message:${2:-}"

I should be better removed definition of LF and TAB in spec_helper.sh of shellspec.

@ko1nksm ko1nksm added bug Something isn't working ACCEPTABLE Pull requests are welcome! WORKING Work in progress labels Feb 4, 2020
ko1nksm added a commit that referenced this issue Feb 4, 2020
@ko1nksm ko1nksm removed WORKING Work in progress ACCEPTABLE Pull requests are welcome! labels Feb 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants