Skip to content

Commit a9b9502

Browse files
committed
Fix failing tests on windows
1. Acknowledge that the CLI has a bug for path serialization on <=2.7.2. Avoid testing the query path on that version. 2. Fix calculation of root path on windows.
1 parent d9c5ecf commit a9b9502

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

extensions/ql-vscode/src/run-remote-query.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ interface Config {
1717
language?: string;
1818
}
1919

20-
interface QlPack {
20+
export interface QlPack {
2121
name: string;
2222
version: string;
2323
dependencies: { [key: string]: string };
2424
defaultSuite?: Record<string, unknown>[];
25-
defaultSuiteFile?: Record<string, unknown>;
25+
defaultSuiteFile?: string;
2626
}
2727
interface RepoListQuickPickItem extends QuickPickItem {
2828
repoList: string[];
@@ -195,7 +195,7 @@ async function findPackRoot(queryFile: string): Promise<string> {
195195
let dir = path.dirname(queryFile);
196196
while (!(await fs.pathExists(path.join(dir, 'qlpack.yml')))) {
197197
dir = path.dirname(dir);
198-
if (dir === '/') {
198+
if (isFileSystemRoot(dir)) {
199199
// there is no qlpack.yml in this direcory or any parent directory.
200200
// just use the query file's directory as the pack root.
201201
return path.dirname(queryFile);
@@ -205,6 +205,11 @@ async function findPackRoot(queryFile: string): Promise<string> {
205205
return dir;
206206
}
207207

208+
function isFileSystemRoot(dir: string): boolean {
209+
const pathObj = path.parse(dir);
210+
return pathObj.root === dir && pathObj.base === '';
211+
}
212+
208213
async function createRemoteQueriesTempDirectory() {
209214
const remoteQueryDir = await tmp.dir({ dir: tmpDir.name, unsafeCleanup: true });
210215
const queryPackDir = path.join(remoteQueryDir.path, 'query-pack');
@@ -445,7 +450,7 @@ async function fixDefaultSuite(queryPackDir: string, packRelativePath: string):
445450
qlpack.defaultSuite = [{
446451
description: 'Query suite for remote query'
447452
}, {
448-
query: packRelativePath.replace('\\', '/')
453+
query: packRelativePath.replace(/\\/g, '/')
449454
}];
450455
await fs.writeFile(packPath, yaml.safeDump(qlpack));
451456
}

extensions/ql-vscode/src/vscode-tests/cli-integration/run-remote-query.test.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@ import * as sinon from 'sinon';
44
import { CancellationToken, extensions, QuickPickItem, Uri, window } from 'vscode';
55
import 'mocha';
66
import * as fs from 'fs-extra';
7+
import * as os from 'os';
78
import * as yaml from 'js-yaml';
89

9-
import { runRemoteQuery } from '../../run-remote-query';
10+
import { QlPack, runRemoteQuery } from '../../run-remote-query';
1011
import { Credentials } from '../../authentication';
1112
import { CliVersionConstraint, CodeQLCliServer } from '../../cli';
1213
import { CodeQLExtensionInterface } from '../../extension';
1314
import { setRemoteControllerRepo, setRemoteRepositoryLists } from '../../config';
1415
import { UserCancellationException } from '../../commandRunner';
16+
import { lte } from 'semver';
1517

1618
describe('Remote queries', function() {
1719
const baseDir = path.join(__dirname, '../../../src/vscode-tests/cli-integration');
@@ -104,7 +106,7 @@ describe('Remote queries', function() {
104106
fs.existsSync(path.join(compiledPackDir, 'codeql-pack.lock.yml'))
105107
).to.be.true;
106108
expect(fs.existsSync(path.join(compiledPackDir, 'not-in-pack.ql'))).to.be.false;
107-
verifyQlPack(path.join(compiledPackDir, 'qlpack.yml'), 'in-pack.ql', 'github/remote-query-pack', '0.0.0');
109+
verifyQlPack(path.join(compiledPackDir, 'qlpack.yml'), 'in-pack.ql', 'github/remote-query-pack', '0.0.0', await pathSerializationBroken());
108110

109111
// dependencies
110112
const libraryDir = path.join(compiledPackDir, '.codeql/libraries/codeql');
@@ -145,7 +147,7 @@ describe('Remote queries', function() {
145147
printDirectoryContents(compiledPackDir);
146148
expect(fs.existsSync(path.join(compiledPackDir, 'in-pack.ql'))).to.be.true;
147149
expect(fs.existsSync(path.join(compiledPackDir, 'qlpack.yml'))).to.be.true;
148-
verifyQlPack(path.join(compiledPackDir, 'qlpack.yml'), 'in-pack.ql', 'codeql-remote/query', '0.0.0');
150+
verifyQlPack(path.join(compiledPackDir, 'qlpack.yml'), 'in-pack.ql', 'codeql-remote/query', '0.0.0', await pathSerializationBroken());
149151

150152
// depending on the cli version, we should have one of these files
151153
expect(
@@ -200,7 +202,7 @@ describe('Remote queries', function() {
200202
expect(fs.existsSync(path.join(compiledPackDir, 'otherfolder/lib.qll'))).to.be.true;
201203
expect(fs.existsSync(path.join(compiledPackDir, 'subfolder/in-pack.ql'))).to.be.true;
202204
expect(fs.existsSync(path.join(compiledPackDir, 'qlpack.yml'))).to.be.true;
203-
verifyQlPack(path.join(compiledPackDir, 'qlpack.yml'), 'subfolder/in-pack.ql', 'github/remote-query-pack', '0.0.0');
205+
verifyQlPack(path.join(compiledPackDir, 'qlpack.yml'), 'subfolder/in-pack.ql', 'github/remote-query-pack', '0.0.0', await pathSerializationBroken());
204206

205207
// depending on the cli version, we should have one of these files
206208
expect(
@@ -236,8 +238,13 @@ describe('Remote queries', function() {
236238
}
237239
});
238240

239-
function verifyQlPack(qlpackPath: string, queryPath: string, packName: string, packVersion: string) {
240-
const qlPack = yaml.safeLoad(fs.readFileSync(qlpackPath, 'utf8'));
241+
function verifyQlPack(qlpackPath: string, queryPath: string, packName: string, packVersion: string, pathSerializationBroken: boolean) {
242+
const qlPack = yaml.safeLoad(fs.readFileSync(qlpackPath, 'utf8')) as QlPack;
243+
244+
if (pathSerializationBroken) {
245+
// the path serialization is broken, so we force it to be the path in the pack to be same as the query path
246+
qlPack.defaultSuite![1].query = queryPath;
247+
}
241248

242249
// don't check the build metadata since it is variable
243250
delete (qlPack as any).buildMetadata;
@@ -257,6 +264,15 @@ describe('Remote queries', function() {
257264
});
258265
}
259266

267+
/**
268+
* In version 2.7.2 and earlier, relative paths were not serialized correctly inside the qlpack.yml file.
269+
* So, ignore part of the test for these versions.
270+
*
271+
* @returns true if path serialization is broken in this run
272+
*/
273+
async function pathSerializationBroken() {
274+
return lte((await cli.getVersion()), '2.7.2') && os.platform() === 'win32';
275+
}
260276
function getFile(file: string): Uri {
261277
return Uri.file(path.join(baseDir, file));
262278
}

0 commit comments

Comments
 (0)