-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunset_template_test.go
More file actions
116 lines (92 loc) · 2.64 KB
/
unset_template_test.go
File metadata and controls
116 lines (92 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package main
import (
"flag"
"io"
"os"
"os/exec"
"testing"
"github.com/urfave/cli/v2"
)
const (
templateFile = ".git/.gitmessage.txt"
)
func setupClearTest() func() {
currentDir, _ := os.Getwd()
dir, err := os.MkdirTemp(os.TempDir(), "test")
if err != nil {
panic(err)
}
_ = os.Chdir(dir)
_ = exec.Command("git", "init").Run()
return func() {
_ = os.Chdir(currentDir)
_ = os.RemoveAll(dir)
}
}
func TestRemoveTemplateFile(t *testing.T) {
teardown := setupClearTest()
defer teardown()
var err error
_, err = os.Create(templateFile)
if err != nil {
panic(err)
}
app := &cli.App{Writer: io.Discard, Commands: []*cli.Command{UnsetTemplateCommand}}
set := flag.NewFlagSet("test", 0)
c := cli.NewContext(app, set, nil)
_ = UnsetTemplateCommand.Run(c, []string{"unset"}...)
// If the respective file is no longer present, os.Stat returns an error, thus we must expect an error here
// if the file has been correctly removed!
_, err = os.Stat(templateFile)
if err == nil {
t.Errorf("Expected %s to not exist", templateFile)
}
}
func TestUnsetCommitTemplateGitConfig(t *testing.T) {
teardown := setupClearTest()
defer teardown()
var err error
err = exec.Command("git", "config", "--local", "commit.template", templateFile).Run()
if err != nil {
panic(err)
}
app := &cli.App{Writer: io.Discard, Commands: []*cli.Command{UnsetTemplateCommand}}
set := flag.NewFlagSet("test", 0)
c := cli.NewContext(app, set, nil)
_ = UnsetTemplateCommand.Run(c, []string{"unset"}...)
// If the respective config is not set, git exits with 1, thus we must expect an error here
// if the config has been correctly removed!
_, err = exec.Command("git", "config", "--local", "commit.template").Output()
if err == nil {
t.Errorf("Git config not unset")
}
}
func TestUnsetOutsideOfGitRepo(t *testing.T) {
currentDir, _ := os.Getwd()
dir, err := os.MkdirTemp(os.TempDir(), "test")
if err != nil {
panic(err)
}
_ = os.Chdir(dir)
origExiter := cli.OsExiter
defer func() {
cli.OsExiter = origExiter
}()
var exitCodeFromOsExiter int
cli.OsExiter = func(exitCode int) {
exitCodeFromOsExiter = exitCode
}
app := &cli.App{Writer: io.Discard, Commands: []*cli.Command{UnsetTemplateCommand}}
set := flag.NewFlagSet("test", 0)
c := cli.NewContext(app, set, nil)
err = UnsetTemplateCommand.Run(c, []string{"unset"}...)
if exitCodeFromOsExiter != 1 {
t.Errorf("Expected app to exit with 1: %d", err.(cli.ExitCoder).ExitCode())
}
expected := "Must run in Git repository"
if err.Error() != expected {
t.Errorf("Expected error not observed, wanted %q, got %q", expected, err.Error())
}
_ = os.Chdir(currentDir)
_ = os.RemoveAll(dir)
}