Skip to content

Commit 4942ecb

Browse files
committed
Adding unit test for debug session logger
1 parent 7bb8308 commit 4942ecb

File tree

3 files changed

+130
-2
lines changed

3 files changed

+130
-2
lines changed

src/client/debugger/extension/adapter/logging.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class DebugSessionLoggingTracker implements DebugAdapterTracker {
2929

3030
public onWillStartSession() {
3131
this.timer.reset();
32-
this.log(`Stopping Session:\n${this.stringify(this.session.configuration)}\n`);
32+
this.log(`Starting Session:\n${this.stringify(this.session.configuration)}\n`);
3333
}
3434

3535
public onWillReceiveMessage(message: DebugProtocol.Message) {

src/test/debugger/extension/adapter/activator.unit.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import { noop } from '../../../core';
3030
import { MockOutputChannel } from '../../../mockClasses';
3131

3232
// tslint:disable-next-line: max-func-body-length
33-
suite('Debugging - Adapter Factory Registration', () => {
33+
suite('Debugging - Adapter Factory and logger Registration', () => {
3434
const oldValueOfVSC_PYTHON_UNIT_TEST = process.env.VSC_PYTHON_UNIT_TEST;
3535
const oldValueOfVSC_PYTHON_CI_TEST = process.env.VSC_PYTHON_CI_TEST;
3636
let activator: IExtensionSingleActivationService;
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
'use strict';
5+
6+
import * as assert from 'assert';
7+
import * as fs from 'fs';
8+
import * as path from 'path';
9+
import { anything, instance, mock, verify, when } from 'ts-mockito';
10+
import { DebugSession, WorkspaceFolder } from 'vscode';
11+
import { DebugProtocol } from 'vscode-debugprotocol';
12+
13+
import { FileSystem } from '../../../../client/common/platform/fileSystem';
14+
import { EXTENSION_ROOT_DIR } from '../../../../client/constants';
15+
import { DebugSessionLoggingFactory } from '../../../../client/debugger/extension/adapter/logging';
16+
17+
// tslint:disable-next-line: max-func-body-length
18+
suite('Debugging - Session Logging', () => {
19+
const oldValueOfVSC_PYTHON_UNIT_TEST = process.env.VSC_PYTHON_UNIT_TEST;
20+
const oldValueOfVSC_PYTHON_CI_TEST = process.env.VSC_PYTHON_CI_TEST;
21+
let loggerFactory: DebugSessionLoggingFactory;
22+
let fsService: FileSystem;
23+
let writeStream: fs.WriteStream;
24+
25+
setup(() => {
26+
fsService = mock(FileSystem);
27+
writeStream = mock(fs.WriteStream);
28+
29+
process.env.VSC_PYTHON_UNIT_TEST = undefined;
30+
process.env.VSC_PYTHON_CI_TEST = undefined;
31+
32+
loggerFactory = new DebugSessionLoggingFactory(instance(fsService));
33+
});
34+
35+
teardown(() => {
36+
process.env.VSC_PYTHON_UNIT_TEST = oldValueOfVSC_PYTHON_UNIT_TEST;
37+
process.env.VSC_PYTHON_CI_TEST = oldValueOfVSC_PYTHON_CI_TEST;
38+
});
39+
40+
function createSession(id: string, workspaceFolder?: WorkspaceFolder): DebugSession {
41+
return {
42+
configuration: {
43+
name: '',
44+
request: 'launch',
45+
type: 'python'
46+
},
47+
id: id,
48+
name: 'python',
49+
type: 'python',
50+
workspaceFolder,
51+
customRequest: () => Promise.resolve()
52+
};
53+
}
54+
55+
function createSessionWithLogging(id: string, logToFile: boolean, workspaceFolder?: WorkspaceFolder): DebugSession {
56+
const session = createSession(id, workspaceFolder);
57+
session.configuration.logToFile = logToFile;
58+
return session;
59+
}
60+
61+
class TestMessage implements DebugProtocol.Message {
62+
public id: number;
63+
public format: string;
64+
public variables?: { [key: string]: string } | undefined;
65+
public sendTelemetry?: boolean | undefined;
66+
public showUser?: boolean | undefined;
67+
public url?: string | undefined;
68+
public urlLabel?: string | undefined;
69+
constructor(id: number, format: string) {
70+
this.id = id;
71+
this.format = format;
72+
}
73+
}
74+
75+
test('Create logger using session without logToFile', async () => {
76+
const session = createSession('test1');
77+
const filePath = path.join(EXTENSION_ROOT_DIR, `debugger.vscode_${session.id}.log`);
78+
79+
loggerFactory.createDebugAdapterTracker(session);
80+
81+
verify(fsService.createWriteStream(filePath)).never();
82+
});
83+
84+
test('Create logger using session with logToFile set to false', async () => {
85+
const session = createSessionWithLogging('test2', false);
86+
const filePath = path.join(EXTENSION_ROOT_DIR, `debugger.vscode_${session.id}.log`);
87+
88+
loggerFactory.createDebugAdapterTracker(session);
89+
90+
verify(fsService.createWriteStream(filePath)).never();
91+
});
92+
93+
test('Create logger using session with logToFile set to true', async () => {
94+
const session = createSessionWithLogging('test3', true);
95+
const filePath = path.join(EXTENSION_ROOT_DIR, `debugger.vscode_${session.id}.log`);
96+
const logs: string[] = [];
97+
98+
when(fsService.createWriteStream(filePath)).thenReturn(writeStream);
99+
when(writeStream.write(anything())).thenCall((msg) => logs.push(msg));
100+
101+
const message = new TestMessage(1, '');
102+
const logger = await loggerFactory.createDebugAdapterTracker(session);
103+
104+
if (logger) {
105+
logger!.onWillStartSession();
106+
assert.ok(logs.pop()!.includes('Starting Session'));
107+
108+
logger!.onDidSendMessage(message);
109+
assert.ok(logs.pop()!.includes('Client --> Adapter'));
110+
111+
logger!.onWillReceiveMessage(message);
112+
assert.ok(logs.pop()!.includes('Client <-- Adapter'));
113+
114+
logger!.onWillStopSession();
115+
assert.ok(logs.pop()!.includes('Stopping Session'));
116+
117+
logger!.onError(new Error('test error message'));
118+
assert.ok(logs.pop()!.includes('Error'));
119+
120+
logger!.onExit(0, undefined);
121+
assert.ok(logs.pop()!.includes('Exit'));
122+
}
123+
124+
verify(fsService.createWriteStream(filePath)).once();
125+
verify(writeStream.write(anything())).times(6);
126+
assert.deepEqual(logs, []);
127+
});
128+
});

0 commit comments

Comments
 (0)