Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 74 additions & 11 deletions e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,7 @@ func TestReportCommandE2E(t *testing.T) {
t.Cleanup(func() { _ = os.Chdir(originalWorkDir) })

// This builds go-licenses CLI to temporary dir.
tempDir, err := os.MkdirTemp("", "")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tempDir)
tempDir := t.TempDir()
goLicensesPath := filepath.Join(tempDir, "go-licenses")
cmd := exec.Command("go", "build", "-o", goLicensesPath)
_, err = cmd.Output()
Expand Down Expand Up @@ -138,11 +134,7 @@ func TestCheckCommandE2E(t *testing.T) {
t.Cleanup(func() { _ = os.Chdir(originalWorkDir) })

// This builds go-licenses CLI to temporary dir.
tempDir, err := os.MkdirTemp("", "")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tempDir)
tempDir := t.TempDir()
goLicensesPath := filepath.Join(tempDir, "go-licenses")
cmd := exec.Command("go", "build", "-o", goLicensesPath)
_, err = cmd.Output()
Expand Down Expand Up @@ -175,7 +167,8 @@ func TestCheckCommandE2E(t *testing.T) {
if err != nil {
exitCode = -1

if exitError, ok := err.(*exec.ExitError); ok {
var exitError *exec.ExitError
if errors.As(err, &exitError) {
exitCode = exitError.ExitCode()
}
}
Expand Down Expand Up @@ -214,6 +207,76 @@ func TestCheckCommandE2E(t *testing.T) {
}
}

func TestSaveCommandE2E(t *testing.T) {
tests := []struct {
workdir string
savePathAlreadyExists bool
wantExitCode int
}{
{workdir: "testdata/modules/cli02", savePathAlreadyExists: false, wantExitCode: 0},
{workdir: "testdata/modules/cli02", savePathAlreadyExists: true, wantExitCode: 1},
}

originalWorkDir, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = os.Chdir(originalWorkDir) })

// This builds go-licenses CLI to temporary dir.
tempDir := t.TempDir()
goLicensesPath := filepath.Join(tempDir, "go-licenses")
cmd := exec.Command("go", "build", "-o", goLicensesPath)
_, err = cmd.Output()
if err != nil {
t.Fatal(err)
}
t.Logf("Built go-licenses binary in %s.", goLicensesPath)

for _, tt := range tests {
t.Run(tt.workdir, func(t *testing.T) {
err := os.Chdir(filepath.Join(originalWorkDir, tt.workdir))
if err != nil {
t.Fatal(err)
}
cmd := exec.Command("go", "mod", "download")
log, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("downloading go modules:\n%s", string(log))
}
ttTempDir := t.TempDir()
savePath := filepath.Join(ttTempDir, "licenses")
if tt.savePathAlreadyExists {
err = os.Mkdir(savePath, os.ModePerm)
if err != nil {
t.Fatal(err)
}
}
args := append([]string{"save", ".", "--save_path", savePath})
cmd = exec.Command(goLicensesPath, args...)
exitCode := 0
t.Logf("%s $ go-licenses save .", tt.workdir)
output, err := cmd.Output()
if err != nil {
exitCode = -1

var exitError *exec.ExitError
if errors.As(err, &exitError) {
exitCode = exitError.ExitCode()
}
}

if len(output) != 0 {
t.Fatalf("unexpected output running go-licenses check: %s.", string(output))
}

if exitCode != tt.wantExitCode {
t.Fatalf("unexpected exit code running go-licenses check, expected %d but got %d", tt.wantExitCode, exitCode)
}
})
}
}

func filterOutput(output string) string {
output = regexp.MustCompile(`(?m)W\d+.*\n`).
ReplaceAllString(output, "")
Expand Down
3 changes: 1 addition & 2 deletions save.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ func saveMain(_ *cobra.Command, args []string) error {

// Check that the save path doesn't exist, otherwise it'd end up with a mix of
// existing files and the output of this command.
if d, err := os.Open(savePath); err == nil {
d.Close()
if _, err := os.Stat(savePath); err == nil {
return fmt.Errorf("%s already exists", savePath)
} else if !os.IsNotExist(err) {
return err
Expand Down