github.com/Bobgy/go-licenses/v2@v2.0.0-dev.0/e2e_test.go (about)

     1  // Copyright 2022 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package main_test
    16  
    17  import (
    18  	"bytes"
    19  	"errors"
    20  	"flag"
    21  	"io/ioutil"
    22  	"os"
    23  	"os/exec"
    24  	"path/filepath"
    25  	"testing"
    26  
    27  	"github.com/google/go-cmp/cmp"
    28  )
    29  
    30  var update = flag.Bool("update", false, "update golden files")
    31  
    32  func TestCsvCommandE2E(t *testing.T) {
    33  	var tests = []struct {
    34  		workdir string
    35  	}{
    36  		{workdir: "testdata/modules/hello01"},
    37  		{workdir: "testdata/modules/cli02"},
    38  		{workdir: "testdata/modules/vendored03"},
    39  		{workdir: "testdata/modules/replace04"},
    40  	}
    41  	originalWorkDir, err := os.Getwd()
    42  	if err != nil {
    43  		t.Fatal(err)
    44  	}
    45  	// This builds and installs go-licenses CLI to $(go env GOPATH)/bin.
    46  	cmd := exec.Command("go", "install", ".")
    47  	_, err = cmd.Output()
    48  	if err != nil {
    49  		t.Fatal(err)
    50  	}
    51  	for _, tc := range tests {
    52  		t.Run(tc.workdir, func(t *testing.T) {
    53  			err := os.Chdir(filepath.Join(originalWorkDir, tc.workdir))
    54  			if err != nil {
    55  				t.Fatal(err)
    56  			}
    57  			cmd := exec.Command("go", "mod", "download")
    58  			log, err := cmd.CombinedOutput()
    59  			if err != nil {
    60  				t.Fatalf("downloading go modules:\n%s", string(log))
    61  			}
    62  			cmd = exec.Command("go-licenses", "csv", ".")
    63  			// Capture stderr to buffer.
    64  			var stderr bytes.Buffer
    65  			cmd.Stderr = &stderr
    66  			t.Logf("%s $ go-licenses csv .", tc.workdir)
    67  			output, err := cmd.Output()
    68  			if err != nil {
    69  				t.Logf("\n=== start of log ===\n%s=== end of log ===\n\n\n", stderr.String())
    70  				t.Fatalf("running go-licenses csv: %s. Full log shown above.", err)
    71  			}
    72  			got := string(output)
    73  			goldenFilePath := "licenses.csv"
    74  			if *update {
    75  				err := ioutil.WriteFile(goldenFilePath, output, 0600)
    76  				if err != nil {
    77  					t.Fatalf("writing golden file: %s", err)
    78  				}
    79  			}
    80  			goldenBytes, err := ioutil.ReadFile(goldenFilePath)
    81  			if err != nil {
    82  				if errors.Is(err, os.ErrNotExist) {
    83  					t.Fatalf("reading golden file: %s. Create a golden file by running `go test --update .`", err)
    84  				}
    85  				t.Fatalf("reading golden file: %s", err)
    86  			}
    87  			golden := string(goldenBytes)
    88  			if got != golden {
    89  				t.Logf("\n=== start of log ===\n%s=== end of log ===\n\n\n", stderr.String())
    90  				t.Fatalf("result of go-licenses csv does not match the golden file.\n"+
    91  					"Diff -golden +got:\n%s\n"+
    92  					"Update the golden by running `go test --update .`",
    93  					cmp.Diff(golden, got))
    94  			}
    95  		})
    96  	}
    97  }