1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-21 21:52:10 +02:00

Properly invoke diff command during test

Contributed by STMicroelectronics

Change-Id: Ic67a3331375cf0eb071d2b24e2c9716114aeb4dd
Signed-off-by: Torbjörn SVENSSON <torbjorn.svensson@st.com>
This commit is contained in:
Torbjörn SVENSSON 2021-02-23 14:01:54 +01:00
parent c809bde381
commit 7760ce2eb7
2 changed files with 17 additions and 44 deletions

View file

@ -18,39 +18,23 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class DiffUtil {
private static final String DIFF_CMD = "diff -ub";
private static DiffUtil fInstance;
private DiffUtil() {
}
public static DiffUtil getInstance() {
if (fInstance == null)
fInstance = new DiffUtil();
return fInstance;
}
private static String createCommand(String location1, String location2) {
StringBuilder buf = new StringBuilder();
buf.append(DIFF_CMD).append(" '").append(location1).append("' '").append(location2).append("'");
return buf.toString();
}
public String diff(String location1, String location2) {
InputStream in = invokeDiff(location1, location2);
if (in == null)
return null;
BufferedReader br;
br = new BufferedReader(new InputStreamReader(in));
String line;
public abstract class DiffUtil {
public static String diff(String location1, String location2) {
StringBuilder buf = new StringBuilder();
try {
while ((line = br.readLine()) != null) {
buf.append("\n");
buf.append(line);
String[] command = new String[] { "diff", "-ub", location1, location2 };
Process p = Runtime.getRuntime().exec(command);
InputStream in = p.getInputStream();
if (in == null) {
return null;
}
try (BufferedReader br = new BufferedReader(new InputStreamReader(in))) {
String line;
while ((line = br.readLine()) != null) {
buf.append("\n");
buf.append(line);
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
@ -59,15 +43,4 @@ public class DiffUtil {
}
return buf.toString();
}
private InputStream invokeDiff(String location1, String location2) {
try {
Process p = Runtime.getRuntime().exec(createCommand(location1, location2));
return p.getInputStream();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}

View file

@ -471,7 +471,7 @@ public class ManagedBuildTestHelper {
: getFileLocation(project, benchmarkFileLocation);
String location2 = testFileLocation.isAbsolute() ? testFileLocation.toString()
: getFileLocation(project, testFileLocation);
String diff = DiffUtil.getInstance().diff(location1, location2);
String diff = DiffUtil.diff(location1, location2);
if (diff == null)
diff = "!diff failed!";
buffer.append(diff);
@ -796,7 +796,7 @@ public class ManagedBuildTestHelper {
buffer.append(">>>>>>>>>>>>>>>start diff: \n");
String location1 = getFileLocation(bmFile.getProject(), bmFile.getProjectRelativePath());
String location2 = getFileLocation(tFile.getProject(), tFile.getProjectRelativePath());
String diff = DiffUtil.getInstance().diff(location1, location2);
String diff = DiffUtil.diff(location1, location2);
if (diff == null)
diff = "!diff failed!";
buffer.append(diff);