Skip to content

Commit 66519b1

Browse files
authored
Make host tests clear out host tracing variables by default (#118419)
1 parent 5c5ed27 commit 66519b1

File tree

1 file changed

+13
-83
lines changed

1 file changed

+13
-83
lines changed

src/installer/tests/TestUtils/Command.cs

Lines changed: 13 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
using System.Runtime.CompilerServices;
1010
using System.Text;
1111
using System.Threading;
12+
using Microsoft.DotNet.CoreSetup.Test;
13+
using static Microsoft.DotNet.CoreSetup.Test.Constants;
1214

1315
namespace Microsoft.DotNet.Cli.Build.Framework
1416
{
@@ -22,11 +24,6 @@ public class Command
2224

2325
public Process Process { get; }
2426

25-
// Priority order of runnable suffixes to look for and run
26-
private static readonly string[] RunnableSuffixes = OperatingSystem.IsWindows()
27-
? new string[] { ".exe", ".cmd", ".bat" }
28-
: new string[] { string.Empty };
29-
3027
private Command(string executable, string args)
3128
{
3229
// Set the things we need
@@ -54,84 +51,17 @@ public static Command Create(string executable, IEnumerable<string> args)
5451

5552
public static Command Create(string executable, string args)
5653
{
57-
ResolveExecutablePath(ref executable, ref args);
58-
59-
return new Command(executable, args);
60-
}
61-
62-
private static void ResolveExecutablePath(ref string executable, ref string args)
63-
{
64-
foreach (string suffix in RunnableSuffixes)
65-
{
66-
var fullExecutable = Path.GetFullPath(Path.Combine(
67-
AppContext.BaseDirectory, executable + suffix));
68-
69-
if (File.Exists(fullExecutable))
70-
{
71-
executable = fullExecutable;
72-
73-
// In priority order we've found the best runnable extension, so break.
74-
break;
75-
}
76-
}
77-
78-
// On Windows, we want to avoid using "cmd" if possible (it mangles the colors, and a bunch of other things)
79-
// So, do a quick path search to see if we can just directly invoke it
80-
var useCmd = ShouldUseCmd(executable);
81-
82-
if (useCmd)
83-
{
84-
var comSpec = System.Environment.GetEnvironmentVariable("ComSpec");
85-
86-
// cmd doesn't like "foo.exe ", so we need to ensure that if
87-
// args is empty, we just run "foo.exe"
88-
if (!string.IsNullOrEmpty(args))
89-
{
90-
executable = (executable + " " + args).Replace("\"", "\\\"");
91-
}
92-
args = $"/C \"{executable}\"";
93-
executable = comSpec;
94-
}
95-
}
96-
97-
private static bool ShouldUseCmd(string executable)
98-
{
99-
if (OperatingSystem.IsWindows())
100-
{
101-
var extension = Path.GetExtension(executable);
102-
if (!string.IsNullOrEmpty(extension))
103-
{
104-
return !string.Equals(extension, ".exe", StringComparison.Ordinal);
105-
}
106-
else if (executable.Contains(Path.DirectorySeparatorChar))
107-
{
108-
// It's a relative path without an extension
109-
if (File.Exists(executable + ".exe"))
110-
{
111-
// It refers to an exe!
112-
return false;
113-
}
114-
}
115-
else
116-
{
117-
// Search the path to see if we can find it
118-
foreach (var path in System.Environment.GetEnvironmentVariable("PATH").Split(Path.PathSeparator))
119-
{
120-
var candidate = Path.Combine(path, executable + ".exe");
121-
if (File.Exists(candidate))
122-
{
123-
// We found an exe!
124-
return false;
125-
}
126-
}
127-
}
128-
129-
// It's a non-exe :(
130-
return true;
131-
}
132-
133-
// Non-windows never uses cmd
134-
return false;
54+
// Clear out .NET root and tracing environment variables by default
55+
string oldPrefix = "COREHOST_";
56+
string prefix = "DOTNET_HOST_";
57+
return new Command(executable, args)
58+
.DotNetRoot(null)
59+
.RemoveEnvironmentVariable(HostTracing.TraceLevelEnvironmentVariable)
60+
.RemoveEnvironmentVariable(HostTracing.TraceFileEnvironmentVariable)
61+
.RemoveEnvironmentVariable(HostTracing.VerbosityEnvironmentVariable)
62+
.RemoveEnvironmentVariable(HostTracing.TraceLevelEnvironmentVariable.Replace(prefix, oldPrefix))
63+
.RemoveEnvironmentVariable(HostTracing.TraceFileEnvironmentVariable.Replace(prefix, oldPrefix))
64+
.RemoveEnvironmentVariable(HostTracing.VerbosityEnvironmentVariable.Replace(prefix, oldPrefix));
13565
}
13666

13767
public Command DisableDumps()

0 commit comments

Comments
 (0)