Skip to content

Commit

Permalink
fix: ensure that testproject is removed even after a failure (#948)
Browse files Browse the repository at this point in the history
* fix: ensure that testproject is removed even after a failure

* fix: defer licenseFile

* style: simply defer os.RemoveAll

* cobra/cmd: add getProject test func
  • Loading branch information
umarcor authored and jharshman committed Sep 17, 2019
1 parent 606aa57 commit 19cf35e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 32 deletions.
21 changes: 2 additions & 19 deletions cobra/cmd/add_test.go
Expand Up @@ -7,31 +7,14 @@ import (
)

func TestGoldenAddCmd(t *testing.T) {

wd, _ := os.Getwd()
command := &Command{
CmdName: "test",
CmdParent: parentName,
Project: &Project{
AbsolutePath: fmt.Sprintf("%s/testproject", wd),
Legal: getLicense(),
Copyright: copyrightLine(),

// required to init
AppName: "testproject",
PkgName: "github.com/spf13/testproject",
Viper: true,
},
Project: getProject(),
}
defer os.RemoveAll(command.AbsolutePath)

// init project first
command.Project.Create()
defer func() {
if _, err := os.Stat(command.AbsolutePath); err == nil {
os.RemoveAll(command.AbsolutePath)
}
}()

if err := command.Create(); err != nil {
t.Fatal(err)
}
Expand Down
23 changes: 10 additions & 13 deletions cobra/cmd/init_test.go
Expand Up @@ -7,29 +7,26 @@ import (
"testing"
)

func TestGoldenInitCmd(t *testing.T) {

func getProject() *Project {
wd, _ := os.Getwd()
project := &Project{
return &Project{
AbsolutePath: fmt.Sprintf("%s/testproject", wd),
PkgName: "github.com/spf13/testproject",
Legal: getLicense(),
Copyright: copyrightLine(),
Viper: true,
AppName: "testproject",
PkgName: "github.com/spf13/testproject",
Viper: true,
}
}

func TestGoldenInitCmd(t *testing.T) {
project := getProject()
defer os.RemoveAll(project.AbsolutePath)

err := project.Create()
if err != nil {
if err := project.Create(); err != nil {
t.Fatal(err)
}

defer func() {
if _, err := os.Stat(project.AbsolutePath); err == nil {
os.RemoveAll(project.AbsolutePath)
}
}()

expectedFiles := []string{"LICENSE", "main.go", "cmd/root.go"}
for _, f := range expectedFiles {
generatedFile := fmt.Sprintf("%s/%s", project.AbsolutePath, f)
Expand Down
1 change: 1 addition & 0 deletions cobra/cmd/project.go
Expand Up @@ -75,6 +75,7 @@ func (p *Project) createLicenseFile() error {
if err != nil {
return err
}
defer licenseFile.Close()

licenseTemplate := template.Must(template.New("license").Parse(p.Legal.Text))
return licenseTemplate.Execute(licenseFile, data)
Expand Down

0 comments on commit 19cf35e

Please sign in to comment.