X Tutup
The Wayback Machine - https://web.archive.org/web/20250521114228/https://github.com/github/vscode-codeql/pull/645
Skip to content

Add integration tests with the CLI #645

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 4 commits into from
Dec 4, 2020

Conversation

aeisenberg
Copy link
Contributor

@aeisenberg aeisenberg commented Oct 26, 2020

Note that even though this PR is labeled as Complexity: High, it is not particularly dangerous in that most of the complexity is around how the tests run.

This commit adds integration tests that run commands using the CLI. This
change introduces a number of enhancements in order to get there.

  1. Augments the index-template.ts file so that it downloads an appropriate cli version if requested.
  2. Adds the ensureCli.ts that performs the download if a a suitable version is not already installed. See the comments in the file for how this is done.
  3. Changes how run-integration-tests is done so that the directories run are specified through a cli argument.
  4. Updates the main.yml workflow so that it also runs the cli-integration tests.
  5. Takes advantage of the return value of the call to activate on the extension. This allows the integration tests to have access to internal variables of the extension like the context, cli, and query server.
  6. And of course, adds a handful of simple tests that ensure we have a cli installed of the correct version.

Resolves #642.

Checklist

  • [n/a] CHANGELOG.md has been updated to incorporate all user visible changes made by this pull request.
  • Issues have been created for any UI or other user-facing changes made by this pull request.
  • [n/a] @github/docs-content-dsp has been cc'd in all issues for UI or other user-facing changes made by this pull request.

@aeisenberg aeisenberg marked this pull request as draft October 26, 2020 15:36
@aeisenberg aeisenberg force-pushed the add-cli-integration-tests branch 11 times, most recently from 8d76a68 to 5769c14 Compare October 28, 2020 21:15
@aeisenberg aeisenberg marked this pull request as ready for review October 28, 2020 21:16
@aeisenberg
Copy link
Contributor Author

OK...I'm making progress here. Tests are still failing, but now it is a different failure. Uncovered by fixing the first failure.

@aeisenberg aeisenberg marked this pull request as draft October 28, 2020 21:34
@aeisenberg aeisenberg force-pushed the add-cli-integration-tests branch 2 times, most recently from 1d88795 to f53d728 Compare October 30, 2020 15:36
@aeisenberg aeisenberg marked this pull request as ready for review October 30, 2020 15:36
@aeisenberg aeisenberg added the Complexity: High Requires careful design or review. label Oct 30, 2020
Copy link
Contributor Author

@aeisenberg aeisenberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a bunch of comments that help explain things in the commit. Some of the comments are reminders for myself to change things later.

strategy:
matrix:
os: [ubuntu-latest, windows-latest]
version: ['v2.3.1', 'v2.3.0']
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can choose as many versions as we like. Though, my suggestion is to use the latest as well as one for each supported version of LGTM.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We've had a few recent releases, so let's go with 2.2.6, 2.3.3, 2.4.0, latest.
https://github.com/github/codeql-cli-binaries/releases/latest will always point you to the latest release, so we should deliberately test against that even if we are slow to update the hardcoded list.

@@ -4,6 +4,7 @@
# Generated files
/dist/
out/
build/
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Over time, I'd like to move all build artifacts to the build directory. This will make it very clear which pieces we can easily clean up.

@@ -687,7 +687,8 @@
"watch:extension": "tsc --watch",
"test": "mocha --exit -r ts-node/register test/pure-tests/**/*.ts",
"preintegration": "rm -rf ./out/vscode-tests && gulp",
"integration": "node ./out/vscode-tests/run-integration-tests.js",
"integration": "node ./out/vscode-tests/run-integration-tests.js no-workspace,minimal-workspace",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: must pass in the directories to test as part of the argument.

/**
* Get the name of the codeql cli installation we prefer to install, based on our current platform.
*/
public static getRequiredAssetName(): string {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made static so we can use this in our integration tests.

beforeEach(async () => {
sandbox = sinon.createSandbox();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use sinon.sandbox where possible. This ensures all stubs and mocks are cleaned up when sandbox.restore() is called. One problem with the integration tests earlier on was that mocks were not being properly restored and we were seeing confusing errors across tests.

const manager = new (createModule().DistributionManager)(undefined as any, { customCodeQlPath: pathToCmd } as any, undefined as any);
it('should not warn when deprecated launcher is used, but no new launcher is available', async function() {
const manager = new (createModule().DistributionManager)(
{ customCodeQlPath: pathToCmd } as any,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DistributionManager has changed. So, need to update these tests.

for (const integrationTestSuite of integrationTestSuites) {
await runTestsWithRetryOnSegfault(integrationTestSuite, 3);
// Which tests to run. Use a comma-separated list of directories.
const testDirsString = process.argv[2];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See package.json for where this is used.

@@ -94,3 +104,25 @@ async function main() {
}

main();


function getLaunchArgs(dir: TestDir) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was uglier than I had hoped. '--disable-extensions' needs to be called for minimal and no workspace runs otherwise ambient extensions get in the way of the run. This may not be a problem on the build server, but it is definitely a problem locally.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Annoying but fine for now, since I see you manually install the Test Explorer.

command.match(/^codeQLDatabases\./)
|| command.match(/^codeQLQueryHistory\./)
|| command.match(/^codeQLAstViewer\./)
) {
scopedCmds.add(command);
expect(title).not.to.be.undefined;
commandTitles[command] = title!;
}
else {
} else if (
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is no longer necessary.

@aeisenberg aeisenberg force-pushed the add-cli-integration-tests branch from f53d728 to 3ceaba9 Compare October 30, 2020 16:23
@aeisenberg aeisenberg force-pushed the add-cli-integration-tests branch 4 times, most recently from 4d566a9 to 7cf33a8 Compare November 24, 2020 19:20
@aeisenberg aeisenberg requested review from alexet and removed request for jcreedcmu November 24, 2020 19:26
@aeisenberg aeisenberg force-pushed the add-cli-integration-tests branch 10 times, most recently from 25a608f to 92c70f8 Compare December 1, 2020 16:26
@aeisenberg aeisenberg force-pushed the add-cli-integration-tests branch 2 times, most recently from c345b10 to ce22b15 Compare December 2, 2020 01:52
Copy link
Contributor

@adityasharad adityasharad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great! Didn't go into the details of the test harness execution from Node (if it runs, I trust you) but the refactoring and tests themselves look good.

@@ -59,16 +59,15 @@ interface QueryHistoryDataProvider extends vscode.TreeDataProvider<CompletedQuer
/**
* Tree data provider for the query history view.
*/
class HistoryTreeDataProvider implements QueryHistoryDataProvider {
class HistoryTreeDataProvider extends DisposableObject implements QueryHistoryDataProvider {
/**
* XXX: This idiom for how to get a `.fire()`-able event emitter was
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still planning to do this?


async () => {
// Set the CLI version here before activation to ensure we don't accidentally try to download a cli
await workspace.getConfiguration().update('codeQL.cli.executablePath', process.env.CLI_PATH, ConfigurationTarget.Global);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call. Would it make sense to also test picking up a CLI from the PATH? (Perhaps we have that tested elsewhere.)

import { assertNever } from '../pure/helpers-pure';

// For some reason, `TestOptions` is not exported directly from `vscode-test`,
// but we can be tricky and import directly from the out file.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

crafty

Comment on lines 31 to 32
NoWorskspace = 'no-workspace',
MinimalWorskspace = 'minimal-workspace',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor typos: Workspace

@@ -94,3 +104,25 @@ async function main() {
}

main();


function getLaunchArgs(dir: TestDir) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Annoying but fine for now, since I see you manually install the Test Explorer.

strategy:
matrix:
os: [ubuntu-latest, windows-latest]
version: ['v2.3.1', 'v2.3.0']
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We've had a few recent releases, so let's go with 2.2.6, 2.3.3, 2.4.0, latest.
https://github.com/github/codeql-cli-binaries/releases/latest will always point you to the latest release, so we should deliberately test against that even if we are slow to update the hardcoded list.


// CLI version to test. Hard code the latest as default. And be sure
// to update the env if it is not otherwise set.
const CLI_VERSION = process.env.CLI_VERSION || 'v2.3.1';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can hardcode latest, though you should check the API docs to make sure the asset download URL still works the same.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, a downside of using latest locally is that once you download latest once, you will never re-download. We check whether or not to re-download based on the presence of the directory name. The directory name is generated based on the version number. I could probably be more sophisticated about this, but that would add complexity.

This commit adds integration tests that run commands using the CLI. This
change introduces a number of enhancements in order to get there.

1. Augments the index-template.ts file so that it downloads an
appropriate cli version if requested.
2. Adds the ensureCli.ts that performs the download if a a suitable
version is not already installed. See the comments in the file for how
this is done.
3. Changes how run-integration-tests is done so that the directories
run are specified through a cli argument.
4. Updates the main.yml workflow so that it also runs the
cli-integration tests.
5. Takes advantage of the return value of the call to `activate` on the
extension. This allows the integration tests to have access to internal
variables of the extension like the context, cli, and query server.
6. And of course, adds a handful of simple tests that ensure we have a
cli installed of the correct version.
In order to do this, needed to add a few extra pieces:

* extracted the simple database download so that it only happens
  once and is shared across all tests.
* needed to update mocha to latest version since that has the new API
* But typings isn't updated yet, so submitted a PR into DefinitelyTyped
  for that.
* Added a concept of helper files for test runs. These helper files
  will contain all the shared global setup.

Unfortunately, at this point, we can't run using a language pack since
we would also need to download the the ql repository from somewhere.
Also, fix a typo and remove comment.
@aeisenberg aeisenberg merged commit 370dbcb into github:main Dec 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Complexity: High Requires careful design or review.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Run integration tests against multiple versions of CodeQL
2 participants
X Tutup