Skip to content

Allow tests in any marked test dir and optionally in sources #5770

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

Merged
merged 2 commits into from
Sep 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/io/flutter/FlutterBundle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,23 @@ settings.open.inspector.on.launch=Open Flutter Inspector view on app launch
settings.hot.reload.on.save=Perform hot reload on save
settings.enable.embedding.devtools=Enable embedding DevTools in the Flutter Inspector tool window
settings.enable.bazel.hot.restart=Enable Bazel hot restart (refreshes generated files)
settings.allow.tests.in.sources=<html>Allow files ending with <b>_test.dart</b> to be recognized as tests
settings.allow.tests.tooltip=The directory must be marked as a sources root, such as <b>lib</b>.
settings.font.packages=Font Packages
settings.show.all.configs=Show all possible run configurations for apps or tests, even if a created configuration already exists
settings.show.all.configs.tooltip=If there is a defined run configuration to watch a specific test and one to just run it, show both.
settings.enable.embedding.devtools.tooltip=Use the web-based DevTools inspector in the Flutter Inspector tool window.
settings.enable.androi.gradle.sync.tooltip=Provides advanced editing capabilities for Java and Kotlin code. Uses Gradle to find Android libraries then links them into the Flutter project.
settings.experiments=Experiments
settings.editor=Editor
settings.show.build.guides=Show UI Guides for build methods
settings.show.build.guides.tooltip=Visualize the widget tree structure from the outline view directly in build methods.
settings.format.code.tooltip=On save, run dartfmt on changed Dart files.
settings.organize.imports.tooltip=On save, organize imports for changed Dart files.
settings.show.closing.labels=Show closing labels in Dart source code
settings.sdk.copy.content=Copy to Clipboard
settings.report.analytics.tooltip=Report anonymized usage information to Google Analytics.
settings.enable.verbose.logging.tooltip=Enables verbose logging (this can be useful for diagnostic purposes).

action.new.project.title=New Flutter Project...
welcome.new.project.title=Create New Flutter Project
Expand Down
46 changes: 41 additions & 5 deletions src/io/flutter/FlutterUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@
import com.intellij.openapi.progress.ProcessCanceledException;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectManager;
import com.intellij.openapi.roots.ModuleRootManager;
import com.intellij.openapi.roots.ModuleSourceOrderEntry;
import com.intellij.openapi.roots.OrderEntry;
import com.intellij.openapi.roots.*;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VfsUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
Expand All @@ -38,6 +37,7 @@
import com.jetbrains.lang.dart.psi.DartFile;
import io.flutter.pub.PubRoot;
import io.flutter.pub.PubRootCache;
import io.flutter.settings.FlutterSettings;
import io.flutter.utils.AndroidUtils;
import io.flutter.utils.FlutterModuleUtils;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -204,12 +204,48 @@ public static boolean isInTestDir(@Nullable DartFile file) {

// Check that we're in a project path that starts with 'test/'.
final String relativePath = root.getRelativePath(file.getVirtualFile());
if (relativePath == null || !(relativePath.startsWith("test/") || relativePath.startsWith("integration_test/"))) {
if (relativePath == null) {
return false;
}
final Module module = root.getModule(file.getProject());
if (!(relativePath.startsWith("test/") || relativePath.startsWith("integration_test/"))) {
if (!isInTestOrSourceRoot(module, file)) {
return false;
}
}

// Check that we're in a Flutter module.
return FlutterModuleUtils.isFlutterModule(root.getModule(file.getProject()));
return FlutterModuleUtils.isFlutterModule(module);
}

private static boolean isInTestOrSourceRoot(Module module, @NotNull DartFile file) {
final ContentEntry[] entries = ModuleRootManager.getInstance(module).getContentEntries();
final VirtualFile virtualFile = file.getContainingFile().getVirtualFile();
boolean foundSourceRoot = false;
for (ContentEntry entry : entries) {
for (SourceFolder folder : entry.getSourceFolders()) {
final VirtualFile folderFile = folder.getFile();
if (folderFile == null) {
continue;
}
if (folderFile.equals(VfsUtil.getCommonAncestor(folderFile, virtualFile))) {
if (folder.getRootType().isForTests()) {
return true; // Test file is in a directory marked as a test source root, but not named 'test'.
}
else {
foundSourceRoot = true;
break;
}
}
}
}
if (foundSourceRoot) {
// The file is in a sources root but not marked as tests.
if (file.getName().endsWith(("_test.dart")) && FlutterSettings.getInstance().isAllowTestsInSourcesRoot()) {
return true;
}
}
return false;
}

public static boolean isIntegrationTestingMode() {
Expand Down
19 changes: 19 additions & 0 deletions src/io/flutter/sdk/FlutterSearchableOptionContributor.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
public class FlutterSearchableOptionContributor extends SearchableOptionContributor {
@Override
public void processOptions(@NotNull SearchableOptionProcessor processor) {
// Keep these in the same order as FlutterBundle.properties
add(processor, FlutterBundle.message("settings.try.out.features.still.under.development"));
add(processor, FlutterBundle.message("settings.enable.android.gradle.sync"));
// For some reason the word "report" is ignored by the search feature, but the other words work.
Expand All @@ -28,6 +29,24 @@ public void processOptions(@NotNull SearchableOptionProcessor processor) {
add(processor, FlutterBundle.message("settings.open.inspector.on.launch"));
add(processor, FlutterBundle.message("settings.hot.reload.on.save"));
add(processor, FlutterBundle.message("settings.enable.embedding.devtools"));
add(processor, FlutterBundle.message("settings.enable.bazel.hot.restart"));
add(processor, FlutterBundle.message("settings.allow.tests.in.sources"));
add(processor, FlutterBundle.message("settings.allow.tests.tooltip"));
add(processor, FlutterBundle.message("settings.font.packages"));
add(processor, FlutterBundle.message("settings.show.all.configs"));
add(processor, FlutterBundle.message("settings.show.all.configs.tooltip"));
add(processor, FlutterBundle.message("settings.enable.embedding.devtools.tooltip"));
add(processor, FlutterBundle.message("settings.enable.androi.gradle.sync.tooltip"));
add(processor, FlutterBundle.message("settings.experiments"));
add(processor, FlutterBundle.message("settings.editor"));
add(processor, FlutterBundle.message("settings.show.build.guides"));
add(processor, FlutterBundle.message("settings.show.build.guides.tooltip"));
add(processor, FlutterBundle.message("settings.format.code.tooltip"));
add(processor, FlutterBundle.message("settings.organize.imports.tooltip"));
add(processor, FlutterBundle.message("settings.show.closing.labels"));
add(processor, FlutterBundle.message("settings.sdk.copy.content"));
add(processor, FlutterBundle.message("settings.report.analytics.tooltip"));
add(processor, FlutterBundle.message("settings.enable.verbose.logging.tooltip"));
}

private static void add(@NotNull SearchableOptionProcessor processor, @NotNull String key) {
Expand Down
43 changes: 26 additions & 17 deletions src/io/flutter/sdk/FlutterSettingsConfigurable.form
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
<grid row="1" column="2" row-span="1" col-span="1" vsize-policy="3" hsize-policy="1" anchor="0" fill="0" indent="1" use-parent-layout="false"/>
</constraints>
<properties>
<toolTipText value="Copy to Clipboard"/>
<toolTipText resource-bundle="io/flutter/FlutterBundle" key="settings.sdk.copy.content"/>
</properties>
</component>
</children>
Expand All @@ -73,7 +73,7 @@
</constraints>
<properties>
<text resource-bundle="io/flutter/FlutterBundle" key="settings.report.google.analytics"/>
<toolTipText value="Report anonymized usage information to Google Analytics."/>
<toolTipText resource-bundle="io/flutter/FlutterBundle" key="settings.report.analytics.tooltip"/>
</properties>
</component>
<component id="be19f" class="javax.swing.JCheckBox" binding="myEnableVerboseLoggingCheckBox" default-binding="true">
Expand All @@ -82,7 +82,7 @@
</constraints>
<properties>
<text resource-bundle="io/flutter/FlutterBundle" key="settings.enable.verbose.logging"/>
<toolTipText value="Enables verbose logging (this can be useful for diagnostic purposes)."/>
<toolTipText resource-bundle="io/flutter/FlutterBundle" key="settings.enable.verbose.logging.tooltip"/>
</properties>
</component>
<component id="c59f0" class="com.intellij.ui.components.labels.LinkLabel" binding="myPrivacyPolicy">
Expand All @@ -92,8 +92,17 @@
<properties>
<horizontalAlignment value="2"/>
<horizontalTextPosition value="2"/>
<text value="www.google.com/policies/privacy"/>
<toolTipText value="http://www.google.com/policies/privacy/"/>
<text value="www.google.com/policies/privacy" noi18n="true"/>
<toolTipText value="http://www.google.com/policies/privacy/" noi18n="true"/>
</properties>
</component>
<component id="efbd7" class="javax.swing.JCheckBox" binding="myAllowTestsInSourcesRoot">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text resource-bundle="io/flutter/FlutterBundle" key="settings.allow.tests.in.sources"/>
<toolTipText resource-bundle="io/flutter/FlutterBundle" key="settings.allow.tests.tooltip"/>
</properties>
</component>
</children>
Expand Down Expand Up @@ -156,15 +165,15 @@
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="etched" title="Editor"/>
<border type="etched" title-resource-bundle="io/flutter/FlutterBundle" title-key="settings.editor"/>
<children>
<component id="70775" class="javax.swing.JCheckBox" binding="myShowBuildMethodGuides">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Show UI Guides for build methods"/>
<toolTipText value="Visualize the widget tree structure from the outline view directly in build methods."/>
<text resource-bundle="io/flutter/FlutterBundle" key="settings.show.build.guides"/>
<toolTipText resource-bundle="io/flutter/FlutterBundle" key="settings.show.build.guides.tooltip"/>
</properties>
</component>
<component id="a0dd8" class="javax.swing.JCheckBox" binding="myFormatCodeOnSaveCheckBox" default-binding="true">
Expand All @@ -173,7 +182,7 @@
</constraints>
<properties>
<text resource-bundle="io/flutter/FlutterBundle" key="settings.format.code.on.save"/>
<toolTipText value="On save, run dartfmt on changed Dart files."/>
<toolTipText resource-bundle="io/flutter/FlutterBundle" key="settings.format.code.tooltip"/>
</properties>
</component>
<component id="8bd46" class="javax.swing.JCheckBox" binding="myOrganizeImportsOnSaveCheckBox" default-binding="true">
Expand All @@ -182,15 +191,15 @@
</constraints>
<properties>
<text resource-bundle="io/flutter/FlutterBundle" key="settings.organize.imports.on.save"/>
<toolTipText value="On save, organize imports for changed Dart files."/>
<toolTipText resource-bundle="io/flutter/FlutterBundle" key="settings.organize.imports.tooltip"/>
</properties>
</component>
<component id="92f7c" class="javax.swing.JCheckBox" binding="myShowClosingLabels" default-binding="true">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Show closing labels in Dart source code"/>
<text resource-bundle="io/flutter/FlutterBundle" key="settings.show.closing.labels"/>
</properties>
</component>
</children>
Expand All @@ -201,7 +210,7 @@
<grid row="4" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="etched" title="Experiments"/>
<border type="etched" title-resource-bundle="io/flutter/FlutterBundle" title-key="settings.experiments"/>
<children>
<component id="42d6c" class="javax.swing.JLabel">
<constraints>
Expand All @@ -217,7 +226,7 @@
</constraints>
<properties>
<text resource-bundle="io/flutter/FlutterBundle" key="settings.enable.android.gradle.sync"/>
<toolTipText value="Provides advanced editing capabilities for Java and Kotlin code. Uses Gradle to find Android libraries then links them into the Flutter project."/>
<toolTipText resource-bundle="io/flutter/FlutterBundle" key="settings.enable.androi.gradle.sync.tooltip"/>
</properties>
</component>
<component id="33088" class="javax.swing.JCheckBox" binding="myEnableHotUiCheckBox">
Expand All @@ -235,16 +244,16 @@
</constraints>
<properties>
<text resource-bundle="io/flutter/FlutterBundle" key="settings.enable.embedding.devtools"/>
<toolTipText value="Use the web-based DevTools inspector in the Flutter Inspector tool window"/>
<toolTipText resource-bundle="io/flutter/FlutterBundle" key="settings.enable.embedding.devtools.tooltip"/>
</properties>
</component>
<component id="e902c" class="javax.swing.JCheckBox" binding="myShowAllRunConfigurationsInContextCheckBox">
<constraints>
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Show all possible run configurations for apps or tests, even if a created configuration already exists"/>
<toolTipText value="If there is a defined run configuration to watch a specific test and one to just run it, show both."/>
<text resource-bundle="io/flutter/FlutterBundle" key="settings.show.all.configs"/>
<toolTipText resource-bundle="io/flutter/FlutterBundle" key="settings.show.all.configs.tooltip"/>
</properties>
</component>
</children>
Expand All @@ -255,7 +264,7 @@
<grid row="6" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="etched" title="Font Packages"/>
<border type="etched" title-resource-bundle="io/flutter/FlutterBundle" title-key="settings.font.packages"/>
<children>
<scrollpane id="4e51e">
<constraints>
Expand Down
7 changes: 7 additions & 0 deletions src/io/flutter/sdk/FlutterSettingsConfigurable.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public class FlutterSettingsConfigurable implements SearchableConfigurable {
private JCheckBox myShowClosingLabels;
private FixedSizeButton myCopyButton;
private JTextArea myFontPackagesTextArea; // This should be changed to a structured list some day.
private JCheckBox myAllowTestsInSourcesRoot;

private final @NotNull Project myProject;
private final WorkspaceCache workspaceCache;
Expand Down Expand Up @@ -235,6 +236,10 @@ public boolean isModified() {
return true;
}

if (settings.isAllowTestsInSourcesRoot() != myAllowTestsInSourcesRoot.isSelected()) {
return true;
}

if (!settings.getFontPackages().equals(myFontPackagesTextArea.getText())) {
return true;
}
Expand Down Expand Up @@ -290,6 +295,7 @@ public void apply() throws ConfigurationException {
settings.setEnableHotUi(myEnableHotUiCheckBox.isSelected());
settings.setEnableEmbeddedBrowsers(myEnableEmbeddedBrowsersCheckBox.isSelected());
settings.setEnableBazelHotRestart(myEnableBazelHotRestartCheckBox.isSelected());
settings.setAllowTestsInSourcesRoot(myAllowTestsInSourcesRoot.isSelected());
settings.setShowAllRunConfigurationsInContext(myShowAllRunConfigurationsInContextCheckBox.isSelected());
settings.setFontPackages(myFontPackagesTextArea.getText());

Expand Down Expand Up @@ -336,6 +342,7 @@ public void reset() {

myEnableEmbeddedBrowsersCheckBox.setSelected(settings.isEnableEmbeddedBrowsers());
myEnableBazelHotRestartCheckBox.setSelected(settings.isEnableBazelHotRestart());
myAllowTestsInSourcesRoot.setSelected(settings.isAllowTestsInSourcesRoot());

myOrganizeImportsOnSaveCheckBox.setEnabled(myFormatCodeOnSaveCheckBox.isSelected());
myIncludeAllStackTraces.setEnabled(myShowStructuredErrors.isSelected());
Expand Down
14 changes: 14 additions & 0 deletions src/io/flutter/settings/FlutterSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public class FlutterSettings {
private static final String enableBazelHotRestartKey = "io.flutter.editor.enableBazelHotRestart";
private static final String showBazelHotRestartWarningKey = "io.flutter.showBazelHotRestartWarning";
private static final String fontPackagesKey = "io.flutter.fontPackages";
private static final String allowTestsInSourcesRootKey = "io.flutter.allowTestsInSources";

// TODO(helin24): This is to change the embedded browser setting back to true only once for Big Sur users. If we
// switch to enabling the embedded browser for everyone, then delete this key.
Expand Down Expand Up @@ -141,6 +142,10 @@ public void sendSettingsToAnalytics(Analytics analytics) {
analytics.sendEvent("settings", afterLastPeriod(changeBigSurToTrueKey));
}

if (isAllowTestsInSourcesRoot()) {
analytics.sendEvent("settings", afterLastPeriod(allowTestsInSourcesRootKey));
}

if (!getFontPackages().isEmpty()) {
analytics.sendEvent("settings", afterLastPeriod(fontPackagesKey));
}
Expand Down Expand Up @@ -362,4 +367,13 @@ public void setChangeBigSurToTrue(boolean value) {
getPropertiesComponent().setValue(changeBigSurToTrueKey, value, true);
fireEvent();
}

public boolean isAllowTestsInSourcesRoot() {
return getPropertiesComponent().getBoolean(allowTestsInSourcesRootKey, false);
}

public void setAllowTestsInSourcesRoot(boolean value) {
getPropertiesComponent().setValue(allowTestsInSourcesRootKey, value, false);
fireEvent();
}
}