Skip to content

Commit 60f0086

Browse files
committed
fixup! fix: Refactor repository custom property
1 parent f467768 commit 60f0086

6 files changed

Lines changed: 117 additions & 73 deletions

github/acc_helpers_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package github
22

33
import (
44
"context"
5+
"encoding/json"
56
"errors"
67
"fmt"
78
"testing"
@@ -12,6 +13,31 @@ import (
1213

1314
const testRandomIDLength = 5
1415

16+
func mustGetTestMockResponse(t *testing.T, uri string, statusCode int, body any) *mockResponse {
17+
resp := &mockResponse{
18+
ExpectedUri: uri,
19+
StatusCode: statusCode,
20+
}
21+
22+
if body != nil {
23+
bodyBytes, err := json.Marshal(body)
24+
if err != nil {
25+
t.Fatalf("failed to marshal mock response body: %v", err)
26+
}
27+
resp.ResponseBody = string(bodyBytes)
28+
}
29+
30+
return resp
31+
}
32+
33+
func mustCreateTestGitHubClient(t *testing.T, baseURL string, opts ...github.ClientOptionsFunc) *github.Client {
34+
client, err := github.NewClient(append([]github.ClientOptionsFunc{github.WithURLs(&baseURL, nil)}, opts...)...)
35+
if err != nil {
36+
t.Fatalf("failed to create GitHub client: %s", err)
37+
}
38+
return client
39+
}
40+
1541
func mustCreateTestOrganizationRepositoryCustomProperty(t *testing.T, valType string, allowed []string) *github.CustomProperty {
1642
t.Helper()
1743

github/data_source_github_app_token_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func TestAccGithubAppTokenDataSource(t *testing.T) {
3232
})
3333
defer ts.Close()
3434

35-
client := mustGitHubClient(t, ts.URL)
35+
client := mustCreateTestGitHubClient(t, ts.URL)
3636

3737
meta := &Owner{
3838
name: owner,

github/helpers_test.go

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 82 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,84 @@
11
package github
22

3-
// TODO: Enable this test once we have a pattern to create a mock client for the test.
4-
5-
// import (
6-
// "context"
7-
// "reflect"
8-
// "testing"
9-
// )
10-
11-
// func Test_resourceGithubCustomPropertyStateUpgradeV0(t *testing.T) {
12-
// t.Parallel()
13-
14-
// for _, d := range []struct {
15-
// testName string
16-
// rawState map[string]any
17-
// want map[string]any
18-
// shouldError bool
19-
// }{
20-
// {
21-
// testName: "migrates v0 to v1",
22-
// rawState: map[string]any{
23-
// "id": "my-org:my-repo:my-property",
24-
// "repository": "my-repo",
25-
// "property_name": "my-property",
26-
// "property_value": "my-value",
27-
// },
28-
// want: map[string]any{
29-
// "id": "my-org:my-repo:my-property",
30-
// "repository": "my-repo",
31-
// "repository_id": 123456,
32-
// "property_name": "my-property",
33-
// "property_value": "my-value",
34-
// },
35-
// shouldError: false,
36-
// },
37-
// } {
38-
// t.Run(d.testName, func(t *testing.T) {
39-
// t.Parallel()
40-
41-
// got, err := resourceGithubCustomPropertyStateUpgradeV0(t.Context(), d.rawState, nil)
42-
// if (err != nil) != d.shouldError {
43-
// t.Fatalf("unexpected error state")
44-
// }
45-
46-
// if !d.shouldError && !reflect.DeepEqual(got, d.want) {
47-
// t.Fatalf("got %+v, want %+v", got, d.want)
48-
// }
49-
// })
50-
// }
51-
// }
3+
import (
4+
"regexp"
5+
"testing"
6+
7+
"github.com/google/go-cmp/cmp"
8+
"github.com/google/go-github/v88/github"
9+
)
10+
11+
func Test_resourceGithubCustomPropertyStateUpgradeV0(t *testing.T) {
12+
for _, tt := range []struct {
13+
testName string
14+
statusCode int
15+
body *github.Repository
16+
rawState map[string]any
17+
want map[string]any
18+
wantErr *string
19+
}{
20+
{
21+
testName: "succeeds_if_repo_found",
22+
statusCode: 200,
23+
body: &github.Repository{
24+
ID: new(int64(123456)),
25+
},
26+
rawState: map[string]any{
27+
"id": "my-org:my-repo:my-property",
28+
"repository": "my-repo",
29+
"property_name": "my-property",
30+
"property_value": "my-value",
31+
},
32+
want: map[string]any{
33+
"id": "my-org:my-repo:my-property",
34+
"repository": "my-repo",
35+
"repository_id": 123456,
36+
"property_name": "my-property",
37+
"property_value": "my-value",
38+
},
39+
},
40+
{
41+
testName: "fails_if_repo_not_found",
42+
statusCode: 404,
43+
body: nil,
44+
rawState: map[string]any{
45+
"id": "my-org:my-repo:my-property",
46+
"repository": "my-repo",
47+
"property_name": "my-property",
48+
"property_value": "my-value",
49+
},
50+
wantErr: new("failed to retrieve repository"),
51+
},
52+
} {
53+
t.Run(tt.testName, func(t *testing.T) {
54+
ts := githubApiMock([]*mockResponse{mustGetTestMockResponse(t, "/repos/my-org/my-repo", tt.statusCode, tt.body)})
55+
defer ts.Close()
56+
57+
meta := &Owner{
58+
name: "my-org",
59+
v3client: mustCreateTestGitHubClient(t, ts.URL),
60+
}
61+
62+
got, err := resourceGithubRepositoryCustomPropertyStateUpgradeV0(t.Context(), tt.rawState, meta)
63+
if err != nil {
64+
if tt.wantErr == nil {
65+
t.Fatalf("unexpected error: %s", err)
66+
}
67+
68+
if !regexp.MustCompile(regexp.QuoteMeta(*tt.wantErr)).MatchString(err.Error()) {
69+
t.Fatalf("unexpected error: %s", err)
70+
}
71+
72+
return
73+
}
74+
75+
if tt.wantErr != nil {
76+
t.Fatalf("expected error: %s", *tt.wantErr)
77+
}
78+
79+
if diff := cmp.Diff(got, tt.want); diff != "" {
80+
t.Fatalf("got %+v, want %+v: %s", got, tt.want, diff)
81+
}
82+
})
83+
}
84+
}

github/resource_github_repository_file_migration_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ func Test_resourceGithubRepositoryFileStateUpgradeV0toV1(t *testing.T) {
165165
ts := githubApiMock(buildMockResponsesForRepositoryFileMigrationV0toV1(meta.name, wantRepositoryName, wantRepositoryID))
166166
defer ts.Close()
167167

168-
client := mustGitHubClient(t, ts.URL)
168+
client := mustCreateTestGitHubClient(t, ts.URL)
169169
meta.v3client = client
170170

171171
got, err := resourceGithubRepositoryFileStateUpgradeV0(t.Context(), d.rawState, meta)

github/transport_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func TestEtagTransport(t *testing.T) {
2828
})
2929
defer ts.Close()
3030

31-
client := mustGitHubClient(t, ts.URL, github.WithTransport(NewEtagTransport(http.DefaultTransport)))
31+
client := mustCreateTestGitHubClient(t, ts.URL, github.WithTransport(NewEtagTransport(http.DefaultTransport)))
3232
ctx := context.WithValue(t.Context(), ctxEtag, "something")
3333
r, _, err := client.Repositories.Get(ctx, "test", "blah")
3434
if err != nil {
@@ -135,7 +135,7 @@ func TestRateLimitTransport_abuseLimit_get(t *testing.T) {
135135
})
136136
defer ts.Close()
137137

138-
client := mustGitHubClient(t, ts.URL, github.WithTransport(NewRateLimitTransport(http.DefaultTransport)))
138+
client := mustCreateTestGitHubClient(t, ts.URL, github.WithTransport(NewRateLimitTransport(http.DefaultTransport)))
139139

140140
ctx := context.WithValue(t.Context(), ctxId, t.Name())
141141
r, _, err := client.Repositories.Get(ctx, "test", "blah")
@@ -164,7 +164,7 @@ func TestRateLimitTransport_abuseLimit_get_cancelled(t *testing.T) {
164164
})
165165
defer ts.Close()
166166

167-
client := mustGitHubClient(t, ts.URL, github.WithTransport(NewRateLimitTransport(http.DefaultTransport)))
167+
client := mustCreateTestGitHubClient(t, ts.URL, github.WithTransport(NewRateLimitTransport(http.DefaultTransport)))
168168

169169
ctx, cancel := context.WithTimeout(t.Context(), 100*time.Millisecond)
170170
defer cancel()
@@ -206,7 +206,7 @@ func TestRateLimitTransport_abuseLimit_post(t *testing.T) {
206206
})
207207
defer ts.Close()
208208

209-
client := mustGitHubClient(t, ts.URL, github.WithTransport(NewRateLimitTransport(http.DefaultTransport)))
209+
client := mustCreateTestGitHubClient(t, ts.URL, github.WithTransport(NewRateLimitTransport(http.DefaultTransport)))
210210

211211
ctx := context.WithValue(t.Context(), ctxId, t.Name())
212212
r, _, err := client.Repositories.Create(ctx, "tada", &github.Repository{
@@ -261,7 +261,7 @@ func TestRateLimitTransport_abuseLimit_post_error(t *testing.T) {
261261
})
262262
defer ts.Close()
263263

264-
client := mustGitHubClient(t, ts.URL, github.WithTransport(NewRateLimitTransport(http.DefaultTransport)))
264+
client := mustCreateTestGitHubClient(t, ts.URL, github.WithTransport(NewRateLimitTransport(http.DefaultTransport)))
265265

266266
ctx := context.WithValue(t.Context(), ctxId, t.Name())
267267
_, _, err := client.Repositories.Create(ctx, "tada", &github.Repository{
@@ -389,7 +389,7 @@ func TestRetryTransport_retry_post_error(t *testing.T) {
389389
})
390390
defer ts.Close()
391391

392-
client := mustGitHubClient(t, ts.URL, github.WithTransport(NewRetryTransport(http.DefaultTransport, WithMaxRetries(1))))
392+
client := mustCreateTestGitHubClient(t, ts.URL, github.WithTransport(NewRetryTransport(http.DefaultTransport, WithMaxRetries(1))))
393393

394394
ctx := context.WithValue(t.Context(), ctxId, t.Name())
395395
_, _, err := client.Repositories.Create(ctx, "tada", &github.Repository{
@@ -447,7 +447,7 @@ func TestRetryTransport_retry_post_success(t *testing.T) {
447447
})
448448
defer ts.Close()
449449

450-
client := mustGitHubClient(t, ts.URL, github.WithTransport(NewRetryTransport(http.DefaultTransport, WithMaxRetries(2), WithRetryDelay(time.Second))))
450+
client := mustCreateTestGitHubClient(t, ts.URL, github.WithTransport(NewRetryTransport(http.DefaultTransport, WithMaxRetries(2), WithRetryDelay(time.Second))))
451451

452452
ctx := context.WithValue(t.Context(), ctxId, t.Name())
453453
_, _, err := client.Repositories.Create(ctx, "tada", &github.Repository{

0 commit comments

Comments
 (0)