schmidmt / shunit2

Automatically exported from code.google.com/p/shunit2
1 stars 0 forks source link

assertNotNull is not reliable with inputs containing single quotes #14

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. assertNotNull 'error-msg-4' "xxx there's no /x/y"

What is the expected output? What do you see instead?
The assertion above fails despite it should not.

What version of the product are you using? On what operating system?
# tested shunit2 versions: shUnit 2.1.5 and shUnit 2.0.3
# shell: GNU bash, version 3.2.48(21)-release (i686-pc-cygwin)
# uname -s: CYGWIN_NT-5.1
# uname -r: 1.5.25(0.156/4/2)
# uname -v: 2008-06-12 19:34
# uname -m: i686
# uname -p: unknown
# uname -i: unknown
# uname -o: Cygwin

Please provide any additional information below.
See attached file (test_assertNotNull.zip) that contains a test script and
the produced output for each of the tested version of shUnit.

Original issue reported on code.google.com by GVa...@gmail.com on 18 May 2009 at 1:45

Attachments:

GoogleCodeExporter commented 9 years ago
Here's my replacement for line 267 of shUnit 2.1.5 to fix this:

  _shunit_escaped_test="${1//\\/\\\\}"
  _shunit_escaped_test="${_shunit_escaped_test//\$/\\\$}"
  _shunit_escaped_test="${_shunit_escaped_test//\`/\\\`}"
  _shunit_escaped_test="${_shunit_escaped_test//\"/\\\"}"
  assertTrue "${shunit_message_}" "[ -n \"${_shunit_escaped_test}\" ]"

And here are some additional test cases for
src/shell/shunit2_test_asserts.sh:testAssertNotNull():

  ( assertNotNull "x'b" >"${stdoutF}" 2>"${stderrF}" )
  th_assertTrueWithNoOutput 'not null, with single-quote' $? "${stdoutF}" "${stderrF}"

  ( assertNotNull 'x"b' >"${stdoutF}" 2>"${stderrF}" )
  th_assertTrueWithNoOutput 'not null, with double-quote' $? "${stdoutF}" "${stderrF}"

  ( assertNotNull 'x$b' >"${stdoutF}" 2>"${stderrF}" )
  th_assertTrueWithNoOutput 'not null, with dollar' $? "${stdoutF}" "${stderrF}"

  ( assertNotNull 'x`b' >"${stdoutF}" 2>"${stderrF}" )
  th_assertTrueWithNoOutput 'not null, with backtick' $? "${stdoutF}" "${stderrF}"

Original comment by wood4...@gmail.com on 19 Mar 2010 at 2:45

GoogleCodeExporter commented 9 years ago
Looking at including a fix for this in 2.1.6. The code by wood4321 is a good 
start, but only works in some shells. 
I'll try to adapt it and make it portable.

Original comment by kate.war...@gtempaccount.com on 11 May 2010 at 8:36

GoogleCodeExporter commented 9 years ago
I've added some improvements for strange chars (like single quotes), but so far 
they only work under OS X due to sed variations. (See CL# 312) 

Original comment by kate.war...@gtempaccount.com on 14 Mar 2011 at 10:47