Skip to content

Commit

Permalink
test: e2e golden testing for csv command
Browse files Browse the repository at this point in the history
  • Loading branch information
Bobgy committed Jan 23, 2022
1 parent 3244bc6 commit aeb8a3d
Show file tree
Hide file tree
Showing 10 changed files with 841 additions and 0 deletions.
85 changes: 85 additions & 0 deletions e2e_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main_test

import (
"flag"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"testing"

"github.com/google/go-cmp/cmp"
)

var update = flag.Bool("update", false, "update golden files")

func TestCsvCmd(t *testing.T) {
var tests = []struct {
workdir string
}{
{workdir: "testdata/modules/hello01"},
{workdir: "testdata/modules/cli02"},
}
originalWorkDir, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
// This installs go-licenses CLI.
cmd := exec.Command("go", "install", ".")
_, err = cmd.Output()
if err != nil {
t.Fatal(err)
}
for _, tc := range tests {
t.Run(fmt.Sprintf("go-licenses csv . in %s", tc.workdir), func(t *testing.T) {
err := os.Chdir(filepath.Join(originalWorkDir, tc.workdir))
if err != nil {
t.Fatal(err)
}
cmd := exec.Command("go", "mod", "download")
log, err := cmd.CombinedOutput()
if err != nil {
t.Errorf("Failed to download go modules:\n%s", string(log))
}
cmd = exec.Command("go-licenses", "csv", ".")
output, err := cmd.Output()
if err != nil {
if exitErr, ok := err.(*exec.ExitError); ok {
t.Fatalf("Failed running go-licenses csv .:\n%s", string(exitErr.Stderr))
}
t.Fatal(err)
}
got := string(output)
goldenFilePath := "licenses.csv"
if *update {
err := ioutil.WriteFile(goldenFilePath, output, 0600)
if err != nil {
t.Fatal(err)
}
}
goldenBytes, err := ioutil.ReadFile(goldenFilePath)
if err != nil {
t.Fatal(err)
}
golden := string(goldenBytes)
if got != golden {
t.Fatalf("licenses.csv does not match the golden file. Update by running `go test -update .`. Diff:\n%s", cmp.Diff(golden, got))
}
})
}
}
4 changes: 4 additions & 0 deletions licenses/library.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ func Libraries(ctx context.Context, classifier Classifier, importPaths ...string
}
libraries = append(libraries, lib)
}
// Sort libraries to produce a stable result for snapshot diffing.
sort.Slice(libraries, func(i, j int) bool {
return libraries[i].Name() < libraries[j].Name()
})
return libraries, nil
}

Expand Down
3 changes: 3 additions & 0 deletions testdata/modules/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Test Modules

The test modules are example modules that we use to run go-licenses tests against.
9 changes: 9 additions & 0 deletions testdata/modules/cli02/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module github.com/google/go-licenses/testdata/modules/cli02

go 1.15

require (
github.com/mitchellh/go-homedir v1.1.0
github.com/spf13/cobra v1.1.3
github.com/spf13/viper v1.8.0
)
669 changes: 669 additions & 0 deletions testdata/modules/cli02/go.sum

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions testdata/modules/cli02/licenses.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
github.com/fsnotify/fsnotify,https://github.com/fsnotify/fsnotify/blob/master/LICENSE,BSD-3-Clause
github.com/google/go-licenses/testdata/modules/cli02,https://github.com/google/go-licenses/blob/master/LICENSE,Apache-2.0
github.com/hashicorp/hcl,https://github.com/hashicorp/hcl/blob/master/LICENSE,MPL-2.0
github.com/magiconair/properties,https://github.com/magiconair/properties/blob/master/LICENSE.md,BSD-2-Clause
github.com/mitchellh/go-homedir,https://github.com/mitchellh/go-homedir/blob/master/LICENSE,MIT
github.com/mitchellh/mapstructure,https://github.com/mitchellh/mapstructure/blob/master/LICENSE,MIT
github.com/pelletier/go-toml,https://github.com/pelletier/go-toml/blob/master/LICENSE,Apache-2.0
github.com/spf13/afero,https://github.com/spf13/afero/blob/master/LICENSE.txt,Apache-2.0
github.com/spf13/cast,https://github.com/spf13/cast/blob/master/LICENSE,MIT
github.com/spf13/cobra,https://github.com/spf13/cobra/blob/master/LICENSE.txt,Apache-2.0
github.com/spf13/jwalterweatherman,https://github.com/spf13/jwalterweatherman/blob/master/LICENSE,MIT
github.com/spf13/pflag,https://github.com/spf13/pflag/blob/master/LICENSE,BSD-3-Clause
github.com/spf13/viper,https://github.com/spf13/viper/blob/master/LICENSE,MIT
github.com/subosito/gotenv,https://github.com/subosito/gotenv/blob/master/LICENSE,MIT
golang.org/x/sys,Unknown,BSD-3-Clause
golang.org/x/text,Unknown,BSD-3-Clause
gopkg.in/ini.v1,Unknown,Apache-2.0
gopkg.in/yaml.v2,Unknown,Apache-2.0
28 changes: 28 additions & 0 deletions testdata/modules/cli02/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
_ "fmt"
_ "os"

_ "github.com/mitchellh/go-homedir"
_ "github.com/spf13/cobra"
_ "github.com/spf13/viper"
)

func main() {
// This module contains dependency the same as an empty CLI tool boilerplate generated by <https://github.com/spf13/cobra>.
}
3 changes: 3 additions & 0 deletions testdata/modules/hello01/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/google/go-licenses/testdata/modules/hello01

go 1.15
1 change: 1 addition & 0 deletions testdata/modules/hello01/licenses.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
github.com/google/go-licenses/testdata/modules/hello01,https://github.com/google/go-licenses/blob/master/LICENSE,Apache-2.0
21 changes: 21 additions & 0 deletions testdata/modules/hello01/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import "fmt"

func main() {
fmt.Println("hello world")
}

0 comments on commit aeb8a3d

Please sign in to comment.