github.com/btwiuse/jiri@v0.0.0-20191125065820-53353bcfef54/cmd/jiri/manifest_test.go (about) 1 // Copyright 2018 The Fuchsia 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 "flag" 9 "io/ioutil" 10 "strings" 11 "testing" 12 13 "github.com/btwiuse/jiri/jiritest" 14 ) 15 16 func TestManifest(t *testing.T) { 17 // Create a test manifest file. 18 testManifestFile, err := ioutil.TempFile("", "test_manifest") 19 if err != nil { 20 t.Fatalf("failed to create test manifest: %s", err) 21 } 22 testManifestFile.Write([]byte(` 23 <?xml version="1.0" encoding="UTF-8"?> 24 <manifest> 25 <imports> 26 <import name="the_import" 27 manifest="the_import_manifest" 28 remote="https://github.com/btwiuse/the_import" 29 revision="the_import_revision" 30 remotebranch="the_import_remotebranch" 31 root="the_import_root"/> 32 </imports> 33 <projects> 34 <project name="the_project" 35 path="path/to/the_project" 36 remote="https://github.com/btwiuse/the_project" 37 remotebranch="the_project_remotebranch" 38 revision="the_project_revision" 39 githooks="the_project_githooks" 40 gerrithost="https://fuchsia-review.googlesource.com" 41 historydepth="2"/> 42 </projects> 43 <packages> 44 <package name="the_package" 45 version="the_package_version" 46 path="path/to/the_package" 47 internal="false" /> 48 </packages> 49 </manifest> 50 `)) 51 52 runCommand := func(t *testing.T, args []string) (stdout string, stderr string) { 53 // Set up a fake Jiri root to pass to our command. 54 fake, cleanup := jiritest.NewFakeJiriRoot(t) 55 defer cleanup() 56 57 // Initialize flags for the command. 58 flagSet := flag.NewFlagSet("manifest-test", flag.ContinueOnError) 59 setManifestFlags(flagSet) 60 61 // Make sure flags parse correctly. 62 if err := flagSet.Parse(args); err != nil { 63 t.Error(err) 64 } 65 66 // Run the command. 67 runCmd := func() { 68 if err := runManifest(fake.X, flagSet.Args()); err != nil { 69 // Capture the error as stderr since Jiri subcommands don't 70 // intenionally print to stderr when they fail. 71 stderr = err.Error() 72 } 73 } 74 75 var err error 76 stdout, _, err = runfunc(runCmd) 77 if err != nil { 78 t.Fatal(err) 79 } 80 81 return stdout, stderr 82 } 83 84 // Expects manifest to return a specific value when given args. 85 expectAttributeValue := func(t *testing.T, args []string, expectedValue string) { 86 stdout, stderr := runCommand(t, args) 87 88 // If an error occurred, fail. 89 if stderr != "" { 90 t.Error("error:", stderr) 91 return 92 } 93 94 // Compare stdout to the expected value. 95 if strings.Trim(stdout, " \n") != expectedValue { 96 t.Errorf("expected %s, got %s", expectedValue, stdout) 97 } 98 } 99 100 // Expects manifest to error when given args. 101 expectError := func(t *testing.T, args []string) { 102 stdout, stderr := runCommand(t, args) 103 104 // Fail if no error was output. 105 if stderr == "" { 106 t.Errorf("expected an error, got %s", stdout) 107 return 108 } 109 } 110 111 t.Run("should fail if manifest file is missing", func(t *testing.T) { 112 expectError(t, []string{ 113 "-element=the_import", 114 "-template={{.Name}}", 115 }) 116 117 expectError(t, []string{ 118 "-element=the_project", 119 "-template={{.Name}}", 120 }) 121 }) 122 123 t.Run("should fail if -attribute is missing", func(t *testing.T) { 124 expectError(t, []string{ 125 "-element=the_import", 126 testManifestFile.Name(), 127 }) 128 129 expectError(t, []string{ 130 "-element=the_project", 131 testManifestFile.Name(), 132 }) 133 }) 134 135 t.Run("should fail if -element is missing", func(t *testing.T) { 136 expectError(t, []string{ 137 "-template={{.Name}}", 138 testManifestFile.Name(), 139 }) 140 141 expectError(t, []string{ 142 "-template={{.Name}}", 143 testManifestFile.Name(), 144 }) 145 }) 146 147 t.Run("should read <project> attributes", func(t *testing.T) { 148 expectAttributeValue(t, []string{ 149 "-element=the_project", 150 "-template={{.Name}}", 151 testManifestFile.Name(), 152 }, 153 "the_project") 154 155 expectAttributeValue(t, []string{ 156 "-element=the_project", 157 "-template={{.Remote}}", 158 testManifestFile.Name(), 159 }, 160 "https://github.com/btwiuse/the_project") 161 162 expectAttributeValue(t, []string{ 163 "-element=the_project", 164 "-template={{.Revision}}", 165 testManifestFile.Name(), 166 }, 167 "the_project_revision") 168 169 expectAttributeValue(t, []string{ 170 "-element=the_project", 171 "-template={{.RemoteBranch}}", 172 testManifestFile.Name(), 173 }, 174 "the_project_remotebranch") 175 176 expectAttributeValue(t, []string{ 177 "-element=the_project", 178 "-template={{.Path}}", 179 testManifestFile.Name(), 180 }, 181 "path/to/the_project") 182 }) 183 184 t.Run("should read <import> attributes", func(t *testing.T) { 185 expectAttributeValue(t, []string{ 186 "-element=the_import", 187 "-template={{.Name}}", 188 testManifestFile.Name(), 189 }, 190 "the_import") 191 192 expectAttributeValue(t, []string{ 193 "-element=the_import", 194 "-template={{.Remote}}", 195 testManifestFile.Name(), 196 }, 197 "https://github.com/btwiuse/the_import") 198 199 expectAttributeValue(t, []string{ 200 "-element=the_import", 201 "-template={{.Manifest}}", 202 testManifestFile.Name(), 203 }, 204 "the_import_manifest") 205 206 expectAttributeValue(t, []string{ 207 "-element=the_import", 208 "-template={{.Revision}}", 209 testManifestFile.Name(), 210 }, 211 "the_import_revision") 212 213 expectAttributeValue(t, []string{ 214 "-element=the_import", 215 "-template={{.RemoteBranch}}", 216 testManifestFile.Name(), 217 }, 218 "the_import_remotebranch") 219 }) 220 221 t.Run("should read <package> attributes", func(t *testing.T) { 222 expectAttributeValue(t, []string{ 223 "-element=the_package", 224 "-template={{.Name}}", 225 testManifestFile.Name(), 226 }, 227 "the_package") 228 229 expectAttributeValue(t, []string{ 230 "-element=the_package", 231 "-template={{.Version}}", 232 testManifestFile.Name(), 233 }, 234 "the_package_version") 235 236 expectAttributeValue(t, []string{ 237 "-element=the_package", 238 "-template={{.Path}}", 239 testManifestFile.Name(), 240 }, 241 "path/to/the_package") 242 243 expectAttributeValue(t, []string{ 244 "-element=the_package", 245 "-template={{.Internal}}", 246 testManifestFile.Name(), 247 }, 248 "false") 249 }) 250 }