Skip to content

Commit

Permalink
feat: [NA] handle config print in case of nil processor instance (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
vpakhuchyi committed Jan 24, 2024
1 parent b6327cf commit f00612a
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 10 deletions.
38 changes: 28 additions & 10 deletions processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,18 +101,35 @@ func (p *Processor) Format(val any) string {
}

// PrintConfig prints the configuration of the censor Processor.
//
//nolint:gomnd
func (p *Processor) PrintConfig() {
const (
lineLength = 69
padLength = 10
)

line := strings.Repeat("-", lineLength) + "\n"
const lineLength = 69

var b strings.Builder
b.WriteString(line)
b.WriteString(strings.Repeat(" ", padLength) + "Censor is configured with the following settings:" + "\n")
b.WriteString(line)

writeLine := func() {
b.WriteString(strings.Repeat("-", lineLength) + "\n")
}

// Handle the case when the censor instance isn't initialized.
if p == nil {
const text = "Censor instance isn't initialized"

writeLine()
b.WriteString(strings.Repeat(" ", (lineLength-len(text))/2) + text + "\n")
writeLine()

fmt.Print(b.String())

return
}

const text = "Censor is configured with the following settings:"

writeLine()
b.WriteString(strings.Repeat(" ", (lineLength-len(text))/2) + text + "\n")
writeLine()

cfg := config.Config{
General: p.cfg.General,
Expand All @@ -131,7 +148,8 @@ func (p *Processor) PrintConfig() {

b.Write(d)

b.WriteString(line)
writeLine()

fmt.Print(b.String())
}

Expand Down
23 changes: 23 additions & 0 deletions processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -808,4 +808,27 @@ func TestProcessor_PrintConfig(t *testing.T) {
require.NoError(t, err)
require.Equal(t, want, got)
})

t.Run("not initialized instance", func(t *testing.T) {
r, w, err := os.Pipe()
require.NoError(t, err)

// Store previous stdout and replace it with our pipe.
stdout := os.Stdout
os.Stdout = w

var p *Processor
p.PrintConfig()

// Restore stdout.
os.Stdout = stdout

want := []byte{0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d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 0x2d, 0xa}

// Read from the pipe.
got := make([]byte, 192)
_, err = r.Read(got)
require.NoError(t, err)
require.Equal(t, want, got)
})
}

0 comments on commit f00612a

Please sign in to comment.