Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 50 additions & 3 deletions test/tools/TestExe/TestExe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,25 @@
using System;
using System.Threading;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace TestExe
{
internal class TestExe
{
private static int Main(string[] args)
{
int exitCode = 0;
if (args.Length > 0)
{
switch (args[0].ToLowerInvariant())
{
case "-echoargs":
EchoArgs(args);
break;
case "-echocmdline":
EchoCmdLine();
break;
case "-createchildprocess":
CreateChildProcess(args);
break;
Expand All @@ -28,17 +33,23 @@ private static int Main(string[] args)
case "-stderr":
Console.Error.WriteLine(args[1]);
break;
case "--help":
case "-h":
PrintHelp();
break;
default:
Console.WriteLine("Unknown test {0}", args[0]);
exitCode = 1;
Console.Error.WriteLine("Unknown test {0}. Run with '-h' for help.", args[0]);
break;
}
}
else
{
Console.WriteLine("Test not specified");
exitCode = 1;
Console.Error.WriteLine("Test not specified");
}

return 0;
return exitCode;
}

// <Summary>
Expand All @@ -52,6 +63,36 @@ private static void EchoArgs(string[] args)
}
}

// <Summary>
// Echos the raw command line received by the process plus the arguments passed in.
// </Summary>
private static void EchoCmdLine()
{
string rawCmdLine = "N/A";
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
nint cmdLinePtr = Interop.GetCommandLineW();
rawCmdLine = Marshal.PtrToStringUni(cmdLinePtr);
}

Console.WriteLine(rawCmdLine);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make more sense to return a non-zero exit code and write message to StdErr?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return a non-zero exit code and write message to StdErr

What is the purpose of doing this? Do you have some testing scenarios in mind that would need it?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is what any well-behaved CLI should do: print error messages to stderr, indicate failure via a non-zero exit code. Except in cases where backward compatibility must (unfortunately) be maintained, there's no good reason not to do this.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with Michael Klement (@mklement0), even though this is a test tool, we want to set good examples as other folks may end up using this code for their own projects.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm afraid I need some clarification on this.
Are you suggesting returning non-zero exit code and write an error message instead of just writing out N/A when running on non-windows? The initial comment looks like suggesting always doing that.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understood Steve Lee (@SteveL-MSFT) correctly, his comment was solely related to this unrelated piece of code, which deals with passing an unknown test name to the utility (the cited code originally showed that).

                    default:
                        Console.WriteLine("Unknown test {0}. Run with '-h' for help.", args[0]);
                        break;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Steve Lee (@SteveL-MSFT) Is that what you mean?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have updated that piece of code based on the feedback. Please let me know if you mean something else.

}

// <Summary>
// Print help content.
// </Summary>
private static void PrintHelp()
{
const string Content = @"
Options for echoing args are:
-echoargs Echos back to stdout the arguments passed in.
-echocmdline Echos the raw command line received by the process.

Other options are for specific tests only. Read source code for details.
";
Console.WriteLine(Content);
}

// <Summary>
// First argument is the number of child processes to create which are instances of itself
// Processes automatically exit after 100 seconds
Expand All @@ -73,4 +114,10 @@ private static void CreateChildProcess(string[] args)
Thread.Sleep(100000);
}
}

internal static partial class Interop
{
[LibraryImport("Kernel32.dll")]
internal static partial nint GetCommandLineW();
}
}