From d3ab48348f2c0d49025e06722b103a816af344da Mon Sep 17 00:00:00 2001 From: Chris Stone Date: Mon, 21 Jul 2025 06:06:18 -0600 Subject: [PATCH] Add initial Bats and ShellSpec test suites --- README.md | 11 +++++++++++ spec/git_flow_spec.sh | 17 +++++++++++++++++ test/bats/gitflow_common.bats | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+) create mode 100644 spec/git_flow_spec.sh create mode 100644 test/bats/gitflow_common.bats diff --git a/README.md b/README.md index af2e30a..a34a744 100644 --- a/README.md +++ b/README.md @@ -225,6 +225,17 @@ the command. The files should be placed in .git/hooks In the directory hooks you can find examples of all the hooks available. +## Testing + +Unit level tests are written with [Bats](https://github.com/bats-core/bats-core) and behavioural tests are written with [ShellSpec](https://github.com/shellspec/shellspec). Both tools must be installed locally and available on your `PATH`. + +Run all tests with: + +```shell +bats test/bats +shellspec +``` + ## Showing your appreciation diff --git a/spec/git_flow_spec.sh b/spec/git_flow_spec.sh new file mode 100644 index 0000000..f83f4bf --- /dev/null +++ b/spec/git_flow_spec.sh @@ -0,0 +1,17 @@ +Describe 'git-flow' + Describe 'version command' + It 'prints current version' + When run script ../git-flow version + The output should eq '2.2.1 (CJS Edition)' + The status should be success + End + End + + Describe 'usage without arguments' + It 'shows help text' + When run script ../git-flow + The status should be failure + The output should start with 'usage: git flow' + End + End +End diff --git a/test/bats/gitflow_common.bats b/test/bats/gitflow_common.bats new file mode 100644 index 0000000..513c6dc --- /dev/null +++ b/test/bats/gitflow_common.bats @@ -0,0 +1,32 @@ +#!/usr/bin/env bats + +setup() { + # Source required scripts + source "$BATS_TEST_DIRNAME/../../gitflow-shFlags" >/dev/null + source "$BATS_TEST_DIRNAME/../../gitflow-common" +} + +@test "check_boolean returns true for yes" { + run bash -c 'check_boolean yes' + [ "$status" -eq 0 ] +} + +@test "check_boolean returns false for no" { + run bash -c 'check_boolean no' + [ "$status" -eq 1 ] +} + +@test "check_boolean returns error for maybe" { + run bash -c 'check_boolean maybe' + [ "$status" -eq 2 ] +} + +@test "startswith detects prefix" { + run bash -c 'startswith foobar foo' + [ "$status" -eq 0 ] +} + +@test "startswith detects absence of prefix" { + run bash -c 'startswith foobar bar' + [ "$status" -ne 0 ] +}