github.com/piiano/go-licenses@v1.2.1-c/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  	workdirs := []string{
    34  		"testdata/modules/hello01",
    35  		"testdata/modules/cli02",
    36  		"testdata/modules/vendored03",
    37  		"testdata/modules/replace04",
    38  	}
    39  	originalWorkDir, err := os.Getwd()
    40  	if err != nil {
    41  		t.Fatal(err)
    42  	}
    43  	// This builds go-licenses CLI to temporary dir.
    44  	tempDir, err := ioutil.TempDir("", "")
    45  	if err != nil {
    46  		t.Fatal(err)
    47  	}
    48  	defer os.RemoveAll(tempDir)
    49  	goLicensesPath := filepath.Join(tempDir, "go-licenses")
    50  	cmd := exec.Command("go", "build", "-o", goLicensesPath)
    51  	_, err = cmd.Output()
    52  	if err != nil {
    53  		t.Fatal(err)
    54  	}
    55  	t.Logf("Built go-licenses binary in %s.", goLicensesPath)
    56  	for _, workdir := range workdirs {
    57  		t.Run(workdir, func(t *testing.T) {
    58  			err := os.Chdir(filepath.Join(originalWorkDir, workdir))
    59  			if err != nil {
    60  				t.Fatal(err)
    61  			}
    62  			cmd := exec.Command("go", "mod", "download")
    63  			log, err := cmd.CombinedOutput()
    64  			if err != nil {
    65  				t.Fatalf("downloading go modules:\n%s", string(log))
    66  			}
    67  			cmd = exec.Command(goLicensesPath, "csv", ".")
    68  			// Capture stderr to buffer.
    69  			var stderr bytes.Buffer
    70  			cmd.Stderr = &stderr
    71  			t.Logf("%s $ go-licenses csv .", workdir)
    72  			output, err := cmd.Output()
    73  			if err != nil {
    74  				t.Logf("\n=== start of log ===\n%s=== end of log ===\n\n\n", stderr.String())
    75  				t.Fatalf("running go-licenses csv: %s. Full log shown above.", err)
    76  			}
    77  			got := string(output)
    78  			goldenFilePath := "licenses.csv"
    79  			if *update {
    80  				err := ioutil.WriteFile(goldenFilePath, output, 0600)
    81  				if err != nil {
    82  					t.Fatalf("writing golden file: %s", err)
    83  				}
    84  			}
    85  			goldenBytes, err := ioutil.ReadFile(goldenFilePath)
    86  			if err != nil {
    87  				if errors.Is(err, os.ErrNotExist) {
    88  					t.Fatalf("reading golden file: %s. Create a golden file by running `go test --update .`", err)
    89  				}
    90  				t.Fatalf("reading golden file: %s", err)
    91  			}
    92  			golden := string(goldenBytes)
    93  			if got != golden {
    94  				t.Logf("\n=== start of log ===\n%s=== end of log ===\n\n\n", stderr.String())
    95  				t.Fatalf("result of go-licenses csv does not match the golden file.\n"+
    96  					"Diff -golden +got:\n%s\n"+
    97  					"Update the golden by running `go test --update .`",
    98  					cmp.Diff(golden, got))
    99  			}
   100  		})
   101  	}
   102  }