File tree Expand file tree Collapse file tree 3 files changed +13
-32
lines changed
Expand file tree Collapse file tree 3 files changed +13
-32
lines changed Original file line number Diff line number Diff line change @@ -7,31 +7,14 @@ import (
77)
88
99func TestGoldenAddCmd (t * testing.T ) {
10-
11- wd , _ := os .Getwd ()
1210 command := & Command {
1311 CmdName : "test" ,
1412 CmdParent : parentName ,
15- Project : & Project {
16- AbsolutePath : fmt .Sprintf ("%s/testproject" , wd ),
17- Legal : getLicense (),
18- Copyright : copyrightLine (),
19-
20- // required to init
21- AppName : "testproject" ,
22- PkgName : "github.com/spf13/testproject" ,
23- Viper : true ,
24- },
13+ Project : getProject (),
2514 }
15+ defer os .RemoveAll (command .AbsolutePath )
2616
27- // init project first
2817 command .Project .Create ()
29- defer func () {
30- if _ , err := os .Stat (command .AbsolutePath ); err == nil {
31- os .RemoveAll (command .AbsolutePath )
32- }
33- }()
34-
3518 if err := command .Create (); err != nil {
3619 t .Fatal (err )
3720 }
Original file line number Diff line number Diff line change @@ -7,29 +7,26 @@ import (
77 "testing"
88)
99
10- func TestGoldenInitCmd (t * testing.T ) {
11-
10+ func getProject () * Project {
1211 wd , _ := os .Getwd ()
13- project := & Project {
12+ return & Project {
1413 AbsolutePath : fmt .Sprintf ("%s/testproject" , wd ),
15- PkgName : "github.com/spf13/testproject" ,
1614 Legal : getLicense (),
1715 Copyright : copyrightLine (),
18- Viper : true ,
1916 AppName : "testproject" ,
17+ PkgName : "github.com/spf13/testproject" ,
18+ Viper : true ,
2019 }
20+ }
21+
22+ func TestGoldenInitCmd (t * testing.T ) {
23+ project := getProject ()
24+ defer os .RemoveAll (project .AbsolutePath )
2125
22- err := project .Create ()
23- if err != nil {
26+ if err := project .Create (); err != nil {
2427 t .Fatal (err )
2528 }
2629
27- defer func () {
28- if _ , err := os .Stat (project .AbsolutePath ); err == nil {
29- os .RemoveAll (project .AbsolutePath )
30- }
31- }()
32-
3330 expectedFiles := []string {"LICENSE" , "main.go" , "cmd/root.go" }
3431 for _ , f := range expectedFiles {
3532 generatedFile := fmt .Sprintf ("%s/%s" , project .AbsolutePath , f )
Original file line number Diff line number Diff line change @@ -75,6 +75,7 @@ func (p *Project) createLicenseFile() error {
7575 if err != nil {
7676 return err
7777 }
78+ defer licenseFile .Close ()
7879
7980 licenseTemplate := template .Must (template .New ("license" ).Parse (p .Legal .Text ))
8081 return licenseTemplate .Execute (licenseFile , data )
You can’t perform that action at this time.
0 commit comments