kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/cxx/extractor/objc_bazel_support_test.cc (about) 1 /* 2 * Copyright 2016 The Kythe Authors. All rights reserved. 3 * 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 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 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. 15 */ 16 17 #include "objc_bazel_support.h" 18 19 #include <string> 20 #include <vector> 21 22 #include "absl/log/initialize.h" 23 #include "google/protobuf/stubs/common.h" 24 #include "gtest/gtest.h" 25 #include "third_party/bazel/src/main/protobuf/extra_actions_base.pb.h" 26 27 namespace kythe { 28 namespace { 29 30 TEST(ObjcExtractorBazelMain, TestSanitizeSimple) { 31 std::string v = SanitizeArgument("V A L"); 32 EXPECT_EQ("'V A L'", v); 33 } 34 35 TEST(ObjcExtractorBazelMain, TestSanitizeWithQuote) { 36 std::string v = SanitizeArgument(R"(V "A" L)"); 37 EXPECT_EQ(R"('V "A" L')", v); 38 } 39 40 TEST(ObjcExtractorBazelMain, TestSanitizeWithQuote2) { 41 std::string v = SanitizeArgument("V 'A' L"); 42 EXPECT_EQ(R"("V 'A' L")", v); 43 } 44 45 TEST(ObjcExtractorBazelMain, TestSanitizeWithAllSpecials) { 46 std::string v = SanitizeArgument(R"(V \ 'A' $ ` " L)"); 47 EXPECT_EQ(R"("V \\ 'A' \$ \` \" L")", v); 48 } 49 50 TEST(ObjcExtractorBazelMain, TestSanitizeWithAllDoubleSpecials) { 51 std::string v = SanitizeArgument(R"(V \\ 'A' $$ `` " L ")"); 52 EXPECT_EQ(R"("V \\\\ 'A' \$\$ \`\` \" L \"")", v); 53 } 54 55 TEST(ObjcExtractorBazelMain, TestExtractOneEnvVar) { 56 std::vector<std::string> args; 57 blaze::SpawnInfo si; 58 auto* e = si.add_variable(); 59 e->set_name("VONE"); 60 e->set_value("VAL"); 61 62 std::string v = BuildEnvVarCommandPrefix(si.variable()); 63 64 EXPECT_EQ("VONE='VAL' ", v); 65 } 66 67 TEST(ObjcExtractorBazelMain, TestExtractManyEnvVars) { 68 std::vector<std::string> args; 69 blaze::SpawnInfo si; 70 auto* e = si.add_variable(); 71 e->set_name("VONE"); 72 e->set_value("VAL"); 73 e = si.add_variable(); 74 e->set_name("VTWO"); 75 e->set_value(""); 76 e = si.add_variable(); 77 e->set_name("VTHREE"); 78 e->set_value("space space"); 79 e = si.add_variable(); 80 e->set_name("VFOUR"); 81 // This is probably invalid input to the spawn info, but we should be able to 82 // handle it without doing something unpredictable. 83 e->set_value("space\\ space"); 84 e = si.add_variable(); 85 e->set_name("VFIVE"); 86 e->set_value("B 'A' G$"); 87 88 std::string v = BuildEnvVarCommandPrefix(si.variable()); 89 90 EXPECT_EQ( 91 "VONE='VAL' VTWO='' VTHREE='space space' VFOUR='space\\ space' VFIVE=\"B " 92 "'A' G\\$\" ", 93 v); 94 } 95 96 TEST(ObjcExtractorBazelMain, TestExtractIgnoreInvalidVarNames) { 97 std::vector<std::string> args; 98 blaze::SpawnInfo si; 99 auto* e = si.add_variable(); 100 e->set_name("VONE"); 101 e->set_value("VAL"); 102 e = si.add_variable(); 103 e->set_name("V TWO"); 104 e->set_value(""); 105 e = si.add_variable(); 106 e->set_name("VT-HREE"); 107 e->set_value("space space"); 108 e = si.add_variable(); 109 e->set_name(" VFOUR"); 110 e->set_value("value"); 111 e = si.add_variable(); 112 e->set_name("VFIVE"); 113 e->set_value("T"); 114 115 std::string v = BuildEnvVarCommandPrefix(si.variable()); 116 117 EXPECT_EQ("VONE='VAL' VFIVE='T' ", v); 118 } 119 120 TEST(ObjcExtractorBazelMain, TestFixArgs) { 121 std::vector<std::string> args; 122 blaze::SpawnInfo si; 123 si.add_argument("__BAZEL_XCODE_DEVELOPER_DIR__/foo"); 124 si.add_argument("__BAZEL_XCODE_SDKROOT__/bar"); 125 // This shouldn't happen but we don't have assurance that it won't. 126 si.add_argument("__BAZEL_XCODE_SDKROOT__/__BAZEL_XCODE_SDKROOT__/bar"); 127 // This shouldn't happen but we don't have assurance that it won't. 128 si.add_argument("__BAZEL_XCODE_SDKROOT__/__BAZEL_XCODE_DEVELOPER_DIR__/bar"); 129 si.add_argument("__BAZEL_XCODE_SDKROOT/bar"); 130 si.add_argument("/baz/bar"); 131 FillWithFixedArgs(args, si, "/usr/devdir", "/usr/sdkroot"); 132 133 // This is verbose so we don't have to pull in a new dep for gmock. 134 EXPECT_EQ("/usr/devdir/foo", args[0]); 135 EXPECT_EQ("/usr/sdkroot/bar", args[1]); 136 EXPECT_EQ("/usr/sdkroot//usr/sdkroot/bar", args[2]); 137 EXPECT_EQ("/usr/sdkroot//usr/devdir/bar", args[3]); 138 EXPECT_EQ("__BAZEL_XCODE_SDKROOT/bar", args[4]); 139 EXPECT_EQ("/baz/bar", args[5]); 140 } 141 142 // Simple test for running a command that should work on (at least) macOS and 143 // Linux. 144 TEST(ObjcExtractorBazelMain, TestRunScript) { 145 EXPECT_EQ("hi", RunScript("echo ' hi '")); 146 } 147 148 TEST(ObjcExtractorBazelMain, TestRunScriptThatFails) { 149 EXPECT_EQ("", RunScript("ls --fail")); 150 } 151 152 // This test passes on linux (2016-09-09). It seems too dangerous to call out 153 // to an unknown binary, so this test is commented out so it does not run 154 // automatically. 155 // TEST(ObjcExtractorBazelMain, TestRunScriptThatDoesNotExist) { 156 // EXPECT_EQ("", RunScript("/usr/bin/xcrun")); 157 //} 158 159 } // namespace 160 } // namespace kythe 161 162 int main(int argc, char** argv) { 163 GOOGLE_PROTOBUF_VERIFY_VERSION; 164 absl::InitializeLog(); 165 ::testing::InitGoogleTest(&argc, argv); 166 int result = RUN_ALL_TESTS(); 167 return result; 168 }