fix: ensure that testproject is removed even after a failure (#948)
* fix: ensure that testproject is removed even after a failure * fix: defer licenseFile * style: simply defer os.RemoveAll * cobra/cmd: add getProject test func
This commit is contained in:
		@ -7,31 +7,14 @@ import (
 | 
				
			|||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func TestGoldenAddCmd(t *testing.T) {
 | 
					func TestGoldenAddCmd(t *testing.T) {
 | 
				
			||||||
 | 
					 | 
				
			||||||
	wd, _ := os.Getwd()
 | 
					 | 
				
			||||||
	command := &Command{
 | 
						command := &Command{
 | 
				
			||||||
		CmdName:   "test",
 | 
							CmdName:   "test",
 | 
				
			||||||
		CmdParent: parentName,
 | 
							CmdParent: parentName,
 | 
				
			||||||
		Project: &Project{
 | 
							Project:   getProject(),
 | 
				
			||||||
			AbsolutePath: fmt.Sprintf("%s/testproject", wd),
 | 
					 | 
				
			||||||
			Legal:        getLicense(),
 | 
					 | 
				
			||||||
			Copyright:    copyrightLine(),
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
			// required to init
 | 
					 | 
				
			||||||
			AppName: "testproject",
 | 
					 | 
				
			||||||
			PkgName: "github.com/spf13/testproject",
 | 
					 | 
				
			||||||
			Viper:   true,
 | 
					 | 
				
			||||||
		},
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						defer os.RemoveAll(command.AbsolutePath)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// init project first
 | 
					 | 
				
			||||||
	command.Project.Create()
 | 
						command.Project.Create()
 | 
				
			||||||
	defer func() {
 | 
					 | 
				
			||||||
		if _, err := os.Stat(command.AbsolutePath); err == nil {
 | 
					 | 
				
			||||||
			os.RemoveAll(command.AbsolutePath)
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	if err := command.Create(); err != nil {
 | 
						if err := command.Create(); err != nil {
 | 
				
			||||||
		t.Fatal(err)
 | 
							t.Fatal(err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
				
			|||||||
@ -7,29 +7,26 @@ import (
 | 
				
			|||||||
	"testing"
 | 
						"testing"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func TestGoldenInitCmd(t *testing.T) {
 | 
					func getProject() *Project {
 | 
				
			||||||
 | 
					 | 
				
			||||||
	wd, _ := os.Getwd()
 | 
						wd, _ := os.Getwd()
 | 
				
			||||||
	project := &Project{
 | 
						return &Project{
 | 
				
			||||||
		AbsolutePath: fmt.Sprintf("%s/testproject", wd),
 | 
							AbsolutePath: fmt.Sprintf("%s/testproject", wd),
 | 
				
			||||||
		PkgName:      "github.com/spf13/testproject",
 | 
					 | 
				
			||||||
		Legal:        getLicense(),
 | 
							Legal:        getLicense(),
 | 
				
			||||||
		Copyright:    copyrightLine(),
 | 
							Copyright:    copyrightLine(),
 | 
				
			||||||
		Viper:        true,
 | 
					 | 
				
			||||||
		AppName:      "testproject",
 | 
							AppName:      "testproject",
 | 
				
			||||||
 | 
							PkgName:      "github.com/spf13/testproject",
 | 
				
			||||||
 | 
							Viper:        true,
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	err := project.Create()
 | 
					func TestGoldenInitCmd(t *testing.T) {
 | 
				
			||||||
	if err != nil {
 | 
						project := getProject()
 | 
				
			||||||
 | 
						defer os.RemoveAll(project.AbsolutePath)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if err := project.Create(); err != nil {
 | 
				
			||||||
		t.Fatal(err)
 | 
							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"}
 | 
						expectedFiles := []string{"LICENSE", "main.go", "cmd/root.go"}
 | 
				
			||||||
	for _, f := range expectedFiles {
 | 
						for _, f := range expectedFiles {
 | 
				
			||||||
		generatedFile := fmt.Sprintf("%s/%s", project.AbsolutePath, f)
 | 
							generatedFile := fmt.Sprintf("%s/%s", project.AbsolutePath, f)
 | 
				
			||||||
 | 
				
			|||||||
@ -75,6 +75,7 @@ func (p *Project) createLicenseFile() error {
 | 
				
			|||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return err
 | 
							return err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						defer licenseFile.Close()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	licenseTemplate := template.Must(template.New("license").Parse(p.Legal.Text))
 | 
						licenseTemplate := template.Must(template.New("license").Parse(p.Legal.Text))
 | 
				
			||||||
	return licenseTemplate.Execute(licenseFile, data)
 | 
						return licenseTemplate.Execute(licenseFile, data)
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user