Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

Put run/debug test links in the context menu instead #1088

Closed
willfaught opened this issue Jul 14, 2017 · 25 comments
Closed

Put run/debug test links in the context menu instead #1088

willfaught opened this issue Jul 14, 2017 · 25 comments

Comments

@willfaught
Copy link

willfaught commented Jul 14, 2017

The blank non-line line the run/debug test links are in looks weird and makes it hard to tell at a glance what I'm looking at.

screen shot 2017-07-13 at 10 43 06 pm

Can they be moved to context menu items instead?

screen shot 2017-07-13 at 10 43 19 pm

@ramya-rao-a
Copy link
Contributor

You can control which commands are showed in the context menu by updating the go.editorContextMenuCommands setting.

This way you can get the run test in the menu but not the debug test
This is because run test at cursor is already a command and there is no command to debug test.

This is easily fixable and PRs are welcome.

@willfaught
Copy link
Author

And that would also make both the run and debug links in all those fake lines disappear?

@ramya-rao-a
Copy link
Contributor

the run/debug links are called codelens in general.

If you don't want to see them in any file that you work on you can set editor.codeLens to false
If you don't want to see them only in Go files, then set the individual codelens (test and references) in go.enableCodeLens to false

@willfaught
Copy link
Author

willfaught commented Jul 18, 2017

(Edited)

OK, so to get the run-test option in the context-menu, I have to add the run-test command to go.editorContextMenuCommands?

To remove the run-test and debug-test buttons from the editor buffer, I have to set go.enableCodeLens to false? Does Code Lens show up for Go files in any other context? Just wondering if I'd be losing anything else by disabling it.

How do I run the debug-test command? "Debug test" in the command palette shows nothing.

@ramya-rao-a
Copy link
Contributor

Does Code Lens show up for Go files in any other context?

Yes, we have references codelens, but this is disabled by default as it tends to be slow on large projects

How do I run the debug-test command? "Debug test" in the command palette shows nothing

There is one command to start debugging using the currently selected debug configuration. But there is none for debugging the test function under a cursor.

It should be pretty straight forward to add it though.

PRs are welcome :)

@willfaught
Copy link
Author

OK, so the closest I can get without adding anything new is to disable the Go Code Lens and add the run-test command to the context menu. Do you know by chance what value needs to be added to go.editorContextMenuCommands for running tests?

@willfaught
Copy link
Author

I see go.editorContextMenuCommands.testAtCursor is true by default, which seems to be the one I want, but as seen above, there's no "Go: Run Test" context menu item. If I set go.editorContextMenuCommands.testFile and .testPackage to true then I see "Go: Test File" and "Go: Test Package" context menu items, so it seems like something similar should be appearing for the test under the cursor.

Also, why are testFile and testPackage defaulted to false? They seem useful.

@willfaught
Copy link
Author

It seems if I set go.enableCodeLens to false I get a JSON lint error saying false is the wrong type, it expects object. If I set it to null or {} or {"runtests": false} then the feature isn't actually disabled. Bug?

@willfaught
Copy link
Author

screen shot 2017-07-19 at 2 40 58 pm

@willfaught
Copy link
Author

Also, I'm happy to add the debug-test context menu item, but I've never contributed before, so I'd have no idea where to start. Got any suggestions?

@ramya-rao-a
Copy link
Contributor

ramya-rao-a commented Jul 20, 2017

You need to disable the editor.codeLens to see the "test at cursor"
I remember adding the extra condition (See https://github.com/Microsoft/vscode-go/blob/0.6.62/package.json#L771) because it was redundant to have the context menu when the codeLens for test is present.

For disabling go codelens

"go.enableCodeLens": {
		"references": false,
		"runtest": false
	}

For Debug Test command

  • Register a new command "Go: Debug Test at Cursor"
    • you will need to add an entry in package.json
    • and do the actual registering in goMain.ts
  • Create a new file goDebugTest.ts for your code

@willfaught
Copy link
Author

willfaught commented Jul 20, 2017

I think it'd still be useful to have the run-test command in the menu even if Code Lens is enabled. Otherwise you have to know where to look to find the feature. Menus should always have what you're looking for, in my opinion.

Is setting go.enableCodeLens as you show above enough, or do I need to set editor.codeLens too (and if so, to what?)?

@willfaught
Copy link
Author

Got run-test working, had to disable editor.codeLens entirely. Whew! Thanks.

@willfaught
Copy link
Author

Made an attempt at adding a debug-test command, but hit a wall once I had the Command. It also wasn't clear where a TextDocument would come from in that context.

@ramya-rao-a
Copy link
Contributor

@willfaught
Copy link
Author

willfaught commented Jul 20, 2017 via email

@ramya-rao-a
Copy link
Contributor

you should be able to get the document from editor.document and get the editor from vscode.window.activeTextEditor

I have a feeling we are talking about 2 different things

Can you point me to your code in your repo where you feel you need the document and don't have it?

@willfaught
Copy link
Author

This is what I have now:

import vscode = require('vscode');
import { getTestFunctions } from './goTests';
import { Command, TextDocument } from 'vscode';
import path = require('path');

let debugConfig: any = {
	'name': 'Launch',
	'type': 'go',
	'request': 'launch',
	'mode': 'test'
};

/**
* Debugs the unit test at the primary cursor. Output is sent to the 'Go' channel.
*
* @param goConfig Configuration for the Go extension.
*/
export function debugTestAtCursor(goConfig: vscode.WorkspaceConfiguration, args: any) {
	let editor = vscode.window.activeTextEditor;
	if (!editor) {
		vscode.window.showInformationMessage('No editor is active.');
		return;
	}
	if (!editor.document.fileName.endsWith('_test.go')) {
		vscode.window.showInformationMessage('No tests found. Current file is not a test file.');
		return;
	}
	if (editor.document.isDirty) {
		vscode.window.showInformationMessage('File has unsaved changes. Save and try again.');
		return;
	}
	getTestFunctions(editor.document).then(testFunctions => {
		let testFunctionName: string;

		// We use functionName if it was provided as argument
		// Otherwise find any test function containing the cursor.
		if (args && args.functionName) {
			testFunctionName = args.functionName;
		} else {
			for (let func of testFunctions) {
				let selection = editor.selection;
				if (selection && func.location.range.contains(selection.start)) {
					testFunctionName = func.name;
					break;
				}
			};
		}

		if (!testFunctionName) {
			vscode.window.showInformationMessage('No test function found at cursor.');
			return;
		}

		const args = ['-test.run', testFunctionName];
		const program = path.dirname(editor.document.fileName);
		const env = goConfig['testEnvVars'] || {};
		const envFile = goConfig['testEnvFile'];

		let config = Object.assign({}, this.debugConfig, { args, program, env, envFile });
		let debugTestAtCursorCmd: Command = {
			title: 'debug test',
			command: 'vscode.startDebug',
			arguments: [ config ]
		};

		return vscode.commands.executeCommand('vscode.startDebug', debugTestAtCursorCmd)
	}).then(null, err => {
		console.error(err);
	});
}

Not sure about the use of executeCommand.

@ramya-rao-a
Copy link
Contributor

executeCommand is fine. That's how we programmatically run a command.

So where in there are you seeing the " there's a "document" TextDocument used that isn't available in the testAtCursor context" ?

@willfaught
Copy link
Author

It was in the const program = path.dirname(editor.document.fileName); part, editor.document was just document, some parameter. Does it look like this will work as-is?

@ramya-rao-a
Copy link
Contributor

it definitely should :)

@Ashiroq
Copy link
Contributor

Ashiroq commented Oct 28, 2018

Hi, I'm new here. I gave it a shot, and it seems to be working. Preparing my PR.

@willfaught
Copy link
Author

@Ashiroq Cool, thanks! My effort fell through.

@ramya-rao-a
Copy link
Contributor

Go: Debug function at Cursor is the command that implements this feature request in the latest update (0.9.0) to the Go extension.

Thanks @willfaught, @Ashiroq and @vladbarosan!

@willfaught
Copy link
Author

Thank you!

@vscodebot vscodebot bot locked and limited conversation to collaborators Mar 21, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants