|
| 1 | +// Copyright 2018 The Bazel Authors. All rights reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +import assert = require("assert"); |
| 16 | +import * as vscode from "vscode"; |
| 17 | +import { |
| 18 | + initializeLogger, |
| 19 | + trace, |
| 20 | + debug, |
| 21 | + info, |
| 22 | + warn, |
| 23 | + error, |
| 24 | +} from "../src/extension/logger"; |
| 25 | + |
| 26 | +describe("The logger module", () => { |
| 27 | + let mockContext: vscode.ExtensionContext; |
| 28 | + let disposables: vscode.Disposable[] = []; |
| 29 | + |
| 30 | + beforeEach(() => { |
| 31 | + // Create a mock extension context |
| 32 | + mockContext = { |
| 33 | + subscriptions: disposables, |
| 34 | + } as vscode.ExtensionContext; |
| 35 | + }); |
| 36 | + |
| 37 | + afterEach(() => { |
| 38 | + // Clean up disposables |
| 39 | + for (const disposable of disposables) { |
| 40 | + disposable.dispose(); |
| 41 | + } |
| 42 | + disposables = []; |
| 43 | + }); |
| 44 | + |
| 45 | + describe("initializeLogger", () => { |
| 46 | + it("can be called without throwing", () => { |
| 47 | + assert.doesNotThrow(() => { |
| 48 | + initializeLogger(mockContext); |
| 49 | + }); |
| 50 | + }); |
| 51 | + |
| 52 | + it("is idempotent - does not create multiple loggers", () => { |
| 53 | + // Call initializeLogger multiple times - it should be safe |
| 54 | + initializeLogger(mockContext); |
| 55 | + const initialLength = disposables.length; |
| 56 | + initializeLogger(mockContext); |
| 57 | + // Note: In the test environment, the logger may already be initialized |
| 58 | + // by extension activation, so we just verify it doesn't throw |
| 59 | + assert.doesNotThrow(() => { |
| 60 | + initializeLogger(mockContext); |
| 61 | + }); |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + describe("logging functions", () => { |
| 66 | + beforeEach(() => { |
| 67 | + initializeLogger(mockContext); |
| 68 | + }); |
| 69 | + |
| 70 | + it("trace logs a message", () => { |
| 71 | + assert.doesNotThrow(() => { |
| 72 | + trace("test trace message"); |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + it("debug logs a message", () => { |
| 77 | + assert.doesNotThrow(() => { |
| 78 | + debug("test debug message"); |
| 79 | + }); |
| 80 | + }); |
| 81 | + |
| 82 | + it("info logs a message", () => { |
| 83 | + assert.doesNotThrow(() => { |
| 84 | + info("test info message"); |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + it("warn logs a message", () => { |
| 89 | + assert.doesNotThrow(() => { |
| 90 | + warn("test warn message"); |
| 91 | + }); |
| 92 | + }); |
| 93 | + |
| 94 | + it("error logs a string message", () => { |
| 95 | + assert.doesNotThrow(() => { |
| 96 | + error("test error message"); |
| 97 | + }); |
| 98 | + }); |
| 99 | + |
| 100 | + it("error logs an Error object", () => { |
| 101 | + assert.doesNotThrow(() => { |
| 102 | + const err = new Error("test error"); |
| 103 | + error(err); |
| 104 | + }); |
| 105 | + }); |
| 106 | + |
| 107 | + it("error logs an Error object with additional args", () => { |
| 108 | + assert.doesNotThrow(() => { |
| 109 | + const err = new Error("test error"); |
| 110 | + error(err, "additional", "args"); |
| 111 | + }); |
| 112 | + }); |
| 113 | + |
| 114 | + it("logging functions accept additional arguments", () => { |
| 115 | + assert.doesNotThrow(() => { |
| 116 | + trace("message", "arg1", 123, { key: "value" }); |
| 117 | + debug("message", "arg1", 123, { key: "value" }); |
| 118 | + info("message", "arg1", 123, { key: "value" }); |
| 119 | + warn("message", "arg1", 123, { key: "value" }); |
| 120 | + error("message", "arg1", 123, { key: "value" }); |
| 121 | + }); |
| 122 | + }); |
| 123 | + }); |
| 124 | +}); |
0 commit comments