Skip to content

Commit 9b3faa4

Browse files
committed
Add runProcessAndWait method overload and coresponding unit test
DEVSIX-4144 Autoported commit. Original commit hash: [edf666a01] Manual files: io/src/main/java/com/itextpdf/io/util/SystemUtil.java io/src/test/java/com/itextpdf/io/util/SystemUtilTest.java
1 parent 15abc4f commit 9b3faa4

File tree

5 files changed

+56
-15
lines changed

5 files changed

+56
-15
lines changed

itext.tests/itext.io.tests/itext/io/util/SystemUtilTest.cs

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,26 @@ source product.
4343

4444
using System;
4545
using System.Diagnostics;
46+
using System.Text;
4647
using iText.Test;
4748

4849
namespace iText.IO.Util {
4950
public class SystemUtilTest : ExtendedITextTest {
5051
public static readonly String SOURCE_FOLDER = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework
51-
.TestContext
52-
.CurrentContext.TestDirectory) +
53-
"/resources/itext/io/util/SystemUtilTest/";
52+
.TestContext.CurrentContext.TestDirectory) + "/resources/itext/io/util/SystemUtilTest/";
53+
54+
55+
private static readonly String DESTINATION_FOLDER = NUnit.Framework.TestContext.CurrentContext.TestDirectory
56+
+ "/test/itext/io/SystemUtilTest/";
5457

5558
// This is empty file that used to check the logic for existed execution file
5659
public static readonly String STUB_EXEC_FILE = SOURCE_FOLDER + "folder with space/stubFile";
5760

61+
[NUnit.Framework.SetUp]
62+
public virtual void SetUp() {
63+
CreateOrClearDestinationFolder(DESTINATION_FOLDER);
64+
}
65+
5866
[NUnit.Framework.Test]
5967
public virtual void SetProcessStartInfoTest() {
6068
Process process = new Process();
@@ -125,5 +133,27 @@ public virtual void SplitIntoProcessArgumentsMagickCompareTest() {
125133
"compare \"D:\\itext\\java\\itextcore\\kernel\\.\\target\\test\\com\\itextpdf\\kernel\\utils\\CompareToolTest\\simple_pdf.pdf-001.png\" \"D:\\itext\\java\\itextcore\\kernel\\.\\target\\test\\com\\itextpdf\\kernel\\utils\\CompareToolTest\\cmp_simple_pdf_with_space .pdf-001.png\"",
126134
processArguments[1]);
127135
}
136+
137+
[NUnit.Framework.Test]
138+
public virtual void RunProcessAndWaitWithWorkingDirectoryTest() {
139+
String imageMagickPath = SystemUtil.GetEnvironmentVariable(ImageMagickHelper.MAGICK_COMPARE_ENVIRONMENT_VARIABLE);
140+
if (imageMagickPath == null) {
141+
imageMagickPath = SystemUtil.GetEnvironmentVariable(ImageMagickHelper.MAGICK_COMPARE_ENVIRONMENT_VARIABLE_LEGACY);
142+
}
143+
String inputImage = "image.jpg";
144+
String cmpImage = "cmp_image.jpg";
145+
String diff = DESTINATION_FOLDER + "diff_differentImages.png";
146+
147+
StringBuilder currCompareParams = new StringBuilder();
148+
currCompareParams
149+
.Append("'")
150+
.Append(inputImage).Append("' '")
151+
.Append(cmpImage).Append("' '")
152+
.Append(diff).Append("'");
153+
bool result = SystemUtil.RunProcessAndWait(imageMagickPath, currCompareParams.ToString(), SOURCE_FOLDER);
154+
155+
NUnit.Framework.Assert.False(result);
156+
NUnit.Framework.Assert.True(FileUtil.FileExists(diff));
157+
}
128158
}
129159
}
10.9 KB
Loading
12.7 KB
Loading

itext/itext.io/itext/io/util/SystemUtil.cs

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,21 @@ public static String GetEnvironmentVariable(String name) {
9393
}
9494

9595
public static bool RunProcessAndWait(String exec, String @params) {
96-
return RunProcessAndGetExitCode(exec, @params) == 0;
96+
return RunProcessAndWait(exec, @params, null);
9797
}
9898

99-
public static int RunProcessAndGetExitCode(String exec, String @params) {
100-
using (Process proc = new Process())
101-
{
102-
SetProcessStartInfo(proc, exec, @params);
99+
public static bool RunProcessAndWait(String exec, String @params, String workingDirPath) {
100+
return RunProcessAndGetExitCode(exec, @params, workingDirPath) == 0;
101+
}
102+
103+
public static int RunProcessAndGetExitCode(String exec, String @params)
104+
{
105+
return RunProcessAndGetExitCode(exec, @params, null);
106+
}
107+
108+
public static int RunProcessAndGetExitCode(String exec, String @params, String workingDirPath) {
109+
using (Process proc = new Process()) {
110+
SetProcessStartInfo(proc, exec, @params, workingDirPath);
103111
proc.Start();
104112
Console.WriteLine(GetProcessOutput(proc));
105113
proc.WaitForExit();
@@ -109,9 +117,8 @@ public static int RunProcessAndGetExitCode(String exec, String @params) {
109117

110118
public static String RunProcessAndGetOutput(String exec, String @params) {
111119
String processOutput;
112-
using (Process proc = new Process())
113-
{
114-
SetProcessStartInfo(proc, exec, @params);
120+
using (Process proc = new Process()) {
121+
SetProcessStartInfo(proc, exec, @params, null);
115122
proc.Start();
116123
processOutput = GetProcessOutput(proc);
117124
proc.WaitForExit();
@@ -122,9 +129,8 @@ public static String RunProcessAndGetOutput(String exec, String @params) {
122129

123130
public static StringBuilder RunProcessAndCollectErrors(String exec, String @params) {
124131
StringBuilder errorsBuilder;
125-
using (Process proc = new Process())
126-
{
127-
SetProcessStartInfo(proc, exec, @params);
132+
using (Process proc = new Process()) {
133+
SetProcessStartInfo(proc, exec, @params, null);
128134
proc.Start();
129135
errorsBuilder = GetProcessErrorsOutput(proc);
130136
Console.Out.WriteLine(errorsBuilder.ToString());
@@ -135,12 +141,17 @@ public static StringBuilder RunProcessAndCollectErrors(String exec, String @para
135141
}
136142

137143
internal static void SetProcessStartInfo(Process proc, String exec, String @params) {
144+
SetProcessStartInfo(proc, exec, @params, null);
145+
}
146+
147+
internal static void SetProcessStartInfo(Process proc, String exec, String @params, String workingDir) {
138148
String[] processArguments = PrepareProcessArguments(exec, @params);
139149
proc.StartInfo = new ProcessStartInfo(processArguments[0], processArguments[1]);
140150
proc.StartInfo.UseShellExecute = false;
141151
proc.StartInfo.RedirectStandardOutput = true;
142152
proc.StartInfo.RedirectStandardError = true;
143153
proc.StartInfo.CreateNoWindow = true;
154+
proc.StartInfo.WorkingDirectory = workingDir;
144155
}
145156

146157
internal static String[] PrepareProcessArguments(String exec, String @params) {

port-hash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
b040aed8b907ab9b707ab9dada73c8957bfe5b62
1+
edf666a015bf150b6fbb47399d6c63c8fc82bec0

0 commit comments

Comments
 (0)