2 * Copyright 2017 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <glog/logging.h>
19 #include <folly/Shell.h>
20 #include <folly/portability/GTest.h>
22 using namespace folly;
24 TEST(Shell, ShellQuote) {
25 EXPECT_EQ(shellQuote("a"), "'a'");
26 EXPECT_EQ(shellQuote("a'b"), "'a'\\''b'");
27 EXPECT_EQ(shellQuote("a\"b"), "'a\"b'");
30 TEST(Shell, Shellify) {
31 auto command = "rm -rf /"_shellify();
32 EXPECT_EQ(command[0], "/bin/sh");
33 EXPECT_EQ(command[1], "-c");
34 EXPECT_EQ(command[2], "rm -rf /");
36 command = "rm -rf {}"_shellify("someFile.txt");
37 EXPECT_EQ(command[2], "rm -rf 'someFile.txt'");
39 command = "rm -rf {}"_shellify(5);
40 EXPECT_EQ(command[2], "rm -rf '5'");
42 command = "ls {}"_shellify("blah'; rm -rf /");
43 EXPECT_EQ(command[2], "ls 'blah'\\''; rm -rf /'");
46 TEST(Shell, Shellify_deprecated) {
47 auto command = shellify("rm -rf /");
48 EXPECT_EQ(command[0], "/bin/sh");
49 EXPECT_EQ(command[1], "-c");
50 EXPECT_EQ(command[2], "rm -rf /");
52 command = shellify("rm -rf {}", "someFile.txt");
53 EXPECT_EQ(command[2], "rm -rf 'someFile.txt'");
55 command = shellify("rm -rf {}", 5);
56 EXPECT_EQ(command[2], "rm -rf '5'");
58 command = shellify("ls {}", "blah'; rm -rf /");
59 EXPECT_EQ(command[2], "ls 'blah'\\''; rm -rf /'");