github.com/btwiuse/jiri@v0.0.0-20191125065820-53353bcfef54/cmd/jiri/snapshot_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 "bytes" 9 "fmt" 10 "io/ioutil" 11 "os" 12 "path/filepath" 13 "testing" 14 15 "github.com/btwiuse/jiri" 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 func checkReadme(t *testing.T, jirix *jiri.X, project, message string) { 23 if _, err := os.Stat(project); err != nil { 24 t.Fatalf("%v", err) 25 } 26 readmeFile := filepath.Join(project, "README") 27 data, err := ioutil.ReadFile(readmeFile) 28 if err != nil { 29 t.Fatalf("%v", err) 30 } 31 if got, want := data, []byte(message); bytes.Compare(got, want) != 0 { 32 t.Fatalf("unexpected content %v:\ngot\n%s\nwant\n%s\n", project, got, want) 33 } 34 } 35 36 func localProjectName(i int) string { 37 return "test-local-project-" + fmt.Sprintf("%d", i+1) 38 } 39 40 func remoteProjectName(i int) string { 41 return "test-remote-project-" + fmt.Sprintf("%d", i+1) 42 } 43 44 func writeReadme(t *testing.T, jirix *jiri.X, projectDir, message string) { 45 path, perm := filepath.Join(projectDir, "README"), os.FileMode(0644) 46 if err := ioutil.WriteFile(path, []byte(message), perm); err != nil { 47 t.Fatalf("%s", err) 48 } 49 cwd, err := os.Getwd() 50 if err != nil { 51 t.Fatalf("%s", err) 52 } 53 defer os.Chdir(cwd) 54 if err := os.Chdir(projectDir); err != nil { 55 t.Fatalf("%s", err) 56 } 57 if err := gitutil.New(jirix, gitutil.UserNameOpt("John Doe"), gitutil.UserEmailOpt("john.doe@example.com")).CommitFile(path, "creating README"); err != nil { 58 t.Fatalf("%s", err) 59 } 60 } 61 62 // TestSnapshot tests creating and checking out a snapshot. 63 func TestSnapshot(t *testing.T) { 64 fake, cleanup := jiritest.NewFakeJiriRoot(t) 65 defer cleanup() 66 67 // Setup the initial remote and local projects. 68 numProjects, remoteProjects := 2, []string{} 69 for i := 0; i < numProjects; i++ { 70 if err := fake.CreateRemoteProject(remoteProjectName(i)); err != nil { 71 t.Fatalf("%v", err) 72 } 73 if err := fake.AddProject(project.Project{ 74 Name: remoteProjectName(i), 75 Path: localProjectName(i), 76 Remote: fake.Projects[remoteProjectName(i)], 77 }); err != nil { 78 t.Fatalf("%v", err) 79 } 80 } 81 82 // Create initial commits in the remote projects and use UpdateUniverse() 83 // to mirror them locally. 84 for i := 0; i < numProjects; i++ { 85 writeReadme(t, fake.X, fake.Projects[remoteProjectName(i)], "revision 1") 86 } 87 if err := project.UpdateUniverse(fake.X, true, false, false, false, false, true /*run-hooks*/, true /*run-packages*/, project.DefaultHookTimeout, project.DefaultPackageTimeout); err != nil { 88 t.Fatalf("%v", err) 89 } 90 91 // Create a snapshot. 92 var stdout bytes.Buffer 93 fake.X.Context = tool.NewContext(tool.ContextOpts{Stdout: &stdout, Env: fake.X.Context.Env()}) 94 95 tmpfile, err := ioutil.TempFile("", "jiri-snapshot-") 96 if err != nil { 97 t.Fatal(err) 98 } 99 defer os.Remove(tmpfile.Name()) 100 101 if err := runSnapshot(fake.X, []string{tmpfile.Name()}); err != nil { 102 t.Fatalf("%v", err) 103 } 104 105 // Remove the local project repositories. 106 for i, _ := range remoteProjects { 107 localProject := filepath.Join(fake.X.Root, localProjectName(i)) 108 if err := os.RemoveAll(localProject); err != nil { 109 t.Fatalf("%v", err) 110 } 111 } 112 113 snapshotFile := tmpfile.Name() 114 if err := project.CheckoutSnapshot(fake.X, snapshotFile, false, true /*run-hooks*/, true /*run-packages*/, project.DefaultHookTimeout, project.DefaultPackageTimeout); err != nil { 115 t.Fatalf("%s", err) 116 } 117 for i, _ := range remoteProjects { 118 localProject := filepath.Join(fake.X.Root, localProjectName(i)) 119 checkReadme(t, fake.X, localProject, "revision 1") 120 } 121 }