Skip to content

Ensure src.zip is prioritized over src folder #1025

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion extensions/ql-vscode/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

## [UNRELEASED]

- Emit a more explicit error message when a user tries to add a database with an unzipped source folder to the workspace. [1021](https://github.com/github/vscode-codeql/pull/1021)
- Emit a more explicit error message when a user tries to add a database with an unzipped source folder to the workspace. [#1021](https://github.com/github/vscode-codeql/pull/1021)
- Ensure `src.zip` archives are used as the canonical source instead of `src` folders when importing databases. [#1025](https://github.com/github/vscode-codeql/pull/1025)

## 1.5.7 - 23 November 2021

Expand Down
12 changes: 6 additions & 6 deletions extensions/ql-vscode/src/databases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,20 +121,21 @@ async function findDataset(parentDirectory: string): Promise<vscode.Uri> {
return vscode.Uri.file(dbAbsolutePath);
}

async function findSourceArchive(
// exported for testing
export async function findSourceArchive(
databasePath: string, silent = false
): Promise<vscode.Uri | undefined> {

const relativePaths = ['src', 'output/src_archive'];

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

if (await fs.pathExists(basePath)) {
return vscode.Uri.file(basePath);
} else if (await fs.pathExists(zipPath)) {
// Prefer using a zip archive over a directory.
if (await fs.pathExists(zipPath)) {
return encodeArchiveBasePath(zipPath);
} else if (await fs.pathExists(basePath)) {
return vscode.Uri.file(basePath);
}
}
if (!silent) {
Expand All @@ -161,7 +162,6 @@ async function resolveDatabase(
datasetUri,
sourceArchiveUri
};

}

/** Gets the relative paths of all `.dbscheme` files in the given directory. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import {
DatabaseManager,
DatabaseItemImpl,
DatabaseContents,
FullDatabaseOptions
FullDatabaseOptions,
findSourceArchive
} from '../../databases';
import { Logger } from '../../logging';
import { QueryServerClient } from '../../queryserver-client';
Expand Down Expand Up @@ -179,7 +180,7 @@ describe('databases', () => {
expect(spy).to.have.been.calledWith(mockEvent);
});

it('should add a database item source archive', async function () {
it('should add a database item source archive', async function() {
const mockDbItem = createMockDB();
mockDbItem.name = 'xxx';
await (databaseManager as any).addDatabaseSourceArchiveFolder(mockDbItem);
Expand Down Expand Up @@ -478,6 +479,49 @@ describe('databases', () => {
const db = createMockDB(sourceLocationUri(), Uri.file('/path/to/dir/dir.testproj'));
expect(await db.isAffectedByTest('/path/to/test.ql')).to.false;
});

});

describe.only('findSourceArchive', function() {
// not sure why, but some of these tests take more than two second to run.
this.timeout(5000);

['src', 'output/src_archive'].forEach(name => {
it(`should find source folder in ${name}`, async () => {
const uri = Uri.file(path.join(dir.name, name));
fs.createFileSync(path.join(uri.fsPath, 'hucairz.txt'));
const srcUri = await findSourceArchive(dir.name);
expect(srcUri!.fsPath).to.eq(uri.fsPath);
});

it(`should find source archive in ${name}.zip`, async () => {
const uri = Uri.file(path.join(dir.name, name + '.zip'));
fs.createFileSync(uri.fsPath);
const srcUri = await findSourceArchive(dir.name);
expect(srcUri!.fsPath).to.eq(uri.fsPath);
});

it(`should prioritize ${name}.zip over ${name}`, async () => {
const uri = Uri.file(path.join(dir.name, name + '.zip'));
fs.createFileSync(uri.fsPath);

const uriFolder = Uri.file(path.join(dir.name, name));
fs.createFileSync(path.join(uriFolder.fsPath, 'hucairz.txt'));

const srcUri = await findSourceArchive(dir.name);
expect(srcUri!.fsPath).to.eq(uri.fsPath);
});
});

it('should prioritize src over output/src_archive', async () => {
const uriSrc = Uri.file(path.join(dir.name, 'src.zip'));
fs.createFileSync(uriSrc.fsPath);
const uriSrcArchive = Uri.file(path.join(dir.name, 'src.zip'));
fs.createFileSync(uriSrcArchive.fsPath);

const resultUri = await findSourceArchive(dir.name);
expect(resultUri!.fsPath).to.eq(uriSrc.fsPath);
});
});

function createMockDB(
Expand Down