|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "os/exec" |
| 8 | + "path/filepath" |
| 9 | + "strings" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/matryer/is" |
| 13 | +) |
| 14 | + |
| 15 | +func TestUnuseWithGitValues(t *testing.T) { |
| 16 | + for _, values := range [][]string{nil, {"work@example.com"}, {"work@example.com", "work@example.com", "extra@example.com"}} { |
| 17 | + t.Run(fmt.Sprintf("%d values", len(values)), func(t *testing.T) { |
| 18 | + is := is.New(t) |
| 19 | + repo, filename := setupCLIExitTest(t, false) |
| 20 | + _, err := exec.Command("git", "-C", repo, "config", "--local", "--unset", "user.email").CombinedOutput() |
| 21 | + is.NoErr(err) |
| 22 | + |
| 23 | + for _, value := range values { |
| 24 | + _, err := exec.Command("git", "-C", repo, "config", "--local", "--add", "user.email", value).CombinedOutput() |
| 25 | + is.NoErr(err) |
| 26 | + } |
| 27 | + |
| 28 | + globalPath := filepath.Join(t.TempDir(), "gitconfig") |
| 29 | + globalConfig := []byte("[user]\n\temail = global@example.com\n\temail = other@example.com\n") |
| 30 | + is.NoErr(os.WriteFile(globalPath, globalConfig, 0o600)) |
| 31 | + t.Setenv("GIT_CONFIG_GLOBAL", globalPath) |
| 32 | + |
| 33 | + // The second invocation must also succeed after the local keys are gone. |
| 34 | + for _, args := range [][]string{{"unuse"}, {"unuse", "work"}} { |
| 35 | + child := cliExitTestCommand(repo, filename, args...) |
| 36 | + |
| 37 | + var stdout, stderr bytes.Buffer |
| 38 | + |
| 39 | + child.Stdout = &stdout |
| 40 | + child.Stderr = &stderr |
| 41 | + is.NoErr(child.Run()) |
| 42 | + is.Equal(child.ProcessState.ExitCode(), 0) |
| 43 | + is.True(strings.Contains(stdout.String(), "Successfully removed")) |
| 44 | + is.Equal(stderr.String(), "") |
| 45 | + |
| 46 | + for _, key := range []string{"user.email", currentProfileKey} { |
| 47 | + query := exec.Command("git", "-C", repo, "config", "--local", "--get-all", key) |
| 48 | + out, err := query.Output() |
| 49 | + is.True(err != nil) |
| 50 | + is.Equal(query.ProcessState.ExitCode(), 1) |
| 51 | + is.Equal(string(out), "") |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + afterGlobal, err := os.ReadFile(globalPath) |
| 56 | + is.NoErr(err) |
| 57 | + is.Equal(afterGlobal, globalConfig) |
| 58 | + }) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +func TestUnuseReportsGitErrors(t *testing.T) { |
| 63 | + for _, failure := range []string{"locked config", "invalid key", "unwritable directory"} { |
| 64 | + t.Run(failure, func(t *testing.T) { |
| 65 | + is := is.New(t) |
| 66 | + repo, filename := setupCLIExitTest(t, false) |
| 67 | + gitDir := filepath.Join(repo, ".git") |
| 68 | + gitConfig := filepath.Join(gitDir, "config") |
| 69 | + before, err := os.ReadFile(gitConfig) |
| 70 | + is.NoErr(err) |
| 71 | + |
| 72 | + switch failure { |
| 73 | + case "locked config": |
| 74 | + is.NoErr(os.WriteFile(gitConfig+".lock", []byte("locked"), 0o600)) |
| 75 | + case "invalid key": |
| 76 | + is.NoErr(os.WriteFile(filename, []byte(`{"profiles":{"work":{"invalid":"value"}}}`), 0o600)) |
| 77 | + case "unwritable directory": |
| 78 | + if os.Geteuid() == 0 { |
| 79 | + t.Skip("root can write to directories regardless of mode bits") |
| 80 | + } |
| 81 | + |
| 82 | + info, err := os.Stat(gitDir) |
| 83 | + is.NoErr(err) |
| 84 | + t.Cleanup(func() { is.NoErr(os.Chmod(gitDir, info.Mode().Perm())) }) |
| 85 | + is.NoErr(os.Chmod(gitDir, 0o500)) |
| 86 | + } |
| 87 | + |
| 88 | + child := cliExitTestCommand(repo, filename, "unuse", "work") |
| 89 | + |
| 90 | + var stdout, stderr bytes.Buffer |
| 91 | + |
| 92 | + child.Stdout = &stdout |
| 93 | + child.Stderr = &stderr |
| 94 | + is.True(child.Run() != nil) |
| 95 | + is.Equal(child.ProcessState.ExitCode(), 1) |
| 96 | + is.Equal(stdout.String(), "") |
| 97 | + is.True(strings.Contains(stderr.String(), "Unable to interact with git to remove current profile")) |
| 98 | + |
| 99 | + after, err := os.ReadFile(gitConfig) |
| 100 | + is.NoErr(err) |
| 101 | + is.Equal(after, before) |
| 102 | + }) |
| 103 | + } |
| 104 | +} |
0 commit comments