-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: e2e golden testing for csv command
- Loading branch information
Showing
10 changed files
with
841 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>. | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |