Skip to content

Commit

Permalink
Merge pull request #1122 from JL03-Yue/JL03-Yue/error-issue
Browse files Browse the repository at this point in the history
fix logging issue and add a logging unit test
  • Loading branch information
JL03-Yue authored Jul 28, 2023
2 parents efcba6b + 84e0e52 commit 2b3b740
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}

Expand All @@ -31,6 +31,6 @@ export class LoggingObserver implements ILoggingObserver {
}

private writeLine(line: string) {
this.log.concat(line);
this.log.push(line);
}
}
Original file line number Diff line number Diff line change
@@ -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?');
});

});
});

0 comments on commit 2b3b740

Please sign in to comment.