Skip to content

Instantly share code, notes, and snippets.

@undeadcat
Last active December 12, 2018 11:53
Show Gist options
  • Save undeadcat/a7b1acd0469139650f51a7fe11437493 to your computer and use it in GitHub Desktop.
Save undeadcat/a7b1acd0469139650f51a7fe11437493 to your computer and use it in GitHub Desktop.
Tests example: something works, but errors are wrongly reported
Index: plugins/CucumberJavaScript/testData/execution/SimplePassed/features/step_definitions/stepDef.js
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- plugins/CucumberJavaScript/testData/execution/SimplePassed/features/step_definitions/stepDef.js (date 1544612688000)
+++ plugins/CucumberJavaScript/testData/execution/SimplePassed/features/step_definitions/stepDef.js (date 1544612688000)
@@ -0,0 +1,14 @@
+var {defineSupportCode} = require('cucumber');
+
+defineSupportCode(function ({And, But, Given, Then, When}) {
+ Given(/^test step$/, function () {
+ });
+ Given(/^I do stuff$/, function () {
+ });
+
+ Given(/^test step2$/, function () {
+ });
+ Given(/^I do stuff2$/, function () {
+ throw "this fails"
+ });
+});
\ No newline at end of file
Index: plugins/CucumberJavaScript/testData/execution/SimplePassed/features/test.feature
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- plugins/CucumberJavaScript/testData/execution/SimplePassed/features/test.feature (date 1544609844000)
+++ plugins/CucumberJavaScript/testData/execution/SimplePassed/features/test.feature (date 1544609844000)
@@ -0,0 +1,8 @@
+
+Feature: Test
+ Scenario: Test
+ Given test step
+ Given I do stuff
+ Scenario: Test2
+ Given test step2
+ Given I do stuff2
\ No newline at end of file
Index: plugins/CucumberJavaScript/test/org/jetbrains/plugins/cucumber/javascript/CucumberJavaScriptExecutionTest.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- plugins/CucumberJavaScript/test/org/jetbrains/plugins/cucumber/javascript/CucumberJavaScriptExecutionTest.java (date 1544615599000)
+++ plugins/CucumberJavaScript/test/org/jetbrains/plugins/cucumber/javascript/CucumberJavaScriptExecutionTest.java (date 1544615599000)
@@ -0,0 +1,164 @@
+package org.jetbrains.plugins.cucumber.javascript;
+
+import com.intellij.execution.ExecutionException;
+import com.intellij.execution.RunManager;
+import com.intellij.execution.RunnerAndConfigurationSettings;
+import com.intellij.execution.actions.ConfigurationContext;
+import com.intellij.execution.configurations.ConfigurationFactory;
+import com.intellij.execution.configurations.GeneralCommandLine;
+import com.intellij.execution.executors.DefaultRunExecutor;
+import com.intellij.execution.process.KillableProcessHandler;
+import com.intellij.execution.process.ProcessAdapter;
+import com.intellij.execution.process.ProcessEvent;
+import com.intellij.execution.process.ProcessHandler;
+import com.intellij.execution.runners.ExecutionEnvironment;
+import com.intellij.execution.runners.ExecutionEnvironmentBuilder;
+import com.intellij.execution.testframework.AbstractTestProxy;
+import com.intellij.execution.testframework.sm.runner.ui.SMTRunnerConsoleView;
+import com.intellij.execution.testframework.sm.runner.ui.TestResultsViewer;
+import com.intellij.ide.DataManager;
+import com.intellij.javascript.nodejs.util.NodePackage;
+import com.intellij.openapi.util.Key;
+import com.intellij.openapi.util.Ref;
+import com.intellij.openapi.vfs.LocalFileSystem;
+import com.intellij.openapi.vfs.VirtualFile;
+import com.intellij.testFramework.fixtures.CodeInsightFixtureTestCase;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.plugins.cucumber.javascript.run.CucumberJavaScriptRunConfiguration;
+import org.jetbrains.plugins.cucumber.javascript.run.CucumberJavaScriptRunConfigurationProducer;
+import org.jetbrains.plugins.cucumber.javascript.run.CucumberJavaScriptRunConfigurationType;
+import org.junit.Assert;
+
+import java.io.File;
+import java.util.concurrent.TimeUnit;
+
+public class CucumberJavaScriptExecutionTest extends CodeInsightFixtureTestCase {
+
+
+ @Override
+ protected String getBasePath() {
+ return CucumberJavaScriptTestUtil.RELATED_TEST_DATA_PATH + "/execution/";
+ }
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+ }
+
+ public void testSimplePassed() throws ExecutionException {
+ myFixture.copyDirectoryToProject(getTestName(false), "");
+ performNpmInstall("[email protected]");
+
+ myFixture.configureFromTempProjectFile("features/test.feature");
+ NodePackage nodePackage = new NodePackage(myFixture.getTempDirPath() + "/node_modules/cucumber");
+ VirtualFile packageVFile = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(new File(nodePackage.getSystemDependentPath()));
+ assertNotNull("Expected installed package in VFS after refresh", packageVFile);
+ AbstractTestProxy testRoot = executeRunConfigurationForPackage(nodePackage);
+ AbstractTestProxy cucumberRoot = testRoot.getChildren().get(0);
+ assertEquals(2, cucumberRoot.getChildren().size());
+ assertEquals("Scenario: Test", cucumberRoot.getChildren().get(0).getName());
+ assertTrue(cucumberRoot.getChildren().get(0).isPassed());
+ assertEquals("Scenario: Test2", cucumberRoot.getChildren().get(1).getName());
+ assertFalse(cucumberRoot.getChildren().get(1).isPassed());
+
+ for (AbstractTestProxy child : testRoot.getChildren()) {
+ System.out.println("child " + child);
+ }
+ }
+
+ private AbstractTestProxy executeRunConfigurationForPackage(NodePackage nodePackage) throws ExecutionException {
+ CucumberJavaScriptUtil.setCucumberPackage(myFixture.getProject(), nodePackage);
+
+ CucumberJavaScriptRunConfigurationProducer configurationProducer = new CucumberJavaScriptRunConfigurationProducer();
+ CucumberJavaScriptRunConfiguration configuration =
+ ((CucumberJavaScriptRunConfiguration)configurationProducer.createConfigurationFromContext(getConfigurationContext()).getConfiguration());
+
+ ConfigurationFactory configurationFactory = CucumberJavaScriptRunConfigurationType.getInstance().getConfigurationFactories()[0];
+
+ final RunnerAndConfigurationSettings runnerAndConfigurationSettings =
+ RunManager.getInstance(myFixture.getProject()).createConfiguration(configuration, configurationFactory);
+
+ //final Executor executor = DefaultRunExecutor.getRunExecutorInstance();
+
+ ExecutionEnvironment env =
+ ExecutionEnvironmentBuilder.create(DefaultRunExecutor.getRunExecutorInstance(), runnerAndConfigurationSettings).build();
+ Ref<ProcessHandler> processHandlerRef = new Ref<>();
+ Ref<SMTRunnerConsoleView> consoleViewRef = new Ref<>();
+ StringBuilder cucumberOutput = new StringBuilder();
+ final com.intellij.util.concurrency.Semaphore s = new com.intellij.util.concurrency.Semaphore();
+ s.down();
+ env.getRunner().execute(env, descriptor -> {
+ //myDescriptor = descriptor;
+ ProcessHandler processHandler = descriptor.getProcessHandler();
+ processHandlerRef.set(processHandler);
+ processHandler.addProcessListener(new ProcessAdapter() {
+ @Override
+ public void onTextAvailable(@NotNull ProcessEvent event, @NotNull Key outputType) {
+ cucumberOutput.append(event.getText());
+ }
+ });
+ SMTRunnerConsoleView console = (SMTRunnerConsoleView)descriptor.getExecutionConsole();
+ consoleViewRef.set(console);
+
+ //Disposer.register(myFixture.getProject(), myTestProxy);
+ console.getResultsViewer().addEventsListener(new TestResultsViewer.SMEventsAdapter() {
+ @Override
+ public void onTestingFinished(@NotNull TestResultsViewer sender) {
+ s.up();
+ }
+ });
+ });
+ s.waitFor(TimeUnit.SECONDS.toMicros(60));
+
+ System.out.println("Cucumber:" + cucumberOutput);
+ //System.out.println("started process");
+ //CucumberJavaScriptRunningState runProfileState = ((CucumberJavaScriptRunningState)configuration.getState(executor, env));
+ //ExecutionResult executionResult = runProfileState.execute(-1);
+ SMTRunnerConsoleView console = consoleViewRef.get();
+ //runProcessWithRedirectOutput(executionResult.getProcessHandler());
+ return console.getResultsViewer().getRoot();
+ }
+
+ private void performNpmInstall(String packageName) throws ExecutionException {
+ GeneralCommandLine commandLine = new GeneralCommandLine()
+ .withWorkDirectory(myFixture.getTempDirPath())
+ .withExePath("npm").withParameters("install", "--save-dev", packageName);
+ KillableProcessHandler processHandler =
+ new KillableProcessHandler(commandLine);
+ runProcessWithRedirectOutput(processHandler);
+ }
+
+ private static void runProcessWithRedirectOutput(ProcessHandler processHandler) {
+ processHandler.addProcessListener(new ProcessAdapter() {
+ @Override
+ public void startNotified(@NotNull ProcessEvent event) {
+ System.out.println("started");
+ }
+
+ @Override
+ public void processTerminated(@NotNull ProcessEvent event) {
+ System.out.println("ended");
+ }
+
+ @Override
+ public void onTextAvailable(@NotNull ProcessEvent event, @NotNull Key outputType) {
+ System.out.println(event.getText());
+ }
+ });
+ processHandler.startNotify();
+
+ long timeout = TimeUnit.SECONDS.toMillis(20);
+ if (!processHandler.waitFor(timeout)) {
+ System.out.println("Process did not finish in " + timeout);
+ processHandler.destroyProcess();
+ Assert.fail("");
+ }
+ //Assert.assertEquals("expected zero exit code", Integer.valueOf(0), processHandler.getExitCode());
+ System.out.println("Process finished");
+ }
+
+ @NotNull
+ private ConfigurationContext getConfigurationContext() {
+ return ConfigurationContext.getFromContext(DataManager.getInstance().getDataContext(myFixture.getEditor().getComponent()));
+ }
+}
Index: plugins/CucumberJavaScript/src/org/jetbrains/plugins/cucumber/javascript/CucumberJavaScriptUtil.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- plugins/CucumberJavaScript/src/org/jetbrains/plugins/cucumber/javascript/CucumberJavaScriptUtil.java (revision c55dfc4aedc25d68b30df60e4f0910a0add0f191)
+++ plugins/CucumberJavaScript/src/org/jetbrains/plugins/cucumber/javascript/CucumberJavaScriptUtil.java (date 1544611956000)
@@ -79,7 +79,7 @@
return FileUtil.toSystemIndependentName(jarFile.getParentFile() + "/" + name);
}
else {
- return FileUtil.toSystemIndependentName(jarFile.getParentFile() + "/CucumberJavaScript/org/jetbrains/plugins/cucumber/javascript/run/" + name);
+ return FileUtil.toSystemIndependentName(jarFile.getParentFile() + "/intellij.javascript.cucumber/org/jetbrains/plugins/cucumber/javascript/run/" + name);
}
}
Index: plugins/CucumberJavaScript/src/org/jetbrains/plugins/cucumber/javascript/run/CucumberJavaScriptRunConfigurationProducer.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- plugins/CucumberJavaScript/src/org/jetbrains/plugins/cucumber/javascript/run/CucumberJavaScriptRunConfigurationProducer.java (revision c55dfc4aedc25d68b30df60e4f0910a0add0f191)
+++ plugins/CucumberJavaScript/src/org/jetbrains/plugins/cucumber/javascript/run/CucumberJavaScriptRunConfigurationProducer.java (date 1544607919000)
@@ -19,7 +19,7 @@
public static final String FEATURES_FOLDER_NAME = "features";
- protected CucumberJavaScriptRunConfigurationProducer() {
+ public CucumberJavaScriptRunConfigurationProducer() {
super(CucumberJavaScriptRunConfigurationType.getInstance());
}
@undeadcat
Copy link
Author

Cucumber failures are incorrectly reported in the IDEA test tree
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment