github.com/golang/dep@v0.5.4/internal/importers/godep/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 godep
     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 TestGodepConfig_Convert(t *testing.T) {
    22  	testCases := map[string]struct {
    23  		importertest.TestCase
    24  		json godepJSON
    25  	}{
    26  		"package without comment": {
    27  			importertest.TestCase{
    28  				WantConstraint: importertest.V1Constraint,
    29  				WantRevision:   importertest.V1Rev,
    30  				WantVersion:    importertest.V1Tag,
    31  			},
    32  			godepJSON{
    33  				Imports: []godepPackage{
    34  					{
    35  						ImportPath: importertest.Project,
    36  						Rev:        importertest.V1Rev,
    37  					},
    38  				},
    39  			},
    40  		},
    41  		"package with comment": {
    42  			importertest.TestCase{
    43  				WantConstraint: importertest.V2Branch,
    44  				WantRevision:   importertest.V2PatchRev,
    45  				WantVersion:    importertest.V2PatchTag,
    46  			},
    47  			godepJSON{
    48  				Imports: []godepPackage{
    49  					{
    50  						ImportPath: importertest.Project,
    51  						Rev:        importertest.V2PatchRev,
    52  						Comment:    importertest.V2Branch,
    53  					},
    54  				},
    55  			},
    56  		},
    57  		"missing package name": {
    58  			importertest.TestCase{
    59  				WantWarning: "Warning: Skipping project. Invalid godep configuration, ImportPath is required",
    60  			},
    61  			godepJSON{
    62  				Imports: []godepPackage{{ImportPath: ""}},
    63  			},
    64  		},
    65  		"missing revision": {
    66  			importertest.TestCase{
    67  				WantWarning: fmt.Sprintf(
    68  					"Warning: Invalid godep configuration, Rev not found for ImportPath %q",
    69  					importertest.Project,
    70  				),
    71  			},
    72  			godepJSON{
    73  				Imports: []godepPackage{
    74  					{
    75  						ImportPath: importertest.Project,
    76  					},
    77  				},
    78  			},
    79  		},
    80  		"package with requirements": {
    81  			importertest.TestCase{
    82  				WantRequired: []string{importertest.Project},
    83  			},
    84  			godepJSON{
    85  				Required: []string{importertest.Project},
    86  			},
    87  		},
    88  		"package with local requirements": {
    89  			importertest.TestCase{
    90  				WantRequired: nil,
    91  			},
    92  			godepJSON{
    93  				Required: []string{"./..."},
    94  			},
    95  		},
    96  	}
    97  
    98  	for name, testCase := range testCases {
    99  		name := name
   100  		testCase := testCase
   101  		t.Run(name, func(t *testing.T) {
   102  			err := testCase.Execute(t, func(logger *log.Logger, sm gps.SourceManager) (*dep.Manifest, *dep.Lock) {
   103  				g := NewImporter(logger, true, sm)
   104  				g.json = testCase.json
   105  				return g.convert(importertest.RootProject)
   106  			})
   107  			if err != nil {
   108  				t.Fatalf("%#v", err)
   109  			}
   110  		})
   111  	}
   112  }
   113  
   114  func TestGodepConfig_Import(t *testing.T) {
   115  	h := test.NewHelper(t)
   116  	defer h.Cleanup()
   117  
   118  	cacheDir := "gps-repocache"
   119  	h.TempDir(cacheDir)
   120  	h.TempDir("src")
   121  	h.TempDir(filepath.Join("src", importertest.RootProject))
   122  	h.TempCopy(filepath.Join(importertest.RootProject, godepPath), "Godeps.json")
   123  
   124  	projectRoot := h.Path(importertest.RootProject)
   125  	sm, err := gps.NewSourceManager(gps.SourceManagerConfig{
   126  		Cachedir: h.Path(cacheDir),
   127  		Logger:   log.New(test.Writer{TB: t}, "", 0),
   128  	})
   129  	h.Must(err)
   130  	defer sm.Release()
   131  
   132  	// Capture stderr so we can verify output
   133  	verboseOutput := &bytes.Buffer{}
   134  	logger := log.New(verboseOutput, "", 0)
   135  
   136  	g := NewImporter(logger, false, sm) // Disable Verbose so that we don't print values that change each test run
   137  	if !g.HasDepMetadata(projectRoot) {
   138  		t.Fatal("Expected the importer to detect godep configuration file")
   139  	}
   140  
   141  	m, l, err := g.Import(projectRoot, importertest.RootProject)
   142  	h.Must(err)
   143  
   144  	if m == nil {
   145  		t.Fatal("Expected the manifest to be generated")
   146  	}
   147  
   148  	if l == nil {
   149  		t.Fatal("Expected the lock to be generated")
   150  	}
   151  
   152  	goldenFile := "golden.txt"
   153  	got := verboseOutput.String()
   154  	want := h.GetTestFileString(goldenFile)
   155  	if want != got {
   156  		if *test.UpdateGolden {
   157  			if err := h.WriteTestFile(goldenFile, got); err != nil {
   158  				t.Fatalf("%+v", errors.Wrapf(err, "Unable to write updated golden file %s", goldenFile))
   159  			}
   160  		} else {
   161  			t.Fatalf("want %s, got %s", want, got)
   162  		}
   163  	}
   164  }
   165  
   166  func TestGodepConfig_JsonLoad(t *testing.T) {
   167  	// This is same as cmd/dep/testdata/init/Godeps.json
   168  	wantJSON := godepJSON{
   169  		Imports: []godepPackage{
   170  			{
   171  				ImportPath: "github.com/sdboyer/deptest",
   172  				Rev:        "3f4c3bea144e112a69bbe5d8d01c1b09a544253f",
   173  			},
   174  			{
   175  				ImportPath: "github.com/sdboyer/deptestdos",
   176  				Rev:        "5c607206be5decd28e6263ffffdcee067266015e",
   177  				Comment:    "v2.0.0",
   178  			},
   179  		},
   180  	}
   181  
   182  	h := test.NewHelper(t)
   183  	defer h.Cleanup()
   184  
   185  	ctx := importertest.NewTestContext(h)
   186  
   187  	h.TempCopy(filepath.Join(importertest.RootProject, godepPath), "Godeps.json")
   188  
   189  	projectRoot := h.Path(importertest.RootProject)
   190  
   191  	g := NewImporter(ctx.Err, true, nil)
   192  	err := g.load(projectRoot)
   193  	if err != nil {
   194  		t.Fatalf("Error while loading... %v", err)
   195  	}
   196  
   197  	if !equalImports(g.json.Imports, wantJSON.Imports) {
   198  		t.Fatalf("Expected imports to be equal. \n\t(GOT): %v\n\t(WNT): %v", g.json.Imports, wantJSON.Imports)
   199  	}
   200  }
   201  
   202  // equalImports compares two slices of godepPackage and checks if they are
   203  // equal.
   204  func equalImports(a, b []godepPackage) bool {
   205  	if a == nil && b == nil {
   206  		return true
   207  	}
   208  
   209  	if a == nil || b == nil {
   210  		return false
   211  	}
   212  
   213  	if len(a) != len(b) {
   214  		return false
   215  	}
   216  
   217  	for i := range a {
   218  		if a[i] != b[i] {
   219  			return false
   220  		}
   221  	}
   222  
   223  	return true
   224  }