Skip to content

Commit dfe7016

Browse files
authored
Add integration tests (#63)
1 parent 98ac87e commit dfe7016

File tree

4 files changed

+42
-34
lines changed

4 files changed

+42
-34
lines changed

.github/workflows/test.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ jobs:
1010
test:
1111
runs-on: ubuntu-latest
1212
timeout-minutes: 5
13-
13+
env:
14+
GITLAB_TOKEN: ${{ secrets.GITLAB_TOKEN }}
15+
GITLAB_BASE_URL: https://gitlab.com
1416
steps:
1517
- uses: actions/checkout@v4
1618

@@ -20,3 +22,5 @@ jobs:
2022
check-latest: true
2123

2224
- run: go test -v -count=1 -race -shuffle=on -cover ./...
25+
26+
- run: go test -tags=integration -run=TestGitLab -shuffle=on -count=1 -race -v ./...

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ test:
2525
@echo test
2626
@go test -shuffle=on -count=1 -race -v ./...
2727

28+
.PHONY: test-integration
29+
test-integration:
30+
@echo test-integration
31+
@go test -tags=integration -run=TestGitLab -shuffle=on -count=1 -race -v ./...
32+
2833
.PHONY: lint
2934
lint:
3035
@echo lint

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,18 @@ To show the changes on GitHub you need to:
6262
- add remote url `git remote add origin [email protected]:username/yourcompany-contributions.git`;
6363
- push changes.
6464

65+
### Integration Tests
66+
67+
To run integration tests:
68+
69+
1. Set `GITLAB_TOKEN` environment variables with the value obtained at <https://gitlab.com/-/user_settings/personal_access_tokens>. Necessary scopes:
70+
- `read_api`;
71+
- `read_user`;
72+
- `read_repository`.
73+
74+
2. Set `GITLAB_BASE_URL` with `https://gitlab.com`.
75+
3. Run `make test-integration`.
76+
6577
## License
6678

6779
[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Falexandear%2Fimport-gitlab-commits.svg?type=large)](https://app.fossa.com/projects/git%2Bgithub.com%2Falexandear%2Fimport-gitlab-commits?ref=badge_large)

internal/gitlab_test.go

Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//go:build integration
2+
13
package app_test
24

35
import (
@@ -13,48 +15,33 @@ import (
1315
"github.com/alexandear/import-gitlab-commits/internal/testutil"
1416
)
1517

16-
func TestGitLabHasUserContributions(t *testing.T) {
17-
git := initGit(t)
18-
s := app.NewGitLab(testutil.NewLog(t), git)
19-
user := newCurrentUser(t, git)
18+
func TestGitLabCurrentUser(t *testing.T) {
19+
gl := app.NewGitLab(testutil.NewLog(t), gitlabClient(t))
20+
21+
user, err := gl.CurrentUser(context.Background())
2022

21-
assert.False(t, s.HasUserContributions(context.Background(), user, 3))
22-
assert.True(t, s.HasUserContributions(context.Background(), user, 575))
23+
require.NoError(t, err)
24+
assert.NotEmpty(t, user.Name)
25+
assert.NotEmpty(t, user.Emails)
26+
assert.NotEmpty(t, user.Username)
27+
assert.False(t, user.CreatedAt.IsZero())
2328
}
2429

25-
func initGit(t *testing.T) *gitlab.Client {
30+
func gitlabClient(t *testing.T) *gitlab.Client {
2631
t.Helper()
2732

2833
token := os.Getenv("GITLAB_TOKEN")
29-
baseURL := os.Getenv("GITLAB_BASE_URL")
30-
31-
if token == "" || baseURL == "" {
32-
t.SkipNow()
34+
if token == "" {
35+
t.Fatal("GITLAB_TOKEN is required")
3336
}
3437

35-
git, err := gitlab.NewClient(token, gitlab.WithBaseURL(baseURL))
36-
require.NoError(t, err)
37-
38-
return git
39-
}
40-
41-
func newCurrentUser(t *testing.T, gitlabClient *gitlab.Client) *app.User {
42-
t.Helper()
43-
44-
user, _, err := gitlabClient.Users.CurrentUser()
45-
require.NoError(t, err)
38+
baseURL := os.Getenv("GITLAB_BASE_URL")
39+
if baseURL == "" {
40+
t.Fatal("GITLAB_BASE_URL is required")
41+
}
4642

47-
emails, _, err := gitlabClient.Users.ListEmails()
43+
client, err := gitlab.NewClient(token, gitlab.WithBaseURL(baseURL))
4844
require.NoError(t, err)
4945

50-
emailAddresses := make([]string, 0, len(emails))
51-
for _, email := range emails {
52-
emailAddresses = append(emailAddresses, email.Email)
53-
}
54-
55-
return &app.User{
56-
Name: user.Name,
57-
Emails: emailAddresses,
58-
CreatedAt: *user.CreatedAt,
59-
}
46+
return client
6047
}

0 commit comments

Comments
 (0)