2
0
mirror of https://github.com/kazhala/dotbare synced 2024-11-04 06:00:45 +00:00
dotbare/tests
2020-07-17 10:02:24 +10:00
..
Dockerfile test(docker): test container now source bash plugin 2020-06-07 14:56:59 +10:00
dotbare.bats fix(test): fix test faile 2020-07-09 11:17:05 +10:00
fadd.bats test: update fcheckout and fadd test with the reverted fzf executable 2020-07-07 13:44:06 +10:00
fbackup.bats test(fbackup): update fbackup test 2020-07-07 11:49:26 +10:00
fcheckout.bats test: update fcheckout and fadd test with the reverted fzf executable 2020-07-07 13:44:06 +10:00
fedit.bats test(fgrep): added test for fgrep 2020-07-15 14:49:08 +10:00
fgrep.bats test(fgrep): added test for fgrep 2020-07-15 14:49:08 +10:00
finit.bats test(finit): test submodule 2020-06-28 10:05:49 +10:00
flog.bats test(flog): update flog test 2020-07-07 14:58:43 +10:00
freset.bats test(freset): update freset test 2020-07-07 15:06:12 +10:00
fstash.bats test(freset): update freset test 2020-07-07 15:15:18 +10:00
fstat.bats test(fstat): update test 2020-06-28 10:05:49 +10:00
funtrack.bats test(funtrack): update funtrack test 2020-07-07 15:18:43 +10:00
fupgrade.bats docs(changlog): update new changes 2020-06-28 14:16:24 +10:00
fzf test(fgrep): added test for fgrep 2020-07-15 14:49:08 +10:00
git test: begin update all mocks 2020-07-07 11:20:22 +10:00
README.md docs(test): added readme for test 2020-07-17 10:02:24 +10:00
set_variable.bats docs(changlog): update new changes 2020-06-28 14:16:24 +10:00
shellcheck.sh test: include zsh plugin shellcheck 2020-07-13 14:41:35 +10:00
tree test: begin update all mocks 2020-07-07 11:20:22 +10:00

Testing dotbare

dotbare is unit tested using the bats testing frame work. Interaction with fzf are mocked using the PATH override method.

Refer to bats homepage for usage of bats and their test basics.

Mocking

One of the challenge when testing dotbare is interaction with fzf, git and some other shell functions. fzf is the most pain because it does require human interaction.

Using the PATH override method, at the very least, we can assert if fzf is invoked with right arguments.

How it works

There is a executable file called fzf in the test folder as well as git, tree etc. Before running test that requires fzf invokations, append the test folder path to the front of PATH variable.

#!/usr/bin/env bats

stage_selected_dir() {
  export PATH="${BATS_TEST_DIRNAME}:$PATH"
  bash "${BATS_TEST_DIRNAME}"/../dotbare fadd --dir
}

This way the executable will be executed by dotbare instead of the real fzf.

The fzf executable

I'm sure there might be more elegant way of doing this, if you have suggestions please help me improve it. Currently for better readability in all the bats test file, I added bunch of if statement in the fzf executable to assert if fzf is being called correctly.

if [[ "$*" =~ '--multi --preview ' ]] && [[ "$*" =~ "tree -L 1 -C --dirsfirst {}" ]]; then
  # dotbare fadd --dir -- "./fadd.bats" @test "fadd stage selected dir"
  echo "fadd_stage_dir"
fi

When the executable is being called, it will check for it's arguments and echo out a shorter word indicating the correct argument is passed.

#!/usr/bin/env bats

@test "fadd stage selected dir" {
  run stage_selected_dir
  # we could assert if fzf is actually being invoked correctly
  # in this case, it's supposed to search directory and using tree to provide preview
  [[ "${output}" =~ "add fadd_stage_dir" ]]
}

Full example

You could always checkout more examples in tests folder

The fzf executable

if [[ "$*" =~ '--header=select a commit' ]] && [[ "$*" =~ "show --color" ]]; then
  # dotbare flog --reset -y -- "./flog.bats" @test "flog reset"
  echo "--flog_reset"
fi

The bats unit test file.

#!/usr/bin/env bats

help() {
  bash "${BATS_TEST_DIRNAME}"/../dotbare flog -h
}
reset() {
  export PATH="${BATS_TEST_DIRNAME}:$PATH"
  bash "${BATS_TEST_DIRNAME}"/../dotbare flog --reset -y
}

@test "flog help" {
  run help
  [ "${status}" -eq 0 ]
  [ "${lines[0]}" = "Usage: dotbare flog [-h] [-r] [-R] [-e] [-c] [-y] ..." ]
}
@test "flog reset" {
  run reset
  [[ "${output}" =~ "reset --flog_reset" ]]
}