Parcourir la Source

added more unit tests and increased coverage

bmallred 10 ans auparavant
Parent
Commettre
c2aad30a9c
1 fichiers modifiés avec 82 ajouts et 1 suppressions
  1. 82 1
      cmd/app_test.go

+ 82 - 1
cmd/app_test.go

@ -6,10 +6,14 @@ import (
6 6
	"testing"
7 7
)
8 8
9
// Test using an extension command
9 10
func TestExtension(t *testing.T) {
10 11
	var buffer bytes.Buffer
11 12
13
	os.Setenv("CODE_VCS", "git")
14
	os.Setenv("CODE_GIT_CHECK", "branch")
12 15
	os.Setenv("CODE_GIT_INCOMING", "log ..@{u}")
16
13 17
	app := App{
14 18
		Args:      []string{"incoming"},
15 19
		Stdin:     &buffer,
@ -24,8 +28,13 @@ func TestExtension(t *testing.T) {
24 28
	}
25 29
}
26 30
31
// Test a raw command not currently defined but can pass through
27 32
func TestRawCommand(t *testing.T) {
28 33
	var buffer bytes.Buffer
34
35
	os.Setenv("CODE_VCS", "git")
36
	os.Setenv("CODE_GIT_CHECK", "branch")
37
29 38
	app := App{
30 39
		Args:      []string{"status"},
31 40
		Stdin:     &buffer,
@ -40,8 +49,34 @@ func TestRawCommand(t *testing.T) {
40 49
	}
41 50
}
42 51
52
// Test behavior when a VCS "check" is not defined
53
func TestMissingCheck(t *testing.T) {
54
	var buffer bytes.Buffer
55
56
	os.Setenv("CODE_VCS", "git")
57
	os.Unsetenv("CODE_GIT_CHECK")
58
59
	app := App{
60
		Args:      []string{"status"},
61
		Stdin:     &buffer,
62
		Stderr:    &buffer,
63
		Stdout:    &buffer,
64
		Directory: getWorkingDirectory(),
65
	}
66
67
	err := app.Run()
68
	if err == nil {
69
		t.FailNow()
70
	}
71
}
72
73
// Test behavior when a subcommand is issued which does not exist
43 74
func TestMissingCommand(t *testing.T) {
44 75
	var buffer bytes.Buffer
76
77
	os.Setenv("CODE_VCS", "git")
78
	os.Setenv("CODE_GIT_CHECK", "branch")
79
45 80
	app := App{
46 81
		Args:      []string{"nonexistant", "command"},
47 82
		Stdin:     &buffer,
@ -56,10 +91,14 @@ func TestMissingCommand(t *testing.T) {
56 91
	}
57 92
}
58 93
94
// Test output if there is currently no VCS repository found
59 95
func TestMissingRepository(t *testing.T) {
96
	var buffer bytes.Buffer
97
60 98
	os.Chdir(os.TempDir())
99
	os.Setenv("CODE_VCS", "git")
100
	os.Setenv("CODE_GIT_CHECK", "branch")
61 101
62
	var buffer bytes.Buffer
63 102
	app := App{
64 103
		Args:      []string{"status"},
65 104
		Stdin:     &buffer,
@ -73,3 +112,45 @@ func TestMissingRepository(t *testing.T) {
73 112
		t.FailNow()
74 113
	}
75 114
}
115
116
// Test handling of no arguments passed
117
func TestNoArguments(t *testing.T) {
118
	var buffer bytes.Buffer
119
120
	os.Setenv("CODE_VCS", "git")
121
	os.Setenv("CODE_GIT_CHECK", "branch")
122
123
	app := App{
124
		Args:      []string{},
125
		Stdin:     &buffer,
126
		Stderr:    &buffer,
127
		Stdout:    &buffer,
128
		Directory: getWorkingDirectory(),
129
	}
130
131
	err := app.Run()
132
	if err == nil {
133
		t.FailNow()
134
	}
135
}
136
137
// Test the creation of a new application
138
func TestNewApp(t *testing.T) {
139
	app := NewApp()
140
141
	if app.Stdin == nil {
142
		t.Fail()
143
	}
144
145
	if app.Stderr == nil {
146
		t.Fail()
147
	}
148
149
	if app.Stdout == nil {
150
		t.Fail()
151
	}
152
153
	if app.Directory == "" {
154
		t.Fail()
155
	}
156
}