Skip to content

Conversation

@robertbrignull
Copy link
Contributor

I'd be interested to know what people think of this. This PR puts all calls to CodeQL behind an object with an interface that we can override in unit tests. Currently it's not possible to write tests for any area that makes CodeQL calls because it won't be available and you don't want to be actually running queries or creating databases during the unit tests.

I'm doing this now because I'd like to move processing of external queries earlier in the process to where we parse the config, and that requires running CodeQL in codepaths that we currently unit test.

As well as making more code testable, this also hopefully makes things easier to understand by keeping all the codeql invocations in one place and imposing that there is a named function interface to it with documentation so it's more clear what each call does.

I had to use a bit of judgement (i.e. random guesses) at where to draw the boundary of the interface. In some cases it includes a little bit of logic, and in one case it's actually two CodeQL calls combined into one. If you have any suggestions for improvements then please say.

@sampart, @Daverlo, @chrisgavin, @rneatherway

Merge / deployment checklist

  • Run test builds as necessary. Can be on this repository or elsewhere as needed in order to test the change - please include links to tests in other repos!
    • CodeQL using init/analyze actions
    • 3rd party tool using upload action
  • Confirm this change is backwards compatible with existing workflows.
  • Confirm the readme has been updated if necessary.

Copy link
Contributor

@sampart sampart left a comment

Choose a reason for hiding this comment

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

Requesting changes because of my suggested code change about adding [ ... ], since that looks like a bug. The point about the parameter name database being overloaded is fairly important for code clarity too, I think.

src/autobuild.ts Outdated
const cmdName = process.platform === 'win32' ? 'autobuild.cmd' : 'autobuild.sh';
const autobuildCmd = path.join(path.dirname(codeqlCmd), language, 'tools', cmdName);

const autobuildCmd = path.join(codeQL.getDir(), language, 'tools', cmdName);
Copy link
Contributor

Choose a reason for hiding this comment

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

What made you decide to not include autobuilding as a method on the CodeQL object? I'm not saying you should have done, I'd just like to understand your thinking.

Copy link
Contributor

@rneatherway rneatherway Jul 1, 2020

Choose a reason for hiding this comment

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

The interface offered by CodeQL may change (for example it may become an official command). If anything that points more strongly in favour of making it a method on the CodeQL object.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I agree I think it fits better as being encapsulated within a method on the CodeQL object.

* Extract code for a scanned language using 'codeql database trace-command'
* and running the language extracter.
*/
extractScannedLanguage(database: string, language: string): Promise<void>;
Copy link
Contributor

Choose a reason for hiding this comment

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

Would databasePath be more meaningful than just database here (and in finalizeDatabase)?

Copy link
Contributor

Choose a reason for hiding this comment

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

Or databaseFolder to match what's used elsewhere (although I prefer databasePath).

Copy link
Contributor

Choose a reason for hiding this comment

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

In general, the term database is used as an argument in various places, and sometimes it's a full path and sometimes just a database name (assuming I've understood correctly). I'd recommend using databasePath and databaseName to differentiate, rather than using the ambiguous database for both.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Very good points. I agree better names would be worth it here. I was a bit lazy when writing this. I'll improve them all to be more descriptive.

src/codeql.ts Outdated
},
getTracerEnv: async function(database: string, compilerSpec: string | undefined) {
let envFile = path.resolve(database, 'working', 'env.tmp');
const compilerSpecArg = compilerSpec ? "--compiler-spec=" + compilerSpec : [];
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
const compilerSpecArg = compilerSpec ? "--compiler-spec=" + compilerSpec : [];
const compilerSpecArg = compilerSpec ? ["--compiler-spec=" + compilerSpec] : [];

without the square brackets here, ...compilerSpecArg a few lines later will split the string ""--compiler-spec=____" into an array of individual characters, which I don't think is what you want.

The deleted version of this function in src/setup-tracer.ts had the square brackets.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you. Good spot.

@rneatherway
Copy link
Contributor

I like the idea, should help a lot with testing.

@robertbrignull
Copy link
Contributor Author

I think the new alerts are false positives due to the inaccuracy of our custom query tracking calls in javascript. Because the entire CodeQL object is defined in that method it's deeming that all the methods are called, even though they're not. I think that's what's happening anyway.

@robertbrignull robertbrignull merged commit 9da537e into main Jul 6, 2020
@robertbrignull robertbrignull deleted the codeql_mock branch July 6, 2020 09:38
@robertbrignull
Copy link
Contributor Author

I'll fix the queries in a separate PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants