diff --git a/vscode-dotnet-runtime-library/src/EventStream/LoggingObserver.ts b/vscode-dotnet-runtime-library/src/EventStream/LoggingObserver.ts index b746bfd93a..48c484d2d3 100644 --- a/vscode-dotnet-runtime-library/src/EventStream/LoggingObserver.ts +++ b/vscode-dotnet-runtime-library/src/EventStream/LoggingObserver.ts @@ -7,7 +7,7 @@ import { IEvent } from './IEvent'; import { ILoggingObserver } from './ILoggingObserver'; export class LoggingObserver implements ILoggingObserver { - private readonly log: string[] = []; + private log: string[] = []; constructor(private readonly logFilePath: string) {} @@ -31,6 +31,6 @@ export class LoggingObserver implements ILoggingObserver { } private writeLine(line: string) { - this.log.concat(line); + this.log.push(line); } } diff --git a/vscode-dotnet-runtime-library/src/test/unit/LoggingObserver.test.ts b/vscode-dotnet-runtime-library/src/test/unit/LoggingObserver.test.ts new file mode 100644 index 0000000000..282f6ed8e4 --- /dev/null +++ b/vscode-dotnet-runtime-library/src/test/unit/LoggingObserver.test.ts @@ -0,0 +1,37 @@ +/* -------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + * ------------------------------------------------------------------------------------------ */ +import * as chai from 'chai'; +import * as fs from 'fs'; +import * as path from 'path'; +import { LoggingObserver } from '../../EventStream/LoggingObserver'; +import { MockEventStream } from '../mocks/MockObjects'; +import { DotnetUninstallAllStarted } from '../../EventStream/EventStreamEvents'; +const assert = chai.assert; + +suite('LoggingObserver Unit Tests', () => { + const eventStream = new MockEventStream(); + const tempPath = path.join(__dirname, `${ new Date().getTime()}` ); + + test('Log file is writing output', async () => { + // Create an empty folder + if (!fs.existsSync(tempPath)) { + fs.mkdirSync(tempPath); + } + // Create a logging observer + const loggingObserver = new LoggingObserver(path.join(tempPath, 'logTest.txt')); + + // Create a fake event and call the post/dispose function + const fakeEvent = new DotnetUninstallAllStarted(); + loggingObserver.post(fakeEvent); + loggingObserver.dispose(); + + // Check if the log file content is same as expected content + fs.readdirSync(tempPath).forEach(file => { + const logContent = fs.readFileSync(path.join(tempPath, file)).toString(); + assert.include(logContent, fakeEvent.eventName, 'The log file does not contain the expected content that should be written to it?'); + }); + + }); +});