Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "testing/gtest/include/gtest/gtest.h" | 5 #include "testing/gtest/include/gtest/gtest.h" |
| 6 #include "tools/gn/escape.h" | 6 #include "tools/gn/escape.h" |
| 7 | 7 |
| 8 TEST(Escape, Ninja) { | 8 TEST(Escape, Ninja) { |
| 9 EscapeOptions opts; | 9 EscapeOptions opts; |
| 10 opts.mode = ESCAPE_NINJA; | 10 opts.mode = ESCAPE_NINJA; |
| 11 std::string result = EscapeString("asdf: \"$\\bar", opts, NULL); | 11 std::string result = EscapeString("asdf: \"$\\bar", opts, NULL); |
| 12 EXPECT_EQ("asdf$:$ \"$$\\bar", result); | 12 EXPECT_EQ("asdf$:$ \"$$\\bar", result); |
| 13 } | 13 } |
| 14 | 14 |
| 15 TEST(Escape, Shell) { | 15 TEST(Escape, WindowsFork) { |
|
scottmg
2014/06/04 22:06:50
WindowsCommand
| |
| 16 EscapeOptions opts; | 16 EscapeOptions opts; |
| 17 opts.mode = ESCAPE_SHELL; | 17 opts.mode = ESCAPE_NINJA_COMMAND; |
| 18 std::string result = EscapeString("asdf: \"$\\bar", opts, NULL); | 18 opts.platform = ESCAPE_PLATFORM_WIN; |
| 19 #if defined(OS_WIN) | 19 |
| 20 // Windows shell doesn't escape backslashes, but it does backslash-escape | 20 // Regular string is passed, even if it has backslashes. |
| 21 // quotes. | 21 EXPECT_EQ("foo\\bar", EscapeString("foo\\bar", opts, NULL)); |
| 22 EXPECT_EQ("\"asdf: \\\"\\$\\bar\"", result); | 22 |
| 23 #else | 23 // Spaces means the string is quoted, normal backslahes untouched. |
| 24 EXPECT_EQ("\"asdf: \\\"\\$\\\\bar\"", result); | 24 bool needs_quoting = false; |
| 25 #endif | 25 EXPECT_EQ("\"foo\\$ bar\"", EscapeString("foo\\ bar", opts, &needs_quoting)); |
| 26 EXPECT_TRUE(needs_quoting); | |
| 27 | |
| 28 // Inhibit quoting. | |
| 29 needs_quoting = false; | |
| 30 opts.inhibit_quoting = true; | |
| 31 EXPECT_EQ("foo\\$ bar", EscapeString("foo\\ bar", opts, &needs_quoting)); | |
| 32 EXPECT_TRUE(needs_quoting); | |
| 33 opts.inhibit_quoting = false; | |
| 34 | |
| 35 // Backslashes at the end of the string get escaped. | |
| 36 EXPECT_EQ("\"foo$ bar\\\\\\\\\"", EscapeString("foo bar\\\\", opts, NULL)); | |
| 37 | |
| 38 // Backslashes preceeding quotes are escaped, and the quote is escaped. | |
| 39 EXPECT_EQ("\"foo\\\\\\\"$ bar\"", EscapeString("foo\\\" bar", opts, NULL)); | |
| 40 } | |
| 41 | |
| 42 TEST(Escape, PosixFork) { | |
|
scottmg
2014/06/04 22:06:50
PosixCommand
| |
| 43 EscapeOptions opts; | |
| 44 opts.mode = ESCAPE_NINJA_COMMAND; | |
| 45 opts.platform = ESCAPE_PLATFORM_POSIX; | |
| 46 | |
| 47 // : and $ ninja escaped with $. Then Shell-escape backslashes and quotes. | |
| 48 EXPECT_EQ("a$:\\$ \\\"\\$$\\\\b", EscapeString("a: \"$\\b", opts, NULL)); | |
| 26 | 49 |
| 27 // Some more generic shell chars. | 50 // Some more generic shell chars. |
| 28 result = EscapeString("a_;<*b", opts, NULL); | 51 EXPECT_EQ("a_\\;\\<\\*b", EscapeString("a_;<*b", opts, NULL)); |
| 29 EXPECT_EQ("a_\\;\\<\\*b", result); | |
| 30 } | 52 } |
| 31 | |
| 32 TEST(Escape, NinjaShell) { | |
| 33 EscapeOptions opts; | |
| 34 opts.mode = ESCAPE_NINJA_SHELL; | |
| 35 | |
| 36 // When escaping for Ninja and the shell, we would escape a $, then escape | |
| 37 // the backslash again. | |
| 38 std::string result = EscapeString("a$b", opts, NULL); | |
| 39 EXPECT_EQ("a\\$$b", result); | |
| 40 } | |
| 41 | |
| 42 TEST(Escape, UsedQuotes) { | |
| 43 EscapeOptions shell_options; | |
| 44 shell_options.mode = ESCAPE_SHELL; | |
| 45 | |
| 46 EscapeOptions ninja_options; | |
| 47 ninja_options.mode = ESCAPE_NINJA; | |
| 48 | |
| 49 EscapeOptions ninja_shell_options; | |
| 50 ninja_shell_options.mode = ESCAPE_NINJA_SHELL; | |
| 51 | |
| 52 // Shell escaping with quoting inhibited. | |
| 53 bool used_quotes = false; | |
| 54 shell_options.inhibit_quoting = true; | |
| 55 EXPECT_EQ("foo bar", EscapeString("foo bar", shell_options, &used_quotes)); | |
| 56 EXPECT_TRUE(used_quotes); | |
| 57 | |
| 58 // Shell escaping with regular quoting. | |
| 59 used_quotes = false; | |
| 60 shell_options.inhibit_quoting = false; | |
| 61 EXPECT_EQ("\"foo bar\"", | |
| 62 EscapeString("foo bar", shell_options, &used_quotes)); | |
| 63 EXPECT_TRUE(used_quotes); | |
| 64 | |
| 65 // Ninja shell escaping should be the same. | |
| 66 used_quotes = false; | |
| 67 EXPECT_EQ("\"foo$ bar\"", | |
| 68 EscapeString("foo bar", ninja_shell_options, &used_quotes)); | |
| 69 EXPECT_TRUE(used_quotes); | |
| 70 | |
| 71 // Ninja escaping shouldn't use quoting. | |
| 72 used_quotes = false; | |
| 73 EXPECT_EQ("foo$ bar", EscapeString("foo bar", ninja_options, &used_quotes)); | |
| 74 EXPECT_FALSE(used_quotes); | |
| 75 | |
| 76 // Used quotes not reset if it's already true. | |
| 77 used_quotes = true; | |
| 78 EscapeString("foo", ninja_options, &used_quotes); | |
| 79 EXPECT_TRUE(used_quotes); | |
| 80 } | |
| OLD | NEW |