Skip to content

Commit 273654d

Browse files
committed
Adding unit test for debug session logger
1 parent 7bb8308 commit 273654d

File tree

3 files changed

+138
-2
lines changed

3 files changed

+138
-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: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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.ProtocolMessage {
62+
public seq: number;
63+
public type: string;
64+
public id: number;
65+
public format: string;
66+
public variables?: { [key: string]: string } | undefined;
67+
public sendTelemetry?: boolean | undefined;
68+
public showUser?: boolean | undefined;
69+
public url?: string | undefined;
70+
public urlLabel?: string | undefined;
71+
constructor(id: number, seq: number, type: string) {
72+
this.id = id;
73+
this.format = 'json';
74+
this.seq = seq;
75+
this.type = type;
76+
}
77+
}
78+
79+
test('Create logger using session without logToFile', async () => {
80+
const session = createSession('test1');
81+
const filePath = path.join(EXTENSION_ROOT_DIR, `debugger.vscode_${session.id}.log`);
82+
83+
loggerFactory.createDebugAdapterTracker(session);
84+
85+
verify(fsService.createWriteStream(filePath)).never();
86+
});
87+
88+
test('Create logger using session with logToFile set to false', async () => {
89+
const session = createSessionWithLogging('test2', false);
90+
const filePath = path.join(EXTENSION_ROOT_DIR, `debugger.vscode_${session.id}.log`);
91+
92+
loggerFactory.createDebugAdapterTracker(session);
93+
94+
verify(fsService.createWriteStream(filePath)).never();
95+
});
96+
97+
test('Create logger using session with logToFile set to true', async () => {
98+
const session = createSessionWithLogging('test3', true);
99+
const filePath = path.join(EXTENSION_ROOT_DIR, `debugger.vscode_${session.id}.log`);
100+
const logs: string[] = [];
101+
102+
when(fsService.createWriteStream(filePath)).thenReturn(instance(writeStream));
103+
when(writeStream.write(anything())).thenCall((msg) => logs.push(msg));
104+
105+
const message = new TestMessage(1, 1, 'test-message');
106+
const logger = await loggerFactory.createDebugAdapterTracker(session);
107+
108+
if (logger) {
109+
logger.onWillStartSession!();
110+
assert.ok(logs.pop()!.includes('Starting Session'));
111+
112+
logger.onDidSendMessage!(message);
113+
const sentMessage = logs.pop();
114+
assert.ok(sentMessage!.includes('Client --> Adapter'));
115+
assert.ok(sentMessage!.includes('test-message'));
116+
117+
logger.onWillReceiveMessage!(message);
118+
const receivedMessage = logs.pop();
119+
assert.ok(receivedMessage!.includes('Client <-- Adapter'));
120+
assert.ok(receivedMessage!.includes('test-message'));
121+
122+
logger.onWillStopSession!();
123+
assert.ok(logs.pop()!.includes('Stopping Session'));
124+
125+
logger.onError!(new Error('test error message'));
126+
assert.ok(logs.pop()!.includes('Error'));
127+
128+
logger.onExit!(0, undefined);
129+
assert.ok(logs.pop()!.includes('Exit'));
130+
}
131+
132+
verify(fsService.createWriteStream(filePath)).once();
133+
verify(writeStream.write(anything())).times(6);
134+
assert.deepEqual(logs, []);
135+
});
136+
});

0 commit comments

Comments
 (0)