|
| 1 | +# shellcheck shell=bash |
| 2 | + |
| 3 | +source "$BATS_TEST_DIRNAME/test_util.sh" |
| 4 | + |
| 5 | +setup_file() { |
| 6 | + test_util.setup_file |
| 7 | +} |
| 8 | + |
| 9 | +# This is ran before each "@test". |
| 10 | +setup() { |
| 11 | + # cd's to a temporary directory that is unique for each "@test". |
| 12 | + test_util.cd_test |
| 13 | + |
| 14 | + # Do initialization (this code is copied from test_git_alias.py) |
| 15 | + git init |
| 16 | + git config --global alias.globalalias status |
| 17 | + git config --global alias.x status |
| 18 | + git config --local alias.localalias status |
| 19 | + git config --local alias.y status |
| 20 | +} |
| 21 | + |
| 22 | +@test "list all works" { |
| 23 | + run git alias |
| 24 | + |
| 25 | + # This is one way to do it, most similar to the "test_list_all" test in |
| 26 | + # test_git_alias.py. It's slightly more accurate because each substring |
| 27 | + # must match a particular line of output (rather than matching any part of the output) |
| 28 | + assert_line 'globalalias = status' |
| 29 | + assert_line 'x = status' |
| 30 | + assert_line 'localalias = status' |
| 31 | + assert_line 'y = status' |
| 32 | + |
| 33 | + # To test the full output, this is one way: |
| 34 | + assert_output 'globalalias = status |
| 35 | +localalias = status |
| 36 | +x = status |
| 37 | +y = status' |
| 38 | + |
| 39 | + # One can use heredocs to also achieve this, which makes the test look a little nicer. Note that the indentation MUST BE DONE WITH TABS. |
| 40 | + assert_output - <<-"EOF" |
| 41 | + globalalias = status |
| 42 | + localalias = status |
| 43 | + x = status |
| 44 | + y = status |
| 45 | +EOF |
| 46 | + |
| 47 | + # I tend to put 'assert_success' at the bottom. This usually makes debugging easier because an "assert_output" written above will usuall fail first. This will cause the the output of the run command to be printed (instead of just the exit code). |
| 48 | + # The downside to this is that sometimes there is a bug: An error "lines: parameter not set" will be shown instead (which is a Bats issue). |
| 49 | + assert_success |
| 50 | +} |
| 51 | + |
| 52 | +@test "list all global works" { |
| 53 | + run git alias --global |
| 54 | + |
| 55 | + # One debugging technique for bats test I've found useful is to redirect to |
| 56 | + # fd3. The variable "$output" is set by Bats, and it is the output of the previous "run" command (in this case, `git alias --global`). Here, we are printing it to fd3 for debugging. Naturally, be sure to run `exec 3>&1` (assuming you are running Bash or Zsh shell) in the terminal before running Bats. |
| 57 | +# echo "$output" >&3 |
| 58 | + |
| 59 | + |
| 60 | + assert_output 'globalalias = status |
| 61 | +x = status' |
| 62 | + assert_success |
| 63 | +} |
0 commit comments