public void TempPathContainsAmpersand2() { string directoryWithAmpersand = "space &nospace"; string newTmp = Path.Combine(Path.GetTempPath(), directoryWithAmpersand); string oldTmp = Environment.GetEnvironmentVariable("TMP"); try { Directory.CreateDirectory(newTmp); if (NativeMethodsShared.GetShortFilePath(newTmp) == newTmp) { // Short file paths not supported, this test will fail. // See: https://github.com/Microsoft/msbuild/issues/1803 return; } Environment.SetEnvironmentVariable("TMP", newTmp); Exec exec = PrepareExec("echo [hello]"); bool taskSucceeded = exec.Execute(); Assert.True(taskSucceeded); // "Task should have succeeded" ((MockEngine)exec.BuildEngine).AssertLogContains("[hello]"); } finally { Environment.SetEnvironmentVariable("TMP", oldTmp); if (Directory.Exists(newTmp)) { FileUtilities.DeleteWithoutTrailingBackslash(newTmp); } } }
/// <summary> /// Adds the arguments for cmd.exe /// </summary> /// <param name="commandLine">command line builder class to add arguments to</param> protected internal override void AddCommandLineCommands(CommandLineBuilderExtension commandLine) { // Create the batch file now, // so we have the file name for the cmd.exe command line CreateTemporaryBatchFile(); string batchFileForCommandLine = _batchFile; // Unix consoles cannot have their encodings changed in place (like chcp on windows). // Instead, unix scripts receive encoding information via environment variables before invocation. // In consequence, encoding setup has to be performed outside the script, not inside it. if (NativeMethodsShared.IsUnixLike) { commandLine.AppendSwitch("-c"); commandLine.AppendTextUnquoted(" \""); commandLine.AppendTextUnquoted("export LANG=en_US.UTF-8; export LC_ALL=en_US.UTF-8; . "); commandLine.AppendFileNameIfNotNull(batchFileForCommandLine); commandLine.AppendTextUnquoted("\""); } else { if (NativeMethodsShared.IsWindows) { commandLine.AppendSwitch("/Q"); // echo off if (!Traits.Instance.EscapeHatches.UseAutoRunWhenLaunchingProcessUnderCmd) { commandLine.AppendSwitch("/D"); // do not load AutoRun configuration from the registry (perf) } commandLine.AppendSwitch("/C"); // run then terminate // If for some crazy reason the path has a & character and a space in it // then get the short path of the temp path, which should not have spaces in it // and then escape the & if (batchFileForCommandLine.Contains("&") && !batchFileForCommandLine.Contains("^&")) { batchFileForCommandLine = NativeMethodsShared.GetShortFilePath(batchFileForCommandLine); batchFileForCommandLine = batchFileForCommandLine.Replace("&", "^&"); } } commandLine.AppendFileNameIfNotNull(batchFileForCommandLine); } }
/// <summary> /// Adds the arguments for cmd.exe /// </summary> /// <param name="commandLine">command line builder class to add arguments to</param> protected internal override void AddCommandLineCommands(CommandLineBuilderExtension commandLine) { // Create the batch file now, // so we have the file name for the cmd.exe command line CreateTemporaryBatchFile(); string batchFileForCommandLine = _batchFile; commandLine.AppendSwitch("/Q"); // echo off commandLine.AppendSwitch("/C"); // run then terminate // If for some crazy reason the path has a & character and a space in it // then get the short path of the temp path, which should not have spaces in it // and then escape the & if (batchFileForCommandLine.Contains("&") && !batchFileForCommandLine.Contains("^&")) { batchFileForCommandLine = NativeMethodsShared.GetShortFilePath(batchFileForCommandLine); batchFileForCommandLine = batchFileForCommandLine.Replace("&", "^&"); } commandLine.AppendFileNameIfNotNull(batchFileForCommandLine); }
public override bool Execute() { bool flag2; if (!this.ValidateParameters()) { return(false); } if (this.EnvironmentVariables != null) { this.environmentVariablePairs = new List <KeyValuePair <object, object> >(this.EnvironmentVariables.Length); foreach (string str in this.EnvironmentVariables) { string[] strArray = str.Split(equalsSplitter, 2); if ((strArray.Length == 1) || ((strArray.Length == 2) && (strArray[0].Length == 0))) { this.LogPrivate.LogErrorWithCodeFromResources("ToolTask.InvalidEnvironmentParameter", new object[] { strArray[0] }); return(false); } this.environmentVariablePairs.Add(new KeyValuePair <object, object>(strArray[0], strArray[1])); } } if (!this.AssignStandardStreamLoggingImportance()) { return(false); } try { if (this.SkipTaskExecution()) { return(true); } string contents = this.GenerateCommandLineCommands(); string message = contents; string responseFileCommands = this.GenerateResponseFileCommands(); if (this.UseCommandProcessor) { this.ToolExe = "cmd.exe"; this.temporaryBatchFile = FileUtilities.GetTemporaryFile(".cmd"); File.AppendAllText(this.temporaryBatchFile, contents, Encoding.ASCII); string temporaryBatchFile = this.temporaryBatchFile; if (temporaryBatchFile.Contains("&") && !temporaryBatchFile.Contains("^&")) { temporaryBatchFile = NativeMethodsShared.GetShortFilePath(temporaryBatchFile).Replace("&", "^&"); } contents = "/C \"" + temporaryBatchFile + "\""; if (this.EchoOff) { contents = "/Q " + contents; } } if ((contents == null) || (contents.Length == 0)) { contents = string.Empty; } else { contents = " " + contents; } HostObjectInitializationStatus status = this.InitializeHostObject(); switch (status) { case HostObjectInitializationStatus.NoActionReturnSuccess: return(true); case HostObjectInitializationStatus.NoActionReturnFailure: this.exitCode = 1; return(this.HandleTaskExecutionErrors()); default: { string pathToTool = this.ComputePathToTool(); if (pathToTool == null) { return(false); } bool alreadyLoggedEnvironmentHeader = false; StringDictionary environmentOverride = this.EnvironmentOverride; if (environmentOverride != null) { foreach (DictionaryEntry entry in environmentOverride) { alreadyLoggedEnvironmentHeader = this.LogEnvironmentVariable(alreadyLoggedEnvironmentHeader, (string)entry.Key, (string)entry.Value); } } if (this.environmentVariablePairs != null) { foreach (KeyValuePair <object, object> pair in this.environmentVariablePairs) { alreadyLoggedEnvironmentHeader = this.LogEnvironmentVariable(alreadyLoggedEnvironmentHeader, (string)pair.Key, (string)pair.Value); } } if (this.UseCommandProcessor) { this.LogToolCommand(pathToTool + contents); this.LogToolCommand(message); } else { this.LogToolCommand(pathToTool + contents + " " + responseFileCommands); } this.exitCode = 0; if (status == HostObjectInitializationStatus.UseHostObjectToExecute) { try { if (!this.CallHostObjectToExecute()) { this.exitCode = 1; } break; } catch (Exception exception) { this.LogPrivate.LogErrorFromException(exception); return(false); } } ErrorUtilities.VerifyThrow(status == HostObjectInitializationStatus.UseAlternateToolToExecute, "Invalid return status"); this.exitCode = this.ExecuteTool(pathToTool, responseFileCommands, contents); break; } } if (this.terminatedTool) { return(false); } if (this.exitCode != 0) { return(this.HandleTaskExecutionErrors()); } flag2 = true; } catch (ArgumentException exception2) { if (!this.terminatedTool) { this.LogPrivate.LogErrorWithCodeFromResources("General.InvalidToolSwitch", new object[] { this.ToolExe, this.GetErrorMessageWithDiagnosticsCheck(exception2) }); } flag2 = false; } catch (Win32Exception exception3) { if (!this.terminatedTool) { this.LogPrivate.LogErrorWithCodeFromResources("ToolTask.CouldNotStartToolExecutable", new object[] { this.ToolExe, this.GetErrorMessageWithDiagnosticsCheck(exception3) }); } flag2 = false; } catch (IOException exception4) { if (!this.terminatedTool) { this.LogPrivate.LogErrorWithCodeFromResources("ToolTask.CouldNotStartToolExecutable", new object[] { this.ToolExe, this.GetErrorMessageWithDiagnosticsCheck(exception4) }); } flag2 = false; } catch (UnauthorizedAccessException exception5) { if (!this.terminatedTool) { this.LogPrivate.LogErrorWithCodeFromResources("ToolTask.CouldNotStartToolExecutable", new object[] { this.ToolExe, this.GetErrorMessageWithDiagnosticsCheck(exception5) }); } flag2 = false; } finally { if ((this.temporaryBatchFile != null) && File.Exists(this.temporaryBatchFile)) { File.Delete(this.temporaryBatchFile); } } return(flag2); }