Skip to content

Commit

Permalink
internal: merge ioutil, logtest, use external ioutil (#546)
Browse files Browse the repository at this point in the history
Use the new go.abhg.dev/io/ioutil package
instead of internal/ioutil's similar functionality.

Merge remaining ioutil and logtest into logutil
as it's all log related at this point.
  • Loading branch information
abhinav authored Jan 18, 2025
1 parent 5c52ae5 commit 9938eb5
Show file tree
Hide file tree
Showing 35 changed files with 168 additions and 412 deletions.
6 changes: 3 additions & 3 deletions branch_submit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.abhg.dev/gs/internal/forge"
"go.abhg.dev/gs/internal/logtest"
"go.abhg.dev/gs/internal/logutil"
)

func TestBranchSubmit_listChangeTemplates(t *testing.T) {
t.Run("NoTimeout", func(t *testing.T) {
log := logtest.New(t)
log := logutil.TestLogger(t)
ctx := context.Background()
tmpl := &forge.ChangeTemplate{}
svc := &spiceTemplateServiceStub{
Expand All @@ -33,7 +33,7 @@ func TestBranchSubmit_listChangeTemplates(t *testing.T) {
})

t.Run("Timeout", func(t *testing.T) {
log := logtest.New(t)
log := logutil.TestLogger(t)
ctx := context.Background()

svc := &spiceTemplateServiceStub{
Expand Down
1 change: 0 additions & 1 deletion codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ ignore:
- internal/forge/shamhub
- internal/forge/forgetest
- internal/git/gittest
- internal/logtest
- internal/mockedit
- internal/secret/secrettest
- internal/termtest
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ require (
github.com/vito/midterm v0.2.1-0.20241224155031-41aafc92c897
github.com/zalando/go-keyring v0.2.6
gitlab.com/gitlab-org/api/client-go v0.119.0
go.abhg.dev/io/ioutil v0.1.0
go.abhg.dev/komplete v0.1.0
go.abhg.dev/requiredfield v0.3.0
go.abhg.dev/testing/stub v0.2.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ github.com/zalando/go-keyring v0.2.6 h1:r7Yc3+H+Ux0+M72zacZoItR3UDxeWfKTcabvkI8u
github.com/zalando/go-keyring v0.2.6/go.mod h1:2TCrxYrbUNYfNS/Kgy/LSrkSQzZ5UPVH85RwfczwvcI=
gitlab.com/gitlab-org/api/client-go v0.119.0 h1:YBZyx9XUTtEDBBYtY36cZWz6JmT7om/8HPSk37IS95g=
gitlab.com/gitlab-org/api/client-go v0.119.0/go.mod h1:ygHmS3AU3TpvK+AC6DYO1QuAxLlv6yxYK+/Votr/WFQ=
go.abhg.dev/io/ioutil v0.1.0 h1:YGGMzh9HT52JYuVWbnr/E5GkYHbL3yRNDjcxFDaUHNk=
go.abhg.dev/io/ioutil v0.1.0/go.mod h1:79IIyZVWxNZE8hBxMtubM8zJFPs+2NCqHYfWeH3X6hM=
go.abhg.dev/komplete v0.1.0 h1:OE/uazFmWxrYYxttaKri9UpVr/i3J2Iv1vh21mYVom8=
go.abhg.dev/komplete v0.1.0/go.mod h1:MYxEW+7RETaNiYbeZv0LOSBmiZ2vfzfk/6m36EC04kc=
go.abhg.dev/requiredfield v0.3.0 h1:/fOqPt7t1SktSZVGUNXMg5djO8GasUh+G1ez111UEcs=
Expand Down
4 changes: 2 additions & 2 deletions internal/forge/github/comment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.abhg.dev/gs/internal/forge"
"go.abhg.dev/gs/internal/logtest"
"go.abhg.dev/gs/internal/logutil"
"go.abhg.dev/testing/stub"
)

Expand Down Expand Up @@ -130,7 +130,7 @@ func TestListChangeComments(t *testing.T) {
repo, err := newRepository(
context.Background(), new(Forge),
"owner", "repo",
logtest.New(t),
logutil.TestLogger(t),
githubv4.NewEnterpriseClient(srv.URL, nil),
"repoID",
)
Expand Down
32 changes: 16 additions & 16 deletions internal/forge/github/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import (
"go.abhg.dev/gs/internal/git"
"go.abhg.dev/gs/internal/graphqlutil"
"go.abhg.dev/gs/internal/httptest"
"go.abhg.dev/gs/internal/ioutil"
"go.abhg.dev/gs/internal/logtest"
"go.abhg.dev/gs/internal/logutil"
"go.abhg.dev/io/ioutil"
"golang.org/x/oauth2"
"gopkg.in/dnaeon/go-vcr.v4/pkg/cassette"
"gopkg.in/dnaeon/go-vcr.v4/pkg/recorder"
Expand Down Expand Up @@ -95,15 +95,15 @@ func TestIntegration_Repository(t *testing.T) {
ctx := context.Background()
rec := newRecorder(t, t.Name())
ghc := newGitHubClient(rec.GetDefaultClient())
_, err := github.NewRepository(ctx, new(github.Forge), "abhinav", "git-spice", logtest.New(t), ghc, nil)
_, err := github.NewRepository(ctx, new(github.Forge), "abhinav", "git-spice", logutil.TestLogger(t), ghc, nil)
require.NoError(t, err)
}

func TestIntegration_Repository_FindChangeByID(t *testing.T) {
ctx := context.Background()
rec := newRecorder(t, t.Name())
ghc := newGitHubClient(rec.GetDefaultClient())
repo, err := github.NewRepository(ctx, new(github.Forge), "abhinav", "git-spice", logtest.New(t), ghc, _gitSpiceRepoID)
repo, err := github.NewRepository(ctx, new(github.Forge), "abhinav", "git-spice", logutil.TestLogger(t), ghc, _gitSpiceRepoID)
require.NoError(t, err)

t.Run("found", func(t *testing.T) {
Expand Down Expand Up @@ -135,7 +135,7 @@ func TestIntegration_Repository_FindChangesByBranch(t *testing.T) {
ctx := context.Background()
rec := newRecorder(t, t.Name())
ghc := newGitHubClient(rec.GetDefaultClient())
repo, err := github.NewRepository(ctx, new(github.Forge), "abhinav", "git-spice", logtest.New(t), ghc, _gitSpiceRepoID)
repo, err := github.NewRepository(ctx, new(github.Forge), "abhinav", "git-spice", logutil.TestLogger(t), ghc, _gitSpiceRepoID)
require.NoError(t, err)

t.Run("found", func(t *testing.T) {
Expand Down Expand Up @@ -168,7 +168,7 @@ func TestIntegration_Repository_ChangesAreMerged(t *testing.T) {
ctx := context.Background()
rec := newRecorder(t, t.Name())
ghc := newGitHubClient(rec.GetDefaultClient())
repo, err := github.NewRepository(ctx, new(github.Forge), "abhinav", "git-spice", logtest.New(t), ghc, _gitSpiceRepoID)
repo, err := github.NewRepository(ctx, new(github.Forge), "abhinav", "git-spice", logutil.TestLogger(t), ghc, _gitSpiceRepoID)
require.NoError(t, err)

merged, err := repo.ChangesAreMerged(ctx, []forge.ChangeID{
Expand All @@ -188,7 +188,7 @@ func TestIntegration_Repository_ListChangeTemplates(t *testing.T) {
t.Run("absent", func(t *testing.T) {
rec := newRecorder(t, t.Name())
ghc := newGitHubClient(rec.GetDefaultClient())
repo, err := github.NewRepository(ctx, new(github.Forge), "abhinav", "git-spice", logtest.New(t), ghc, _gitSpiceRepoID)
repo, err := github.NewRepository(ctx, new(github.Forge), "abhinav", "git-spice", logutil.TestLogger(t), ghc, _gitSpiceRepoID)
require.NoError(t, err)

templates, err := repo.ListChangeTemplates(ctx)
Expand All @@ -199,7 +199,7 @@ func TestIntegration_Repository_ListChangeTemplates(t *testing.T) {
t.Run("present", func(t *testing.T) {
rec := newRecorder(t, t.Name())
ghc := newGitHubClient(rec.GetDefaultClient())
repo, err := github.NewRepository(ctx, new(github.Forge), "golang", "go", logtest.New(t), ghc, nil)
repo, err := github.NewRepository(ctx, new(github.Forge), "golang", "go", logutil.TestLogger(t), ghc, nil)
require.NoError(t, err)

templates, err := repo.ListChangeTemplates(ctx)
Expand All @@ -217,7 +217,7 @@ func TestIntegration_Repository_NewChangeMetadata(t *testing.T) {

rec := newRecorder(t, t.Name())
ghc := newGitHubClient(rec.GetDefaultClient())
repo, err := github.NewRepository(ctx, new(github.Forge), "abhinav", "git-spice", logtest.New(t), ghc, _gitSpiceRepoID)
repo, err := github.NewRepository(ctx, new(github.Forge), "abhinav", "git-spice", logutil.TestLogger(t), ghc, _gitSpiceRepoID)
require.NoError(t, err)

t.Run("valid", func(t *testing.T) {
Expand Down Expand Up @@ -254,7 +254,7 @@ func TestIntegration_Repository_SubmitEditChange(t *testing.T) {
t.Setenv("GIT_COMMITTER_EMAIL", "[email protected]")
t.Setenv("GIT_COMMITTER_NAME", "gs-test[bot]")

output := ioutil.TestOutputWriter(t, "[git] ")
output := ioutil.TestLogWriter(t, "[git] ")

t.Logf("Cloning test-repo...")
repoDir := t.TempDir()
Expand All @@ -265,7 +265,7 @@ func TestIntegration_Repository_SubmitEditChange(t *testing.T) {

var err error
gitRepo, err = git.Open(ctx, repoDir, git.OpenOptions{
Log: logtest.New(t),
Log: logutil.TestLogger(t),
})
require.NoError(t, err, "failed to open git repo")

Expand Down Expand Up @@ -309,7 +309,7 @@ func TestIntegration_Repository_SubmitEditChange(t *testing.T) {
rec := newRecorder(t, t.Name())
ghc := newGitHubClient(rec.GetDefaultClient())
repo, err := github.NewRepository(
ctx, new(github.Forge), "abhinav", "test-repo", logtest.New(t), ghc, _testRepoID,
ctx, new(github.Forge), "abhinav", "test-repo", logutil.TestLogger(t), ghc, _testRepoID,
)
require.NoError(t, err)

Expand Down Expand Up @@ -393,7 +393,7 @@ func TestIntegration_Repository_comments(t *testing.T) {
rec := newRecorder(t, t.Name())
ghc := newGitHubClient(rec.GetDefaultClient())
repo, err := github.NewRepository(
ctx, new(github.Forge), "abhinav", "test-repo", logtest.New(t), ghc, _testRepoID,
ctx, new(github.Forge), "abhinav", "test-repo", logutil.TestLogger(t), ghc, _testRepoID,
)
require.NoError(t, err)

Expand Down Expand Up @@ -432,7 +432,7 @@ func TestIntegration_Repository_ListChangeComments_simple(t *testing.T) {
rec := newRecorder(t, t.Name())
ghc := newGitHubClient(rec.GetDefaultClient())
repo, err := github.NewRepository(
ctx, new(github.Forge), "abhinav", "git-spice", logtest.New(t), ghc, _gitSpiceRepoID,
ctx, new(github.Forge), "abhinav", "git-spice", logutil.TestLogger(t), ghc, _gitSpiceRepoID,
)
require.NoError(t, err)

Expand Down Expand Up @@ -476,7 +476,7 @@ func TestIntegration_Repository_ListChangeComments_paginated(t *testing.T) {
rec := newRecorder(t, t.Name())
ghc := newGitHubClient(rec.GetDefaultClient())
repo, err := github.NewRepository(
ctx, new(github.Forge), "abhinav", "test-repo", logtest.New(t), ghc, _testRepoID,
ctx, new(github.Forge), "abhinav", "test-repo", logutil.TestLogger(t), ghc, _testRepoID,
)
require.NoError(t, err)

Expand Down Expand Up @@ -523,7 +523,7 @@ func TestIntegration_Repository_notFoundError(t *testing.T) {
client := rec.GetDefaultClient()
client.Transport = graphqlutil.WrapTransport(client.Transport)
ghc := newGitHubClient(client)
_, err := github.NewRepository(ctx, new(github.Forge), "abhinav", "does-not-exist-repo", logtest.New(t), ghc, nil)
_, err := github.NewRepository(ctx, new(github.Forge), "abhinav", "does-not-exist-repo", logutil.TestLogger(t), ghc, nil)
require.Error(t, err)
assert.ErrorIs(t, err, graphqlutil.ErrNotFound)

Expand Down
4 changes: 2 additions & 2 deletions internal/forge/gitlab/comment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/stretchr/testify/require"
gitlab "gitlab.com/gitlab-org/api/client-go"
"go.abhg.dev/gs/internal/forge"
"go.abhg.dev/gs/internal/logtest"
"go.abhg.dev/gs/internal/logutil"
)

// SetListChangeCommentsPageSize changes the page size
Expand Down Expand Up @@ -171,7 +171,7 @@ func TestListChangeComments(t *testing.T) {
repo, err := newRepository(
context.Background(), new(Forge),
"owner", "repo",
logtest.New(t),
logutil.TestLogger(t),
client,
&repoID,
)
Expand Down
32 changes: 16 additions & 16 deletions internal/forge/gitlab/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (
"go.abhg.dev/gs/internal/forge/gitlab"
"go.abhg.dev/gs/internal/git"
"go.abhg.dev/gs/internal/httptest"
"go.abhg.dev/gs/internal/ioutil"
"go.abhg.dev/gs/internal/logtest"
"go.abhg.dev/gs/internal/logutil"
"go.abhg.dev/io/ioutil"
"gopkg.in/dnaeon/go-vcr.v4/pkg/cassette"
"gopkg.in/dnaeon/go-vcr.v4/pkg/recorder"
)
Expand Down Expand Up @@ -99,15 +99,15 @@ func TestIntegration_Repository(t *testing.T) {
ctx := context.Background()
rec := newRecorder(t, t.Name())
ghc := newGitLabClient(rec.GetDefaultClient())
_, err := gitlab.NewRepository(ctx, new(gitlab.Forge), "abg", "test-repo", logtest.New(t), ghc, nil)
_, err := gitlab.NewRepository(ctx, new(gitlab.Forge), "abg", "test-repo", logutil.TestLogger(t), ghc, nil)
require.NoError(t, err)
}

func TestIntegration_Repository_FindChangeByID(t *testing.T) {
ctx := context.Background()
rec := newRecorder(t, t.Name())
ghc := newGitLabClient(rec.GetDefaultClient())
repo, err := gitlab.NewRepository(ctx, new(gitlab.Forge), "abg", "test-repo", logtest.New(t), ghc, _testRepoID)
repo, err := gitlab.NewRepository(ctx, new(gitlab.Forge), "abg", "test-repo", logutil.TestLogger(t), ghc, _testRepoID)
require.NoError(t, err)

t.Run("found", func(t *testing.T) {
Expand Down Expand Up @@ -138,7 +138,7 @@ func TestIntegration_Repository_FindChangesByBranch(t *testing.T) {
ctx := context.Background()
rec := newRecorder(t, t.Name())
ghc := newGitLabClient(rec.GetDefaultClient())
repo, err := gitlab.NewRepository(ctx, new(gitlab.Forge), "abg", "test-repo", logtest.New(t), ghc, _testRepoID)
repo, err := gitlab.NewRepository(ctx, new(gitlab.Forge), "abg", "test-repo", logutil.TestLogger(t), ghc, _testRepoID)
require.NoError(t, err)

t.Run("found", func(t *testing.T) {
Expand Down Expand Up @@ -170,7 +170,7 @@ func TestIntegration_Repository_ChangesAreMerged(t *testing.T) {
ctx := context.Background()
rec := newRecorder(t, t.Name())
ghc := newGitLabClient(rec.GetDefaultClient())
repo, err := gitlab.NewRepository(ctx, new(gitlab.Forge), "abg", "test-repo", logtest.New(t), ghc, _testRepoID)
repo, err := gitlab.NewRepository(ctx, new(gitlab.Forge), "abg", "test-repo", logutil.TestLogger(t), ghc, _testRepoID)
require.NoError(t, err)

merged, err := repo.ChangesAreMerged(ctx, []forge.ChangeID{
Expand All @@ -188,7 +188,7 @@ func TestIntegration_Repository_ListChangeTemplates(t *testing.T) {
t.Run("absent", func(t *testing.T) {
rec := newRecorder(t, t.Name())
ghc := newGitLabClient(rec.GetDefaultClient())
repo, err := gitlab.NewRepository(ctx, new(gitlab.Forge), "abg", "test-repo", logtest.New(t), ghc, _testRepoID)
repo, err := gitlab.NewRepository(ctx, new(gitlab.Forge), "abg", "test-repo", logutil.TestLogger(t), ghc, _testRepoID)
require.NoError(t, err)

templates, err := repo.ListChangeTemplates(ctx)
Expand All @@ -199,7 +199,7 @@ func TestIntegration_Repository_ListChangeTemplates(t *testing.T) {
t.Run("present", func(t *testing.T) {
rec := newRecorder(t, t.Name())
ghc := newGitLabClient(rec.GetDefaultClient())
repo, err := gitlab.NewRepository(ctx, new(gitlab.Forge), "gitlab-org", "cli", logtest.New(t), ghc, nil)
repo, err := gitlab.NewRepository(ctx, new(gitlab.Forge), "gitlab-org", "cli", logutil.TestLogger(t), ghc, nil)
require.NoError(t, err)

templates, err := repo.ListChangeTemplates(ctx)
Expand All @@ -217,7 +217,7 @@ func TestIntegration_Repository_NewChangeMetadata(t *testing.T) {

rec := newRecorder(t, t.Name())
ghc := newGitLabClient(rec.GetDefaultClient())
repo, err := gitlab.NewRepository(ctx, new(gitlab.Forge), "abg", "test-repo", logtest.New(t), ghc, _testRepoID)
repo, err := gitlab.NewRepository(ctx, new(gitlab.Forge), "abg", "test-repo", logutil.TestLogger(t), ghc, _testRepoID)
require.NoError(t, err)

t.Run("valid", func(t *testing.T) {
Expand Down Expand Up @@ -252,7 +252,7 @@ func TestIntegration_Repository_SubmitEditChange(t *testing.T) {
t.Setenv("GIT_COMMITTER_EMAIL", "[email protected]")
t.Setenv("GIT_COMMITTER_NAME", "gs-test[bot]")

output := ioutil.TestOutputWriter(t, "[git] ")
output := ioutil.TestLogWriter(t, "[git] ")

t.Logf("Cloning test-repo...")
repoDir := t.TempDir()
Expand All @@ -263,7 +263,7 @@ func TestIntegration_Repository_SubmitEditChange(t *testing.T) {

var err error
gitRepo, err = git.Open(ctx, repoDir, git.OpenOptions{
Log: logtest.New(t),
Log: logutil.TestLogger(t),
})
require.NoError(t, err, "failed to open git repo")

Expand Down Expand Up @@ -307,7 +307,7 @@ func TestIntegration_Repository_SubmitEditChange(t *testing.T) {
rec := newRecorder(t, t.Name())
ghc := newGitLabClient(rec.GetDefaultClient())
repo, err := gitlab.NewRepository(
ctx, new(gitlab.Forge), "abg", "test-repo", logtest.New(t), ghc, _testRepoID,
ctx, new(gitlab.Forge), "abg", "test-repo", logutil.TestLogger(t), ghc, _testRepoID,
)
require.NoError(t, err)

Expand Down Expand Up @@ -391,7 +391,7 @@ func TestIntegration_Repository_comments(t *testing.T) {
rec := newRecorder(t, t.Name())
ghc := newGitLabClient(rec.GetDefaultClient())
repo, err := gitlab.NewRepository(
ctx, new(gitlab.Forge), "abg", "test-repo", logtest.New(t), ghc, _testRepoID,
ctx, new(gitlab.Forge), "abg", "test-repo", logutil.TestLogger(t), ghc, _testRepoID,
)
require.NoError(t, err)

Expand Down Expand Up @@ -428,7 +428,7 @@ func TestIntegration_Repository_ListChangeComments_simple(t *testing.T) {
rec := newRecorder(t, t.Name())
ghc := newGitLabClient(rec.GetDefaultClient())
repo, err := gitlab.NewRepository(
ctx, new(gitlab.Forge), "abg", "test-repo", logtest.New(t), ghc, _testRepoID,
ctx, new(gitlab.Forge), "abg", "test-repo", logutil.TestLogger(t), ghc, _testRepoID,
)
require.NoError(t, err)

Expand Down Expand Up @@ -472,7 +472,7 @@ func TestIntegration_Repository_ListChangeComments_paginated(t *testing.T) {
rec := newRecorder(t, t.Name())
ghc := newGitLabClient(rec.GetDefaultClient())
repo, err := gitlab.NewRepository(
ctx, new(gitlab.Forge), "abg", "test-repo", logtest.New(t), ghc, _testRepoID,
ctx, new(gitlab.Forge), "abg", "test-repo", logutil.TestLogger(t), ghc, _testRepoID,
)
require.NoError(t, err)

Expand Down Expand Up @@ -518,7 +518,7 @@ func TestIntegration_Repository_notFoundError(t *testing.T) {
rec := newRecorder(t, t.Name())
client := rec.GetDefaultClient()
ghc := newGitLabClient(client)
_, err := gitlab.NewRepository(ctx, new(gitlab.Forge), "abg", "does-not-exist-repo", logtest.New(t), ghc, nil)
_, err := gitlab.NewRepository(ctx, new(gitlab.Forge), "abg", "does-not-exist-repo", logutil.TestLogger(t), ghc, nil)
require.Error(t, err)
assert.ErrorContains(t, err, "404 Not Found")
}
Expand Down
4 changes: 2 additions & 2 deletions internal/forge/shamhub/change.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

"github.com/charmbracelet/log"
"go.abhg.dev/gs/internal/forge"
"go.abhg.dev/gs/internal/ioutil"
"go.abhg.dev/gs/internal/logutil"
)

// ListChanges reports all changes known to the forge.
Expand Down Expand Up @@ -136,7 +136,7 @@ type ChangeBranch struct {
}

func (sh *ShamHub) toChangeBranch(owner, repo, ref string) (*ChangeBranch, error) {
logw, flush := ioutil.LogWriter(sh.log, log.DebugLevel)
logw, flush := logutil.Writer(sh.log, log.DebugLevel)
defer flush()

cmd := exec.Command(sh.gitExe, "rev-parse", ref)
Expand Down
Loading

0 comments on commit 9938eb5

Please sign in to comment.