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