Skip to content

Commit

Permalink
Merge pull request #269 from atc0005/i267-fix-setoutputtarget-arg-han…
Browse files Browse the repository at this point in the history
…dling-dev-branch

Fix `Plugin.SetOutputTarget` method
  • Loading branch information
atc0005 authored Oct 17, 2024
2 parents 0af2aa7 + df5e49a commit f83b810
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
5 changes: 4 additions & 1 deletion nagios.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,11 +467,14 @@ func (p *Plugin) AddUniqueError(errs ...error) {
}

// SetOutputTarget assigns a target for Nagios plugin output. By default
// output is emitted to os.Stdout.
// output is emitted to os.Stdout. If given an invalid output target the
// default output target will be used instead.
func (p *Plugin) SetOutputTarget(w io.Writer) {
// Guard against potential nil argument.
if w == nil {
p.outputSink = os.Stdout

return
}

p.outputSink = w
Expand Down
54 changes: 54 additions & 0 deletions unexported_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ package nagios
import (
_ "embed"
"fmt"
"os"
"strings"
"testing"

Expand Down Expand Up @@ -48,6 +49,59 @@ const (
// smallPlaintextPayloadUnencoded string = "Hello, World!"
)

func TestPluginSetOutputTargetIsValidWithValidInput(t *testing.T) {
t.Parallel()

plugin := NewPlugin()

// Assert that plugin.outputSink is still unset
if plugin.outputSink != nil {
t.Fatal("ERROR: plugin outputSink is not at the expected default unset value.")
} else {
t.Log("OK: plugin outputSink is at the expected default unset value.")
}

var outputBuffer strings.Builder

plugin.SetOutputTarget(&outputBuffer)

// Assert that plugin.outputSink is set as expected.
switch {
case plugin.outputSink == nil:
t.Fatal("ERROR: plugin outputSink is unset instead of the given custom value.")
case plugin.outputSink == os.Stdout:
t.Fatal("ERROR: plugin outputSink is set to the default/fallback value instead of the expected custom value.")
default:
t.Log("OK: plugin outputSink is at the expected custom value.")
}
}

// TestPluginSetOutputTargetIsValidWithInvalidInput asserts that when given an
// invalid output target that the method falls back to a safe default value.
func TestPluginSetOutputTargetIsValidWithInvalidInput(t *testing.T) {
t.Parallel()

plugin := NewPlugin()

// Assert that plugin.outputSink is still unset
if plugin.outputSink != nil {
t.Fatal("ERROR: plugin outputSink is not at the expected default unset value.")
} else {
t.Log("OK: plugin outputSink is at the expected default unset value.")
}

t.Log("Attempting to set invalid output target. This should cause the default output sink to be set instead.")
plugin.SetOutputTarget(nil)

// Assert that plugin.outputSink is set to a non-nil default/fallback
// value as expected.
if plugin.outputSink == nil {
t.Fatal("ERROR: plugin outputSink is not at the expected default/fallback value.")
} else {
t.Log("OK: plugin outputSink is at the expected default/fallback value.")
}
}

// TestServiceOutputIsNotInterpolated is intended to prevent further
// regressions of formatting being applied to literal/preformatted Service
// Output (aka, "one-line summary" output).
Expand Down

0 comments on commit f83b810

Please sign in to comment.