| // Copyright 2016 The Fuchsia Authors. All rights reserved. | |
| // Use of this source code is governed by a BSD-style | |
| // license that can be found in the LICENSE file. | |
| package integrationtests | |
| import ( | |
| "fmt" | |
| "os" | |
| "path/filepath" | |
| "strconv" | |
| "strings" | |
| "testing" | |
| "go.fuchsia.dev/jiri/project" | |
| ) | |
| func setupUploadTest(t *testing.T, numProjects int) (string, []project.Project, []string) { | |
| t.Helper() | |
| root := t.TempDir() | |
| manifestRemote := t.TempDir() | |
| manifest := project.Manifest{ | |
| Projects: []project.Project{ | |
| { | |
| Name: "manifest", | |
| Path: "manifest", | |
| Remote: manifestRemote, | |
| }, | |
| }, | |
| } | |
| var localProjects []project.Project | |
| var remoteDirs []string | |
| for i := range numProjects { | |
| name := fmt.Sprintf("project-%d", i) | |
| relPath := fmt.Sprintf("path-%d", i) | |
| remoteDir := t.TempDir() | |
| setupGitRepo(t, remoteDir, map[string]any{ | |
| "README": "initial readme", | |
| }) | |
| remoteDirs = append(remoteDirs, remoteDir) | |
| manifest.Projects = append(manifest.Projects, project.Project{ | |
| Name: name, | |
| Path: relPath, | |
| Remote: remoteDir, | |
| }) | |
| localProjects = append(localProjects, project.Project{ | |
| Name: name, | |
| Path: filepath.Join(root, relPath), | |
| Remote: remoteDir, | |
| }) | |
| } | |
| setupGitRepo(t, manifestRemote, map[string]any{ | |
| "manifest": manifest, | |
| }) | |
| jiri := jiriInit(t, root) | |
| jiri("import", "manifest", manifestRemote) | |
| jiri("update") | |
| return root, localProjects, remoteDirs | |
| } | |
| func commitUploadFiles(t *testing.T, dir string, filenames []string) { | |
| t.Helper() | |
| for _, filename := range filenames { | |
| writeFile(t, filepath.Join(dir, filename), "This is file "+filename) | |
| runSubprocess(t, dir, "git", "add", filename) | |
| runSubprocess(t, dir, "git", "commit", "-m", "Commit "+filename) | |
| } | |
| } | |
| func assertUploadPushedFilesToRef(t *testing.T, gerritPath string, pushedRef string, files []string) { | |
| t.Helper() | |
| runSubprocess(t, gerritPath, "git", "checkout", pushedRef) | |
| for _, file := range files { | |
| if !fileExists(t, filepath.Join(gerritPath, file)) { | |
| t.Fatalf("expected file %v to exist but it did not", file) | |
| } | |
| out := strings.TrimSpace(runSubprocess(t, gerritPath, "git", "ls-files", file)) | |
| if out == "" { | |
| t.Fatalf("expected file %v to be committed but it is not", file) | |
| } | |
| } | |
| } | |
| func assertUploadFilesNotPushedToRef(t *testing.T, gerritPath string, pushedRef string, files []string) { | |
| t.Helper() | |
| runSubprocess(t, gerritPath, "git", "checkout", pushedRef) | |
| for _, file := range files { | |
| if fileExists(t, filepath.Join(gerritPath, file)) { | |
| t.Fatalf("expected file %v to not exist but it did", file) | |
| } | |
| } | |
| } | |
| func TestUpload(t *testing.T) { | |
| t.Parallel() | |
| root, localProjects, remoteDirs := setupUploadTest(t, 1) | |
| projDir := localProjects[0].Path | |
| gerritPath := remoteDirs[0] | |
| branch := "my-branch" | |
| runSubprocess(t, projDir, "git", "checkout", "-b", branch, "--track", "origin/main") | |
| files := []string{"file1"} | |
| commitUploadFiles(t, projDir, files) | |
| jiriRunInDir(t, root, projDir, "upload") | |
| expectedRef := "refs/for/main" | |
| assertUploadPushedFilesToRef(t, gerritPath, expectedRef, files) | |
| jiriRunInDir(t, root, projDir, "upload", "-remoteBranch=new-branch", "-set-topic=true") | |
| topic := fmt.Sprintf("%s-%s", os.Getenv("USER"), branch) | |
| expectedRef = fmt.Sprintf("refs/for/new-branch%%topic=%s", topic) | |
| assertUploadPushedFilesToRef(t, gerritPath, expectedRef, files) | |
| } | |
| func TestUploadRef(t *testing.T) { | |
| t.Parallel() | |
| root, localProjects, remoteDirs := setupUploadTest(t, 1) | |
| projDir := localProjects[0].Path | |
| gerritPath := remoteDirs[0] | |
| branch := "my-branch" | |
| runSubprocess(t, projDir, "git", "checkout", "-b", branch, "--track", "origin/main") | |
| files := []string{"file1", "file2"} | |
| commitUploadFiles(t, projDir, files) | |
| jiriRunInDir(t, root, projDir, "upload", "HEAD~1") | |
| expectedRef := "refs/for/main" | |
| assertUploadPushedFilesToRef(t, gerritPath, expectedRef, files[0:1]) | |
| assertUploadFilesNotPushedToRef(t, gerritPath, expectedRef, files[1:]) | |
| } | |
| func TestUploadFromDetachedHead(t *testing.T) { | |
| t.Parallel() | |
| root, localProjects, _ := setupUploadTest(t, 1) | |
| projDir := localProjects[0].Path | |
| expectedErr := "Current project is not on any branch. Either provide a topic or set flag \"-set-topic\" to false." | |
| errOut := jiriMustFailInDir(t, root, projDir, "upload", "-set-topic=true") | |
| if !strings.Contains(errOut, expectedErr) { | |
| t.Fatalf("expected error containing %q, got: %s", expectedErr, errOut) | |
| } | |
| expectedErr = "Current project is not on any branch. Multipart uploads require project to be on a branch." | |
| errOut = jiriMustFailInDir(t, root, projDir, "upload", "-multipart=true") | |
| if !strings.Contains(errOut, expectedErr) { | |
| t.Fatalf("expected error containing %q, got: %s", expectedErr, errOut) | |
| } | |
| jiriRunInDir(t, root, projDir, "upload") | |
| jiriRunInDir(t, root, projDir, "upload", "-topic=topic") | |
| } | |
| func TestUploadMultipart(t *testing.T) { | |
| t.Parallel() | |
| root, localProjects, remoteDirs := setupUploadTest(t, 2) | |
| branch := "my-branch" | |
| for i := range 2 { | |
| projDir := localProjects[i].Path | |
| runSubprocess(t, projDir, "git", "checkout", "-b", branch, "--track", "origin/main") | |
| commitUploadFiles(t, projDir, []string{"file-1" + strconv.Itoa(i)}) | |
| commitUploadFiles(t, projDir, []string{"file-2" + strconv.Itoa(i)}) | |
| } | |
| gerritPath := remoteDirs[0] | |
| projDir1 := localProjects[1].Path | |
| jiriRunInDir(t, root, projDir1, "upload", "-multipart=true") | |
| expectedRef := "refs/for/main" | |
| assertUploadPushedFilesToRef(t, gerritPath, expectedRef, []string{"file-10", "file-20"}) | |
| jiriRunInDir(t, root, projDir1, "upload", "-multipart=true", "-remoteBranch=new-branch", "-set-topic=true") | |
| topic := fmt.Sprintf("%s-%s", os.Getenv("USER"), branch) | |
| expectedRef = fmt.Sprintf("refs/for/new-branch%%topic=%s", topic) | |
| assertUploadPushedFilesToRef(t, gerritPath, expectedRef, []string{"file-10", "file-20"}) | |
| } | |
| func TestUploadMultipartWithBranchFlagSimple(t *testing.T) { | |
| t.Parallel() | |
| root, localProjects, remoteDirs := setupUploadTest(t, 2) | |
| branch := "my-branch" | |
| for i := range 2 { | |
| projDir := localProjects[i].Path | |
| runSubprocess(t, projDir, "git", "checkout", "-b", branch, "--track", "origin/main") | |
| commitUploadFiles(t, projDir, []string{"file-1" + strconv.Itoa(i)}) | |
| commitUploadFiles(t, projDir, []string{"file-2" + strconv.Itoa(i)}) | |
| } | |
| gerritPath := remoteDirs[0] | |
| projDir1 := localProjects[1].Path | |
| jiriRunInDir(t, root, projDir1, "upload", "-multipart=true", "-branch="+branch) | |
| expectedRef := "refs/for/main" | |
| assertUploadPushedFilesToRef(t, gerritPath, expectedRef, []string{"file-10", "file-20"}) | |
| jiriRunInDir(t, root, projDir1, "upload", "-multipart=true", "-branch="+branch, "-set-topic=true") | |
| topic := fmt.Sprintf("%s-%s", os.Getenv("USER"), branch) | |
| expectedRef = "refs/for/main%topic=" + topic | |
| assertUploadPushedFilesToRef(t, gerritPath, expectedRef, []string{"file-10", "file-20"}) | |
| } | |
| func TestUploadRebase(t *testing.T) { | |
| t.Parallel() | |
| root, localProjects, remoteDirs := setupUploadTest(t, 1) | |
| projDir := localProjects[0].Path | |
| gerritPath := remoteDirs[0] | |
| branch := "my-branch" | |
| runSubprocess(t, projDir, "git", "checkout", "-b", branch, "--track", "origin/main") | |
| localFiles := []string{"file1"} | |
| commitUploadFiles(t, projDir, localFiles) | |
| remoteFiles := []string{"file2"} | |
| commitUploadFiles(t, projDir, remoteFiles) | |
| jiriRunInDir(t, root, projDir, "upload", "-rebase=true") | |
| expectedRef := "refs/for/main" | |
| assertUploadPushedFilesToRef(t, gerritPath, expectedRef, localFiles) | |
| assertUploadPushedFilesToRef(t, projDir, branch, remoteFiles) | |
| } | |
| func TestUploadMultipleCommits(t *testing.T) { | |
| t.Parallel() | |
| root, localProjects, remoteDirs := setupUploadTest(t, 1) | |
| projDir := localProjects[0].Path | |
| gerritPath := remoteDirs[0] | |
| branch := "my-branch" | |
| runSubprocess(t, projDir, "git", "checkout", "-b", branch, "--track", "origin/main") | |
| files1 := []string{"file1"} | |
| commitUploadFiles(t, projDir, files1) | |
| files2 := []string{"file2"} | |
| commitUploadFiles(t, projDir, files2) | |
| jiriRunInDir(t, root, projDir, "upload") | |
| expectedRef := "refs/for/main" | |
| assertUploadPushedFilesToRef(t, gerritPath, expectedRef, append(files1, files2...)) | |
| } | |
| func TestUploadUntrackedBranch(t *testing.T) { | |
| t.Parallel() | |
| root, localProjects, remoteDirs := setupUploadTest(t, 1) | |
| projDir := localProjects[0].Path | |
| gerritPath := remoteDirs[0] | |
| branch := "my-branch" | |
| runSubprocess(t, projDir, "git", "checkout", "-b", branch) | |
| files := []string{"file1"} | |
| commitUploadFiles(t, projDir, files) | |
| jiriRunInDir(t, root, projDir, "upload") | |
| expectedRef := "refs/for/main" | |
| assertUploadPushedFilesToRef(t, gerritPath, expectedRef, files) | |
| jiriRunInDir(t, root, projDir, "upload", "-remoteBranch=new-branch") | |
| expectedRef = "refs/for/new-branch" | |
| assertUploadPushedFilesToRef(t, gerritPath, expectedRef, files) | |
| } | |
| func TestGitOptions(t *testing.T) { | |
| t.Parallel() | |
| root, localProjects, remoteDirs := setupUploadTest(t, 1) | |
| projDir := localProjects[0].Path | |
| gerritPath := remoteDirs[0] | |
| files := []string{"file1"} | |
| commitUploadFiles(t, projDir, files) | |
| jiriRunInDir(t, root, projDir, "upload") | |
| files = []string{"file2"} | |
| commitUploadFiles(t, projDir, files) | |
| jiriRunInDir(t, root, projDir, "upload", "-git-options=--dry-run") | |
| expectedRef := "refs/for/main" | |
| assertUploadFilesNotPushedToRef(t, gerritPath, expectedRef, files) | |
| } |