Skip to content

Commit 2b3b740

Browse files
authored
Merge pull request #1122 from JL03-Yue/JL03-Yue/error-issue
fix logging issue and add a logging unit test
2 parents efcba6b + 84e0e52 commit 2b3b740

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

vscode-dotnet-runtime-library/src/EventStream/LoggingObserver.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { IEvent } from './IEvent';
77
import { ILoggingObserver } from './ILoggingObserver';
88

99
export class LoggingObserver implements ILoggingObserver {
10-
private readonly log: string[] = [];
10+
private log: string[] = [];
1111

1212
constructor(private readonly logFilePath: string) {}
1313

@@ -31,6 +31,6 @@ export class LoggingObserver implements ILoggingObserver {
3131
}
3232

3333
private writeLine(line: string) {
34-
this.log.concat(line);
34+
this.log.push(line);
3535
}
3636
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/* --------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
* ------------------------------------------------------------------------------------------ */
5+
import * as chai from 'chai';
6+
import * as fs from 'fs';
7+
import * as path from 'path';
8+
import { LoggingObserver } from '../../EventStream/LoggingObserver';
9+
import { MockEventStream } from '../mocks/MockObjects';
10+
import { DotnetUninstallAllStarted } from '../../EventStream/EventStreamEvents';
11+
const assert = chai.assert;
12+
13+
suite('LoggingObserver Unit Tests', () => {
14+
const eventStream = new MockEventStream();
15+
const tempPath = path.join(__dirname, `${ new Date().getTime()}` );
16+
17+
test('Log file is writing output', async () => {
18+
// Create an empty folder
19+
if (!fs.existsSync(tempPath)) {
20+
fs.mkdirSync(tempPath);
21+
}
22+
// Create a logging observer
23+
const loggingObserver = new LoggingObserver(path.join(tempPath, 'logTest.txt'));
24+
25+
// Create a fake event and call the post/dispose function
26+
const fakeEvent = new DotnetUninstallAllStarted();
27+
loggingObserver.post(fakeEvent);
28+
loggingObserver.dispose();
29+
30+
// Check if the log file content is same as expected content
31+
fs.readdirSync(tempPath).forEach(file => {
32+
const logContent = fs.readFileSync(path.join(tempPath, file)).toString();
33+
assert.include(logContent, fakeEvent.eventName, 'The log file does not contain the expected content that should be written to it?');
34+
});
35+
36+
});
37+
});

0 commit comments

Comments
 (0)