Skip to content

Commit a1bcb75

Browse files
committed
Ensure src.zip is prioritized over src folder
Fixes a bug where legacy databases with both unzipped and zipped sources were incorrectly being loaded with the src folder.
1 parent b481441 commit a1bcb75

2 files changed

Lines changed: 52 additions & 8 deletions

File tree

extensions/ql-vscode/src/databases.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,20 +121,21 @@ async function findDataset(parentDirectory: string): Promise<vscode.Uri> {
121121
return vscode.Uri.file(dbAbsolutePath);
122122
}
123123

124-
async function findSourceArchive(
124+
// exported for testing
125+
export async function findSourceArchive(
125126
databasePath: string, silent = false
126127
): Promise<vscode.Uri | undefined> {
127-
128128
const relativePaths = ['src', 'output/src_archive'];
129129

130130
for (const relativePath of relativePaths) {
131131
const basePath = path.join(databasePath, relativePath);
132132
const zipPath = basePath + '.zip';
133133

134-
if (await fs.pathExists(basePath)) {
135-
return vscode.Uri.file(basePath);
136-
} else if (await fs.pathExists(zipPath)) {
134+
// Prefer using a zip archive over a directory.
135+
if (await fs.pathExists(zipPath)) {
137136
return encodeArchiveBasePath(zipPath);
137+
} else if (await fs.pathExists(basePath)) {
138+
return vscode.Uri.file(basePath);
138139
}
139140
}
140141
if (!silent) {
@@ -161,7 +162,6 @@ async function resolveDatabase(
161162
datasetUri,
162163
sourceArchiveUri
163164
};
164-
165165
}
166166

167167
/** Gets the relative paths of all `.dbscheme` files in the given directory. */

extensions/ql-vscode/src/vscode-tests/minimal-workspace/databases.test.ts

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import {
1212
DatabaseManager,
1313
DatabaseItemImpl,
1414
DatabaseContents,
15-
FullDatabaseOptions
15+
FullDatabaseOptions,
16+
findSourceArchive
1617
} from '../../databases';
1718
import { Logger } from '../../logging';
1819
import { QueryServerClient } from '../../queryserver-client';
@@ -179,7 +180,7 @@ describe('databases', () => {
179180
expect(spy).to.have.been.calledWith(mockEvent);
180181
});
181182

182-
it('should add a database item source archive', async function () {
183+
it('should add a database item source archive', async function() {
183184
const mockDbItem = createMockDB();
184185
mockDbItem.name = 'xxx';
185186
await (databaseManager as any).addDatabaseSourceArchiveFolder(mockDbItem);
@@ -478,6 +479,49 @@ describe('databases', () => {
478479
const db = createMockDB(sourceLocationUri(), Uri.file('/path/to/dir/dir.testproj'));
479480
expect(await db.isAffectedByTest('/path/to/test.ql')).to.false;
480481
});
482+
483+
});
484+
485+
describe.only('findSourceArchive', function() {
486+
// not sure why, but some of these tests take more than two second to run.
487+
this.timeout(5000);
488+
489+
['src', 'output/src_archive'].forEach(name => {
490+
it(`should find source folder in ${name}`, async () => {
491+
const uri = Uri.file(path.join(dir.name, name));
492+
fs.createFileSync(path.join(uri.fsPath, 'hucairz.txt'));
493+
const srcUri = await findSourceArchive(dir.name);
494+
expect(srcUri!.fsPath).to.eq(uri.fsPath);
495+
});
496+
497+
it(`should find source archive in ${name}.zip`, async () => {
498+
const uri = Uri.file(path.join(dir.name, name + '.zip'));
499+
fs.createFileSync(uri.fsPath);
500+
const srcUri = await findSourceArchive(dir.name);
501+
expect(srcUri!.fsPath).to.eq(uri.fsPath);
502+
});
503+
504+
it(`should prioritize ${name}.zip over ${name}`, async () => {
505+
const uri = Uri.file(path.join(dir.name, name + '.zip'));
506+
fs.createFileSync(uri.fsPath);
507+
508+
const uriFolder = Uri.file(path.join(dir.name, name));
509+
fs.createFileSync(path.join(uriFolder.fsPath, 'hucairz.txt'));
510+
511+
const srcUri = await findSourceArchive(dir.name);
512+
expect(srcUri!.fsPath).to.eq(uri.fsPath);
513+
});
514+
});
515+
516+
it('should prioritize src over output/src_archive', async () => {
517+
const uriSrc = Uri.file(path.join(dir.name, 'src.zip'));
518+
fs.createFileSync(uriSrc.fsPath);
519+
const uriSrcArchive = Uri.file(path.join(dir.name, 'src.zip'));
520+
fs.createFileSync(uriSrcArchive.fsPath);
521+
522+
const resultUri = await findSourceArchive(dir.name);
523+
expect(resultUri!.fsPath).to.eq(uriSrc.fsPath);
524+
});
481525
});
482526

483527
function createMockDB(

0 commit comments

Comments
 (0)