Skip to content

Commit 27454d9

Browse files
committed
coverage
1 parent 6d1cde4 commit 27454d9

1 file changed

Lines changed: 93 additions & 7 deletions

File tree

main_test.go

Lines changed: 93 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -203,16 +203,61 @@ func TestExitCodeForError_FindsErrnoInJoinedError(t *testing.T) {
203203

204204
func TestMainFn(t *testing.T) {
205205
flag.Parse()
206-
*flagGoPackage = true
206+
oldWD, err := os.Getwd()
207+
if err != nil {
208+
t.Fatal(err)
209+
}
210+
defer func() { _ = os.Chdir(oldWD) }()
211+
212+
origGit, origOut, origName := *flagGit, *flagOut, *flagName
213+
origDebug, origGoPackage := *flagDebug, *flagGoPackage
214+
origNoFetch, origNoNewline := *flagNoFetch, *flagNoNewline
215+
origIncPatch, origBranch := *flagIncPatch, *flagBranch
216+
origTestMode := testMode
217+
defer func() {
218+
*flagGit, *flagOut, *flagName = origGit, origOut, origName
219+
*flagDebug, *flagGoPackage = origDebug, origGoPackage
220+
*flagNoFetch, *flagNoNewline = origNoFetch, origNoNewline
221+
*flagIncPatch, *flagBranch = origIncPatch, origBranch
222+
testMode = origTestMode
223+
}()
224+
225+
work := t.TempDir()
226+
runGit(t, work, "init", "-q")
227+
runGit(t, work, "branch", "-M", "main")
228+
runGit(t, work, "config", "user.email", "test@example.com")
229+
runGit(t, work, "config", "user.name", "Test")
230+
if err := os.WriteFile(filepath.Join(work, "go.mod"), []byte("module example.com/gitsemvertest\n\ngo 1.26\n"), 0o644); err != nil {
231+
t.Fatal(err)
232+
}
233+
if err := os.WriteFile(filepath.Join(work, "a.txt"), []byte("a\n"), 0o644); err != nil {
234+
t.Fatal(err)
235+
}
236+
runGit(t, work, "add", "go.mod", "a.txt")
237+
runGit(t, work, "commit", "-q", "-m", "c1")
238+
runGit(t, work, "tag", "v1.0.0")
239+
if err := os.Chdir(work); err != nil {
240+
t.Fatal(err)
241+
}
242+
243+
*flagGit = "git"
207244
*flagOut = "test.out"
245+
*flagName = ""
246+
*flagDebug = false
247+
*flagGoPackage = true
248+
*flagNoFetch = true
249+
*flagNoNewline = false
250+
*flagIncPatch = false
251+
*flagBranch = false
252+
testMode = true
253+
208254
if code := mainfn(); code != 0 {
209255
t.Fatalf("mainfn failed with code %d", code)
210256
}
211-
b, err := os.ReadFile("test.out")
257+
b, err := os.ReadFile(filepath.Join(work, "test.out"))
212258
if err == nil {
213-
defer os.Remove("test.out")
214259
s := string(b)
215-
if !strings.Contains(s, "package gitsemver") || !strings.Contains(s, "PkgName = \"gitsemver\"") {
260+
if !strings.Contains(s, "package gitsemvertest") || !strings.Contains(s, "PkgName = \"gitsemvertest\"") {
216261
t.Error(s)
217262
}
218263
} else {
@@ -222,15 +267,56 @@ func TestMainFn(t *testing.T) {
222267

223268
func TestMainFnBranch(t *testing.T) {
224269
flag.Parse()
225-
*flagBranch = true
270+
oldWD, err := os.Getwd()
271+
if err != nil {
272+
t.Fatal(err)
273+
}
274+
defer func() { _ = os.Chdir(oldWD) }()
275+
276+
origGit, origOut, origName := *flagGit, *flagOut, *flagName
277+
origDebug, origGoPackage := *flagDebug, *flagGoPackage
278+
origNoFetch, origNoNewline := *flagNoFetch, *flagNoNewline
279+
origIncPatch, origBranch := *flagIncPatch, *flagBranch
280+
origTestMode := testMode
281+
defer func() {
282+
*flagGit, *flagOut, *flagName = origGit, origOut, origName
283+
*flagDebug, *flagGoPackage = origDebug, origGoPackage
284+
*flagNoFetch, *flagNoNewline = origNoFetch, origNoNewline
285+
*flagIncPatch, *flagBranch = origIncPatch, origBranch
286+
testMode = origTestMode
287+
}()
288+
289+
work := t.TempDir()
290+
runGit(t, work, "init", "-q")
291+
runGit(t, work, "branch", "-M", "main")
292+
runGit(t, work, "config", "user.email", "test@example.com")
293+
runGit(t, work, "config", "user.name", "Test")
294+
if err := os.WriteFile(filepath.Join(work, "a.txt"), []byte("a\n"), 0o644); err != nil {
295+
t.Fatal(err)
296+
}
297+
runGit(t, work, "add", "a.txt")
298+
runGit(t, work, "commit", "-q", "-m", "c1")
299+
runGit(t, work, "tag", "v1.0.0")
300+
if err := os.Chdir(work); err != nil {
301+
t.Fatal(err)
302+
}
303+
304+
*flagGit = "git"
226305
*flagOut = "test.out"
306+
*flagName = ""
307+
*flagDebug = false
308+
*flagGoPackage = false
309+
*flagNoFetch = true
310+
*flagNoNewline = false
227311
*flagIncPatch = true
312+
*flagBranch = true
313+
testMode = true
314+
228315
if code := mainfn(); code != 0 {
229316
t.Fatalf("mainfn failed with code %d", code)
230317
}
231-
b, err := os.ReadFile("test.out")
318+
b, err := os.ReadFile(filepath.Join(work, "test.out"))
232319
if err == nil {
233-
defer os.Remove("test.out")
234320
s := string(b)
235321
if !strings.Contains(s, "main") {
236322
t.Error(s)

0 commit comments

Comments
 (0)