Skip to content

Generate working main() methods in test files #3582

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

Closed
athomas opened this issue Sep 28, 2021 · 4 comments
Closed

Generate working main() methods in test files #3582

athomas opened this issue Sep 28, 2021 · 4 comments
Labels
in editor Relates to code editing or language features in lsp/analysis server Something to be fixed in the Dart analysis server is enhancement
Milestone

Comments

@athomas
Copy link

athomas commented Sep 28, 2021

Is your feature request related to a problem? Please describe.
When you create a new test file for use with package:test, you need a main method without arguments (e.g. void main()). However, the main template in vscode will insert void main(List<String> arguments) {.... The analyzer can't find an issue with that because it's a valid main method. However, running the tests fails because package:test barfs at the main method.

Describe the solution you'd like
A context aware template that inserts the main method with arguments in regular dart files but inserts a main method without arguments in _test.dart files. (Or alternatively, a second template that also comes up when you type main and inserts something appropriate for tests, perhaps even contains a bit more common test skeleton code like an actual test method or setUp/tearDown calls).

@pq

@DanTup
Copy link
Member

DanTup commented Sep 28, 2021

This is related to #2059 (and came up in #3347 and others). The proper fix is to move the snippets from the client to the server. This would allow them to adjust to many things like language versions (eg. conditionally include ?), account for lints, not show stful etc. in places it isn't valid to create a class, and also have different mains based on whether a file is a test (although I've found that "a test file" has conflicting definitions depending on who you ask :-) )

@DanTup DanTup added in editor Relates to code editing or language features in lsp/analysis server Something to be fixed in the Dart analysis server labels Sep 28, 2021
@DanTup DanTup added this to the On Deck milestone Sep 28, 2021
@pq
Copy link

pq commented Oct 1, 2021

/fyi @bwilkerson and @jwren

@DanTup DanTup modified the milestones: On Deck, v3.39.0 Apr 4, 2022
@DanTup
Copy link
Member

DanTup commented Apr 4, 2022

If you're using the latest VS Code extensions (just published) and on Flutter master, you can now user the dart.enableServerSnippets setting to switch to server-provided snippets. These will become enabled by default in a future release, but any feedback in the meantime is appreciated.

Server-provided snippets are more dynamic/context aware and handle this issue, plus some others:

  • main methods in test files won't have args
  • Null operators like ? will only be inserted when null safety is enabled
  • Snippets won't show up in contexts they're not valid (for example stful will no longer show up when you're not at the top level)

Right now this is only in VS Code, but with some work in the IntelliJ extension (+ adding to the original analysis server protocol) it should be possible to use the same shared snippets there.

@DanTup
Copy link
Member

DanTup commented Apr 28, 2022

In the upcoming release of Dart-Code, server snippets will be enabled by default if your SDK supports them (which if you're on stable won't be until the next SDK releases, but if you're on master/dev, it probably already does). The server-provided snippets will conditionally include List<String> args depending on whether the file is a test file.

@DanTup DanTup closed this as completed Apr 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in editor Relates to code editing or language features in lsp/analysis server Something to be fixed in the Dart analysis server is enhancement
Projects
None yet
Development

No branches or pull requests

3 participants