ApprovalTests.Java
ApprovalTests.Java copied to clipboard
GenericDiffReporter handling paths with spaces
On my system i didnt get GenericDiffReporter to find WinMerge due to the custom path containing spaces. handling the parameters in a slightly different way solved the problem here is my altered GenericDiffReporter
import java.io.File;
import java.util.Arrays;
import java.util.List;
import org.approvaltests.reporters.EnvironmentAwareReporter;
import com.spun.util.io.FileUtils;
public class GenericDiffReporter implements EnvironmentAwareReporter
{
protected String diffProgram;
protected String diffProgramNotFoundMessage;
private List<String> validExtensions;
public static List<String> TEXT_FILE_EXTENSIONS = Arrays.asList(".txt", ".csv", ".htm", ".html", ".xml",
".eml", ".java", ".css", ".js");
public static List<String> IMAGE_FILE_EXTENSIONS = Arrays.asList(".png", ".gif", ".jpg", ".jpeg", ".bmp",
".tif", ".tiff");
public GenericDiffReporter(String diffProgram, String diffProgramNotFoundMessage)
{
this(diffProgram, diffProgramNotFoundMessage, TEXT_FILE_EXTENSIONS);
}
public GenericDiffReporter(String diffProgram, String diffProgramNotFoundMessage,
List<String> validFileExtensions)
{
this.diffProgram = diffProgram;
this.diffProgramNotFoundMessage = diffProgramNotFoundMessage;
validExtensions = validFileExtensions;
}
@Override
public void report(String received, String approved) throws Exception
{
if (!isWorkingInThisEnvironment(received)) { throw new RuntimeException(diffProgramNotFoundMessage); }
FileUtils.createIfNeeded(approved);
Process exec = Runtime.getRuntime().exec(getCommandLine(received, approved));
}
public String[] getCommandLine(String received, String approved)
{
return new String[]{diffProgram,received,approved};
}
@Override
public boolean isWorkingInThisEnvironment(String forFile)
{
return new File(diffProgram).exists() && isFileExtensionHandled(forFile);
}
public boolean isFileExtensionHandled(String forFile)
{
return isFileExtensionValid(forFile, validExtensions);
}
public static boolean isFileExtensionValid(String forFile, List<String> validExtensionsWithDot)
{
String extensionWithDot = FileUtils.getExtensionWithDot(forFile);
return validExtensionsWithDot.contains(extensionWithDot);
}
}
My more modern approval java port at (https://github.com/nikolavp/approval) uses the PATH(%path%) variable only for finding executables. This should handle paths with spaces as long as they are defined properly in the %path% variable.