Skip to content

Commit 5232119

Browse files
authored
Tests now use a Test Logger (#781)
* Start of TestLogger * WIP - Remove existing TestLogger, tests now use a new test logger not tied to the main (winston) logger * wip - logger test migration * wip - test migration * wip - test convergence * wip - test convergence * Impl TestLogger * Fix tests * Comment removal * Code clenaup * Added test messages * Remove initialize call from test * lint fix
1 parent ad0e9f5 commit 5232119

26 files changed

+180
-421
lines changed

src/shared/logger/loggableType.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,7 @@
77
* Loggable types: Error, string
88
*/
99
export type Loggable = Error | string
10+
11+
export function isLoggableError(loggable: Loggable): boolean {
12+
return loggable instanceof Error
13+
}

src/shared/logger/logger.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import { Loggable } from './loggableType'
77

8-
let toolkitLogger: Logger
8+
let toolkitLogger: Logger | undefined
99

1010
export interface Logger {
1111
debug(...message: Loggable[]): void
@@ -18,15 +18,23 @@ export interface Logger {
1818
export type LogLevel = keyof Logger
1919

2020
/**
21-
* Gets the default logger if it has been initialized with the initialize() function
21+
* Gets the logger if it has been initialized
2222
*/
2323
export function getLogger(): Logger {
24-
if (toolkitLogger) {
25-
return toolkitLogger
24+
if (!toolkitLogger) {
25+
throw new Error(
26+
'Logger not initialized. Extension code should call initialize() from shared/logger/activation, test code should call setLogger().'
27+
)
2628
}
27-
throw new Error('Default Logger not initialized. Call logger.initialize() first.')
29+
30+
return toolkitLogger
2831
}
2932

30-
export function setLogger(logger: Logger) {
33+
/**
34+
* Sets (or clears) the logger that is accessible to code.
35+
* The Extension is expected to call this only once.
36+
* Tests should call this to set up a logger prior to executing code that accesses a logger.
37+
*/
38+
export function setLogger(logger: Logger | undefined) {
3139
toolkitLogger = logger
3240
}

src/shared/loggerUtils.ts

Lines changed: 0 additions & 53 deletions
This file was deleted.

src/test/awsExplorer/awsExplorer.test.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import * as assert from 'assert'
77
import { AwsExplorer } from '../../awsexplorer/awsExplorer'
88
import { AwsContextTreeCollection } from '../../shared/awsContextTreeCollection'
9-
import { TestLogger } from '../../shared/loggerUtils'
109
import { RegionNode } from '../../shared/treeview/nodes/regionNode'
1110
import { MockOutputChannel } from '../mockOutputChannel'
1211
import {
@@ -18,16 +17,6 @@ import {
1817
} from '../utilities/fakeAwsContext'
1918

2019
describe('AwsExplorer', () => {
21-
let logger: TestLogger
22-
23-
before(async () => {
24-
logger = await TestLogger.createTestLogger()
25-
})
26-
27-
after(async () => {
28-
await logger.cleanupLogger()
29-
})
30-
3120
it('displays region nodes with user-friendly region names', async () => {
3221
const awsContext = new FakeAwsContext()
3322
const regionProvider = new FakeRegionProvider()

src/test/globalSetup.test.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*!
2+
* Copyright 2018-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
/**
7+
* Before/After hooks for all "unit" tests
8+
*/
9+
10+
import * as assert from 'assert'
11+
import { getLogger } from '../shared/logger'
12+
import { setLogger } from '../shared/logger/logger'
13+
import { TestLogger } from './testLogger'
14+
15+
// Expectation: Tests are not run concurrently
16+
let testLogger: TestLogger | undefined
17+
18+
beforeEach(async () => {
19+
// Set every test up so that TestLogger is the logger used by toolkit code
20+
testLogger = setupTestLogger()
21+
})
22+
23+
afterEach(async () => {
24+
// Prevent other tests from using the same TestLogger instance
25+
teardownTestLogger()
26+
testLogger = undefined
27+
})
28+
29+
/**
30+
* Provides the TestLogger to tests that want to access it.
31+
* Verifies that the TestLogger instance is still the one set as the toolkit's logger.
32+
*/
33+
export function getTestLogger(): TestLogger {
34+
const logger = getLogger()
35+
assert.strictEqual(logger, testLogger, 'The expected test logger is not the current logger')
36+
assert.ok(testLogger, 'TestLogger was expected to exist')
37+
38+
return testLogger!
39+
}
40+
41+
function setupTestLogger(): TestLogger {
42+
const logger = new TestLogger()
43+
setLogger(logger)
44+
45+
return logger
46+
}
47+
48+
function teardownTestLogger() {
49+
setLogger(undefined)
50+
}

src/test/lambda/commands/deploySamApplication.test.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import {
1616
import { SamDeployWizardResponse } from '../../../lambda/wizards/samDeployWizard'
1717
import { AwsContext } from '../../../shared/awsContext'
1818
import { makeTemporaryToolkitFolder } from '../../../shared/filesystemUtilities'
19-
import { TestLogger } from '../../../shared/loggerUtils'
2019
import { RegionInfo } from '../../../shared/regions/regionInfo'
2120
import { RegionProvider } from '../../../shared/regions/regionProvider'
2221
import { SamCliContext } from '../../../shared/sam/cli/samCliContext'
@@ -136,10 +135,8 @@ describe('deploySamApplication', async () => {
136135

137136
const extContext = new FakeExtensionContext()
138137

139-
let logger: TestLogger
140138
let tempToolkitFolder: string
141139
beforeEach(async () => {
142-
logger = await TestLogger.createTestLogger()
143140
channelLogger = new FakeChannelLogger()
144141

145142
tempToolkitFolder = await makeTemporaryToolkitFolder()
@@ -160,7 +157,6 @@ describe('deploySamApplication', async () => {
160157
})
161158

162159
afterEach(async () => {
163-
await logger.cleanupLogger()
164160
await del([tempToolkitFolder], { force: true })
165161
})
166162

@@ -358,9 +354,9 @@ function assertGeneralErrorLogged(channelLogger: FakeChannelLogger) {
358354

359355
function assertErrorLogsContain(text: string, channelLogger: FakeChannelLogger, exactMatch: boolean) {
360356
assert.ok(
361-
channelLogger.logger.errorEntries.some(
362-
e => e instanceof Error && (exactMatch ? e.message === text : e.message.indexOf(text) !== -1)
363-
),
357+
channelLogger.logger
358+
.getLoggedEntries('error')
359+
.some(e => e instanceof Error && (exactMatch ? e.message === text : e.message.indexOf(text) !== -1)),
364360
`Expected to find ${text} in the error logs`
365361
)
366362
}

src/test/lambda/explorer/cloudFormationNodes.test.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import { EcsClient } from '../../../shared/clients/ecsClient'
1818
import { LambdaClient } from '../../../shared/clients/lambdaClient'
1919
import { StsClient } from '../../../shared/clients/stsClient'
2020
import { ext } from '../../../shared/extensionGlobals'
21-
import { TestLogger } from '../../../shared/loggerUtils'
2221
import { RegionInfo } from '../../../shared/regions/regionInfo'
2322
import { ErrorNode } from '../../../shared/treeview/nodes/errorNode'
2423
import { PlaceholderNode } from '../../../shared/treeview/nodes/placeholderNode'
@@ -31,11 +30,9 @@ async function* asyncGenerator<T>(items: T[]): AsyncIterableIterator<T> {
3130

3231
describe('DefaultCloudFormationStackNode', () => {
3332
let fakeStackSummary: CloudFormation.StackSummary
34-
let logger: TestLogger
3533

3634
before(async () => {
3735
setupTestIconPaths()
38-
logger = await TestLogger.createTestLogger()
3936
fakeStackSummary = {
4037
CreationTime: new Date(),
4138
StackId: '1',
@@ -46,7 +43,6 @@ describe('DefaultCloudFormationStackNode', () => {
4643

4744
after(async () => {
4845
clearTestIconPaths()
49-
await logger.cleanupLogger()
5046
})
5147

5248
// Validates we tagged the node correctly.
@@ -213,16 +209,6 @@ describe('DefaultCloudFormationStackNode', () => {
213209
})
214210

215211
describe('DefaultCloudFormationNode', () => {
216-
let logger: TestLogger
217-
218-
before(async () => {
219-
logger = await TestLogger.createTestLogger()
220-
})
221-
222-
after(async () => {
223-
await logger.cleanupLogger()
224-
})
225-
226212
class StackNamesMockCloudFormationClient extends MockCloudFormationClient {
227213
public constructor(
228214
public readonly stackNames: string[] = [],

src/test/lambda/explorer/lambdaNodes.test.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import { EcsClient } from '../../../shared/clients/ecsClient'
1717
import { LambdaClient } from '../../../shared/clients/lambdaClient'
1818
import { StsClient } from '../../../shared/clients/stsClient'
1919
import { ext } from '../../../shared/extensionGlobals'
20-
import { TestLogger } from '../../../shared/loggerUtils'
2120
import { RegionInfo } from '../../../shared/regions/regionInfo'
2221
import { ErrorNode } from '../../../shared/treeview/nodes/errorNode'
2322
import { MockLambdaClient } from '../../shared/clients/mockClients'
@@ -30,11 +29,9 @@ async function* asyncGenerator<T>(items: T[]): AsyncIterableIterator<T> {
3029

3130
describe('DefaultLambdaFunctionNode', () => {
3231
let fakeFunctionConfig: Lambda.FunctionConfiguration
33-
let logger: TestLogger
3432

3533
before(async () => {
3634
setupTestIconPaths()
37-
logger = await TestLogger.createTestLogger()
3835
fakeFunctionConfig = {
3936
FunctionName: 'testFunctionName',
4037
FunctionArn: 'testFunctionARN'
@@ -43,7 +40,6 @@ describe('DefaultLambdaFunctionNode', () => {
4340

4441
after(async () => {
4542
clearTestIconPaths()
46-
await logger.cleanupLogger()
4743
})
4844

4945
// Validates we tagged the node correctly
@@ -92,16 +88,6 @@ describe('DefaultLambdaFunctionNode', () => {
9288
})
9389

9490
describe('DefaultLambdaFunctionGroupNode', () => {
95-
let logger: TestLogger
96-
97-
before(async () => {
98-
logger = await TestLogger.createTestLogger()
99-
})
100-
101-
after(async () => {
102-
await logger.cleanupLogger()
103-
})
104-
10591
class FunctionNamesMockLambdaClient extends MockLambdaClient {
10692
public constructor(
10793
public readonly functionNames: string[] = [],

src/test/shared/codelens/localLambdaRunner.test.ts

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -11,57 +11,15 @@ import { DebugConfiguration } from '../../../lambda/local/debugConfiguration'
1111
import * as localLambdaRunner from '../../../shared/codelens/localLambdaRunner'
1212
import * as fs from '../../../shared/filesystem'
1313
import * as fsUtils from '../../../shared/filesystemUtilities'
14-
import { Loggable, Logger } from '../../../shared/logger'
15-
import { TestLogger } from '../../../shared/loggerUtils'
1614
import { ChildProcessResult } from '../../../shared/utilities/childProcess'
1715
import { ExtensionDisposableFiles } from '../../../shared/utilities/disposableFiles'
18-
import { ChannelLogger } from '../../../shared/utilities/vsCodeUtils'
1916
import { FakeExtensionContext } from '../../fakeExtensionContext'
17+
import { FakeChannelLogger } from '../fakeChannelLogger'
2018
import { assertRejects } from '../utilities/assertUtils'
2119

22-
class FakeChannelLogger implements Pick<ChannelLogger, 'info' | 'error' | 'logger'> {
23-
public readonly loggedInfoKeys: Set<string> = new Set<string>()
24-
public readonly loggedErrorKeys: Set<string> = new Set<string>()
25-
public readonly logger: FakeLogger = new FakeLogger()
26-
27-
public info(nlsKey: string, nlsTemplate: string, ...templateTokens: Loggable[]): void {
28-
this.loggedInfoKeys.add(nlsKey)
29-
}
30-
31-
public error(nlsKey: string, nlsTemplate: string, ...templateTokens: Loggable[]): void {
32-
this.loggedErrorKeys.add(nlsKey)
33-
}
34-
}
35-
36-
class FakeLogger implements Logger {
37-
public readonly loggedDebugEntries: Loggable[] = []
38-
39-
public debug(...message: Loggable[]): void {
40-
this.loggedDebugEntries.push(...message)
41-
}
42-
43-
public verbose(...message: Loggable[]): void {
44-
throw new Error('verbose() not used')
45-
}
46-
47-
public info(...message: Loggable[]): void {
48-
throw new Error('info() not used')
49-
}
50-
51-
public warn(...message: Loggable[]): void {
52-
throw new Error('warn() not used')
53-
}
54-
55-
public error(...message: Loggable[]): void {
56-
throw new Error('error() not used')
57-
}
58-
}
59-
6020
describe('localLambdaRunner', async () => {
61-
let logger: TestLogger
6221
let tempDir: string
6322
before(async () => {
64-
logger = await TestLogger.createTestLogger()
6523
await ExtensionDisposableFiles.initialize(new FakeExtensionContext())
6624
})
6725

@@ -73,10 +31,6 @@ describe('localLambdaRunner', async () => {
7331
await del(tempDir, { force: true })
7432
})
7533

76-
after(async () => {
77-
await logger.cleanupLogger()
78-
})
79-
8034
describe('attachDebugger', async () => {
8135
let actualRetries: number = 0
8236
let channelLogger: FakeChannelLogger

0 commit comments

Comments
 (0)