github.com/golang/dep@v0.5.4/internal/importers/govend/importer_test.go (about)

     1  // Copyright 2017 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package govend
     6  
     7  import (
     8  	"bytes"
     9  	"fmt"
    10  	"log"
    11  	"path/filepath"
    12  	"testing"
    13  
    14  	"github.com/golang/dep"
    15  	"github.com/golang/dep/gps"
    16  	"github.com/golang/dep/internal/importers/importertest"
    17  	"github.com/golang/dep/internal/test"
    18  	"github.com/pkg/errors"
    19  )
    20  
    21  func TestGovendConfig_Convert(t *testing.T) {
    22  	testCases := map[string]struct {
    23  		yaml govendYAML
    24  		importertest.TestCase
    25  	}{
    26  		"package": {
    27  			govendYAML{
    28  				Imports: []govendPackage{
    29  					{
    30  						Path:     importertest.Project,
    31  						Revision: importertest.V1Rev,
    32  					},
    33  				},
    34  			},
    35  			importertest.TestCase{
    36  				WantConstraint: importertest.V1Constraint,
    37  				WantRevision:   importertest.V1Rev,
    38  				WantVersion:    importertest.V1Tag,
    39  			},
    40  		},
    41  		"missing package name": {
    42  			govendYAML{
    43  				Imports: []govendPackage{
    44  					{
    45  						Path: "",
    46  					},
    47  				},
    48  			},
    49  			importertest.TestCase{
    50  				WantWarning: "Warning: Skipping project. Invalid govend configuration, path is required",
    51  			},
    52  		},
    53  
    54  		"missing revision": {
    55  			govendYAML{
    56  				Imports: []govendPackage{
    57  					{
    58  						Path: importertest.Project,
    59  					},
    60  				},
    61  			},
    62  			importertest.TestCase{
    63  				WantWarning: fmt.Sprintf(
    64  					"  Warning: Skipping import with empty constraints. "+
    65  						"The solve step will add the dependency to the lock if needed: %q\n",
    66  					importertest.Project,
    67  				),
    68  			},
    69  		},
    70  	}
    71  
    72  	for name, testCase := range testCases {
    73  		name := name
    74  		testCase := testCase
    75  		t.Run(name, func(t *testing.T) {
    76  			err := testCase.Execute(t, func(logger *log.Logger, sm gps.SourceManager) (*dep.Manifest, *dep.Lock) {
    77  				g := NewImporter(logger, true, sm)
    78  				g.yaml = testCase.yaml
    79  				return g.convert(importertest.RootProject)
    80  			})
    81  			if err != nil {
    82  				t.Fatalf("%#v", err)
    83  			}
    84  		})
    85  	}
    86  }
    87  
    88  func TestGovendConfig_Import(t *testing.T) {
    89  	h := test.NewHelper(t)
    90  	defer h.Cleanup()
    91  
    92  	cacheDir := "gps-repocache"
    93  	h.TempDir(cacheDir)
    94  	h.TempDir("src")
    95  	h.TempDir(filepath.Join("src", importertest.RootProject))
    96  	h.TempCopy(filepath.Join(importertest.RootProject, govendYAMLName), "vendor.yml")
    97  
    98  	projectRoot := h.Path(importertest.RootProject)
    99  	sm, err := gps.NewSourceManager(gps.SourceManagerConfig{Cachedir: h.Path(cacheDir)})
   100  	h.Must(err)
   101  	defer sm.Release()
   102  
   103  	// Capture stderr so we can verify the import output
   104  	verboseOutput := &bytes.Buffer{}
   105  	logger := log.New(verboseOutput, "", 0)
   106  
   107  	// Disable Verbose so that we don't print values that change each test run
   108  	g := NewImporter(logger, false, sm)
   109  	if !g.HasDepMetadata(projectRoot) {
   110  		t.Fatal("Expected the importer to detect govend configuration file")
   111  	}
   112  
   113  	m, l, err := g.Import(projectRoot, importertest.RootProject)
   114  	h.Must(err)
   115  
   116  	if m == nil {
   117  		t.Fatal("Expected the manifest to be generated")
   118  	}
   119  
   120  	if l == nil {
   121  		t.Fatal("Expected the lock to be generated")
   122  	}
   123  
   124  	govendImportOutputFile := "golden.txt"
   125  	got := verboseOutput.String()
   126  	want := h.GetTestFileString(govendImportOutputFile)
   127  	if want != got {
   128  		if *test.UpdateGolden {
   129  			if err := h.WriteTestFile(govendImportOutputFile, got); err != nil {
   130  				t.Fatalf("%+v", errors.Wrapf(err, "Unable to write updated golden file %s", govendImportOutputFile))
   131  			}
   132  		} else {
   133  			t.Fatalf("want %s, got %s", want, got)
   134  		}
   135  	}
   136  
   137  }
   138  
   139  func TestGovendConfig_YAMLLoad(t *testing.T) {
   140  	// This is same as cmd/testdata/init/govend/vendor.yml
   141  	wantYaml := govendYAML{
   142  		Imports: []govendPackage{
   143  			{
   144  				Path:     "github.com/sdboyer/deptest",
   145  				Revision: "3f4c3bea144e112a69bbe5d8d01c1b09a544253f",
   146  			},
   147  			{
   148  				Path:     "github.com/sdboyer/deptestdos",
   149  				Revision: "5c607206be5decd28e6263ffffdcee067266015e",
   150  			},
   151  		},
   152  	}
   153  	h := test.NewHelper(t)
   154  	defer h.Cleanup()
   155  
   156  	ctx := importertest.NewTestContext(h)
   157  	h.TempCopy(filepath.Join(importertest.RootProject, govendYAMLName), "vendor.yml")
   158  
   159  	projectRoot := h.Path(importertest.RootProject)
   160  
   161  	g := NewImporter(ctx.Err, true, nil)
   162  	err := g.load(projectRoot)
   163  	if err != nil {
   164  		t.Fatalf("Error while loading %v", err)
   165  	}
   166  
   167  	if !equalGovendImports(g.yaml.Imports, wantYaml.Imports) {
   168  		t.Fatalf("Expected import to be equal. \n\t(GOT): %v\n\t(WNT): %v", g.yaml.Imports, wantYaml.Imports)
   169  	}
   170  }
   171  
   172  func equalGovendImports(a, b []govendPackage) bool {
   173  	if a == nil && b == nil {
   174  		return true
   175  	}
   176  
   177  	if a == nil || b == nil {
   178  		return false
   179  	}
   180  
   181  	if len(a) != len(b) {
   182  		return false
   183  	}
   184  
   185  	for i := range a {
   186  		if a[i] != b[i] {
   187  			return false
   188  		}
   189  	}
   190  	return true
   191  }