fix: HavePatternsMatcher reports actual content instead of Go type for io.Reader inputs#1
Closed
fix: HavePatternsMatcher reports actual content instead of Go type for io.Reader inputs#1
Conversation
…r io.Reader inputs
When a Command stdout or File contents check failed pattern matching, the
FailureResult and NegatedFailureResult methods rendered the actual value as
"object: *bytes.Reader" (or *os.File, *strings.Reader) instead of showing
the actual content that was read from the io.Reader.
This happened because the default branch in the type switch used
fmt.Sprintf("object: %T", actual) for any non-string/non-[]string type,
which includes all io.Reader inputs (the primary path for Command and File
resource outputs).
The fix caches the lines read during Match() and uses the cached content
in the failure result output. This ensures that:
- Failed pattern matches show the actual command/file output
- Operators can see what content was tested against their patterns
- No other matchers are affected (only HavePatternsMatcher had this issue)
- All non-Reader paths (string, []string) remain untouched
The existing test suite explicitly tests io.Reader inputs (basic_reader test
case). The test fixtures previously expected "object: *strings.Reader" as
the output, confirming this was a known-but-unaddressed bug. The fixtures
now expect the actual content.
Owner
Author
|
Closing -- duplicate of goss-org#1055 which takes a cleaner approach by fixing at the validate.go level. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
HavePatternsMatcher.FailureResult()andNegatedFailureResult()renderio.Readerinputs as their Go type string (e.g."object: *bytes.Reader","object: *os.File") instead of the actual content read from the reader.This affects every Command
stdoutand Filecontentscheck that uses pattern matching -- when a pattern fails, the output is useless for debugging because it shows the type instead of what was actually read.Root Cause
The
defaultbranch in the type switch used:for any type that isn't
stringor[]string. Since Command and File resources pass their output asio.Reader, the actual content was never displayed.Fix
Cache the lines read during
Match()and use the cached content in failure result output. This ensures:HavePatternsMatcherhad this issuestring,[]string) remain untouchedbasic_readertest case now expects actual content instead of"object: *strings.Reader"Evidence
Tested against CIS benchmark gossfiles on both Linux and Windows:
object: *bytes.ReaderoccurrencesThe upstream binary also returned significantly fewer results on Windows (155 vs 881) suggesting the broken output may cause downstream parsing failures.
Test Fixtures Updated
The
basic_readertest fixtures previously expected"object: *strings.Reader"-- confirming this was a known-but-unaddressed bug. Updated to expect the actual content ("foo bar\nmoo cow").