Skip to content

Commit

Permalink
feat: add SetOutput for easy testing in caller side
Browse files Browse the repository at this point in the history
  • Loading branch information
bcho committed Feb 8, 2023
1 parent f0df3ec commit 903f5ec
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 10 deletions.
4 changes: 4 additions & 0 deletions cilog/ado.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ func AzurePipeline(opts ...AzurePipelineOpts) Logger {
return rv
}

func (apt *azurePipelineT) SetOutput(out io.Writer) {
apt.out = out
}

func (apt *azurePipelineT) Capabilities() map[Capability]struct{} {
return map[Capability]struct{}{
CapabilityLog: {},
Expand Down
5 changes: 2 additions & 3 deletions cilog/ado_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ import (

func TestAzurePipeline(t *testing.T) {
b := new(bytes.Buffer)
apt := AzurePipeline(applyOptsFunc[azurePipelineT](func(apt *azurePipelineT) {
apt.out = b
}))
apt := AzurePipeline()
apt.SetOutput(b)

caps := apt.Capabilities()
assert.Contains(t, caps, CapabilityLog)
Expand Down
4 changes: 4 additions & 0 deletions cilog/generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ func generic(opts ...applyOpts[genericT]) Logger {

var _ Logger = (*genericT)(nil)

func (gt *genericT) SetOutput(out io.Writer) {
gt.out = out
}

func (gt *genericT) Capabilities() map[Capability]struct{} {
return map[Capability]struct{}{
CapabilityLog: {},
Expand Down
5 changes: 2 additions & 3 deletions cilog/generic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ import (

func TestGeneric(t *testing.T) {
b := new(bytes.Buffer)
gt := generic(applyOptsFunc[genericT](func(gt *genericT) {
gt.out = b
}))
gt := generic()
gt.SetOutput(b)

caps := gt.Capabilities()
assert.Contains(t, caps, CapabilityLog)
Expand Down
4 changes: 4 additions & 0 deletions cilog/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ func GitHubActions(opts ...GitHubOpts) Logger {
return rv
}

func (gat *githubActionsT) SetOutput(out io.Writer) {
gat.out = out
}

func (gat *githubActionsT) Capabilities() map[Capability]struct{} {
return map[Capability]struct{}{
CapabilityLog: {},
Expand Down
5 changes: 2 additions & 3 deletions cilog/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ import (

func TestGitHubActions(t *testing.T) {
b := new(bytes.Buffer)
gat := GitHubActions(applyOptsFunc[githubActionsT](func(gat *githubActionsT) {
gat.out = b
}))
gat := GitHubActions()
gat.SetOutput(b)

caps := gat.Capabilities()
assert.Contains(t, caps, CapabilityLog)
Expand Down
10 changes: 9 additions & 1 deletion cilog/logger.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package cilog

import "github.com/b4fun/ci"
import (
"io"

"github.com/b4fun/ci"
)

type Capability int

Expand Down Expand Up @@ -35,6 +39,10 @@ type GroupLogParams struct {

// Logger provides improved CI logging output.
type Logger interface {
// SetOutput sets the output of the logger.
// This is useful unit testing usage.
SetOutput(io.Writer)

// Capabilities returns the capabilities of the logger.
Capabilities() map[Capability]struct{}

Expand Down
4 changes: 4 additions & 0 deletions cilog/mute.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package cilog

import "io"

// Mute implements all Logger methods by muting output.
type Mute struct {
}

var _ Logger = (*Mute)(nil)

func (m *Mute) SetOutput(o io.Writer) {}

func (m *Mute) Capabilities() map[Capability]struct{} {
return map[Capability]struct{}{}
}
Expand Down

0 comments on commit 903f5ec

Please sign in to comment.