|
| 1 | +package workflow |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +func TestExecGH(t *testing.T) { |
| 10 | + tests := []struct { |
| 11 | + name string |
| 12 | + ghToken string |
| 13 | + githubToken string |
| 14 | + expectGHToken bool |
| 15 | + expectValue string |
| 16 | + }{ |
| 17 | + { |
| 18 | + name: "GH_TOKEN is set", |
| 19 | + ghToken: "gh-token-123", |
| 20 | + githubToken: "", |
| 21 | + expectGHToken: false, // Should use existing GH_TOKEN from environment |
| 22 | + expectValue: "", |
| 23 | + }, |
| 24 | + { |
| 25 | + name: "GITHUB_TOKEN is set, GH_TOKEN is not", |
| 26 | + ghToken: "", |
| 27 | + githubToken: "github-token-456", |
| 28 | + expectGHToken: true, |
| 29 | + expectValue: "github-token-456", |
| 30 | + }, |
| 31 | + { |
| 32 | + name: "Both GH_TOKEN and GITHUB_TOKEN are set", |
| 33 | + ghToken: "gh-token-123", |
| 34 | + githubToken: "github-token-456", |
| 35 | + expectGHToken: false, // Should prefer existing GH_TOKEN |
| 36 | + expectValue: "", |
| 37 | + }, |
| 38 | + { |
| 39 | + name: "Neither GH_TOKEN nor GITHUB_TOKEN is set", |
| 40 | + ghToken: "", |
| 41 | + githubToken: "", |
| 42 | + expectGHToken: false, |
| 43 | + expectValue: "", |
| 44 | + }, |
| 45 | + } |
| 46 | + |
| 47 | + for _, tt := range tests { |
| 48 | + t.Run(tt.name, func(t *testing.T) { |
| 49 | + // Save original environment |
| 50 | + originalGHToken := os.Getenv("GH_TOKEN") |
| 51 | + originalGitHubToken := os.Getenv("GITHUB_TOKEN") |
| 52 | + defer func() { |
| 53 | + os.Setenv("GH_TOKEN", originalGHToken) |
| 54 | + os.Setenv("GITHUB_TOKEN", originalGitHubToken) |
| 55 | + }() |
| 56 | + |
| 57 | + // Set up test environment |
| 58 | + if tt.ghToken != "" { |
| 59 | + os.Setenv("GH_TOKEN", tt.ghToken) |
| 60 | + } else { |
| 61 | + os.Unsetenv("GH_TOKEN") |
| 62 | + } |
| 63 | + if tt.githubToken != "" { |
| 64 | + os.Setenv("GITHUB_TOKEN", tt.githubToken) |
| 65 | + } else { |
| 66 | + os.Unsetenv("GITHUB_TOKEN") |
| 67 | + } |
| 68 | + |
| 69 | + // Execute the helper |
| 70 | + cmd := ExecGH("api", "/user") |
| 71 | + |
| 72 | + // Verify the command |
| 73 | + if cmd.Path != "gh" && !strings.HasSuffix(cmd.Path, "/gh") { |
| 74 | + t.Errorf("Expected command path to be 'gh', got: %s", cmd.Path) |
| 75 | + } |
| 76 | + |
| 77 | + // Verify arguments |
| 78 | + if len(cmd.Args) != 3 || cmd.Args[1] != "api" || cmd.Args[2] != "/user" { |
| 79 | + t.Errorf("Expected args [gh api /user], got: %v", cmd.Args) |
| 80 | + } |
| 81 | + |
| 82 | + // Verify environment |
| 83 | + if tt.expectGHToken { |
| 84 | + found := false |
| 85 | + expectedEnv := "GH_TOKEN=" + tt.expectValue |
| 86 | + for _, env := range cmd.Env { |
| 87 | + if env == expectedEnv { |
| 88 | + found = true |
| 89 | + break |
| 90 | + } |
| 91 | + } |
| 92 | + if !found { |
| 93 | + t.Errorf("Expected environment to contain %s, but it wasn't found", expectedEnv) |
| 94 | + } |
| 95 | + } else { |
| 96 | + // When GH_TOKEN is already set or neither token is set, cmd.Env should be nil (uses parent process env) |
| 97 | + if cmd.Env != nil { |
| 98 | + t.Errorf("Expected cmd.Env to be nil (inherit parent environment), got: %v", cmd.Env) |
| 99 | + } |
| 100 | + } |
| 101 | + }) |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +func TestExecGHWithMultipleArgs(t *testing.T) { |
| 106 | + // Save original environment |
| 107 | + originalGHToken := os.Getenv("GH_TOKEN") |
| 108 | + originalGitHubToken := os.Getenv("GITHUB_TOKEN") |
| 109 | + defer func() { |
| 110 | + os.Setenv("GH_TOKEN", originalGHToken) |
| 111 | + os.Setenv("GITHUB_TOKEN", originalGitHubToken) |
| 112 | + }() |
| 113 | + |
| 114 | + // Set up test environment |
| 115 | + os.Unsetenv("GH_TOKEN") |
| 116 | + os.Setenv("GITHUB_TOKEN", "test-token") |
| 117 | + |
| 118 | + // Test with multiple arguments |
| 119 | + cmd := ExecGH("api", "repos/owner/repo/git/ref/tags/v1.0", "--jq", ".object.sha") |
| 120 | + |
| 121 | + // Verify command |
| 122 | + if cmd.Path != "gh" && !strings.HasSuffix(cmd.Path, "/gh") { |
| 123 | + t.Errorf("Expected command path to be 'gh', got: %s", cmd.Path) |
| 124 | + } |
| 125 | + |
| 126 | + // Verify all arguments are preserved |
| 127 | + expectedArgs := []string{"gh", "api", "repos/owner/repo/git/ref/tags/v1.0", "--jq", ".object.sha"} |
| 128 | + if len(cmd.Args) != len(expectedArgs) { |
| 129 | + t.Errorf("Expected %d args, got %d: %v", len(expectedArgs), len(cmd.Args), cmd.Args) |
| 130 | + } |
| 131 | + |
| 132 | + for i, expected := range expectedArgs { |
| 133 | + if i >= len(cmd.Args) || cmd.Args[i] != expected { |
| 134 | + t.Errorf("Arg %d: expected %s, got %s", i, expected, cmd.Args[i]) |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + // Verify environment contains GH_TOKEN |
| 139 | + found := false |
| 140 | + for _, env := range cmd.Env { |
| 141 | + if env == "GH_TOKEN=test-token" { |
| 142 | + found = true |
| 143 | + break |
| 144 | + } |
| 145 | + } |
| 146 | + if !found { |
| 147 | + t.Errorf("Expected environment to contain GH_TOKEN=test-token") |
| 148 | + } |
| 149 | +} |
0 commit comments