vscode-codeql icon indicating copy to clipboard operation
vscode-codeql copied to clipboard

Add support for viewing unit test databases with the AST viewer

Open ewillonermsft opened this issue 2 years ago • 4 comments

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 avatar Apr 05 '23 18:04 ewillonermsft

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 https://github.com/github/vscode-codeql/issues/1324, but could you confirm in case you have any extra context since it was opened last year? 😄)

shati-patel avatar Apr 11 '23 13:04 shati-patel

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: https://github.com/github/vscode-codeql/blob/2410d2bfdd54f703a20138fd99e344af2cd74cfa/extensions/ql-vscode/src/databases/local-databases/database-item-impl.ts#L56-L57

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.

Marcono1234 avatar Oct 06 '23 23:10 Marcono1234

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.

ewillonermsft avatar Oct 10 '23 21:10 ewillonermsft

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.

Marcono1234 avatar Oct 10 '23 23:10 Marcono1234