MCPcopy Create free account
hub / github.com/github/gh-aw / TestSwitchBranch

Function TestSwitchBranch

pkg/cli/git_test.go:120–163  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

118}
119
120func TestSwitchBranch(t *testing.T) {
121 tmpDir := testutil.TempDir(t, "test-*")
122
123 originalDir, err := os.Getwd()
124 require.NoError(t, err, "get current directory for test setup")
125 defer func() {
126 _ = os.Chdir(originalDir)
127 }()
128
129 require.NoError(t, os.Chdir(tmpDir), "change to temp directory for test setup")
130
131 // Initialize git repo
132 if err := exec.Command("git", "init").Run(); err != nil {
133 t.Skip("Git not available")
134 }
135
136 // Configure git
137 exec.Command("git", "config", "user.name", "Test User").Run()
138 exec.Command("git", "config", "user.email", "test@example.com").Run()
139
140 // Create initial commit
141 require.NoError(t, os.WriteFile("test.txt", []byte("test"), 0644), "create initial test file")
142 exec.Command("git", "add", "test.txt").Run()
143 if err := exec.Command("git", "commit", "-m", "Initial commit").Run(); err != nil {
144 t.Skip("Failed to create initial commit")
145 }
146
147 // Get initial branch name
148 initialBranch, err := getCurrentBranch()
149 require.NoError(t, err, "get initial branch")
150
151 // Create a new branch
152 newBranch := "feature-branch"
153 require.NoError(t, exec.Command("git", "checkout", "-b", newBranch).Run(), "create a new branch for switch testing")
154
155 // Switch back to initial branch
156 err = switchBranch(initialBranch, false)
157 require.NoError(t, err, "switch back to the initial branch")
158
159 // Verify we're on the initial branch
160 currentBranch, err := getCurrentBranch()
161 require.NoError(t, err, "get current branch after switching back")
162 assert.Equal(t, initialBranch, currentBranch, "current branch should match the original branch")
163}
164
165func TestCommitChanges(t *testing.T) {
166 tmpDir := testutil.TempDir(t, "test-*")

Callers

nothing calls this directly

Calls 4

TempDirFunction · 0.92
switchBranchFunction · 0.85
getCurrentBranchFunction · 0.70
RunMethod · 0.45

Tested by

no test coverage detected