Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
go-nexus-telemetry
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Public
go-nexus-telemetry
Commits
212034e8
Commit
212034e8
authored
1 year ago
by
Jack Robertson
Browse files
Options
Downloads
Patches
Plain Diff
test: add NewLoggerWithLeveler test
parent
7ff5003d
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
logger_test.go
+41
-1
41 additions, 1 deletion
logger_test.go
with
41 additions
and
1 deletion
logger_test.go
+
41
−
1
View file @
212034e8
...
@@ -17,7 +17,7 @@ type log struct {
...
@@ -17,7 +17,7 @@ type log struct {
Source
slog
.
Source
`json:"source"`
Source
slog
.
Source
`json:"source"`
}
}
func
TestNew
(
t
*
testing
.
T
)
{
func
TestNew
Logger
(
t
*
testing
.
T
)
{
// Arrange
// Arrange
var
buf
bytes
.
Buffer
var
buf
bytes
.
Buffer
...
@@ -70,3 +70,43 @@ func TestNew(t *testing.T) {
...
@@ -70,3 +70,43 @@ func TestNew(t *testing.T) {
assert
.
False
(
t
,
scanner
.
Scan
())
assert
.
False
(
t
,
scanner
.
Scan
())
assert
.
NoError
(
t
,
scanner
.
Err
())
assert
.
NoError
(
t
,
scanner
.
Err
())
}
}
func
TestNewLoggerWithLeveler
(
t
*
testing
.
T
)
{
// Arrange
var
buf
bytes
.
Buffer
programLevel
:=
new
(
slog
.
LevelVar
)
programLevel
.
Set
(
slog
.
LevelError
)
logger
:=
NewLogger
(
WithWriter
(
&
buf
),
WithLeveler
(
programLevel
))
// Act
logger
.
Warn
(
"Never gonna give"
,
slog
.
String
(
"you"
,
"up"
))
programLevel
.
Set
(
slog
.
LevelWarn
)
logger
.
Warn
(
"Never gonna let"
,
slog
.
String
(
"you"
,
"down"
))
// Assert
scanner
:=
bufio
.
NewScanner
(
&
buf
)
// Scan the first log
assert
.
True
(
t
,
scanner
.
Scan
())
var
(
rawWarnLog
=
scanner
.
Bytes
()
warnLog
=
struct
{
log
You
string
`json:"you"`
}{}
)
if
err
:=
json
.
Unmarshal
(
rawWarnLog
,
&
warnLog
);
err
!=
nil
{
t
.
Errorf
(
"Failed to unmarshal warn log: %s"
,
err
)
}
assert
.
Equal
(
t
,
"warn"
,
warnLog
.
Status
)
assert
.
Equal
(
t
,
"Never gonna let"
,
warnLog
.
Message
)
assert
.
Equal
(
t
,
"down"
,
warnLog
.
You
)
assert
.
WithinDuration
(
t
,
time
.
Now
(),
warnLog
.
Date
,
1
*
time
.
Second
)
// Assert that there are no more logs
assert
.
False
(
t
,
scanner
.
Scan
())
assert
.
NoError
(
t
,
scanner
.
Err
())
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment