Skip to content

Commit

Permalink
Make helper private to prevent someone from calling it and fix caller…
Browse files Browse the repository at this point in the history
…s to previous helper
  • Loading branch information
JohnMcPMS committed Sep 22, 2023
1 parent 65c3ba4 commit 1bdc332
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 59 deletions.
4 changes: 2 additions & 2 deletions src/AppInstallerCLIE2ETests/AppShutdownTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public void RegisterApplicationTest()
// This just waits for the app termination event.
var testCmdTask = new Task<TestCommon.RunCommandResult>(() =>
{
return TestCommon.RunAICLICommandViaInvokeCommandInDesktopPackage("test", "appshutdown", timeOut: 300000, throwOnTimeout: false);
return TestCommon.RunAICLICommand("test", "appshutdown", timeOut: 300000, throwOnTimeout: false);
});

// Register the app with the updated version.
Expand Down Expand Up @@ -108,7 +108,7 @@ public void RegisterApplicationTest_Force()
throw new NullReferenceException("AICLIPackagePath");
}

var result = TestCommon.RunAICLICommandViaInvokeCommandInDesktopPackage("test", "appshutdown --force", timeOut: 300000, throwOnTimeout: false);
var result = TestCommon.RunAICLICommand("test", "appshutdown --force", timeOut: 300000, throwOnTimeout: false);
TestContext.Out.Write(result.StdOut);
Assert.True(result.StdOut.Contains("Succeeded waiting for app shutdown event"));
}
Expand Down
116 changes: 59 additions & 57 deletions src/AppInstallerCLIE2ETests/Helpers/TestCommon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,9 @@ public enum TestModuleLocation
/// <param name="parameters">Parameters.</param>
/// <param name="stdIn">Optional std in.</param>
/// <param name="timeOut">Optional timeout.</param>
/// <param name="throwOnTimeout">Throw on timeout.</param>
/// <returns>The result of the command.</returns>
public static RunCommandResult RunAICLICommand(string command, string parameters, string stdIn = null, int timeOut = 60000)
public static RunCommandResult RunAICLICommand(string command, string parameters, string stdIn = null, int timeOut = 60000, bool throwOnTimeout = true)
{
string inputMsg =
"AICLI path: " + TestSetup.Parameters.AICLIPath +
Expand All @@ -96,62 +97,7 @@ public static RunCommandResult RunAICLICommand(string command, string parameters

TestContext.Out.WriteLine($"Starting command run. {inputMsg}");

return RunAICLICommandViaDirectProcess(command, parameters, stdIn, timeOut);
}

/// <summary>
/// Run winget command via direct process.
/// </summary>
/// <param name="command">Command to run.</param>
/// <param name="parameters">Parameters.</param>
/// <param name="stdIn">Optional std in.</param>
/// <param name="timeOut">Optional timeout.</param>
/// <returns>The result of the command.</returns>
public static RunCommandResult RunAICLICommandViaDirectProcess(string command, string parameters, string stdIn = null, int timeOut = 60000)
{
RunCommandResult result = new ();
Process p = new Process();
p.StartInfo = new ProcessStartInfo(TestSetup.Parameters.AICLIPath, command + ' ' + parameters);
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;

if (!string.IsNullOrEmpty(stdIn))
{
p.StartInfo.RedirectStandardInput = true;
}

p.Start();

if (!string.IsNullOrEmpty(stdIn))
{
p.StandardInput.Write(stdIn);
}

if (p.WaitForExit(timeOut))
{
result.ExitCode = p.ExitCode;
result.StdOut = p.StandardOutput.ReadToEnd();
result.StdErr = p.StandardError.ReadToEnd();

TestContext.Out.WriteLine("Command run completed with exit code: " + result.ExitCode);

if (!string.IsNullOrEmpty(result.StdErr))
{
TestContext.Error.WriteLine("Command run error. Error: " + result.StdErr);
}

if (TestSetup.Parameters.VerboseLogging && !string.IsNullOrEmpty(result.StdOut))
{
TestContext.Out.WriteLine("Command run output. Output: " + result.StdOut);
}
}
else
{
throw new TimeoutException($"Direct winget command run timed out: {command} {parameters}");
}

return result;
return RunAICLICommandViaDirectProcess(command, parameters, stdIn, timeOut, throwOnTimeout);
}

/// <summary>
Expand Down Expand Up @@ -930,6 +876,62 @@ public static string GetExpectedModulePath(TestModuleLocation location)
}
}

/// <summary>
/// Run winget command via direct process.
/// </summary>
/// <param name="command">Command to run.</param>
/// <param name="parameters">Parameters.</param>
/// <param name="stdIn">Optional std in.</param>
/// <param name="timeOut">Optional timeout.</param>
/// <param name="throwOnTimeout">Throw on timeout.</param>
/// <returns>The result of the command.</returns>
private static RunCommandResult RunAICLICommandViaDirectProcess(string command, string parameters, string stdIn, int timeOut, bool throwOnTimeout)
{
RunCommandResult result = new ();
Process p = new Process();
p.StartInfo = new ProcessStartInfo(TestSetup.Parameters.AICLIPath, command + ' ' + parameters);
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;

if (!string.IsNullOrEmpty(stdIn))
{
p.StartInfo.RedirectStandardInput = true;
}

p.Start();

if (!string.IsNullOrEmpty(stdIn))
{
p.StandardInput.Write(stdIn);
}

if (p.WaitForExit(timeOut))
{
result.ExitCode = p.ExitCode;
result.StdOut = p.StandardOutput.ReadToEnd();
result.StdErr = p.StandardError.ReadToEnd();

TestContext.Out.WriteLine("Command run completed with exit code: " + result.ExitCode);

if (!string.IsNullOrEmpty(result.StdErr))
{
TestContext.Error.WriteLine("Command run error. Error: " + result.StdErr);
}

if (TestSetup.Parameters.VerboseLogging && !string.IsNullOrEmpty(result.StdOut))
{
TestContext.Out.WriteLine("Command run output. Output: " + result.StdOut);
}
}
else if (throwOnTimeout)
{
throw new TimeoutException($"Direct winget command run timed out: {command} {parameters}");
}

return result;
}

/// <summary>
/// Run command result.
/// </summary>
Expand Down

0 comments on commit 1bdc332

Please sign in to comment.