Skip to content

Commit c78bee7

Browse files
committed
removing processwrapper
1 parent 5fa899b commit c78bee7

File tree

3 files changed

+62
-75
lines changed

3 files changed

+62
-75
lines changed

test/Azure.Functions.Cli.Tests/E2E/Helpers/ProcessHelper.cs

Lines changed: 53 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -94,34 +94,69 @@ public static int GetAvailablePort()
9494
}
9595
}
9696

97-
private static string ExecuteCommand(string command)
97+
public static void RunProcess(string fileName, string arguments, string workingDirectory, Action<string> writeOutput = null, Action<string> writeError = null)
9898
{
99-
using (Process p = new Process())
100-
{
101-
string commandOut = string.Empty;
99+
TimeSpan procTimeout = TimeSpan.FromMinutes(3);
102100

103-
p.StartInfo.FileName = CommandExe;
104-
p.StartInfo.Arguments = command;
105-
p.StartInfo.UseShellExecute = false;
106-
p.StartInfo.CreateNoWindow = true;
107-
p.StartInfo.RedirectStandardOutput = true;
108-
p.StartInfo.RedirectStandardError = true;
109-
p.Start();
101+
ProcessStartInfo startInfo = new()
102+
{
103+
CreateNoWindow = true,
104+
UseShellExecute = false,
105+
WorkingDirectory = workingDirectory,
106+
FileName = fileName,
107+
RedirectStandardError = true,
108+
RedirectStandardOutput = true
109+
};
110+
111+
if (!string.IsNullOrEmpty(arguments))
112+
{
113+
startInfo.Arguments = arguments;
114+
}
110115

111-
commandOut = p.StandardOutput.ReadToEnd();
112-
string errors = p.StandardError.ReadToEnd();
116+
Process testProcess = Process.Start(startInfo);
117+
string standardOut = null;
118+
string standardError = null;
113119

114-
try
120+
testProcess.OutputDataReceived += (sender, e) =>
121+
{
122+
if (e.Data != null)
115123
{
116-
p.WaitForExit(TimeSpan.FromSeconds(2).Milliseconds);
124+
standardOut = e.Data + Environment.NewLine;
125+
writeOutput?.Invoke(e.Data);
117126
}
118-
catch (Exception exp)
127+
};
128+
129+
testProcess.ErrorDataReceived += (sender, e) =>
130+
{
131+
if (e.Data != null)
119132
{
120-
Console.WriteLine(exp.ToString());
133+
standardOut += e.Data + Environment.NewLine;
134+
writeError?.Invoke(e.Data);
121135
}
136+
};
137+
138+
bool completed = testProcess.WaitForExit((int)procTimeout.TotalMilliseconds);
122139

123-
return commandOut;
140+
if (!completed)
141+
{
142+
testProcess.Kill();
143+
throw new TimeoutException($"Process '{fileName} {arguments}' in working directory '{workingDirectory}' did not complete in {procTimeout}.");
124144
}
145+
146+
if (testProcess.ExitCode != 0)
147+
{
148+
throw new InvalidOperationException($"Process '{fileName} {arguments}' in working directory '{workingDirectory}' exited with code '{testProcess.ExitCode}'.{Environment.NewLine}" +
149+
$"Output:{Environment.NewLine}{standardOut}{Environment.NewLine}Error:{Environment.NewLine}{standardError}");
150+
}
151+
}
152+
153+
private static string ExecuteCommand(string command)
154+
{
155+
string output = null;
156+
157+
RunProcess(CommandExe, command, null, writeOutput: o => output = o);
158+
159+
return output;
125160
}
126161
}
127162
}

test/Azure.Functions.Cli.Tests/ProcessWrapper.cs

Lines changed: 0 additions & 49 deletions
This file was deleted.

test/Azure.Functions.Cli.Tests/ZipHelperTests.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Threading.Tasks;
77
using Azure.Functions.Cli.Common;
88
using Azure.Functions.Cli.Helpers;
9+
using Azure.Functions.Cli.Tests.E2E.Helpers;
910
using Xunit;
1011
using Xunit.Abstractions;
1112

@@ -87,7 +88,7 @@ private async Task<string> BuildAndCopyFileToZipAsync(string rid)
8788
// build the project for the rid
8889
var csproj = dir.GetFiles($"{proj}.csproj", SearchOption.AllDirectories).FirstOrDefault();
8990
var csprojDir = csproj.Directory.FullName;
90-
ProcessWrapper.RunProcess("dotnet", $"build -r {rid}", csprojDir, writeOutput: WriteOutput);
91+
ProcessHelper.RunProcess("dotnet", $"build -r {rid}", csprojDir, writeOutput: WriteOutput);
9192

9293
var outPath = Path.Combine(csprojDir, "bin", "Debug", "net8.0", rid);
9394

@@ -118,7 +119,7 @@ private static void VerifyWindowsZip(string zipFile)
118119
string exeOutput = null;
119120
string exeError = null;
120121

121-
ProcessWrapper.RunProcess(Path.Combine(unzipPath, "ZippedExe.exe"), string.Empty, unzipPath, o => exeOutput = o, e => exeError = e);
122+
ProcessHelper.RunProcess(Path.Combine(unzipPath, "ZippedExe.exe"), string.Empty, unzipPath, o => exeOutput += o + Environment.NewLine, e => exeError += e + Environment.NewLine);
122123

123124
Assert.Equal(string.Empty, exeError);
124125
Assert.Equal("Hello, World!", exeOutput.Trim());
@@ -131,7 +132,7 @@ private void VerifyLinuxZip(string zipFile)
131132

132133
void CaptureOutput(string output)
133134
{
134-
stdout = output;
135+
stdout += output + Environment.NewLine;
135136
WriteOutput(output);
136137
}
137138

@@ -142,10 +143,10 @@ void CaptureOutput(string output)
142143
Directory.CreateDirectory(mntDir);
143144

144145
// this is what our hosting environment does; we need to validate we can run the exe when mounted like this
145-
ProcessWrapper.RunProcess("fuse-zip", $"./{zipFileName} ./mnt -r", zipDir, writeOutput: CaptureOutput);
146-
ProcessWrapper.RunProcess("bash", $"-c \"ls -l\"", mntDir, writeOutput: CaptureOutput);
146+
ProcessHelper.RunProcess("fuse-zip", $"./{zipFileName} ./mnt -r", zipDir, writeOutput: CaptureOutput);
147+
ProcessHelper.RunProcess("bash", $"-c \"ls -l\"", mntDir, writeOutput: CaptureOutput);
147148

148-
var outputLines = stdout.Split('\n', StringSplitOptions.RemoveEmptyEntries);
149+
var outputLines = stdout.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries);
149150
Assert.Equal(14, outputLines.Length);
150151

151152
// ignore first ('total ...') to validate file perms
@@ -184,8 +185,8 @@ void CaptureOutput(string output)
184185
}
185186
}
186187

187-
ProcessWrapper.RunProcess($"{Path.Combine(mntDir, exeName)}", string.Empty, mntDir, writeOutput: CaptureOutput);
188-
outputLines = stdout.Split('\n', StringSplitOptions.RemoveEmptyEntries);
188+
ProcessHelper.RunProcess($"{Path.Combine(mntDir, exeName)}", string.Empty, mntDir, writeOutput: CaptureOutput);
189+
outputLines = stdout.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries);
189190
Assert.Equal("Hello, World!", outputLines.Last());
190191
}
191192

0 commit comments

Comments
 (0)