1

I have a bash script that calls linux mount.

I want to unit test that script. I'm currently using shunit2.

One thing that I tried was to create a file mount and tried to use this one to respond 0 or -1 or +1 depending on what I wanted to test.

It obviously didn't work :)

Is there a way to make it work? Any suggestions?

Alex Harvey
  • 14,494
  • 5
  • 61
  • 97
vianna77
  • 495
  • 1
  • 7
  • 17
  • Wow. Didn't even know that there was a unit testing framework for shell. Excuse me while I unit test all my scripts. We will probably need the problematic code to make any sort of suggestions though... – franklin Feb 25 '16 at 14:32
  • 1
    I kind of already solved the problem. As soon I arrive at my desk on Monday, I will post the answer here. – vianna77 Feb 27 '16 at 19:39

1 Answers1

0

I would create a mock for the mount command this way:

mount() {
  return $DESIRED_MOUNT_RETURN_CODE
}

setUp() {
  export DESIRED_MOUNT_RETURN_CODE=0 # default
}

testMountSucceeds() {
  . $script_under_test > /dev/null
  ...
}

testMountFails() {
  export DESIRED_MOUNT_RETURN_CODE=1
  . $script_under_test > /dev/null
  ...
}

. shunit2
Alex Harvey
  • 14,494
  • 5
  • 61
  • 97