Skip to content

Commit 2a437c0

Browse files
committed
Merge remote-tracking branch 'origin/main' into koesie10/msw-integration-test
2 parents 00145bb + bc10a7a commit 2a437c0

File tree

93 files changed

+84164
-261
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

93 files changed

+84164
-261
lines changed

.vscode/launch.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,8 @@
4444
"bdd",
4545
"--colors",
4646
"--diff",
47-
"-r",
48-
"ts-node/register",
49-
"-r",
50-
"test/mocha.setup.js",
47+
"--config",
48+
".mocharc.json",
5149
"test/pure-tests/**/*.ts"
5250
],
5351
"stopOnEntry": false,

CONTRIBUTING.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,49 @@ The CLI integration tests require the CodeQL standard libraries in order to run
144144

145145
3. Run the VSCode task from the "Run and Debug" view called _Launch Integration Tests - With CLI_.
146146

147+
#### Using a mock GitHub API server
148+
149+
Multi-Repo Variant Analyses (MRVA) rely on the GitHub API. In order to make development and testing easy, we have functionality that allows us to intercept requests to the GitHub API and provide mock responses.
150+
151+
##### Using a pre-recorded test scenario
152+
153+
To run a mock MRVA scenario, follow these steps:
154+
1. Enable the mock GitHub API server by adding the following in your VS Code user settings (which can be found by running the `Preferences: Open User Settings (JSON)` VS Code command):
155+
```json
156+
"codeQL.mockGitHubApiServer": {
157+
"enabled": true
158+
}
159+
```
160+
161+
1. Run the `CodeQL: Mock GitHub API Server: Load Scenario` command from the command pallet, and choose one of the scenarios to load.
162+
1. Execute a normal MRVA. At this point you should see the scenario being played out, rather than an actual MRVA running.
163+
1. Once you're done, you can stop using the mock scenario with `CodeQL: Mock GitHub API Server: Unload Scenario`
164+
165+
If you want to replay the same scenario you should unload and reload it so requests are replayed from the start.
166+
167+
##### Recording a new test scenario
168+
To record a new mock MRVA scenario, follow these steps:
169+
170+
1. Enable the mock GitHub API server by adding the following in your VS Code user settings (which can be found by running the `Preferences: Open User Settings (JSON)` VS Code command):
171+
```json
172+
"codeQL.mockGitHubApiServer": {
173+
"enabled": true
174+
}
175+
```
176+
177+
1. Run the `CodeQL: Mock GitHub API Server: Start Scenario Recording` VS Code command from the command pallet.
178+
1. Execute a normal MRVA.
179+
1. Once what you wanted to record is done (e.g. the MRVA has finished), then run the `CodeQL: Mock GitHub API Server: Save Scenario` command from the command pallet.
180+
1. The scenario should then be available for replaying.
181+
182+
If you want to cancel recording, run the `CodeQL: Mock GitHub API Server: Cancel Scenario Recording` command.
183+
184+
Once the scenario has been recorded, it's often useful to remove some of the requests to speed up the replay, particularly ones that fetch the variant analysis status. Once some of the request files have manually been removed, the [fix-scenario-file-numbering script](./extensions/ql-vscode/scripts/fix-scenario-file-numbering.ts) can be used to update the number of the files. See the script file for details on how to use.
185+
186+
#### Scenario data location
187+
188+
Pre-recorded scenarios are stored in `./src/mocks/scenarios`. However, it's possible to configure the location, by setting the `codeQL.mockGitHubApiServer.scenariosPath` configuration property in the VS Code user settings.
189+
147190
## Releasing (write access required)
148191

149192
1. Double-check the `CHANGELOG.md` contains all desired change comments and has the version to be released with date at the top.

extensions/ql-vscode/.mocharc.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"exit": true,
3+
"require": [
4+
"test/mocha.setup.js"
5+
]
6+
}

extensions/ql-vscode/package-lock.json

Lines changed: 54 additions & 55 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

extensions/ql-vscode/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,7 +1248,7 @@
12481248
"watch:extension": "tsc --watch",
12491249
"watch:webpack": "gulp watchView",
12501250
"test": "npm-run-all -p test:*",
1251-
"test:unit": "mocha --exit -r ts-node/register -r test/mocha.setup.js test/pure-tests/**/*.ts",
1251+
"test:unit": "mocha --config .mocharc.json test/pure-tests/**/*.ts",
12521252
"test:view": "jest",
12531253
"preintegration": "rm -rf ./out/vscode-tests && gulp",
12541254
"integration": "node ./out/vscode-tests/run-integration-tests.js no-workspace,minimal-workspace",
@@ -1354,6 +1354,7 @@
13541354
"@types/xml2js": "~0.4.4",
13551355
"@typescript-eslint/eslint-plugin": "^4.26.0",
13561356
"@typescript-eslint/parser": "^4.26.0",
1357+
"@vscode/test-electron": "^2.2.0",
13571358
"ajv": "^8.11.0",
13581359
"ansi-colors": "^4.1.1",
13591360
"applicationinsights": "^2.3.5",
@@ -1395,7 +1396,6 @@
13951396
"typescript": "^4.5.5",
13961397
"typescript-formatter": "^7.2.2",
13971398
"vsce": "^2.7.0",
1398-
"vscode-test": "^1.4.0",
13991399
"webpack": "^5.62.2",
14001400
"webpack-cli": "^4.6.0"
14011401
},
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/**
2+
* This scripts helps after recording a scenario to be used for replaying
3+
* with the mock GitHub API server.
4+
*
5+
* Once the scenario has been recorded, it's often useful to remove some of
6+
* the requests to speed up the replay, particularly ones that fetch the
7+
* variant analysis status. Once some of the requests have manually been
8+
* removed, this script can be used to update the numbering of the files.
9+
*
10+
* Usage: npx ts-node scripts/fix-scenario-file-numbering.ts <scenario-name>
11+
*/
12+
13+
import * as fs from 'fs-extra';
14+
import * as path from 'path';
15+
16+
if (process.argv.length !== 3) {
17+
console.error('Expected 1 argument - the scenario name')
18+
}
19+
20+
const scenarioName = process.argv[2];
21+
22+
const extensionDirectory = path.resolve(__dirname, '..');
23+
const scenariosDirectory = path.resolve(extensionDirectory, 'src/mocks/scenarios');
24+
const scenarioDirectory = path.resolve(scenariosDirectory, scenarioName);
25+
26+
async function fixScenarioFiles() {
27+
console.log(scenarioDirectory);
28+
if (!(await fs.pathExists(scenarioDirectory))) {
29+
console.error('Scenario directory does not exist: ' + scenarioDirectory);
30+
return;
31+
}
32+
33+
const files = await fs.readdir(scenarioDirectory);
34+
35+
const orderedFiles = files.sort((a, b) => {
36+
const aNum = parseInt(a.split('-')[0]);
37+
const bNum = parseInt(b.split('-')[0]);
38+
return aNum - bNum;
39+
});
40+
41+
let index = 0;
42+
for (let file of orderedFiles) {
43+
const ext = path.extname(file);
44+
if (ext === '.json') {
45+
const fileName = path.basename(file, ext);
46+
const fileCurrentIndex = parseInt(fileName.split('-')[0]);
47+
const fileNameWithoutIndex = fileName.split('-')[1];
48+
if (fileCurrentIndex !== index) {
49+
const newFileName = `${index}-${fileNameWithoutIndex}${ext}`;
50+
const oldFilePath = path.join(scenarioDirectory, file);
51+
const newFilePath = path.join(scenarioDirectory, newFileName);
52+
console.log(`Rename: ${oldFilePath} -> ${newFilePath}`);
53+
await fs.rename(oldFilePath, newFilePath);
54+
55+
if (fileNameWithoutIndex === 'getVariantAnalysisRepoResult') {
56+
const oldZipFileName = `${fileCurrentIndex}-getVariantAnalysisRepoResult.body.zip`;
57+
const newZipFileName = `${index}-getVariantAnalysisRepoResult.body.zip`;
58+
const oldZipFilePath = path.join(scenarioDirectory, oldZipFileName);
59+
const newZipFilePath = path.join(scenarioDirectory, newZipFileName);
60+
console.log(`Rename: ${oldZipFilePath} -> ${newZipFilePath}`);
61+
await fs.rename(oldZipFilePath, newZipFilePath);
62+
63+
const json = await fs.readJson(newFilePath);
64+
json.response.body = `file:${newZipFileName}`;
65+
console.log(`Response.body change to ${json.response.body}`);
66+
await fs.writeJSON(newFilePath, json);
67+
}
68+
}
69+
70+
index++;
71+
}
72+
}
73+
}
74+
75+
fixScenarioFiles().catch(e => {
76+
console.error(e);
77+
process.exit(2);
78+
});

extensions/ql-vscode/src/extension.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ import {
116116
} from './remote-queries/gh-api/variant-analysis';
117117
import { VariantAnalysisManager } from './remote-queries/variant-analysis-manager';
118118
import { createVariantAnalysisContentProvider } from './remote-queries/variant-analysis-content-provider';
119-
import { MockGitHubApiServer } from './mocks/mock-gh-api-server';
119+
import { VSCodeMockGitHubApiServer } from './mocks/vscode-mock-gh-api-server';
120120
import { VariantAnalysisResultsManager } from './remote-queries/variant-analysis-results-manager';
121121

122122
/**
@@ -1194,7 +1194,7 @@ async function activateWithInstalledDistribution(
11941194
)
11951195
);
11961196

1197-
const mockServer = new MockGitHubApiServer(ctx);
1197+
const mockServer = new VSCodeMockGitHubApiServer(ctx);
11981198
ctx.subscriptions.push(mockServer);
11991199
ctx.subscriptions.push(
12001200
commandRunner(

0 commit comments

Comments
 (0)