Skip to content

Commit 88615ce

Browse files
committed
Initial scaffold
1 parent 5c3fb1b commit 88615ce

File tree

5 files changed

+121
-0
lines changed

5 files changed

+121
-0
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "vendor/bats-all"]
2+
path = vendor/bats-all
3+
url = https://github.com/hyperupcall/bats-all

tests/git-abort.bats

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
}

tests/git-alias.bats

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
setup() {
10+
test_util.cd_test
11+
12+
# Any extra setup goes here.
13+
}
14+
15+
@test "blah 1" {
16+
:
17+
}
18+
19+
@test "blah 2" {
20+
:
21+
}

tests/test_util.sh

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# shellcheck shell=bash
2+
3+
# Bats has some very (official) nifty utility libraries to make testing easier.
4+
# For example, `bats-support` adds `fail()`
5+
# For example, `bats-assert` adds `assert()`, `assert_success`, `assert_output`.
6+
# These helper functions:
7+
# - _Significantly_ improve error messages (this is the main reason why I like to use these)
8+
# - Make the test more semantic and readable (ex. assert_success reads better than [ $? == 0 ])
9+
# I created a repository that combines each of the three Bats utility libraries. This makes testing easier, but separate submodules for each Bats utility library can be crated (ex. vendor/bats-all, vendor/bats-support, vendor/bats-assert)
10+
# This `load.bash` sources all Bats utility libraries
11+
source "$BATS_TEST_DIRNAME/../vendor/bats-all/load.bash"
12+
13+
# Various helper functions. I always prefix them with "test_util" so their intent and
14+
# location of definition (this file) is obvious wherever they are used.
15+
16+
test_util.setup_file() {
17+
# Bats automatically sets 'set -e', so we don't have to do any "|| exit 1" stuff
18+
cd "$BATS_FILE_TMPDIR"
19+
20+
# Export some variables so Git neither reads nor mutates the users' Git config.
21+
export GIT_CONFIG_NOSYSTEM=1
22+
export GIT_CONFIG_GLOBAL="$PWD/git_config"
23+
# This removes default warning about default "master" branch on some Git versions.
24+
git config --global init.defaultBranch main
25+
26+
# Append to path so that we can access all commands included from git-extras
27+
# TODO: This currently breaks with commands that are included in "not_needed_git_repo" etc.
28+
PATH="$BATS_TEST_DIRNAME/../bin:$PATH"
29+
}
30+
31+
test_util.cd_test() {
32+
cd "$BATS_TEST_TMPDIR"
33+
}

vendor/bats-all

Submodule bats-all added at a16a4a2

0 commit comments

Comments
 (0)