-
Notifications
You must be signed in to change notification settings - Fork 296
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
log: Add shared Spy implementation (#692)
This consolidates the multiple Spy fx.Printer implementations into one. The previous implementations operated by wrapping a Buffer and accessing the result with `String()`. The methods used by us were, Reset() String() string The new Spy implementation exposes both these methods, as well as a `Messages() []string` method which exposes all logged messages separately, which allows for a more accurate way to verify the logged entries.
- Loading branch information
Showing
4 changed files
with
134 additions
and
29 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright (c) 2020 Uber Technologies, Inc. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
|
||
package fxlog | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
// Spy is an Fx Printer that captures logged statements. It may be used in | ||
// tests of Fx logs. | ||
type Spy struct { | ||
msgs []string | ||
} | ||
|
||
var _ Printer = &Spy{} | ||
|
||
// Printf logs the given message, formatting it in printf-style. | ||
func (s *Spy) Printf(msg string, args ...interface{}) { | ||
s.msgs = append(s.msgs, fmt.Sprintf(msg, args...)) | ||
} | ||
|
||
// Messages returns a copy of all captured messages. | ||
func (s *Spy) Messages() []string { | ||
msgs := make([]string, len(s.msgs)) | ||
copy(msgs, s.msgs) | ||
return msgs | ||
} | ||
|
||
// String returns all logged messages as a single newline-delimited string. | ||
func (s *Spy) String() string { | ||
if len(s.msgs) == 0 { | ||
return "" | ||
} | ||
|
||
// trailing newline because all log entries should have a newline | ||
// after them. | ||
return strings.Join(s.msgs, "\n") + "\n" | ||
} | ||
|
||
// Reset clears all messages from the Spy. | ||
func (s *Spy) Reset() { | ||
s.msgs = s.msgs[:0] | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Copyright (c) 2020 Uber Technologies, Inc. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
|
||
package fxlog | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestSpy(t *testing.T) { | ||
var s Spy | ||
|
||
t.Run("empty spy", func(t *testing.T) { | ||
assert.Empty(t, s.Messages(), "messages must be empty") | ||
assert.Empty(t, s.String(), "string must be empty") | ||
}) | ||
|
||
s.Printf("foo bar") | ||
t.Run("unformatted message", func(t *testing.T) { | ||
assert.Equal(t, []string{"foo bar"}, s.Messages(), "messages must match") | ||
assert.Equal(t, "foo bar\n", s.String(), "string must match") | ||
}) | ||
|
||
s.Printf("something went wrong: %v", errors.New("great sadness")) | ||
t.Run("formatted message", func(t *testing.T) { | ||
assert.Equal(t, []string{ | ||
"foo bar", | ||
"something went wrong: great sadness", | ||
}, s.Messages()) | ||
assert.Equal(t, "foo bar\nsomething went wrong: great sadness\n", s.String()) | ||
}) | ||
|
||
s.Reset() | ||
t.Run("reset", func(t *testing.T) { | ||
assert.Empty(t, s.Messages(), "messages must be empty") | ||
assert.Empty(t, s.String(), "string must be empty") | ||
}) | ||
|
||
s.Printf("baz qux") | ||
t.Run("use after reset", func(t *testing.T) { | ||
assert.Equal(t, []string{"baz qux"}, s.Messages(), "messages must match") | ||
assert.Equal(t, "baz qux\n", s.String(), "string must match") | ||
}) | ||
} |