sstephenson / bats

Bash Automated Testing System
MIT License
7.12k stars 519 forks source link

Need to specify the full path to date command otherwise date returns empty string #228

Open abauman-7signal opened 7 years ago

abauman-7signal commented 7 years ago

The following files demonstrate the issue:

~/work/Katas/bash/BashUnitTesting/test (master)$ cat test_helper.bash load 'helpers/mocks/stub' load 'helpers/bats-support/load' load 'helpers/bats-assert/load'

:~/work/Katas/bash/BashUnitTesting/test (master)$ cat test1.bats

!/usr/bin/env bats

source test_helper.bash

@test "test date command with full path on MAC OSX" { export USE_FULL_PATH="TRUE" run ./test1 assert_line --partial "17" }

@test "test date command without using full path on MAC OSX" { export USE_FULL_PATH="FALSE" run ./test1 assert_line --partial "17" }

~/work/Katas/bash/BashUnitTesting/test (master)$ cat test1

!/bin/bash

if [ $USE_FULL_PATH = "TRUE" ]; then echo /bin/date '+%y%m%d-%H%M%S' else echo date '+%y%m%d-%H%M%S' fi

~/work/Katas/bash/BashUnitTesting/test (master)$ sudo bats test1.bats ✓ test date command with full path on MAC OSX ✓ test date command without using full path on MAC OSX

2 tests, 0 failures

~/work/Katas/bash/BashUnitTesting/test (master)$ bats test1.bats ✓ test date command with full path on MAC OSX ✗ test date command without using full path on MAC OSX (from function assert_line' in file helpers/bats-assert/src/assert.bash, line 491, in test file test1.bats, line 14) assert_line --partial "17"' failed

-- no output line contains substring -- substring : 17 output :

2 tests, 1 failure

The version of MAC OSX is Yosemite 10.10.5

dimo414 commented 7 years ago

What makes you think this is an issue with Bats? A much simpler test case passes:

$ cat datetest.bats
#!/usr/bin/env bats

OS_VERSION=$(defaults read loginwindow SystemVersionStampAsString)

@test "test date command without full path on $OS_VERSION" {
  date '+%y%m%d-%H%M%S'
}

@test "test date command with full path on $OS_VERSION" {
  /bin/date '+%y%m%d-%H%M%S'
}

$ bats datetest.bats
 ✓ test date command without full path on 10.12.6
 ✓ test date command with full path on 10.12.6

2 tests, 0 failures

It seems more likely this is either an issue with your system, or with these Bats extensions you're using. If the latter, you should file an issue with the offending extension(s).

abauman-7signal commented 7 years ago

Did you check the output of date? As your test is written, there is no confirmation that the date command did anything expected. What I observed is that when specifying the full path, date returned a timestamp value per the formatting. But when called without the full path, it returned an empty string. My simpler scripts don't show that level of detail other than that the assert failed. But if you log the output to a file, I was able to see the issue. Again, this is running on a Mac. I did not try this on a version of Linux.

xmik commented 7 years ago

I checked your example on Linux and it works there. Instead of:

if [ $USE_FULL_PATH = "TRUE" ]; then
    echo /bin/date '+%y%m%d-%H%M%S'
else
    echo date '+%y%m%d-%H%M%S'
fi

I used:

if [ $USE_FULL_PATH = "TRUE" ]; then
    /bin/date '+%y%m%d-%H%M%S'
else
    date '+%y%m%d-%H%M%S'
fi

Which, I think, you also intended to do. Also, I didn't use: load 'helpers/mocks/stub', because I don't know what it does and I have never used it before.

When you invoke those commands on Mac directly, without bats:

date '+%y%m%d-%H%M%S'
/bin/date '+%y%m%d-%H%M%S'

do they return the same output?

dimo414 commented 7 years ago

What I observed is that when specifying the full path, date returned a timestamp value per the formatting. But when called without the full path, it returned an empty string.

I don't see that either, both variants produce output:

$ cat datetest.bats
#!/usr/bin/env bats

OS_VERSION=$(defaults read loginwindow SystemVersionStampAsString)

@test "test date command without full path on $OS_VERSION" {
  [[ -n "$(date '+%y%m%d-%H%M%S')" ]]
}

@test "test date command with full path on $OS_VERSION" {
  [[ -n "$(/bin/date '+%y%m%d-%H%M%S')" ]]
}

$ bats datetest.bats
 ✓ test date command without full path on 10.12.6
 ✓ test date command with full path on 10.12.6

2 tests, 0 failures

Can you provide a simpler test case that demonstrates your issue without any extensions?