github.com/derekparker/go-pear@v1.3.2/support_test.go (about) 1 package main 2 3 import ( 4 "io/ioutil" 5 "os" 6 "path" 7 "testing" 8 9 "github.com/libgit2/git2go" 10 ) 11 12 type repoTestFunc func(conf *git.Config) 13 14 func withinStubRepo(t *testing.T, repoPath string, repoTest repoTestFunc) { 15 cwd, err := os.Getwd() 16 if err != nil { 17 t.Fatal(err) 18 } 19 20 conf, err := initializeRepo(repoPath) 21 if err != nil { 22 t.Fatal(err) 23 } 24 defer os.RemoveAll(repoPath) 25 26 err = os.Chdir(repoPath) 27 if err != nil { 28 t.Fatal(err) 29 } 30 31 repoTest(conf) 32 33 err = os.Chdir(cwd) 34 if err != nil { 35 t.Fatal(err) 36 } 37 } 38 39 func mockHomeEnv(dir string) { 40 cwd, err := os.Getwd() 41 if err != nil { 42 os.Stderr.WriteString("Could not get current directory\n") 43 os.Exit(2) 44 } 45 46 dir = path.Join(cwd, dir) 47 _, err = os.Open(dir) 48 if err != nil { 49 err = os.Mkdir(dir, os.ModePerm) 50 if err != nil { 51 os.Stderr.WriteString("Could not create directory\n") 52 os.Exit(2) 53 } 54 } 55 56 os.Setenv("HOME", dir) 57 } 58 59 func initializeRepo(p string) (*git.Config, error) { 60 repo, err := git.InitRepository(p, true) 61 if err != nil { 62 return nil, err 63 } 64 65 conf, err := repo.Config() 66 if err != nil { 67 return nil, err 68 } 69 70 return conf, nil 71 } 72 73 func closeFile(f *os.File) { 74 name := f.Name() 75 f.Close() 76 os.Remove(name) 77 } 78 79 func initTestGitConfig(path string, t *testing.T) *git.Config { 80 gitconfig, err := git.NewConfig() 81 if err != nil { 82 t.Error(err) 83 } 84 85 err = gitconfig.AddFile(path, git.ConfigLevelHighest, false) 86 if err != nil { 87 t.Error(err) 88 } 89 90 return gitconfig 91 } 92 93 func createPearrc(t *testing.T, contents []byte) *os.File { 94 p := path.Join(os.Getenv("HOME"), ".pearrc") 95 f, err := os.Create(p) 96 if err != nil { 97 os.Stdout = os.Stderr 98 t.Fatalf("Could not create .pearrc %s", err) 99 } 100 101 _, err = f.Write(contents) 102 if err != nil { 103 os.Stdout = os.Stderr 104 t.Fatal("Could not write to .pearrc %s", err) 105 } 106 107 return f 108 } 109 110 func mockStdin(t *testing.T, contents string) *os.File { 111 tmp, err := ioutil.TempFile("", "") 112 if err != nil { 113 t.Fatal(err) 114 } 115 116 _, err = tmp.WriteString(contents + "\n") 117 if err != nil { 118 t.Fatal(err) 119 } 120 121 _, err = tmp.Seek(0, os.SEEK_SET) 122 if err != nil { 123 t.Fatal(err) 124 } 125 126 os.Stdin = tmp 127 128 return tmp 129 } 130 131 func mockStdout(t *testing.T) (*os.File, *os.File) { 132 oldstdout := os.Stdout 133 tmp, err := ioutil.TempFile("", "") 134 if err != nil { 135 t.Error("Could not create temp file") 136 } 137 138 os.Stdout = tmp 139 140 return tmp, oldstdout 141 } 142 143 func cleanupStdout(t *testing.T, tmp *os.File, stdout *os.File) { 144 err := tmp.Close() 145 if err != nil { 146 t.Error(err) 147 } 148 149 os.Stdout = stdout 150 } 151 152 func restorePearrc(t *testing.T, contents []byte) { 153 p := path.Join(os.Getenv("HOME"), ".pearrc") 154 err := ioutil.WriteFile(p, contents, os.ModeExclusive) 155 if err != nil { 156 t.Error(err) 157 } 158 }