1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00

DiffUtil for benchmark test file comparison

This commit is contained in:
Mikhail Sennikovsky 2007-05-15 17:17:24 +00:00
parent 37f1aca1c0
commit 1c3c08ff84
2 changed files with 100 additions and 1 deletions

View file

@ -0,0 +1,86 @@
/*******************************************************************************
* Copyright (c) 2007 Intel Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Intel Corporation - Initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.managedbuilder.testplugin;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import org.eclipse.cdt.utils.spawner.ProcessFactory;
public class DiffUtil {
private static final String DIFF_CMD = "diff -u";
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){
StringBuffer buf = new StringBuffer();
buf.append(DIFF_CMD).append(" ").append(location1).append(" ").append(location2);
return buf.toString();
}
public String diff(String location1, String location2){
InputStream in = invokeDiff(location1, location2);
if(in == null)
return null;
String charSet = null;
BufferedReader br;
if (null == charSet) {
br = new BufferedReader(new InputStreamReader(in));
} else {
try {
br = new BufferedReader(new InputStreamReader(in, charSet));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
String line;
String prev_key = null; // previous key read and saved in envVars
String prev_value = null; // value of previous key
StringBuffer buf = new StringBuffer();
try {
while ((line = br.readLine()) != null) {
buf.append("\n");
buf.append(line);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
return buf.toString();
}
private InputStream invokeDiff(String location1, String location2){
try {
Process p = ProcessFactory.getFactory().exec(createCommand(location1, location2));
return p.getInputStream();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}

View file

@ -391,7 +391,17 @@ public class ManagedBuildTestHelper {
buffer.append("but was:\n ");
buffer.append("\"").append(testBuffer).append("\"");
buffer.append("\n\n ");
buffer.append(">>>>>>>>>>>>>>>start diff: \n");
String location1 = getFileLocation(project, benchmarkFile);
String location2 = getFileLocation(project, testFile);
String diff = DiffUtil.getInstance().diff(location1, location2);
if(diff == null)
diff = "!diff failed!";
buffer.append(diff);
buffer.append("\n<<<<<<<<<<<end diff");
buffer.append("\n\n ");
Assert.fail(buffer.toString());
}
}
@ -426,6 +436,9 @@ public class ManagedBuildTestHelper {
return true;
}
static public String getFileLocation(IProject project, IPath path){
return project.getLocation().append(path).toString();
}
static public StringBuffer readContentsStripLineEnds(IProject project, IPath path) {
StringBuffer buff = new StringBuffer();
IPath fullPath = project.getLocation().append(path);