Skip to content

Commit

Permalink
Add test reproducing workspace issue
Browse files Browse the repository at this point in the history
  • Loading branch information
trptcolin committed Mar 25, 2022
1 parent 541496c commit 4a22d09
Showing 1 changed file with 98 additions and 0 deletions.
98 changes: 98 additions & 0 deletions cmd/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path"
"path/filepath"
"testing"

Expand Down Expand Up @@ -80,3 +82,99 @@ func TestGoldenInitCmd(t *testing.T) {
})
}
}

func TestGoldenInitCmdWithinWorkspace(t *testing.T) {

dir, err := ioutil.TempDir("", "cobra-init")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)

cmd := exec.Command("go", "work", "init")
cmd.Dir = dir
err = cmd.Run()
if err != nil {
t.Fatal(err)
}

tests := []struct {
name string
args []string
pkgName string
expectErr bool
}{
{
name: "successfully initializes an existing project within a workspace",
args: []string{"testproject"},
pkgName: "github.com/spf13/testproject",
expectErr: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
preExistingModule := "pre-existing-fake-project"
makeModuleInWorkspace(t, dir, preExistingModule)
makeModuleInWorkspace(t, dir, tt.args[0])
err := os.Chdir(dir)
if err != nil {
t.Fatal(err)
}

viper.Set("useViper", true)
viper.Set("license", "apache")
projectPath, err := initializeProject(tt.args)
defer func() {
if projectPath != "" {
os.RemoveAll(projectPath)
}
}()

if !tt.expectErr && err != nil {
t.Fatalf("did not expect an error, got %s", err)
}
if tt.expectErr {
if err == nil {
t.Fatal("expected an error but got none")
} else {
// got an expected error nothing more to do
return
}
}

expectedFiles := []string{"LICENSE", "main.go", "cmd/root.go"}
for _, f := range expectedFiles {
generatedFile := fmt.Sprintf("%s/%s", projectPath, f)
goldenFile := fmt.Sprintf("testdata/%s.golden", filepath.Base(f))
err := compareFiles(generatedFile, goldenFile)
if err != nil {
t.Fatal(err)
}
}
})
}
}

func makeModuleInWorkspace(t *testing.T, workspaceDir, moduleName string) {
cmd := exec.Command("mkdir", moduleName)
cmd.Dir = workspaceDir
err := cmd.Run()
if err != nil {
t.Fatal(err)
}

cmd = exec.Command("go", "mod", "init", moduleName)
cmd.Dir = path.Join(workspaceDir, moduleName)
err = cmd.Run()
if err != nil {
t.Fatal(err)
}

cmd = exec.Command("go", "work", "use", moduleName)
cmd.Dir = workspaceDir
err = cmd.Run()
if err != nil {
t.Fatal(err)
}
}

0 comments on commit 4a22d09

Please sign in to comment.