func (fake FakeJiriRoot) writeManifest(manifest *project.Manifest, dir, path string) error { git := gitutil.New(fake.X.NewSeq(), gitutil.RootDirOpt(dir)) if err := manifest.ToFile(fake.X, path); err != nil { return err } if err := git.Add(path); err != nil { return err } if err := git.Commit(); err != nil { return err } return nil }
func runImport(jirix *jiri.X, args []string) error { if len(args) != 2 { return jirix.UsageErrorf("wrong number of arguments") } // Initialize manifest. var manifest *project.Manifest if !flagImportOverwrite { m, err := project.ManifestFromFile(jirix, jirix.JiriManifestFile()) if err != nil && !runutil.IsNotExist(err) { return err } manifest = m } if manifest == nil { manifest = &project.Manifest{} } // There's not much error checking when writing the .jiri_manifest file; // errors will be reported when "jiri update" is run. manifest.Imports = append(manifest.Imports, project.Import{ Manifest: args[0], Name: flagImportName, Protocol: flagImportProtocol, Remote: args[1], RemoteBranch: flagImportRemoteBranch, Root: flagImportRoot, }) // Write output to stdout or file. outFile := flagImportOut if outFile == "" { outFile = jirix.JiriManifestFile() } if outFile == "-" { bytes, err := manifest.ToBytes() if err != nil { return err } _, err = os.Stdout.Write(bytes) return err } return manifest.ToFile(jirix, outFile) }
func TestRemoteImportCycle(t *testing.T) { fake, cleanup := jiritest.NewFakeJiriRoot(t) defer cleanup() // Set up two remote manifest projects, remote1 and remote1. if err := fake.CreateRemoteProject("remote1"); err != nil { t.Fatal(err) } if err := fake.CreateRemoteProject("remote2"); err != nil { t.Fatal(err) } remote1 := fake.Projects["remote1"] remote2 := fake.Projects["remote2"] fileA, fileB := filepath.Join(remote1, "A"), filepath.Join(remote2, "B") // Set up the cycle .jiri_manifest -> remote1+A -> remote2+B -> remote1+A jiriManifest := project.Manifest{ Imports: []project.Import{ {Manifest: "A", Name: "n1", Remote: remote1}, }, } manifestA := project.Manifest{ Imports: []project.Import{ {Manifest: "B", Name: "n2", Remote: remote2}, }, } manifestB := project.Manifest{ Imports: []project.Import{ {Manifest: "A", Name: "n3", Remote: remote1}, }, } if err := jiriManifest.ToFile(fake.X, fake.X.JiriManifestFile()); err != nil { t.Fatal(err) } if err := manifestA.ToFile(fake.X, fileA); err != nil { t.Fatal(err) } if err := manifestB.ToFile(fake.X, fileB); err != nil { t.Fatal(err) } commitFile(t, fake.X, remote1, fileA, "commit A") commitFile(t, fake.X, remote2, fileB, "commit B") // The update should complain about the cycle. err := project.UpdateUniverse(fake.X, false) if got, want := fmt.Sprint(err), "import cycle detected in remote manifest imports"; !strings.Contains(got, want) { t.Errorf("got error %v, want substr %v", got, want) } }
func TestFileImportCycle(t *testing.T) { jirix, cleanup := jiritest.NewX(t) defer cleanup() // Set up the cycle .jiri_manifest -> A -> B -> A jiriManifest := project.Manifest{ LocalImports: []project.LocalImport{ {File: "A"}, }, } manifestA := project.Manifest{ LocalImports: []project.LocalImport{ {File: "B"}, }, } manifestB := project.Manifest{ LocalImports: []project.LocalImport{ {File: "A"}, }, } if err := jiriManifest.ToFile(jirix, jirix.JiriManifestFile()); err != nil { t.Fatal(err) } if err := manifestA.ToFile(jirix, filepath.Join(jirix.Root, "A")); err != nil { t.Fatal(err) } if err := manifestB.ToFile(jirix, filepath.Join(jirix.Root, "B")); err != nil { t.Fatal(err) } // The update should complain about the cycle. err := project.UpdateUniverse(jirix, false) if got, want := fmt.Sprint(err), "import cycle detected in local manifest files"; !strings.Contains(got, want) { t.Errorf("got error %v, want substr %v", got, want) } }
// TestLocalProjects tests the behavior of the LocalProjects method with // different ScanModes. func TestLocalProjects(t *testing.T) { jirix, cleanup := jiritest.NewX(t) defer cleanup() // Create some projects. numProjects, projectPaths := 3, []string{} for i := 0; i < numProjects; i++ { s := jirix.NewSeq() name := projectName(i) path := filepath.Join(jirix.Root, name) if err := s.MkdirAll(path, 0755).Done(); err != nil { t.Fatal(err) } // Initialize empty git repository. The commit is necessary, otherwise // "git rev-parse master" fails. git := gitutil.New(s, gitutil.RootDirOpt(path)) if err := git.Init(path); err != nil { t.Fatal(err) } if err := git.Commit(); err != nil { t.Fatal(err) } // Write project metadata. p := project.Project{ Path: path, Name: name, } if err := project.InternalWriteMetadata(jirix, p, path); err != nil { t.Fatalf("writeMetadata %v %v) failed: %v\n", p, path, err) } projectPaths = append(projectPaths, path) } // Create a latest update snapshot but only tell it about the first project. manifest := project.Manifest{ Projects: []project.Project{ { Name: projectName(0), Path: projectPaths[0], }, }, } if err := jirix.NewSeq().MkdirAll(jirix.UpdateHistoryDir(), 0755).Done(); err != nil { t.Fatalf("MkdirAll(%v) failed: %v", jirix.UpdateHistoryDir(), err) } if err := manifest.ToFile(jirix, jirix.UpdateHistoryLatestLink()); err != nil { t.Fatalf("manifest.ToFile(%v) failed: %v", jirix.UpdateHistoryLatestLink(), err) } // LocalProjects with scanMode = FastScan should only find the first // project. foundProjects, err := project.LocalProjects(jirix, project.FastScan) if err != nil { t.Fatalf("LocalProjects(%v) failed: %v", project.FastScan, err) } checkProjectsMatchPaths(t, foundProjects, projectPaths[:1]) // LocalProjects with scanMode = FullScan should find all projects. foundProjects, err = project.LocalProjects(jirix, project.FullScan) if err != nil { t.Fatalf("LocalProjects(%v) failed: %v", project.FastScan, err) } checkProjectsMatchPaths(t, foundProjects, projectPaths[:]) // Check that deleting a project forces LocalProjects to run a full scan, // even if FastScan is specified. if err := jirix.NewSeq().RemoveAll(projectPaths[0]).Done(); err != nil { t.Fatalf("RemoveAll(%v) failed: %v", projectPaths[0]) } foundProjects, err = project.LocalProjects(jirix, project.FastScan) if err != nil { t.Fatalf("LocalProjects(%v) failed: %v", project.FastScan, err) } checkProjectsMatchPaths(t, foundProjects, projectPaths[1:]) }
// WriteJiriManifest writes the given manifest to the .jiri_manifest file. func (fake FakeJiriRoot) WriteJiriManifest(manifest *project.Manifest) error { return manifest.ToFile(fake.X, fake.X.JiriManifestFile()) }