Skip to content

Commit 55edeee

Browse files
committed
run tests through the cli for better coverage
1 parent 01b9bc7 commit 55edeee

File tree

7 files changed

+36
-23
lines changed

7 files changed

+36
-23
lines changed

custom_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@ func TestCustom(t *testing.T) {
1212
setCustomName("_gen_test.go")
1313
defer revertCustomName()
1414

15-
// ensure that generated _gen.go uses the same imports as built into this package
16-
custom()
15+
// create custom file
16+
if err := runMain([]string{"gen", "custom"}); err != nil {
17+
t.Error(err)
18+
}
1719
defer os.Remove(customName)
1820

21+
// ensure that generated _gen.go uses the same imports as built into this package
1922
fset := token.NewFileSet()
2023
c, err := parser.ParseFile(fset, customName, nil, parser.ImportsOnly)
2124

execute.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
package main
22

33
import (
4-
"bytes"
54
"io"
65
"io/ioutil"
76
"os"
87
"os/exec"
98
"path/filepath"
10-
"strings"
119
"text/template"
1210
)
1311

@@ -81,10 +79,6 @@ func executeCustom(src io.Reader, imports []string, body string) error {
8179
return nil
8280
}
8381

84-
func trimBuffer(b bytes.Buffer) string {
85-
return strings.Trim(b.String(), "\n")
86-
}
87-
8882
// set up temp directory under current directory
8983
// make sure to defer os.RemoveAll() in caller
9084
func getTempDir() (string, error) {

get_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,12 @@ func TestGetImports(t *testing.T) {
7070
}
7171

7272
// custom get
73-
if err := get([]string{}); err != nil {
73+
if err := runMain([]string{"gen", "get"}); err != nil {
74+
t.Error(err)
75+
}
76+
77+
// custom get with param
78+
if err := runMain([]string{"gen", "get", "-d"}); err != nil {
7479
t.Error(err)
7580
}
7681
}

help_test.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,23 @@ func TestHelp(t *testing.T) {
1111
setOutput(&b)
1212
defer revertOutput()
1313

14-
// just run it; not a great test but at least covers the code
15-
if err := help(); err != nil {
14+
if err := runMain([]string{"gen", "help"}); err != nil {
1615
t.Error(err)
1716
}
1817

1918
if b.Len() == 0 {
2019
t.Errorf("help() should have resulted in output")
2120
}
21+
22+
// grab the text for later comparison
23+
text := b.String()
24+
b.Reset()
25+
26+
if err := runMain([]string{"gen", "foo"}); err != nil {
27+
t.Error(err)
28+
}
29+
30+
if text != b.String() {
31+
t.Errorf("running an unknown command should return help text; returned %s", b.String())
32+
}
2233
}

list_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func TestList(t *testing.T) {
2020
os.Remove(customName)
2121

2222
// standard
23-
if err := list(); err != nil {
23+
if err := runMain([]string{"gen", "list"}); err != nil {
2424
t.Error(err)
2525
}
2626

@@ -56,7 +56,7 @@ func TestList(t *testing.T) {
5656
}
5757

5858
// custom file now exists
59-
if err := list(); err != nil {
59+
if err := runMain([]string{"gen", "list"}); err != nil {
6060
t.Error(err)
6161
}
6262

main.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,29 @@ func main() {
1212
}
1313
}()
1414

15-
args := os.Args
15+
err = runMain(os.Args)
16+
}
1617

18+
func runMain(args []string) error {
1719
if len(args) == 1 {
1820
// simply typed 'gen'; run is the default command
19-
err = run()
20-
return // see defer
21+
return run()
2122
}
2223

2324
cmd := args[1]
2425

2526
switch cmd {
2627
case "custom":
27-
err = custom()
28+
return custom()
2829
case "get":
2930
var tail []string
3031
if len(args) > 2 {
3132
tail = args[2:]
3233
}
33-
err = get(tail)
34+
return get(tail)
3435
case "list":
35-
err = list()
36+
return list()
3637
default:
37-
err = help()
38+
return help()
3839
}
39-
return // see defer
4040
}

run_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func TestRun(t *testing.T) {
2222
os.Remove(fooName)
2323

2424
// standard run
25-
if err := run(); err != nil {
25+
if err := runMain([]string{"gen"}); err != nil {
2626
t.Error(err)
2727
}
2828

@@ -59,7 +59,7 @@ func TestRun(t *testing.T) {
5959
}
6060

6161
// custom run
62-
if err := run(); err != nil {
62+
if err := runMain([]string{"gen"}); err != nil {
6363
t.Error(err)
6464
}
6565

0 commit comments

Comments
 (0)