-
Notifications
You must be signed in to change notification settings - Fork 485
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(logger): test logger does not format objects #6083
## Problem The test logger does not format additional arguements into the logs in the same way as ToolkitLogger. Ex. `getLogger().debug('here is the obj: %O', obj)` will produce two `LogEntries` one with `"here is the obj: %O"` and one with the object itself. This is problematic because it causes confusion (see: #5895 (comment)) and it also can cause `assertLogsContain` to throw an error since there is now a non-string/error entry in the log entry list (see: https://github.com/aws/aws-toolkit-vscode/blob/338ea67f2ba0294fc535a9a949fd1cdaeaa96d98/packages/core/src/test/globalSetup.test.ts#L171-L185). ## Solution - Have test logger inherit from `BaseLogger` to minimize implementation dupe. - If we see a string, format it with extra inputs before pushing it into entries. - Add tests for the `testLogger`. - Adjust existing tests that relied on this behavior. - If on web, we simply concat the strings to avoid reimplementing `util.format`: https://github.com/nodejs/node/blob/3178a762d6a2b1a37b74f02266eea0f3d86603be/lib/internal/util/inspect.js#L2191-L2315 --- <!--- REMINDER: Ensure that your PR meets the guidelines in CONTRIBUTING.md --> License: I confirm that my contribution is made under the terms of the Apache 2.0 license.
- Loading branch information
1 parent
0eb9012
commit 2b6987a
Showing
7 changed files
with
97 additions
and
49 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
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,61 @@ | ||
/*! | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import assert from 'assert' | ||
import { getLogger } from '../shared' | ||
import { assertLogsContain, getTestLogger } from './globalSetup.test' | ||
|
||
describe('TestLogger', function () { | ||
describe('assertLogsContain', function () { | ||
it('checks only at specified log level', function () { | ||
const logger = getLogger() | ||
logger.info('here is some info') | ||
logger.debug('here is some debug') | ||
|
||
assertLogsContain('here is some info', true, 'info') | ||
assert.throws(() => assertLogsContain('here is some info', true, 'debug')) | ||
|
||
assertLogsContain('here is some debug', true, 'debug') | ||
assert.throws(() => assertLogsContain('here is some debug', true, 'info')) | ||
}) | ||
|
||
it('only requires substring without exactMatch=true', function () { | ||
const logger = getLogger() | ||
logger.info('here is some info') | ||
logger.debug('here is some debug') | ||
|
||
assertLogsContain('some info', false, 'info') | ||
assertLogsContain('some debug', false, 'debug') | ||
}) | ||
}) | ||
|
||
it('formats objects into logs', function () { | ||
const testObj = { | ||
info: 'some info', | ||
} | ||
|
||
getLogger().debug('here is my testObj: %O', testObj) | ||
assertLogsContain(`here is my testObj: { info: 'some info' }`, true, 'debug') | ||
}) | ||
|
||
it('has one logging entry for each logging statement', function () { | ||
const logger = getLogger() | ||
const startingEntries = getTestLogger().getLoggedEntries().length | ||
logger.info('here is some info %O', { info: 'this is info' }) | ||
logger.debug('here is some debug %O', { debug: 'this is debug' }) | ||
assert.strictEqual(getTestLogger().getLoggedEntries().length - startingEntries, 2) | ||
}) | ||
|
||
it('returns entry number on each log statement', function () { | ||
const logger = getLogger() | ||
const startingEntryNumber = getTestLogger().getLoggedEntries().length | ||
const entry1 = logger.info('here is some info %O', { info: 'this is info' }) | ||
const entry2 = logger.debug('here is some debug %O', { debug: 'this is debug' }) | ||
const entry3 = logger.debug('here is some warn %O', { warn: 'this is warn' }) | ||
assert.strictEqual(entry1, startingEntryNumber) | ||
assert.strictEqual(entry2, startingEntryNumber + 1) | ||
assert.strictEqual(entry3, startingEntryNumber + 2) | ||
}) | ||
}) |
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