github.com/vanadium-archive/go.jiri@v0.0.0-20160715023856-abfb8b131290/cmd/jiri/import_test.go (about) 1 // Copyright 2015 The Vanadium 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 main 6 7 import ( 8 "fmt" 9 "io/ioutil" 10 "os" 11 "path/filepath" 12 "strings" 13 "testing" 14 15 "v.io/jiri" 16 "v.io/x/lib/gosh" 17 ) 18 19 type importTestCase struct { 20 Args []string 21 Filename string 22 Exist, Want string 23 Stdout, Stderr string 24 } 25 26 func TestImport(t *testing.T) { 27 tests := []importTestCase{ 28 { 29 Stderr: `wrong number of arguments`, 30 }, 31 { 32 Args: []string{"a"}, 33 Stderr: `wrong number of arguments`, 34 }, 35 { 36 Args: []string{"a", "b", "c"}, 37 Stderr: `wrong number of arguments`, 38 }, 39 // Remote imports, default append behavior 40 { 41 Args: []string{"-name=name", "-remote-branch=remotebranch", "-root=root", "foo", "https://github.com/new.git"}, 42 Want: `<manifest> 43 <imports> 44 <import manifest="foo" name="name" remote="https://github.com/new.git" remotebranch="remotebranch" root="root"/> 45 </imports> 46 </manifest> 47 `, 48 }, 49 { 50 Args: []string{"foo", "https://github.com/new.git"}, 51 Want: `<manifest> 52 <imports> 53 <import manifest="foo" name="manifest" remote="https://github.com/new.git"/> 54 </imports> 55 </manifest> 56 `, 57 }, 58 { 59 Args: []string{"-out=file", "foo", "https://github.com/new.git"}, 60 Filename: `file`, 61 Want: `<manifest> 62 <imports> 63 <import manifest="foo" name="manifest" remote="https://github.com/new.git"/> 64 </imports> 65 </manifest> 66 `, 67 }, 68 { 69 Args: []string{"-out=-", "foo", "https://github.com/new.git"}, 70 Stdout: `<manifest> 71 <imports> 72 <import manifest="foo" name="manifest" remote="https://github.com/new.git"/> 73 </imports> 74 </manifest> 75 `, 76 }, 77 { 78 Args: []string{"foo", "https://github.com/new.git"}, 79 Exist: `<manifest> 80 <imports> 81 <import manifest="bar" name="manifest" remote="https://github.com/orig.git"/> 82 </imports> 83 </manifest> 84 `, 85 Want: `<manifest> 86 <imports> 87 <import manifest="bar" name="manifest" remote="https://github.com/orig.git"/> 88 <import manifest="foo" name="manifest" remote="https://github.com/new.git"/> 89 </imports> 90 </manifest> 91 `, 92 }, 93 // Remote imports, explicit overwrite behavior 94 { 95 Args: []string{"-overwrite", "foo", "https://github.com/new.git"}, 96 Want: `<manifest> 97 <imports> 98 <import manifest="foo" name="manifest" remote="https://github.com/new.git"/> 99 </imports> 100 </manifest> 101 `, 102 }, 103 { 104 Args: []string{"-overwrite", "-out=file", "foo", "https://github.com/new.git"}, 105 Filename: `file`, 106 Want: `<manifest> 107 <imports> 108 <import manifest="foo" name="manifest" remote="https://github.com/new.git"/> 109 </imports> 110 </manifest> 111 `, 112 }, 113 { 114 Args: []string{"-overwrite", "-out=-", "foo", "https://github.com/new.git"}, 115 Stdout: `<manifest> 116 <imports> 117 <import manifest="foo" name="manifest" remote="https://github.com/new.git"/> 118 </imports> 119 </manifest> 120 `, 121 }, 122 { 123 Args: []string{"-overwrite", "foo", "https://github.com/new.git"}, 124 Exist: `<manifest> 125 <imports> 126 <import manifest="bar" name="manifest" remote="https://github.com/orig.git"/> 127 </imports> 128 </manifest> 129 `, 130 Want: `<manifest> 131 <imports> 132 <import manifest="foo" name="manifest" remote="https://github.com/new.git"/> 133 </imports> 134 </manifest> 135 `, 136 }, 137 } 138 sh := gosh.NewShell(t) 139 defer sh.Cleanup() 140 jiriTool := gosh.BuildGoPkg(sh, sh.MakeTempDir(), "v.io/jiri/cmd/jiri") 141 for _, test := range tests { 142 if err := testImport(t, jiriTool, test); err != nil { 143 t.Errorf("%v: %v", test.Args, err) 144 } 145 } 146 } 147 148 func testImport(t *testing.T, jiriTool string, test importTestCase) error { 149 sh := gosh.NewShell(t) 150 defer sh.Cleanup() 151 tmpDir := sh.MakeTempDir() 152 jiriRoot := filepath.Join(tmpDir, "root") 153 if err := os.Mkdir(jiriRoot, 0755); err != nil { 154 return err 155 } 156 sh.Pushd(jiriRoot) 157 filename := test.Filename 158 if filename == "" { 159 filename = ".jiri_manifest" 160 } 161 // Set up manfile for the local file import tests. It should exist in both 162 // the tmpDir (for ../manfile tests) and jiriRoot. 163 for _, dir := range []string{tmpDir, jiriRoot} { 164 if err := ioutil.WriteFile(filepath.Join(dir, "manfile"), nil, 0644); err != nil { 165 return err 166 } 167 } 168 // Set up an existing file if it was specified. 169 if test.Exist != "" { 170 if err := ioutil.WriteFile(filename, []byte(test.Exist), 0644); err != nil { 171 return err 172 } 173 } 174 // Run import and check the error. 175 sh.Vars[jiri.RootEnv] = jiriRoot 176 cmd := sh.Cmd(jiriTool, append([]string{"import"}, test.Args...)...) 177 if test.Stderr != "" { 178 cmd.ExitErrorIsOk = true 179 } 180 stdout, stderr := cmd.StdoutStderr() 181 if got, want := stdout, test.Stdout; !strings.Contains(got, want) || (got != "" && want == "") { 182 return fmt.Errorf("stdout got %q, want substr %q", got, want) 183 } 184 if got, want := stderr, test.Stderr; !strings.Contains(got, want) || (got != "" && want == "") { 185 return fmt.Errorf("stderr got %q, want substr %q", got, want) 186 } 187 // Make sure the right file is generated. 188 if test.Want != "" { 189 data, err := ioutil.ReadFile(filename) 190 if err != nil { 191 return err 192 } 193 if got, want := string(data), test.Want; got != want { 194 return fmt.Errorf("GOT\n%s\nWANT\n%s", got, want) 195 } 196 } 197 return nil 198 }