|
| 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