github.com/btwiuse/jiri@v0.0.0-20191125065820-53353bcfef54/cmd/jiri/source_manifest_test.go (about) 1 // Copyright 2017 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 "bytes" 9 "encoding/json" 10 "fmt" 11 "io/ioutil" 12 "os" 13 "path/filepath" 14 "testing" 15 16 "github.com/btwiuse/jiri/gitutil" 17 "github.com/btwiuse/jiri/jiritest" 18 "github.com/btwiuse/jiri/project" 19 "github.com/btwiuse/jiri/tool" 20 ) 21 22 // TestSourceManifest tests creation of source manifest. 23 func TestSourceManifest(t *testing.T) { 24 fake, cleanup := jiritest.NewFakeJiriRoot(t) 25 defer cleanup() 26 27 // Setup the initial remote and local projects. 28 numProjects := 4 29 for i := 0; i < numProjects; i++ { 30 if err := fake.CreateRemoteProject(remoteProjectName(i)); err != nil { 31 t.Fatalf("%s", err) 32 } 33 rb := "" 34 if i == 2 { 35 rb = "test-branch" 36 g := gitutil.New(fake.X, gitutil.RootDirOpt(fake.Projects[remoteProjectName(i)])) 37 if err := g.CreateAndCheckoutBranch(rb); err != nil { 38 t.Fatal(err) 39 } 40 } 41 if err := fake.AddProject(project.Project{ 42 Name: remoteProjectName(i), 43 Path: localProjectName(i), 44 Remote: fake.Projects[remoteProjectName(i)], 45 RemoteBranch: rb, 46 }); err != nil { 47 t.Fatalf("%s", err) 48 } 49 } 50 51 // Create initial commits in the remote projects and use UpdateUniverse() 52 // to mirror them locally. 53 for i := 0; i < numProjects; i++ { 54 writeReadme(t, fake.X, fake.Projects[remoteProjectName(i)], fmt.Sprintf("proj %d", i)) 55 } 56 if err := project.UpdateUniverse(fake.X, true, false, false, false, false, true /*run-hooks*/, true /*run-packages*/, project.DefaultHookTimeout, project.DefaultPackageTimeout); err != nil { 57 t.Fatalf("%s", err) 58 } 59 60 // test when current revision is not in any branch 61 writeReadme(t, fake.X, filepath.Join(fake.X.Root, localProjectName(3)), "file") 62 63 // Get local revision 64 paths := []string{"manifest"} 65 for i := 0; i < numProjects; i++ { 66 paths = append(paths, localProjectName(i)) 67 } 68 revMap := make(map[string]string) 69 for _, path := range paths { 70 scm := gitutil.New(fake.X, gitutil.RootDirOpt(filepath.Join(fake.X.Root, path))) 71 if rev, err := scm.CurrentRevision(); err != nil { 72 t.Fatal(err) 73 } else { 74 revMap[path] = rev 75 } 76 77 } 78 79 var stdout bytes.Buffer 80 fake.X.Context = tool.NewContext(tool.ContextOpts{Stdout: &stdout, Env: fake.X.Context.Env()}) 81 82 smTmpfile, err := ioutil.TempFile("", "jiri-sm-") 83 if err != nil { 84 t.Fatal(err) 85 } 86 defer os.Remove(smTmpfile.Name()) 87 88 if err := runSourceManifest(fake.X, []string{smTmpfile.Name()}); err != nil { 89 t.Fatalf("%s", err) 90 } 91 92 sm := &project.SourceManifest{ 93 Version: project.SourceManifestVersion, 94 } 95 sm.Directories = make(map[string]*project.SourceManifest_Directory) 96 sm.Directories["manifest"] = &project.SourceManifest_Directory{ 97 GitCheckout: &project.SourceManifest_GitCheckout{ 98 RepoUrl: fake.Projects["manifest"], 99 Revision: revMap["manifest"], 100 FetchRef: "refs/heads/master", 101 }, 102 } 103 for i := 0; i < numProjects; i++ { 104 ref := "refs/heads/master" 105 if i == 2 { 106 ref = "refs/heads/test-branch" 107 } else if i == 3 { 108 ref = "" 109 } 110 sm.Directories[localProjectName(i)] = &project.SourceManifest_Directory{ 111 GitCheckout: &project.SourceManifest_GitCheckout{ 112 RepoUrl: fake.Projects[remoteProjectName(i)], 113 Revision: revMap[localProjectName(i)], 114 FetchRef: ref, 115 }, 116 } 117 } 118 119 want, err := json.MarshalIndent(sm, "", " ") 120 if err != nil { 121 t.Fatalf("failed to serialize JSON output: %s\n", err) 122 } 123 124 got, _ := ioutil.ReadFile(smTmpfile.Name()) 125 if string(got) != string(want) { 126 t.Fatalf("GOT:\n%s, \nWANT:\n%s", (string(got)), string(want)) 127 } 128 }