Skip to content
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

Add support for viewing unit test databases with the AST viewer #2277

Open
ewillonermsft opened this issue Apr 5, 2023 · 4 comments
Open
Labels
enhancement New feature or request VSCode

Comments

@ewillonermsft
Copy link

Is your feature request related to a problem? Please describe.
When developing CodeQL queries, it is convenient to write unit tests and query the database that is output from running them to iteratively test/develop a query. The alternative is to maintain a separate project and database which increases the number of moving parts and commands that need to be run when altering code or building additional tests and debugging. Currently you can import a unit test database using the CodeQL extension, however you will not be able to view the AST of its source code.

The current workaround is cumbersome and time consuming, it would be great if it could be automatically handled by the extension.

Describe the solution you'd like
Unit test databases imported by CodeQL VSCode extension should be able to have their AST viewed without performing extra steps.

Describe alternatives you've considered
Currently, the work around looks like this:

  1. Run the unit test using the unit test tab in VS Code.
  2. Navigate to the unit test path. You will see a XYZ.testproj folder.
  3. Remove .testproj from the folder name
  4. Navigate to XYZ/src and zip the folder inside. Rename to src.zip
  5. Move the newly created src.zip to the XYZ unit test directory
  6. Delete the src/ folder
  7. Import the database with the CodeQL extension. You will now be able to view the AST.

Additional context
I have only tested this on Windows, I am unsure of if the existing workaround works on Linux or MacOS.

@ewillonermsft ewillonermsft added the enhancement New feature or request label Apr 5, 2023
@github-actions github-actions bot added the VSCode label Apr 5, 2023
@shati-patel
Copy link
Contributor

Hi @ewillonermsft, thank you for the issue! 😊

The extension doesn't really support unzipped source folders, so I think your workaround of manually zipping the src folder is still the best way to do this.

(cc @aeisenberg - This looks like a duplicate of #1324, but could you confirm in case you have any extra context since it was opened last year? 😄)

@Marcono1234
Copy link
Contributor

Or duplicate / relates to #393?


  1. Remove .testproj from the folder name

This is quite interesting though because the extension apparently explicitly prevents adding the source archive if the folder name ends with .testproj:

// Ignore the source archive for QLTest databases.
return extname(this.databaseUri.fsPath) === ".testproj";

It is not clear to me why that is the case; the code and the comments don't seem to give any hint. Maybe due to potential issues when the test which created the database is executed again?

Either way, the current behavior is a bit annoying because there is no indication that the source archive is intentionally ignored. I was wondering why even after I zipped the source files as src.zip the extension failed to add the source files, saying:

Could not add source folder because MyTest.testproj has no source archive.

@ewillonermsft
Copy link
Author

This workaround hasn't worked for me for several months, though I'm not sure exactly when it stopped. It would be awesome if this work flow could get improved rather than constantly looking for work arounds.

@Marcono1234
Copy link
Contributor

Your workaround still works for me with extension version 1.9.1 and CodeQL CLI version 2.14.6, also on Windows.

A few important points:

  • Removing .testproj from the folder name is important (as pointed out above)
  • Deleting the src/ directory does not seem to be necessary
  • Make sure that the src.zip does not contain a nested src directory as root
  • If the source files weren't added to the workspace
    • Have a look at the extension log ("View" > "Output" > "CodeQL Extension Log") regarding why the source could not be added
    • Try right-clicking the database and select "Add Database Source to Workspace"
  • For viewing the AST, make sure you are viewing the source code (e.g. the test Java class) from the database sources, and not directly from the unit test directory; otherwise there will be no option for showing the AST

Maybe there could also be a problem with your VS Code workspace? For me it works when the project was already opened as workspace; and in case it was only opened as regular folder, the CodeQL VS Code extension seems to convert it to a workspace automatically. Maybe some of this is not working for you?

Though I agree that a proper solution instead of these workarounds would be great.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request VSCode
Projects
None yet
Development

No branches or pull requests

3 participants