Skip to content

Commit 4619210

Browse files
committed
(maint) Add a logging interface
Previously the logging object had no interface which made testing difficult. This commit adds a new interface which new features can use, which makes mocking the logging object possible.
1 parent 411b08a commit 4619210

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

src/logging.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,19 @@ export enum LogLevel {
1616
Error,
1717
}
1818

19-
export class Logger {
19+
/** Interface for logging operations. New features should use this interface for the "type" of logger.
20+
* This will allow for easy mocking of the logger during unit tests.
21+
*/
22+
export interface ILogger {
23+
write(message: string, ...additionalMessages: string[]);
24+
writeDiagnostic(message: string, ...additionalMessages: string[]);
25+
writeVerbose(message: string, ...additionalMessages: string[]);
26+
writeWarning(message: string, ...additionalMessages: string[]);
27+
writeAndShowWarning(message: string, ...additionalMessages: string[]);
28+
writeError(message: string, ...additionalMessages: string[]);
29+
}
30+
31+
export class Logger implements ILogger {
2032

2133
public logBasePath: string;
2234
public logSessionPath: string;

test/test_utils.ts

+2-14
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,9 @@
22
* Copyright (C) Microsoft Corporation. All rights reserved.
33
*--------------------------------------------------------*/
44

5-
import { Logger, LogLevel } from "../src/logging";
6-
7-
export class MockLogger extends Logger {
8-
// Note - This is not a true mock as the constructor is inherited and causes errors due to trying load
9-
// the "PowerShell Extension Logs" multiple times. Ideally logging should be via an interface and then
10-
// we can mock correctly.
11-
12-
public dispose() { return undefined; }
13-
14-
public getLogFilePath(baseName: string): string { return "mock"; }
15-
16-
public writeAtLevel(logLevel: LogLevel, message: string, ...additionalMessages: string[]) { return undefined; }
5+
import { ILogger } from "../src/logging";
176

7+
export class MockLogger implements ILogger {
188
public write(message: string, ...additionalMessages: string[]) { return undefined; }
199

2010
public writeDiagnostic(message: string, ...additionalMessages: string[]) { return undefined; }
@@ -28,6 +18,4 @@ export class MockLogger extends Logger {
2818
public writeError(message: string, ...additionalMessages: string[]) { return undefined; }
2919

3020
public writeAndShowError(message: string, ...additionalMessages: string[]) { return undefined; }
31-
32-
public startNewLog(minimumLogLevel: string = "Normal") { return undefined; }
3321
}

0 commit comments

Comments
 (0)