Skip to content

Commit a5bff5d

Browse files
LarsEckartisidorebrianberzins
committed
B!! Files launch on linux
Co-Authored-By: Llewellyn Falco <[email protected]> Co-Authored-By: brianberzins <[email protected]>
1 parent 28236b2 commit a5bff5d

File tree

7 files changed

+109
-19
lines changed

7 files changed

+109
-19
lines changed

approvaltests-util/src/main/java/com/spun/util/SystemUtils.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
public class SystemUtils
99
{
1010
/**
11-
* @deprecated Use {@link #isWindowsEnvironment} instead.
11+
* @deprecated Use {@link #isWindowsEnvironment()} instead.
1212
* For example inline your usage of this method.
1313
*/
1414
@Deprecated
@@ -66,7 +66,16 @@ else if (windowsOs)
6666
return fileName.replace(" ", "\\ ");
6767
}
6868
}
69+
/**
70+
* @deprecated Use {@link #isMacEnvironment()} instead.
71+
* For example inline your usage of this method.
72+
*/
73+
@Deprecated
6974
public static boolean isMacEnviroment()
75+
{
76+
return isMacEnvironment();
77+
}
78+
public static boolean isMacEnvironment()
7079
{
7180
String osName = System.getProperty("os.name").toLowerCase();
7281
boolean isMacOs = osName.startsWith("mac os x");
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.spun.util.tests;
2+
3+
import com.spun.util.io.FileUtils;
4+
5+
public class LinuxOpener implements Opener
6+
{
7+
@Override
8+
public boolean open(String fileName)
9+
{
10+
return executeOnLinux(fileName, "xdg-open");
11+
}
12+
public static boolean executeOnLinux(String fileName, String program)
13+
{
14+
if (which(program) == null)
15+
{ return false; }
16+
String cmd = String.format(program + " %s", fileName);
17+
Opener.execute(cmd);
18+
return true;
19+
}
20+
public static String which(String command)
21+
{
22+
try
23+
{
24+
String command1 = "which " + command;
25+
Process p = Runtime.getRuntime().exec(command1);
26+
String output = FileUtils.readStream(p.getInputStream()).trim();
27+
p.waitFor();
28+
int exitValue = p.exitValue();
29+
return exitValue == 0 ? output : null;
30+
}
31+
catch (Exception e)
32+
{
33+
return null;
34+
}
35+
}
36+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.spun.util.tests;
2+
3+
import static com.spun.util.tests.LinuxOpener.executeOnLinux;
4+
5+
public class MacOpener implements Opener
6+
{
7+
@Override
8+
public boolean open(String fileName)
9+
{
10+
return executeOnLinux(fileName, "open");
11+
}
12+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.spun.util.tests;
2+
3+
import com.spun.util.ObjectUtils;
4+
5+
public interface Opener
6+
{
7+
public boolean open(String fileName);
8+
public static void execute(String cmd)
9+
{
10+
try
11+
{
12+
Runtime.getRuntime().exec(cmd);
13+
Thread.sleep(2000);
14+
}
15+
catch (Exception e)
16+
{
17+
throw ObjectUtils.throwAsError(e);
18+
}
19+
}
20+
}

approvaltests-util/src/main/java/com/spun/util/tests/TestUtils.java

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.spun.util.tests;
22

33
import com.spun.util.ClassUtils;
4+
import com.spun.util.FormattedException;
45
import com.spun.util.ObjectUtils;
56
import com.spun.util.ThreadUtils;
67
import com.spun.util.WindowUtils;
@@ -17,6 +18,7 @@
1718
import java.awt.image.BufferedImage;
1819
import java.io.File;
1920
import java.io.FileOutputStream;
21+
import java.util.List;
2022
import java.util.Random;
2123
import java.util.regex.Matcher;
2224
import java.util.regex.Pattern;
@@ -143,27 +145,22 @@ public static void displayEmail(Message email)
143145
throw ObjectUtils.throwAsError(e);
144146
}
145147
}
148+
private static final List<Opener> openers = Queryable.as(new MacOpener(), new WindowsOpener(),
149+
new LinuxOpener());
150+
public static void registerOpener(Opener opener)
151+
{
152+
openers.add(0, opener);
153+
}
146154
public static void displayFile(String fileName)
147155
{
148-
String cmd = "";
149-
if (File.separatorChar == '\\')
150-
{
151-
cmd = "cmd /C start \"Needed Title\" \"%s\" /B";
152-
}
153-
else
156+
for (Opener opener : openers)
154157
{
155-
cmd = "open %s";
156-
}
157-
try
158-
{
159-
cmd = String.format(cmd, fileName);
160-
Runtime.getRuntime().exec(cmd);
161-
Thread.sleep(2000);
162-
}
163-
catch (Exception e)
164-
{
165-
throw ObjectUtils.throwAsError(e);
158+
if (opener.open(fileName))
159+
{ return; }
166160
}
161+
throw new FormattedException("No match found for your OS.\n"
162+
+ "Please add your details at https://github.com/approvals/ApprovalTests.Java/issues/251\n"
163+
+ "In the meantime, call TestUtils.registerOpener(yourCustomOpener).");
167164
}
168165
public static double getTimerMultiplier()
169166
{
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.spun.util.tests;
2+
3+
import com.spun.util.SystemUtils;
4+
5+
public class WindowsOpener implements Opener
6+
{
7+
@Override
8+
public boolean open(String fileName)
9+
{
10+
if (!SystemUtils.isWindowsEnvironment())
11+
{ return false; }
12+
String cmd = String.format("cmd /C start \"Needed Title\" \"%s\" /B", fileName);
13+
Opener.execute(cmd);
14+
return true;
15+
}
16+
}

approvaltests/src/main/java/org/approvaltests/reporters/intellij/IntelliJPathResolver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ private Path getPath(Version version)
7272
private String runtimeSuffix()
7373
{
7474
String runtimeSuffix;
75-
if (SystemUtils.isWindowsEnviroment())
75+
if (SystemUtils.isWindowsEnvironment())
7676
{
7777
runtimeSuffix = "/bin/idea64.exe";
7878
}

0 commit comments

Comments
 (0)