github.com/kilpkonn/gtm-enhanced@v1.3.5/project/project_test.go (about) 1 // Copyright 2016 Michael Schenk. All rights reserved. 2 // Use of this source code is governed by a MIT-style 3 // license that can be found in the LICENSE file. 4 5 package project 6 7 import ( 8 "fmt" 9 "io/ioutil" 10 "os" 11 "os/exec" 12 "path" 13 "path/filepath" 14 "strings" 15 "testing" 16 17 "github.com/git-time-metric/gtm/util" 18 ) 19 20 func TestInitialize(t *testing.T) { 21 rootPath, err := ioutil.TempDir("", "gtm") 22 if err != nil { 23 t.Fatalf("Unable to create tempory directory %s, %s", rootPath, err) 24 } 25 defer func() { 26 if err = os.RemoveAll(rootPath); err != nil { 27 fmt.Printf("Error removing %s dir, %s", rootPath, err) 28 } 29 }() 30 31 savedCurDir, _ := os.Getwd() 32 if err := os.Chdir(rootPath); err != nil { 33 t.Fatalf("Unable to change working directory, %s", err) 34 } 35 defer func() { 36 if err = os.Chdir(savedCurDir); err != nil { 37 fmt.Printf("Unable to change working directory, %s", err) 38 } 39 }() 40 41 cmd := exec.Command("git", "init") 42 b, err := cmd.Output() 43 if err != nil { 44 t.Fatalf("Unable to initialize git repo, %s", string(b)) 45 } 46 47 s, err := Initialize(false, []string{}, false) 48 if err != nil { 49 t.Errorf("Initialize(), want error nil got error %s", err) 50 } 51 if !strings.Contains(s, "Git Time Metric initialized") { 52 t.Errorf("Initialize(), want Git Time Metric has been initialized, got %s", s) 53 } 54 55 for hook, command := range GitHooks { 56 fp := filepath.Join(rootPath, ".git", "hooks", hook) 57 if _, err := os.Stat(fp); os.IsNotExist(err) { 58 t.Errorf("Initialize(), want file post-commit, got %s", err) 59 } 60 if b, err = ioutil.ReadFile(fp); err != nil { 61 t.Fatalf("Initialize(), want error nil, got %s", err) 62 } 63 if !strings.Contains(string(b), command.Command) { 64 t.Errorf("Initialize(), want %s got %s", command.Command, string(b)) 65 } 66 } 67 68 cmd = exec.Command("git", "config", "-l") 69 b, err = cmd.Output() 70 if err != nil { 71 t.Fatalf("Unable to initialize git repo, %s", string(b)) 72 } 73 for k, v := range GitConfig { 74 want := fmt.Sprintf("%s=%s", k, v) 75 if !strings.Contains(string(b), want) { 76 t.Errorf("Initialize(), want %s got %s", want, string(b)) 77 } 78 } 79 80 fp := filepath.Join(rootPath, ".gitignore") 81 if _, err := os.Stat(fp); os.IsNotExist(err) { 82 t.Errorf("Initialize(), want file .gitignore, got %s", err) 83 } 84 if b, err = ioutil.ReadFile(fp); err != nil { 85 t.Fatalf("Initialize(), want error nil, got %s", err) 86 } 87 if !strings.Contains(string(b), GitIgnore+"\n") { 88 t.Errorf("Initialize(), want %s got %s", GitIgnore, string(b)) 89 } 90 fp = filepath.Join(rootPath, ".gtm", "terminal.app") 91 if _, err := os.Stat(fp); !os.IsNotExist(err) { 92 t.Errorf("Initialize(), want file terminal.app does not exist, got %s", err) 93 } 94 95 // let's reinitialize with terminal tracking enabled 96 s, err = Initialize(true, []string{}, false) 97 if err != nil { 98 t.Errorf("Initialize(true), want error nil got error %s", err) 99 } 100 if !strings.Contains(s, "Git Time Metric initialized") { 101 t.Errorf("Initialize(true), want Git Time Metric has been initialized, got %s", s) 102 } 103 104 for hook, command := range GitHooks { 105 fp := filepath.Join(rootPath, ".git", "hooks", hook) 106 if _, err := os.Stat(fp); os.IsNotExist(err) { 107 t.Errorf("Initialize(true), want file post-commit, got %s", err) 108 } 109 if b, err = ioutil.ReadFile(fp); err != nil { 110 t.Fatalf("Initialize(true), want error nil, got %s", err) 111 } 112 if !strings.Contains(string(b), command.Command) { 113 t.Errorf("Initialize(true), want %s got %s", command.Command, string(b)) 114 } 115 } 116 117 cmd = exec.Command("git", "config", "-l") 118 b, err = cmd.Output() 119 if err != nil { 120 t.Fatalf("Unable to initialize git repo, %s", string(b)) 121 } 122 for k, v := range GitConfig { 123 want := fmt.Sprintf("%s=%s", k, v) 124 if !strings.Contains(string(b), want) { 125 t.Errorf("Initialize(true), want %s got %s", want, string(b)) 126 } 127 } 128 129 fp = filepath.Join(rootPath, ".gitignore") 130 if _, err := os.Stat(fp); os.IsNotExist(err) { 131 t.Errorf("Initialize(true), want file .gitignore, got %s", err) 132 } 133 if b, err = ioutil.ReadFile(fp); err != nil { 134 t.Fatalf("Initialize(true), want error nil, got %s", err) 135 } 136 if !strings.Contains(string(b), GitIgnore+"\n") { 137 t.Errorf("Initialize(true), want %s got %s", GitIgnore, string(b)) 138 } 139 fp = filepath.Join(rootPath, ".gtm", "terminal.app") 140 if _, err := os.Stat(fp); os.IsNotExist(err) { 141 t.Errorf("Initialize(true), want file terminal.app, got %s", err) 142 } 143 } 144 145 func TestUninitialize(t *testing.T) { 146 rootPath, err := ioutil.TempDir("", "gtm") 147 if err != nil { 148 t.Fatalf("Unable to create tempory directory %s, %s", rootPath, err) 149 } 150 defer func() { 151 if err = os.RemoveAll(rootPath); err != nil { 152 fmt.Printf("Error removing %s dir, %s", rootPath, err) 153 } 154 }() 155 156 savedCurDir, _ := os.Getwd() 157 if err := os.Chdir(rootPath); err != nil { 158 t.Fatalf("Unable to change working directory, %s", err) 159 } 160 defer func() { 161 if err = os.Chdir(savedCurDir); err != nil { 162 fmt.Printf("Unable to change working directory, %s", err) 163 } 164 }() 165 166 cmd := exec.Command("git", "init") 167 b, err := cmd.Output() 168 if err != nil { 169 t.Fatalf("Unable to initialize git repo, %s", string(b)) 170 } 171 172 _, err = Initialize(false, []string{}, false) 173 if err != nil { 174 t.Fatalf("Want error nil got error %s", err) 175 } 176 177 s, err := Uninitialize() 178 if err != nil { 179 t.Fatalf("Uninitialize(), want error nil got error %s", err) 180 } 181 if !strings.Contains(s, "Git Time Metric uninitialized") { 182 t.Errorf("Uninitialize(), want Git Time Metric uninitialized, got %s", s) 183 } 184 185 for hook, command := range GitHooks { 186 fp := filepath.Join(rootPath, ".git", "hooks", hook) 187 if b, err = ioutil.ReadFile(fp); err != nil { 188 t.Fatalf("Uninitialize(), want error nil, got %s", err) 189 } 190 if strings.Contains(string(b), command.Command+"\n") { 191 t.Errorf("Uinitialize(), do not want %s got %s", command.Command, string(b)) 192 } 193 } 194 195 cmd = exec.Command("git", "config", "-l") 196 b, err = cmd.Output() 197 if err != nil { 198 t.Fatalf("Want error nil got error %s, %s", string(b), err) 199 } 200 for k, v := range GitConfig { 201 donotwant := fmt.Sprintf("%s=%s", k, v) 202 if strings.Contains(string(b), donotwant) { 203 t.Errorf("Uninitialize(), do not want %s got %s", donotwant, string(b)) 204 } 205 } 206 207 fp := filepath.Join(rootPath, ".gitignore") 208 if b, err = ioutil.ReadFile(fp); err != nil { 209 t.Fatalf("Uninitialize(), want error nil, got %s", err) 210 } 211 if strings.Contains(string(b), GitIgnore+"\n") { 212 t.Errorf("Uninitialize(), do not want %s got %s", GitIgnore, string(b)) 213 } 214 215 if _, err := os.Stat(path.Join(rootPath, ".gtm")); !os.IsNotExist(err) { 216 t.Errorf("Uninitialize(), error directory .gtm exists") 217 } 218 } 219 220 func TestClean(t *testing.T) { 221 rootPath, err := ioutil.TempDir("", "gtm") 222 if err != nil { 223 t.Fatalf("Unable to create tempory directory %s, %s", rootPath, err) 224 } 225 defer func() { 226 if err = os.RemoveAll(rootPath); err != nil { 227 fmt.Printf("Error removing %s dir, %s", rootPath, err) 228 } 229 }() 230 231 savedCurDir, _ := os.Getwd() 232 if err := os.Chdir(rootPath); err != nil { 233 t.Fatalf("Unable to change working directory, %s", err) 234 } 235 defer func() { 236 if err = os.Chdir(savedCurDir); err != nil { 237 fmt.Printf("Unable to change working directory, %s", err) 238 } 239 }() 240 241 cmd := exec.Command("git", "init") 242 b, err := cmd.Output() 243 if err != nil { 244 t.Fatalf("Unable to initialize git repo, %s", string(b)) 245 } 246 247 _, err = Initialize(false, []string{}, false) 248 if err != nil { 249 t.Fatalf("Want error nil got error %s", err) 250 } 251 252 gtmPath := filepath.Join(rootPath, GTMDir) 253 if _, err := os.Stat(gtmPath); os.IsNotExist(err) { 254 t.Fatalf("%s directory not found", gtmPath) 255 } 256 257 testFiles := []string{"a.event", "b.event", "a.metric", "a.txt"} 258 for _, f := range testFiles { 259 if err := ioutil.WriteFile(filepath.Join(gtmPath, f), []byte{}, 0644); err != nil { 260 t.Errorf("Want error nil got %s", err) 261 } 262 } 263 // write a terminal event file 264 if err := ioutil.WriteFile(filepath.Join(gtmPath, "t.event"), []byte("terminal.app"), 0644); err != nil { 265 t.Errorf("Want error nil got %s", err) 266 } 267 268 // lets only delete terminal events 269 err = Clean(util.AfterNow(0), true) 270 files, err := ioutil.ReadDir(gtmPath) 271 if err != nil { 272 t.Fatalf("Want error nil got %s", err) 273 } 274 for _, f := range files { 275 if !(f.Name() == "a.txt" || f.Name() == "a.event" || f.Name() == "b.event") { 276 t.Errorf("Clean(), want only a.txt, a.event and b.event got %s", f.Name()) 277 } 278 } 279 280 // lets clean all events 281 err = Clean(util.AfterNow(0), false) 282 files, err = ioutil.ReadDir(gtmPath) 283 if err != nil { 284 t.Fatalf("Want error nil got %s", err) 285 } 286 for _, f := range files { 287 if f.Name() != "a.txt" { 288 t.Errorf("Clean(), want only a.txt got %s", f.Name()) 289 } 290 } 291 }