diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 754bf9a..9d7c97c 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -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 @@ -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 ./... diff --git a/Makefile b/Makefile index 1d8d938..bd622dc 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/README.md b/README.md index a3d26a1..a5f6bb4 100644 --- a/README.md +++ b/README.md @@ -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 . 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%2Bgithub.com%2Falexandear%2Fimport-gitlab-commits.svg?type=large)](https://app.fossa.com/projects/git%2Bgithub.com%2Falexandear%2Fimport-gitlab-commits?ref=badge_large) diff --git a/internal/gitlab_test.go b/internal/gitlab_test.go index a1d394d..ac8d30a 100644 --- a/internal/gitlab_test.go +++ b/internal/gitlab_test.go @@ -1,3 +1,5 @@ +//go:build integration + package app_test import ( @@ -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 }