kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/cxx/extractor/testdata/main_source_file_env_dep_test.cc (about) 1 /* 2 * Copyright 2019 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 "absl/algorithm/container.h" 18 #include "absl/strings/string_view.h" 19 #include "gmock/gmock.h" 20 #include "gtest/gtest.h" 21 #include "kythe/cxx/extractor/testlib.h" 22 #include "kythe/proto/analysis.pb.h" 23 #include "protobuf-matchers/protocol-buffer-matchers.h" 24 25 namespace kythe { 26 namespace { 27 using ::kythe::proto::CompilationUnit; 28 using ::protobuf_matchers::EquivToProto; 29 30 proto::CompilationUnit ExpectedCompilation() { 31 return ParseTextCompilationUnitOrDie(R"pb( 32 v_name { language: "c++" } 33 required_input { 34 v_name { 35 path: "kythe/cxx/extractor/testdata/main_source_file_env_dep.cc" 36 } 37 info { 38 path: "kythe/cxx/extractor/testdata/main_source_file_env_dep.cc" 39 digest: "7fc8bce46febc9e8c660fdaa7caa9e27e64a9f85769d69ca9ecfc8a2ca751335" 40 } 41 details { 42 [type.googleapis.com/kythe.proto.ContextDependentVersion] { 43 row { source_context: "hash0" always_process: true } 44 } 45 } 46 } 47 argument: "/dummy/bin/g++" 48 argument: "-target" 49 argument: "dummy-target" 50 argument: "-DKYTHE_IS_RUNNING=1" 51 argument: "-resource-dir" 52 argument: "/kythe_builtins" 53 argument: "--driver-mode=g++" 54 argument: "-I./kythe/cxx/extractor/testdata" 55 argument: "-DMACRO" 56 argument: "./kythe/cxx/extractor/testdata/main_source_file_env_dep.cc" 57 argument: "-fsyntax-only" 58 source_file: "kythe/cxx/extractor/testdata/main_source_file_env_dep.cc" 59 working_directory: "/root" 60 entry_context: "hash0" 61 )pb"); 62 } 63 64 TEST(CxxExtractorTest, TestSourceFileEnvDepWithoutMacro) { 65 CompilationUnit unit = ExtractSingleCompilationOrDie( 66 {{"--with_executable", "/dummy/bin/g++", 67 "-I./kythe/cxx/extractor/testdata", 68 "./kythe/cxx/extractor/testdata/main_source_file_env_dep.cc"}}); 69 70 // Fix up things which may legitmately vary between runs. 71 // TODO(shahms): use gMock protobuf matchers when available. 72 CanonicalizeHashes(&unit); 73 unit.set_argument(2, "dummy-target"); 74 unit.clear_details(); 75 76 CompilationUnit expected = ExpectedCompilation(); 77 // The only difference between the two is the lack of "-DMACRO" arguments. 78 expected.mutable_argument()->erase( 79 absl::c_find(*expected.mutable_argument(), "-DMACRO")); 80 81 EXPECT_THAT(unit, EquivToProto(expected)); 82 } 83 84 TEST(CxxExtractorTest, TestSourceFileEnvDepWithMacro) { 85 CompilationUnit unit = ExtractSingleCompilationOrDie( 86 {{"--with_executable", "/dummy/bin/g++", 87 "-I./kythe/cxx/extractor/testdata", "-DMACRO", 88 "./kythe/cxx/extractor/testdata/main_source_file_env_dep.cc"}}); 89 90 // Fix up things which may legitmately vary between runs. 91 // TODO(shahms): use gMock protobuf matchers when available. 92 CanonicalizeHashes(&unit); 93 unit.set_argument(2, "dummy-target"); 94 unit.clear_details(); 95 96 EXPECT_THAT(unit, EquivToProto(ExpectedCompilation())); 97 } 98 99 } // namespace 100 } // namespace kythe