Skip to content
Myr

fix: ensure that testproject is removed even after a failure (#948)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* fix: ensure that testproject is removed even after a failure * fix: defer licenseFile * style: simply defer os.RemoveAll * cobra/cmd: add getProject test func

authored by umarcor · committed by Joshua Harshman · parent 606aa57 · Browse files · 3 files+13 added−32 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+13 added−32 removed

  • other:+13 added−32 removed

3 files+13 added−32 removed

modified:cobra/cmd/add_test.go+2 added-19 removed
5 unmodified lines
6 "testing"7)89func TestGoldenAddCmd(t *testing.T) {1011 wd, _ := os.Getwd()10 command := &Command{11 CmdName: "test",12 CmdParent: parentName,15 Project: &Project{16 AbsolutePath: fmt.Sprintf("%s/testproject", wd),17 Legal: getLicense(),18 Copyright: copyrightLine(),1920 // required to init21 AppName: "testproject",22 PkgName: "github.com/spf13/testproject",23 Viper: true,24 },13 Project: getProject(),14 }15 defer os.RemoveAll(command.AbsolutePath)1627 // init project first17 command.Project.Create()29 defer func() {30 if _, err := os.Stat(command.AbsolutePath); err == nil {31 os.RemoveAll(command.AbsolutePath)32 }33 }()3418 if err := command.Create(); err != nil {19 t.Fatal(err)20 }21
31 unmodified lines
modified:cobra/cmd/init_test.go+10 added-13 removed
5 unmodified lines
6 "path/filepath"7 "testing"8)910func TestGoldenInitCmd(t *testing.T) {1110func getProject() *Project {11 wd, _ := os.Getwd()13 project := &Project{12 return &Project{13 AbsolutePath: fmt.Sprintf("%s/testproject", wd),15 PkgName: "github.com/spf13/testproject",14 Legal: getLicense(),15 Copyright: copyrightLine(),18 Viper: true,16 AppName: "testproject",17 PkgName: "github.com/spf13/testproject",18 Viper: true,19 }20}2122 err := project.Create()23 if err != nil {22func TestGoldenInitCmd(t *testing.T) {23 project := getProject()24 defer os.RemoveAll(project.AbsolutePath)2526 if err := project.Create(); err != nil {27 t.Fatal(err)28 }2927 defer func() {28 if _, err := os.Stat(project.AbsolutePath); err == nil {29 os.RemoveAll(project.AbsolutePath)30 }31 }()3230 expectedFiles := []string{"LICENSE", "main.go", "cmd/root.go"}31 for _, f := range expectedFiles {32 generatedFile := fmt.Sprintf("%s/%s", project.AbsolutePath, f)33 goldenFile := fmt.Sprintf("testdata/%s.golden", filepath.Base(f))
6 unmodified lines
modified:cobra/cmd/project.go+1 added
73 unmodified lines
74 licenseFile, err := os.Create(fmt.Sprintf("%s/LICENSE", p.AbsolutePath))75 if err != nil {76 return err77 }78 defer licenseFile.Close()7980 licenseTemplate := template.Must(template.New("license").Parse(p.Legal.Text))81 return licenseTemplate.Execute(licenseFile, data)82}
15 unmodified lines