private CommandSpec CreateCommandSpecWrappedWithCmd( string command, IEnumerable <string> args) { var comSpec = Environment.GetEnvironmentVariable("ComSpec") ?? "cmd.exe"; // Handle the case where ComSpec is already the command if (command.Equals(comSpec, StringComparison.OrdinalIgnoreCase)) { command = args.FirstOrDefault(); args = args.Skip(1); } var cmdEscapedArgs = ArgumentEscaper.EscapeAndConcatenateArgArrayForCmdProcessStart(args); if (!ArgumentEscaper.IsSurroundedWithQuotes(command) && // Don't quote already quoted strings ArgumentEscaper.ShouldSurroundWithQuotes(command)) { command = $"\"{command}\""; } var escapedArgString = $"/s /c \"{command} {cmdEscapedArgs}\""; return(new CommandSpec(comSpec, escapedArgString)); }
public void ShouldSurroundWithQuotesShouldReturnFalseIfItIsNotSurroundWithQuotesAndHasNoWhiteSpace() { string stringWithoutSpace = "someStringWithNoWhiteSpace"; Assert.IsFalse(ArgumentEscaper.ShouldSurroundWithQuotes(stringWithoutSpace)); }
public void ShouldSurroundWithQuotesShouldReturnFalseIfAlreadySurroundWithQuotes() { string stringSurroundWithQuotes = "\"some string\""; Assert.IsFalse(ArgumentEscaper.ShouldSurroundWithQuotes(stringSurroundWithQuotes)); }
public void ShouldSurroundWithQuotesShouldReturnTrueIfItIsNotSurroundWithQuotesAndHasWhiteSpace() { string stringWithSpace = "some String With WhiteSpace"; Assert.IsTrue(ArgumentEscaper.ShouldSurroundWithQuotes(stringWithSpace)); }