github.com/sc0rp1us/gb@v0.4.1-0.20160319180011-4ba8cf1baa5a/vendor/manifest_test.go (about) 1 package vendor 2 3 import ( 4 "bytes" 5 "os" 6 "path/filepath" 7 "testing" 8 9 "github.com/constabulary/gb/fileutils" 10 ) 11 12 func mktemp(t *testing.T) string { 13 s, err := mktmp() 14 if err != nil { 15 t.Fatal(err) 16 } 17 return s 18 } 19 20 func assertNotExists(t *testing.T, path string) { 21 _, err := os.Stat(path) 22 if err == nil || !os.IsNotExist(err) { 23 t.Fatalf("expected %q to be not found, got %v", path, err) 24 } 25 } 26 27 func assertExists(t *testing.T, path string) { 28 _, err := os.Stat(path) 29 if err != nil { 30 t.Fatalf("expected %q to be found, got %v", path, err) 31 } 32 } 33 34 func TestManifest(t *testing.T) { 35 root := mktemp(t) 36 defer fileutils.RemoveAll(root) 37 38 mf := filepath.Join(root, "vendor") 39 40 // check that reading an non existant manifest 41 // does not return an error 42 m, err := ReadManifest(mf) 43 if err != nil { 44 t.Fatalf("reading a non existant manifest should not fail: %v", err) 45 } 46 47 // check that no manifest file was created 48 assertNotExists(t, mf) 49 50 // add a dep 51 m.Dependencies = append(m.Dependencies, Dependency{ 52 Importpath: "github.com/foo/bar/baz", 53 Repository: "https://github.com/foo/bar", 54 Revision: "cafebad", 55 Branch: "master", 56 Path: "/baz", 57 }) 58 59 // write it back 60 if err := WriteManifest(mf, m); err != nil { 61 t.Fatalf("WriteManifest failed: %v", err) 62 } 63 64 // check the manifest was written 65 assertExists(t, mf) 66 67 // remove it 68 m.Dependencies = nil 69 if err := WriteManifest(mf, m); err != nil { 70 t.Fatalf("WriteManifest failed: %v", err) 71 } 72 73 // check that no manifest file was removed 74 assertNotExists(t, mf) 75 } 76 77 func TestEmptyPathIsNotWritten(t *testing.T) { 78 m := Manifest{ 79 Version: 0, 80 Dependencies: []Dependency{{ 81 Importpath: "github.com/foo/bar", 82 Repository: "https://github.com/foo/bar", 83 Revision: "abcdef", 84 Branch: "master", 85 }}, 86 } 87 var buf bytes.Buffer 88 if err := writeManifest(&buf, &m); err != nil { 89 t.Fatal(err) 90 } 91 want := `{ 92 "version": 0, 93 "dependencies": [ 94 { 95 "importpath": "github.com/foo/bar", 96 "repository": "https://github.com/foo/bar", 97 "revision": "abcdef", 98 "branch": "master" 99 } 100 ] 101 }` 102 got := buf.String() 103 if want != got { 104 t.Fatalf("want: %s, got %s", want, got) 105 } 106 }