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));
        }
예제 #2
0
        public void ShouldSurroundWithQuotesShouldReturnFalseIfItIsNotSurroundWithQuotesAndHasNoWhiteSpace()
        {
            string stringWithoutSpace = "someStringWithNoWhiteSpace";

            Assert.IsFalse(ArgumentEscaper.ShouldSurroundWithQuotes(stringWithoutSpace));
        }
예제 #3
0
        public void ShouldSurroundWithQuotesShouldReturnFalseIfAlreadySurroundWithQuotes()
        {
            string stringSurroundWithQuotes = "\"some string\"";

            Assert.IsFalse(ArgumentEscaper.ShouldSurroundWithQuotes(stringSurroundWithQuotes));
        }
예제 #4
0
        public void ShouldSurroundWithQuotesShouldReturnTrueIfItIsNotSurroundWithQuotesAndHasWhiteSpace()
        {
            string stringWithSpace = "some String With WhiteSpace";

            Assert.IsTrue(ArgumentEscaper.ShouldSurroundWithQuotes(stringWithSpace));
        }