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

Function TestGetHostFromOriginRemote

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

Source from the content-addressed store, hash-verified

436}
437
438func TestGetHostFromOriginRemote(t *testing.T) {
439 tmpDir := testutil.TempDir(t, "test-get-host-*")
440
441 originalDir, err := os.Getwd()
442 require.NoError(t, err, "get current directory for test setup")
443 defer func() {
444 if err := os.Chdir(originalDir); err != nil {
445 t.Logf("Warning: failed to restore directory: %v", err)
446 }
447 }()
448
449 require.NoError(t, os.Chdir(tmpDir), "change to temp directory for test setup")
450
451 // Initialize a git repo
452 if err := exec.Command("git", "init").Run(); err != nil {
453 t.Skip("Git not available")
454 }
455
456 t.Run("no remote defaults to github.com", func(t *testing.T) {
457 got := getHostFromOriginRemote()
458 assert.Equal(t, "github.com", got, "getHostFromOriginRemote should default to github.com without remotes")
459 })
460
461 t.Run("public GitHub remote", func(t *testing.T) {
462 require.NoError(t, exec.Command("git", "remote", "add", "origin", "https://github.com/owner/repo.git").Run(), "add origin remote")
463 defer func() { _ = exec.Command("git", "remote", "remove", "origin").Run() }()
464
465 got := getHostFromOriginRemote()
466 assert.Equal(t, "github.com", got, "getHostFromOriginRemote should return the origin host")
467 })
468
469 t.Run("GHES remote", func(t *testing.T) {
470 require.NoError(t, exec.Command("git", "remote", "add", "origin", "https://ghes.example.com/org/repo.git").Run(), "add GHES origin remote")
471 defer func() { _ = exec.Command("git", "remote", "remove", "origin").Run() }()
472
473 got := getHostFromOriginRemote()
474 assert.Equal(t, "ghes.example.com", got, "getHostFromOriginRemote should return the GHES origin host")
475 })
476
477 t.Run("non-origin single remote falls back to that remote", func(t *testing.T) {
478 require.NoError(t, exec.Command("git", "remote", "add", "upstream", "https://github.com/owner/repo.git").Run(), "add upstream remote")
479 defer func() { _ = exec.Command("git", "remote", "remove", "upstream").Run() }()
480
481 got := getHostFromOriginRemote()
482 assert.Equal(t, "github.com", got, "getHostFromOriginRemote should fall back to a single non-origin remote")
483 })
484
485 t.Run("multiple remotes without origin defaults to github.com", func(t *testing.T) {
486 require.NoError(t, exec.Command("git", "remote", "add", "myorg", "https://github.com/myorg/repo.git").Run(), "add first non-origin remote")
487 defer func() { _ = exec.Command("git", "remote", "remove", "myorg").Run() }()
488 require.NoError(t, exec.Command("git", "remote", "add", "other", "https://github.com/other/repo.git").Run(), "add second non-origin remote")
489 defer func() { _ = exec.Command("git", "remote", "remove", "other").Run() }()
490
491 got := getHostFromOriginRemote()
492 assert.Equal(t, "github.com", got, "getHostFromOriginRemote should default to github.com with multiple non-origin remotes")
493 })
494}
495

Callers

nothing calls this directly

Calls 3

TempDirFunction · 0.92
getHostFromOriginRemoteFunction · 0.85
RunMethod · 0.45

Tested by

no test coverage detected