Skip to content

Commit 19d2e2d

Browse files
committed
Merge pull request #25 from agallou/fix_composer_bin_dir
fix the run of the test when a bin-dir has ben defined in the composer.json
2 parents ec8b867 + 702e39c commit 19d2e2d

File tree

1 file changed

+39
-9
lines changed

1 file changed

+39
-9
lines changed

src/pl/projectspace/idea/plugins/php/atoum/actions/AtoumRunTests.java

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import com.intellij.openapi.actionSystem.AnActionEvent;
1414
import com.intellij.openapi.actionSystem.PlatformDataKeys;
1515
import com.intellij.openapi.project.Project;
16-
import com.intellij.openapi.util.IconLoader;
1716
import com.intellij.openapi.util.Key;
1817
import com.intellij.openapi.wm.ToolWindow;
1918
import com.intellij.openapi.wm.ToolWindowAnchor;
@@ -22,8 +21,16 @@
2221
import com.intellij.ui.content.ContentManager;
2322
import com.jetbrains.php.lang.psi.PhpFile;
2423
import com.jetbrains.php.lang.psi.elements.PhpClass;
24+
import org.codehaus.jettison.json.JSONException;
25+
import org.codehaus.jettison.json.JSONObject;
2526
import org.jetbrains.annotations.Nullable;
2627

28+
import java.io.BufferedReader;
29+
import java.io.File;
30+
import java.io.IOException;
31+
import java.io.InputStreamReader;
32+
import java.nio.file.Files;
33+
import java.nio.file.Paths;
2734
import java.util.Arrays;
2835

2936
public class AtoumRunTests extends AnAction {
@@ -69,35 +76,58 @@ protected String runTest(PhpClass currentTestClass, Project project) {
6976

7077
try {
7178
OSProcessHandler processHandler = ScriptRunnerUtil.execute(
72-
"./vendor/bin/atoum",
73-
project.getBasePath(),
79+
findAtoumBinPath(project),
80+
project.getBasePath(),
7481
null,
7582
commandLineArgs
7683
);
7784

78-
processHandler.addProcessListener(new ProcessAdapter()
79-
{
85+
processHandler.addProcessListener(new ProcessAdapter() {
8086
public void onTextAvailable(ProcessEvent event, Key outputType) {
81-
outputBuilder.append(event.getText());
87+
outputBuilder.append(event.getText());
8288
}
8389
});
8490

8591
processHandler.startNotify();
86-
while (true)
87-
{
92+
while (true) {
8893
boolean finished = processHandler.waitFor(1000L);
8994
if (finished) {
9095
break;
9196
}
9297
}
93-
9498
return outputBuilder.toString();
9599
} catch (ExecutionException e1) {
96100
e1.printStackTrace();
97101
return "ERROR launching tests" + e1.getMessage();
98102
}
99103
}
100104

105+
protected String findAtoumBinPath(Project project)
106+
{
107+
String defaultBinPath = project.getBasePath() + "/vendor/bin/atoum";
108+
109+
String binDir = getComposerBinDir(project.getBasePath() + "/composer.json");
110+
String binPath = project.getBasePath() + "/" + binDir + "/atoum";
111+
if (null != binDir && new File(binPath).exists()) {
112+
return binPath;
113+
}
114+
115+
return defaultBinPath;
116+
}
117+
118+
@Nullable
119+
protected String getComposerBinDir(String composerPath) {
120+
try {
121+
String composerJsonContent = new String(Files.readAllBytes(Paths.get(composerPath)));
122+
JSONObject obj = new JSONObject(composerJsonContent);
123+
return obj.getJSONObject("config").get("bin-dir").toString();
124+
} catch (JSONException e) {
125+
return null;
126+
} catch (IOException e) {
127+
return null;
128+
}
129+
}
130+
101131

102132
protected ToolWindow getToolWindow(Project project) {
103133
ToolWindow toolWindow;

0 commit comments

Comments
 (0)