Skip to content

Commit e591236

Browse files
committed
Update tests
1 parent 41f4e04 commit e591236

File tree

1 file changed

+46
-6
lines changed

1 file changed

+46
-6
lines changed

extensions/ql-vscode/test/pure-tests/logging.test.ts

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ describe('OutputChannelLogger tests', () => {
4848
});
4949

5050
it('should create a side log in the workspace area', async () => {
51-
logger.init(tempFolders.storagePath.name);
51+
logger.setLogStoragePath(tempFolders.storagePath.name, false);
5252

5353
await logger.log('xxx', { additionalLogLocation: 'first' });
5454
await logger.log('yyy', { additionalLogLocation: 'second' });
@@ -65,7 +65,7 @@ describe('OutputChannelLogger tests', () => {
6565
});
6666

6767
it('should delete side logs on dispose', async () => {
68-
logger.init(tempFolders.storagePath.name);
68+
logger.setLogStoragePath(tempFolders.storagePath.name, false);
6969
await logger.log('xxx', { additionalLogLocation: 'first' });
7070
await logger.log('yyy', { additionalLogLocation: 'second' });
7171

@@ -79,8 +79,23 @@ describe('OutputChannelLogger tests', () => {
7979
expect(mockOutputChannel.dispose).to.have.been.calledWith();
8080
});
8181

82+
it('should not delete side logs on dispose in a custom directory', async () => {
83+
logger.setLogStoragePath(tempFolders.storagePath.name, true);
84+
await logger.log('xxx', { additionalLogLocation: 'first' });
85+
await logger.log('yyy', { additionalLogLocation: 'second' });
86+
87+
const testLoggerFolder = path.join(tempFolders.storagePath.name, 'test-logger');
88+
expect(fs.readdirSync(testLoggerFolder).length).to.equal(2);
89+
90+
await logger.dispose();
91+
// need to wait for disposable-object to dispose
92+
await waitABit();
93+
expect(fs.readdirSync(testLoggerFolder).length).to.equal(2);
94+
expect(mockOutputChannel.dispose).to.have.been.calledWith();
95+
});
96+
8297
it('should remove an additional log location', async () => {
83-
logger.init(tempFolders.storagePath.name);
98+
logger.setLogStoragePath(tempFolders.storagePath.name, false);
8499
await logger.log('xxx', { additionalLogLocation: 'first' });
85100
await logger.log('yyy', { additionalLogLocation: 'second' });
86101

@@ -94,11 +109,36 @@ describe('OutputChannelLogger tests', () => {
94109
expect(fs.readFileSync(path.join(testLoggerFolder, 'second'), 'utf8')).to.equal('yyy\n');
95110
});
96111

97-
it('should delete an existing folder on init', async () => {
112+
it('should not remove an additional log location in a custom directory', async () => {
113+
logger.setLogStoragePath(tempFolders.storagePath.name, true);
114+
await logger.log('xxx', { additionalLogLocation: 'first' });
115+
await logger.log('yyy', { additionalLogLocation: 'second' });
116+
117+
const testLoggerFolder = path.join(tempFolders.storagePath.name, 'test-logger');
118+
expect(fs.readdirSync(testLoggerFolder).length).to.equal(2);
119+
120+
await logger.removeAdditionalLogLocation('first');
121+
// need to wait for disposable-object to dispose
122+
await waitABit();
123+
expect(fs.readdirSync(testLoggerFolder).length).to.equal(2);
124+
expect(fs.readFileSync(path.join(testLoggerFolder, 'second'), 'utf8')).to.equal('yyy\n');
125+
});
126+
127+
it('should delete an existing folder when setting the log storage path', async () => {
98128
fs.createFileSync(path.join(tempFolders.storagePath.name, 'test-logger', 'xxx'));
99-
logger.init(tempFolders.storagePath.name);
129+
logger.setLogStoragePath(tempFolders.storagePath.name, false);
100130
// should be empty dir
101131

132+
const testLoggerFolder = path.join(tempFolders.storagePath.name, 'test-logger');
133+
// TODO: Why does this test pass? I'd expect the length to be 0 if it's correctly deleted the existing folder.
134+
expect(fs.readdirSync(testLoggerFolder).length).to.equal(1);
135+
});
136+
137+
it('should not delete an existing folder when setting the log storage path for a custom directory', async () => {
138+
fs.createFileSync(path.join(tempFolders.storagePath.name, 'test-logger', 'xxx'));
139+
logger.setLogStoragePath(tempFolders.storagePath.name, true);
140+
// should not be empty dir
141+
102142
const testLoggerFolder = path.join(tempFolders.storagePath.name, 'test-logger');
103143
expect(fs.readdirSync(testLoggerFolder).length).to.equal(1);
104144
});
@@ -130,7 +170,7 @@ describe('OutputChannelLogger tests', () => {
130170
});
131171
}
132172

133-
function waitABit(ms = 50): Promise<void> {
173+
function waitABit(ms = 50): Promise<void> {
134174
return new Promise(resolve => setTimeout(resolve, ms));
135175
}
136176
});

0 commit comments

Comments
 (0)