Skip to content
This repository has been archived by the owner on Apr 29, 2021. It is now read-only.

saving $IFS in run() not altered for code using it - fix #89 #90

Merged
merged 2 commits into from
Feb 25, 2015

Conversation

Sylvain303
Copy link
Contributor

IFS was modified by run() becoming '\n' and so relying to its bash default
was failing tests.

Also some wrong tests corrected because was relying on this behavior to pass.

Fix #89

IFS was modified by run() becoming '\n' and so relying to its bash default
was failing tests.

Also some wrong tests corrected because was relying on this behavior to pass.

Fix sstephenson#89
@@ -37,7 +37,7 @@ fixtures bats
run bats "$FIXTURE_ROOT/passing.bats"
[ $status -eq 0 ]
[ ${lines[0]} = "1..1" ]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you quote ${lines[0]} on this line as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You want me to quote it too? I only quoted string compare with a space inside it.

bats/test$ grep -RHn '[^"] = "[^" ]\+' 
bats.bats:33:  [ $output = "1..0" ]
bats.bats:39:  [ ${lines[0]} = "1..1" ]
suite.bats:9:  [ $output = "1..0" ]
suite.bats:15:  [ ${lines[0]} = "1..1" ]
suite.bats:32:  [ ${lines[0]} = "1..3" ]
suite.bats:41:  [ ${lines[0]} = "1..3" ]
suite.bats:48:  [ ${lines[0]} = "1..3" ]

but nothing with a space on the right side of the compare

bats/test$ grep -RHn '[^"] = "[^" ]\+ ' #  <= there's a space here.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All strings (unless you're absolutely sure it will be a single number or word) should be quoted, especially in conditional expressions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem. Just that it' was not "part" of the modification I made. I'm modifying it. no more pingpong about it. 👍

@mislav
Copy link
Collaborator

mislav commented Jan 29, 2015

Good catch! This looks good.

mislav added a commit that referenced this pull request Feb 25, 2015
saving $IFS in run() not altered for code using it
@mislav mislav merged commit 955309a into sstephenson:master Feb 25, 2015
@sstephenson
Copy link
Owner

Sorry for the silence. I've held off on merging this patch because I'm worried it'll break tests for people who might rely on the old, erroneous IFS behavior. Even the Bats tests were not immune.

Can we revert until we're ready to make another release where this backwards-incompatible change is documented, with an explanation and a guide for how to ensure your assertions are properly quoted?

@mislav
Copy link
Collaborator

mislav commented Feb 25, 2015

Sure! Any reason why we couldn't just add documentation on top of this merge in master, before tagging a new release, without having to revert first?

@sstephenson
Copy link
Owner

I expect to see support issues from people whose Chef test suites clone Bats master :/

@mislav
Copy link
Collaborator

mislav commented Feb 26, 2015

Sure, but the documentation added to our README won't prevent their test suites to get broken by Chef re-runs. I'll open a PR to revert this now but I don't think we'll ever be able to protect those that are riding the bleeding edge from breaking changes.

@Sylvain303
Copy link
Contributor Author

Do you want I produce more thing? documentation or sed / perl snippet quoting recursively test?

In fact, my first test written with bats is failing due to $IFS alteration. I don't know exactly if its the expected bash behavior to set the $IFS on this line of code.

IFS=$'\n' lines=($output)

it's somewhat a double affectation.
I'm quite surprised it wasn't noticed before.

Did you try to run Chef testsuite with my patched bats?

@md5
Copy link

md5 commented Oct 11, 2015

We got bit by the IFS=$'\n' issue using bats 0.4.0 in the test suite for jwilder/nginx-proxy (see nginx-proxy/nginx-proxy#246). It would be nice to see the fix in a future version of Bats. If not, I'm not sure what the right workaround should be. I just ended up setting IFS back to the default before each for loop that was expecting the normal IFS.

@md5
Copy link

md5 commented Oct 12, 2015

I'll also add that those using ppa:duggan/bats on Debian or Ubuntu already have this patch included in their "0.4.0". That being said, it appears that the proposed package for Ubuntu vivid and the one in wily don't have the patch (cf. https://code.launchpad.net/ubuntu/+source/bats). Neither does the Homebrew packaging of 0.4.0, which is what I was using.

jinuxstyle pushed a commit to jinuxstyle/bats that referenced this pull request Jul 1, 2016
@ztombol ztombol mentioned this pull request Dec 13, 2016
18 tasks
@jasonkarns jasonkarns mentioned this pull request Oct 2, 2017
6 tasks
yarikoptic pushed a commit to neurodebian/bats that referenced this pull request Aug 6, 2019
Closes sstephenson#90. Closes sstephenson#83. Closes sstephenson#56. Closes sstephenson#55. Closes sstephenson#53. Resolves
outstanding issues from sstephenson#32.

Per all these earlier issues and PRs:

- The original bin/bats symlink was causing problems on Windows.
- The attempted fix from sstephenson#32 created problems for install.sh.
- Per sstephenson#90, changing bin/bats to a one-line script in sstephenson#88 broke some Bats
  installations that rely on symlink schemes (such as when installed via
  https://github.com/basherpm/basher).

For an idea of why I wanted to keep bin/bats as a script due to how
symlinks complicate things on Windows, see the following results I
discovered during a search for "git symlink windows":

- https://github.com/git-for-windows/git/wiki/Symbolic-Links
- https://blogs.windows.com/buildingapps/2016/12/02/symlinks-windows-10/
- libgit2/libgit2#4107
- https://stackoverflow.com/q/5917249

In the course of applying these changes, I realized libexec/bats
computed some extraneous variables, so I removed them, eliminating a few
external processes and subshells. I also cleaned up other small bits of
logic.

On top of making install.sh, bin/bats, and libexec/bats more resilient
and compact, the existing test suite (before adding the new
test/installer.bats file) sped up significantly, running 0.6s-0.7s
faster on my MacBook Pro (2.9 GHz Intel Core i5, 8GB RAM):

  Bash 3.2.57(1)-release before:
  58 tests, 0 failures

  real    0m4.924s
  user    0m3.045s
  sys     0m1.798s

  Bash 3.2.57(1)-release after:
  58 tests, 0 failures

  real    0m4.341s
  user    0m2.808s
  sys     0m1.540s

  Bash 4.4.23(1)-release before:
  58 tests, 0 failures

  real    0m5.228s
  user    0m3.046s
  sys     0m1.952s

  Bash 4.4.23(1)-release after:
  58 tests, 0 failures

  real    0m4.582s
  user    0m2.791s
  sys     0m1.643s

Also tweaks the Dockerfile to update the symlink to point to bin/bats,
not libexec/bats.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

IFS broken after first run
4 participants