1+ #! /usr/bin/env bats
2+
3+ load ' test_helper/bats-support/load'
4+ load ' test_helper/bats-assert/load'
5+
6+ # Set up test environment
7+ export GIV_HOME=" $BATS_TEST_DIRNAME /.giv"
8+ export GIV_LIB_DIR=" $BATS_TEST_DIRNAME /../src/lib"
9+ export GIV_DEBUG=" false"
10+
11+ setup () {
12+ # Create test directory
13+ mkdir -p " $GIV_HOME "
14+ cd " $BATS_TEST_TMPDIR "
15+ }
16+
17+ teardown () {
18+ # Clean up test files
19+ rm -f test_* .md test_* .txt
20+ }
21+
22+ @test " portable_mktemp creates valid temporary files" {
23+ # Test that portable_mktemp creates valid files and returns proper paths
24+ . " $GIV_LIB_DIR /system.sh"
25+
26+ # Test with mktemp available
27+ if command -v mktemp > /dev/null 2>&1 ; then
28+ result=$( portable_mktemp " test.XXXXXX" )
29+ assert [ -n " $result " ]
30+ assert [ -f " $result " ]
31+ # Verify it doesn't contain malformed variable expansions
32+ refute_output --partial " TMPDIR:-"
33+ rm -f " $result "
34+ fi
35+ }
36+
37+ @test " portable_mktemp_dir sets correct base path" {
38+ # Test that portable_mktemp_dir doesn't create malformed paths
39+ . " $GIV_LIB_DIR /system.sh"
40+
41+ # Unset GIV_TMP_DIR to test fallback behavior
42+ unset GIV_TMP_DIR
43+ portable_mktemp_dir
44+
45+ # Verify GIV_TMP_DIR is set and doesn't contain literal "TMPDIR:-"
46+ assert [ -n " $GIV_TMP_DIR " ]
47+ refute [[ " $GIV_TMP_DIR " == * " TMPDIR:-" * ]]
48+ assert [ -d " $GIV_TMP_DIR " ]
49+ }
50+
51+ @test " manage_section works with proper arguments" {
52+ . " $GIV_LIB_DIR /system.sh"
53+ . " $GIV_LIB_DIR /markdown.sh"
54+
55+ # Create test files
56+ echo " # Changelog" > test_changelog.md
57+ echo " - New feature" > test_content.txt
58+
59+ # Test manage_section with valid arguments
60+ result=$( manage_section " # Changelog" test_changelog.md test_content.txt update " 1.0.0" " ##" )
61+ assert [ $? -eq 0 ]
62+ assert [ -n " $result " ]
63+ assert [ -f " $result " ]
64+
65+ # Verify content was properly merged - use literal strings without dashes
66+ assert_file_contains " $result " " # Changelog"
67+ assert_file_contains " $result " " ## 1.0.0"
68+ grep -q " New feature" " $result "
69+ }
70+
71+ @test " manage_section fails gracefully with invalid mode" {
72+ . " $GIV_LIB_DIR /system.sh"
73+ . " $GIV_LIB_DIR /markdown.sh"
74+
75+ # Create test files
76+ echo " # Changelog" > test_changelog.md
77+ echo " - New feature" > test_content.txt
78+
79+ # Test manage_section with invalid mode
80+ run manage_section " # Changelog" test_changelog.md test_content.txt " " " 1.0.0" " ##"
81+ assert_failure
82+ assert_output --partial " Invalid mode provided"
83+ }
84+
85+ @test " build_history skips empty diff sections" {
86+ . " $GIV_LIB_DIR /system.sh"
87+ . " $GIV_LIB_DIR /history.sh"
88+
89+ # Create a test directory for this test
90+ test_dir=$( mktemp -d)
91+ cd " $test_dir "
92+
93+ # Initialize git repo
94+ git init -q
95+ git config user.name " Test User"
96+ git config user.email " test@example.com"
97+
98+ # Create initial commit
99+ echo " initial content" > file.txt
100+ git add file.txt
101+ git commit -q -m " Initial commit"
102+
103+ # Test build_diff directly with no changes - should return empty
104+ diff_result=$( build_diff " --current" " " )
105+
106+ # With no changes, build_diff should return empty string
107+ assert [ -z " $diff_result " ]
108+
109+ cd " $BATS_TEST_TMPDIR "
110+ rm -rf " $test_dir "
111+ }
112+
113+ @test " changelog uses sensible version defaults" {
114+ # Test that changelog command handles missing output_version gracefully
115+ . " $GIV_LIB_DIR /system.sh"
116+
117+ # Test default version fallback
118+ GIV_OUTPUT_VERSION=" "
119+ version_result=" "
120+
121+ # Simulate the changelog.sh version defaulting logic
122+ if [ -z " $GIV_OUTPUT_VERSION " ]; then
123+ version_result=" Unreleased" # This is the fallback we implemented
124+ fi
125+
126+ assert [ " $version_result " = " Unreleased" ]
127+ }
128+
129+ @test " GIV_OUTPUT_MODE defaults to auto" {
130+ . " $GIV_LIB_DIR /system.sh"
131+
132+ # Test that GIV_OUTPUT_MODE has proper default
133+ assert [ " $GIV_OUTPUT_MODE " = " auto" ]
134+ }
135+
136+ # Helper function to check if file contains content
137+ assert_file_contains () {
138+ local file=" $1 "
139+ local content=" $2 "
140+
141+ if ! grep -qF " $content " " $file " ; then
142+ echo " File $file does not contain: $content "
143+ echo " Actual content:"
144+ cat " $file "
145+ return 1
146+ fi
147+ }
148+
149+ # Helper function to check if file does NOT contain content
150+ refute_file_contains () {
151+ local file=" $1 "
152+ local content=" $2 "
153+
154+ if grep -qF " $content " " $file " ; then
155+ echo " File $file should not contain: $content "
156+ echo " Actual content:"
157+ cat " $file "
158+ return 1
159+ fi
160+ }
0 commit comments