Skip to content

Commit 0036c7f

Browse files
committed
fix: logger tests, simple re-instantiation on settings change
1 parent c1053fd commit 0036c7f

File tree

4 files changed

+21
-67
lines changed

4 files changed

+21
-67
lines changed

packages/graphql-language-service-server/src/Logger.ts

Lines changed: 10 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -19,39 +19,6 @@ export class Logger implements VSCodeLogger {
1919
debug?: boolean,
2020
) {
2121
this.logLevel = debug ? 1 : 0;
22-
// first detect the debug flag on initialization
23-
void (async () => {
24-
try {
25-
const config = await this._connection?.workspace?.getConfiguration(
26-
'vscode-graphql',
27-
);
28-
const debugSetting = config?.get('debug');
29-
if (debugSetting === true) {
30-
this.logLevel = 1;
31-
}
32-
if (debugSetting === false || debugSetting === null) {
33-
this.logLevel = 0;
34-
}
35-
} catch {
36-
// ignore
37-
}
38-
})();
39-
// then watch for it to change. doesn't require re-creating the logger!
40-
this._connection?.onDidChangeConfiguration(config => {
41-
const debugSetting =
42-
config?.settings && config.settings['vscode-graphql']?.debug;
43-
// if it's undefined, it's not being passed
44-
if (debugSetting === undefined) {
45-
return;
46-
}
47-
// if it's true, set it to 1, we will eventually do log levels properly
48-
if (debugSetting === true) {
49-
this.logLevel = 1;
50-
}
51-
if (debugSetting === false || debugSetting === null) {
52-
this.logLevel = 0;
53-
}
54-
});
5522
}
5623

5724
error(message: string): void {
@@ -71,11 +38,21 @@ export class Logger implements VSCodeLogger {
7138
this._connection.console.log(message);
7239
}
7340
}
41+
set level(level: number) {
42+
this.logLevel = level;
43+
}
44+
get level() {
45+
return this.logLevel;
46+
}
7447
}
7548

7649
export class NoopLogger implements VSCodeLogger {
7750
error() {}
7851
warn() {}
7952
info() {}
8053
log() {}
54+
set level(_level: number) {}
55+
get level() {
56+
return 0;
57+
}
8158
}

packages/graphql-language-service-server/src/MessageProcessor.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,8 @@ export class MessageProcessor {
225225
};
226226

227227
try {
228+
// now we have the settings so we can re-build the logger
229+
this._logger.level = this._settings?.debug === true ? 1 : 0;
228230
// createServer() can be called with a custom config object, and
229231
// this is a public interface that may be used by customized versions of the server
230232
if (this._providedConfig) {

packages/graphql-language-service-server/src/__tests__/Logger.test.ts

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -21,53 +21,19 @@ describe('Logger', () => {
2121
it('should initialize with default log level, and ignore .log intentionally', () => {
2222
const logger = new Logger(connection as any);
2323
expect(logger).toBeDefined();
24-
expect(connection.onDidChangeConfiguration).toHaveBeenCalledTimes(1);
2524
expect(logger.logLevel).toBe(0);
2625
logger.log('test');
2726
expect(connection.console.log).toHaveBeenCalledTimes(0);
2827
});
2928

30-
it('should initialize with default log level, then change to logging with new settings, then back when they are disabled', () => {
31-
const logger = new Logger(connection as any);
32-
expect(logger).toBeDefined();
33-
expect(connection.onDidChangeConfiguration).toHaveBeenCalledTimes(1);
34-
expect(logger.logLevel).toBe(0);
35-
logger.log('test');
36-
expect(connection.console.log).toHaveBeenCalledTimes(0);
37-
connection.onDidChangeConfiguration.mock.calls[0][0]({
38-
settings: { 'vscode-graphql': { debug: true } },
39-
});
40-
expect(logger.logLevel).toBe(1);
41-
logger.log('test');
42-
expect(connection.console.log).toHaveBeenCalledTimes(1);
43-
connection.onDidChangeConfiguration.mock.calls[0][0]({
44-
settings: { 'vscode-graphql': { debug: false } },
45-
});
46-
expect(logger.logLevel).toBe(0);
47-
logger.log('test');
48-
// and not a second time
49-
expect(connection.console.log).toHaveBeenCalledTimes(1);
50-
});
51-
5229
it('should not change log level when settings are not passed', () => {
5330
const logger = new Logger(connection as any, true);
5431
expect(logger).toBeDefined();
55-
expect(connection.onDidChangeConfiguration).toHaveBeenCalledTimes(1);
5632
expect(logger.logLevel).toBe(1);
5733
logger.log('test');
5834
expect(connection.console.log).toHaveBeenCalledTimes(1);
59-
connection.onDidChangeConfiguration.mock.calls[0][0]({});
6035
expect(logger.logLevel).toBe(1);
6136
logger.log('test');
6237
expect(connection.console.log).toHaveBeenCalledTimes(2);
6338
});
64-
65-
it('should initialize with debug log level, and .log is visible now', () => {
66-
const logger = new Logger(connection as any, true);
67-
expect(logger).toBeDefined();
68-
expect(connection.onDidChangeConfiguration).toHaveBeenCalledTimes(1);
69-
expect(logger.logLevel).toBe(1);
70-
logger.log('test');
71-
expect(connection.console.log).toHaveBeenCalledTimes(1);
72-
});
7339
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import startServer from '../startServer';
2+
3+
describe('startServer', () => {
4+
it('should start the server', async () => {
5+
await startServer({});
6+
// if the server starts, we're good
7+
expect(true).toBe(true);
8+
});
9+
});

0 commit comments

Comments
 (0)