1// Copyright 2020 Google LLC 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// https://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15package client 16 17import ( 18 "io/ioutil" 19 "os" 20 "os/exec" 21 "path" 22 "testing" 23 24 "android.googlesource.com/platform/tools/treble.git/hacksaw/bind" 25 "android.googlesource.com/platform/tools/treble.git/hacksaw/codebase" 26 "android.googlesource.com/platform/tools/treble.git/hacksaw/config" 27) 28 29func TestHelpCommand(t *testing.T) { 30 cmd := NewCommand(bind.NewFakePathBinder(), "") 31 args := []string{"hacksaw", "help"} 32 err := cmd.Handle(args) 33 if err != nil { 34 t.Error(err) 35 } 36} 37 38func TestNoCommand(t *testing.T) { 39 cmd := NewCommand(bind.NewFakePathBinder(), "") 40 args := []string{"hacksaw"} 41 err := cmd.Handle(args) 42 if err != nil { 43 t.Error(err) 44 } 45} 46 47func TestBadCommand(t *testing.T) { 48 cmd := NewCommand(bind.NewFakePathBinder(), "") 49 args := []string{"hacksaw", "doesnotexist"} 50 if err := cmd.Handle(args); err == nil { 51 t.Errorf("Failed to handle bad command") 52 } 53} 54 55func TestEmptyCodebaseCommand(t *testing.T) { 56 cmd := NewCommand(bind.NewFakePathBinder(), "") 57 args := []string{"hacksaw", "codebase"} 58 if err := cmd.Handle(args); err == nil { 59 t.Errorf("Allowed an empty codebase command") 60 } 61} 62 63func TestAddCodebase(t *testing.T) { 64 defer config.GetConfig().Reset() 65 codebaseDir, err := ioutil.TempDir("", "test") 66 if err != nil { 67 t.Error(err) 68 } 69 defer os.RemoveAll(codebaseDir) 70 cmd := NewCommand(bind.NewFakePathBinder(), "") 71 args := []string{"hacksaw", "codebase", "add", "test-codebase", codebaseDir} 72 if err = cmd.Handle(args); err != nil { 73 t.Error(err) 74 } 75} 76 77func TestDefaultCodebase(t *testing.T) { 78 defer config.GetConfig().Reset() 79 codebaseDir, err := ioutil.TempDir("", "test") 80 if err != nil { 81 t.Error(err) 82 } 83 defer os.RemoveAll(codebaseDir) 84 gitDir := path.Join(codebaseDir, "project", ".git") 85 if err = os.MkdirAll(gitDir, os.ModePerm); err != nil { 86 t.Error(err) 87 } 88 repoDir := path.Join(codebaseDir, ".repo") 89 if err = os.Mkdir(repoDir, os.ModePerm); err != nil { 90 t.Error(err) 91 } 92 listContents := []byte("project") 93 listPath := path.Join(repoDir, "project.list") 94 if err = ioutil.WriteFile(listPath, listContents, os.ModePerm); err != nil { 95 t.Error(err) 96 } 97 cmd := NewCommand(bind.NewFakePathBinder(), "") 98 args := []string{"hacksaw", "codebase", "add", "test-codebase", codebaseDir} 99 if err = cmd.Handle(args); err != nil { 100 t.Error(err) 101 } 102 args = []string{"hacksaw", "codebase", "default", "test-codebase"} 103 if err = cmd.Handle(args); err != nil { 104 t.Error(err) 105 } 106 107 wsTempDir, err := ioutil.TempDir("", "workspace") 108 if err != nil { 109 t.Error(err) 110 } 111 defer os.RemoveAll(wsTempDir) 112 wsTopDir := path.Join(wsTempDir, "hacksaw") 113 if err = os.Mkdir(wsTopDir, os.ModePerm); err != nil { 114 t.Error(err) 115 } 116 cmd = NewCommand(bind.NewFakePathBinder(), wsTopDir) 117 args = []string{"hacksaw", "workspace", "new", "test-workspace"} 118 if err = cmd.Handle(args); err != nil { 119 t.Error(err) 120 } 121} 122 123func TestListCodebases(t *testing.T) { 124 cmd := NewCommand(bind.NewFakePathBinder(), "") 125 args := []string{"hacksaw", "codebase", "list"} 126 if err := cmd.Handle(args); err != nil { 127 t.Error(err) 128 } 129} 130 131func TestRemoveCodebase(t *testing.T) { 132 defer config.GetConfig().Reset() 133 codebaseDir, err := ioutil.TempDir("", "test") 134 if err != nil { 135 t.Error(err) 136 } 137 defer os.RemoveAll(codebaseDir) 138 cmd := NewCommand(bind.NewFakePathBinder(), "") 139 args := []string{"hacksaw", "codebase", "add", "test-codebase", codebaseDir} 140 if err = cmd.Handle(args); err != nil { 141 t.Error(err) 142 } 143 args = []string{"hacksaw", "codebase", "remove", "test-codebase"} 144 if err = cmd.Handle(args); err != nil { 145 t.Error(err) 146 } 147} 148 149func TestEmptyWorkspaceCommand(t *testing.T) { 150 cmd := NewCommand(bind.NewFakePathBinder(), "") 151 args := []string{"hacksaw", "workspace"} 152 if err := cmd.Handle(args); err == nil { 153 t.Errorf("Allowed an empty workspace command") 154 } 155} 156 157func TestCreateWorkspace(t *testing.T) { 158 defer config.GetConfig().Reset() 159 codebaseDir, err := ioutil.TempDir("", "test") 160 if err != nil { 161 t.Error(err) 162 } 163 defer os.RemoveAll(codebaseDir) 164 gitDir := path.Join(codebaseDir, "project", ".git") 165 if err = os.MkdirAll(gitDir, os.ModePerm); err != nil { 166 t.Error(err) 167 } 168 repoDir := path.Join(codebaseDir, ".repo") 169 if err = os.Mkdir(repoDir, os.ModePerm); err != nil { 170 t.Error(err) 171 } 172 listContents := []byte("project") 173 listPath := path.Join(repoDir, "project.list") 174 if err = ioutil.WriteFile(listPath, listContents, os.ModePerm); err != nil { 175 t.Error(err) 176 } 177 _, err = codebase.Add("test-codebase", codebaseDir) 178 if err != nil { 179 t.Error(err) 180 } 181 182 wsTempDir, err := ioutil.TempDir("", "workspace") 183 if err != nil { 184 t.Error(err) 185 } 186 defer os.RemoveAll(wsTempDir) 187 wsTopDir := path.Join(wsTempDir, "hacksaw") 188 if err = os.Mkdir(wsTopDir, os.ModePerm); err != nil { 189 t.Error(err) 190 } 191 cmd := NewCommand(bind.NewFakePathBinder(), wsTopDir) 192 args := []string{"hacksaw", "workspace", "new", "test-workspace", "test-codebase"} 193 if err = cmd.Handle(args); err != nil { 194 t.Error(err) 195 } 196} 197 198func TestRecreateWorkspace(t *testing.T) { 199 defer config.GetConfig().Reset() 200 codebaseDir, err := ioutil.TempDir("", "test") 201 if err != nil { 202 t.Error(err) 203 } 204 defer os.RemoveAll(codebaseDir) 205 gitDir := path.Join(codebaseDir, "project", ".git") 206 if err = os.MkdirAll(gitDir, os.ModePerm); err != nil { 207 t.Error(err) 208 } 209 repoDir := path.Join(codebaseDir, ".repo") 210 if err = os.Mkdir(repoDir, os.ModePerm); err != nil { 211 t.Error(err) 212 } 213 listContents := []byte("project") 214 listPath := path.Join(repoDir, "project.list") 215 if err = ioutil.WriteFile(listPath, listContents, os.ModePerm); err != nil { 216 t.Error(err) 217 } 218 _, err = codebase.Add("test-codebase", codebaseDir) 219 if err != nil { 220 t.Error(err) 221 } 222 223 wsTempDir, err := ioutil.TempDir("", "workspace") 224 if err != nil { 225 t.Error(err) 226 } 227 defer os.RemoveAll(wsTempDir) 228 wsTopDir := path.Join(wsTempDir, "hacksaw") 229 if err = os.Mkdir(wsTopDir, os.ModePerm); err != nil { 230 t.Error(err) 231 } 232 cmd := NewCommand(bind.NewFakePathBinder(), wsTopDir) 233 args := []string{"hacksaw", "workspace", "new", "test-workspace", "test-codebase"} 234 if err = cmd.Handle(args); err != nil { 235 t.Error(err) 236 } 237 args = []string{"hacksaw", "workspace", "recreate", "test-workspace"} 238 if err = cmd.Handle(args); err != nil { 239 t.Error(err) 240 } 241 args = []string{"hacksaw", "workspace", "recreate", "does-not-exist"} 242 if err = cmd.Handle(args); err == nil { 243 t.Error("Allowed to recreate an unexistant workspace") 244 } 245} 246 247func TestListWorkspace(t *testing.T) { 248 defer config.GetConfig().Reset() 249 codebaseDir, err := ioutil.TempDir("", "test") 250 if err != nil { 251 t.Error(err) 252 } 253 defer os.RemoveAll(codebaseDir) 254 gitDir := path.Join(codebaseDir, "project", ".git") 255 if err = os.MkdirAll(gitDir, os.ModePerm); err != nil { 256 t.Error(err) 257 } 258 repoDir := path.Join(codebaseDir, ".repo") 259 if err = os.Mkdir(repoDir, os.ModePerm); err != nil { 260 t.Error(err) 261 } 262 listContents := []byte("project") 263 listPath := path.Join(repoDir, "project.list") 264 if err = ioutil.WriteFile(listPath, listContents, os.ModePerm); err != nil { 265 t.Error(err) 266 } 267 _, err = codebase.Add("test-codebase", codebaseDir) 268 if err != nil { 269 t.Error(err) 270 } 271 wsTempDir, err := ioutil.TempDir("", "workspace") 272 if err != nil { 273 t.Error(err) 274 } 275 defer os.RemoveAll(wsTempDir) 276 wsTopDir := path.Join(wsTempDir, "hacksaw") 277 if err = os.Mkdir(wsTopDir, os.ModePerm); err != nil { 278 t.Error(err) 279 } 280 cmd := NewCommand(bind.NewFakePathBinder(), wsTopDir) 281 282 args := []string{"hacksaw", "workspace", "new", "test-workspace", "test-codebase"} 283 if err = cmd.Handle(args); err != nil { 284 t.Error(err) 285 } 286 287 args = []string{"hacksaw", "workspace", "list"} 288 if err = cmd.Handle(args); err != nil { 289 t.Error(err) 290 } 291} 292 293func TestRemoveWorkspace(t *testing.T) { 294 defer config.GetConfig().Reset() 295 codebaseDir, err := ioutil.TempDir("", "test") 296 if err != nil { 297 t.Error(err) 298 } 299 defer os.RemoveAll(codebaseDir) 300 gitDir := path.Join(codebaseDir, "project", ".git") 301 if err = os.MkdirAll(gitDir, os.ModePerm); err != nil { 302 t.Error(err) 303 } 304 repoDir := path.Join(codebaseDir, ".repo") 305 if err = os.Mkdir(repoDir, os.ModePerm); err != nil { 306 t.Error(err) 307 } 308 listContents := []byte("project") 309 listPath := path.Join(repoDir, "project.list") 310 if err = ioutil.WriteFile(listPath, listContents, os.ModePerm); err != nil { 311 t.Error(err) 312 } 313 _, err = codebase.Add("test-codebase", codebaseDir) 314 if err != nil { 315 t.Error(err) 316 } 317 wsTempDir, err := ioutil.TempDir("", "workspace") 318 if err != nil { 319 t.Error(err) 320 } 321 defer os.RemoveAll(wsTempDir) 322 wsTopDir := path.Join(wsTempDir, "hacksaw") 323 if err = os.Mkdir(wsTopDir, os.ModePerm); err != nil { 324 t.Error(err) 325 } 326 cmd := NewCommand(bind.NewFakePathBinder(), wsTopDir) 327 328 args := []string{"hacksaw", "workspace", "new", "test-workspace", "test-codebase"} 329 if err = cmd.Handle(args); err != nil { 330 t.Error(err) 331 } 332 333 args = []string{"hacksaw", "workspace", "remove", "test-workspace"} 334 if err = cmd.Handle(args); err != nil { 335 t.Error(err) 336 } 337} 338 339func TestEditWorkspace(t *testing.T) { 340 defer config.GetConfig().Reset() 341 codebaseDir, err := ioutil.TempDir("", "codebase") 342 if err != nil { 343 t.Error(err) 344 } 345 defer os.RemoveAll(codebaseDir) 346 projectDir := path.Join(codebaseDir, "project") 347 if err = os.MkdirAll(projectDir, os.ModePerm); err != nil { 348 t.Error(err) 349 } 350 gitCmd := exec.Command("git", "-C", projectDir, "init") 351 output, err := gitCmd.CombinedOutput() 352 if err != nil { 353 t.Errorf("Command\n%s\nfailed with the following:\n%s\n%s", 354 gitCmd.String(), err.Error(), output) 355 } 356 gitCmd = exec.Command("git", "-C", projectDir, "commit", `--message="Initial commit"`, "--allow-empty") 357 output, err = gitCmd.CombinedOutput() 358 if err != nil { 359 t.Errorf("Command\n%s\nfailed with the following:\n%s\n%s", 360 gitCmd.String(), err.Error(), output) 361 } 362 repoDir := path.Join(codebaseDir, ".repo") 363 if err = os.Mkdir(repoDir, os.ModePerm); err != nil { 364 t.Error(err) 365 } 366 listContents := []byte("project") 367 listPath := path.Join(repoDir, "project.list") 368 if err = ioutil.WriteFile(listPath, listContents, os.ModePerm); err != nil { 369 t.Error(err) 370 } 371 _, err = codebase.Add("test-codebase", codebaseDir) 372 if err != nil { 373 t.Error(err) 374 } 375 wsTempDir, err := ioutil.TempDir("", "workspace") 376 if err != nil { 377 t.Error(err) 378 } 379 defer os.RemoveAll(wsTempDir) 380 wsTopDir := path.Join(wsTempDir, "hacksaw") 381 if err = os.Mkdir(wsTopDir, os.ModePerm); err != nil { 382 t.Error(err) 383 } 384 cmd := NewCommand(bind.NewFakePathBinder(), wsTopDir) 385 386 args := []string{"hacksaw", "workspace", "new", "test-workspace", "test-codebase"} 387 if err = cmd.Handle(args); err != nil { 388 t.Error(err) 389 } 390 wsProjectDir := path.Join(wsTopDir, "test-workspace", "project") 391 args = []string{"hacksaw", "edit", wsProjectDir} 392 if err = cmd.Handle(args); err != nil { 393 t.Error(err) 394 } 395} 396