github.com/golang/dep@v0.5.4/analyzer_test.go (about) 1 // Copyright 2016 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 dep 6 7 import ( 8 "path/filepath" 9 "testing" 10 11 "github.com/golang/dep/internal/test" 12 ) 13 14 func TestAnalyzerDeriveManifestAndLock(t *testing.T) { 15 h := test.NewHelper(t) 16 defer h.Cleanup() 17 18 h.TempDir("dep") 19 golden := filepath.Join("analyzer", ManifestName) 20 want := h.GetTestFileString(golden) 21 h.TempCopy(filepath.Join("dep", ManifestName), golden) 22 23 a := Analyzer{} 24 25 m, l, err := a.DeriveManifestAndLock(h.Path("dep"), "my/fake/project") 26 if err != nil { 27 t.Fatal(err) 28 } 29 30 got, err := m.(*Manifest).MarshalTOML() 31 if err != nil { 32 t.Fatal(err) 33 } 34 35 if want != string(got) { 36 if *test.UpdateGolden { 37 if err := h.WriteTestFile(golden, string(got)); err != nil { 38 t.Fatal(err) 39 } 40 } else { 41 t.Fatalf("(WNT):\n%s\n(GOT):\n%s", want, string(got)) 42 } 43 } 44 45 if l != nil { 46 t.Fatalf("expected lock to be nil, got: %#v", l) 47 } 48 } 49 50 func TestAnalyzerDeriveManifestAndLockDoesNotExist(t *testing.T) { 51 h := test.NewHelper(t) 52 defer h.Cleanup() 53 54 h.TempDir("dep") 55 56 a := Analyzer{} 57 58 m, l, err := a.DeriveManifestAndLock(h.Path("dep"), "my/fake/project") 59 if m != nil || l != nil || err != nil { 60 t.Fatalf("expected manifest & lock & err to be nil: m -> %#v l -> %#v err-> %#v", m, l, err) 61 } 62 } 63 64 func TestAnalyzerDeriveManifestAndLockCannotOpen(t *testing.T) { 65 h := test.NewHelper(t) 66 defer h.Cleanup() 67 68 h.TempDir("dep") 69 70 // Simulate an inaccessible manifest file. 71 h.TempFile(filepath.Join("dep", ManifestName), "") 72 closer, err := makeUnreadable(filepath.Join(h.Path("dep"), ManifestName)) 73 if err != nil { 74 t.Fatal(err) 75 } 76 defer closer.Close() 77 78 a := Analyzer{} 79 80 // Verify that the solver rejects the manifest, rather than treating it as 81 // offering no constraints. 82 m, l, err := a.DeriveManifestAndLock(h.Path("dep"), "my/fake/project") 83 if m != nil || l != nil || err == nil { 84 t.Fatalf("expected manifest & lock to be nil, err to be not nil: m -> %#v l -> %#v err -> %#v", m, l, err) 85 } 86 } 87 88 func TestAnalyzerDeriveManifestAndLockInvalidManifest(t *testing.T) { 89 h := test.NewHelper(t) 90 defer h.Cleanup() 91 92 h.TempDir("dep") 93 94 // Create a manifest with invalid contents 95 h.TempFile(filepath.Join("dep", ManifestName), "invalid manifest") 96 97 a := Analyzer{} 98 99 m, l, err := a.DeriveManifestAndLock(h.Path("dep"), "my/fake/project") 100 if m != nil || l != nil || err == nil { 101 t.Fatalf("expected manifest & lock & err to be nil: m -> %#v l -> %#v err-> %#v", m, l, err) 102 } 103 } 104 105 func TestAnalyzerInfo(t *testing.T) { 106 a := Analyzer{} 107 108 info := a.Info() 109 110 if info.Name != "dep" || info.Version != 1 { 111 t.Fatalf("expected name to be 'dep' and version to be 1: name -> %q vers -> %d", info.Name, info.Version) 112 } 113 }