kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/cxx/extractor/testdata/build_config_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 <algorithm>
    18  
    19  #include "absl/strings/string_view.h"
    20  #include "gmock/gmock.h"
    21  #include "google/protobuf/message.h"
    22  #include "gtest/gtest.h"
    23  #include "kythe/cxx/extractor/testlib.h"
    24  #include "kythe/proto/analysis.pb.h"
    25  #include "kythe/proto/buildinfo.pb.h"
    26  #include "protobuf-matchers/protocol-buffer-matchers.h"
    27  
    28  namespace kythe {
    29  namespace {
    30  using ::protobuf_matchers::EquivToProto;
    31  
    32  proto::CompilationUnit ExpectedCompilation() {
    33    return ParseTextCompilationUnitOrDie(R"pb(
    34      v_name { language: "c++" }
    35      required_input {
    36        v_name { path: "kythe/cxx/extractor/testdata/build_config.cc" }
    37        info {
    38          path: "kythe/cxx/extractor/testdata/build_config.cc"
    39          digest: "49e4a60bd04c5ec2070a81f53d7a19db4a3538db6764e5804047c219be5f9309"
    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"
    55      argument: "./kythe/cxx/extractor/testdata/build_config.cc"
    56      argument: "-fsyntax-only"
    57      source_file: "kythe/cxx/extractor/testdata/build_config.cc"
    58      working_directory: "/root"
    59      entry_context: "hash0"
    60      details {
    61        # The TextFormat parser does not like our custom type_url, but generally
    62        # disregards the part before the type name.
    63        [type.googleapis.com/kythe.proto.BuildDetails] {
    64          build_target: "//this/is/a/build:target"
    65          build_config: "test-build-config"
    66        }
    67      }
    68    )pb");
    69  }
    70  
    71  TEST(CxxExtractorTest, TestBuildConfigExtraction) {
    72    google::protobuf::LinkMessageReflection<kythe::proto::BuildDetails>();
    73    kythe::proto::CompilationUnit unit = ExtractSingleCompilationOrDie({
    74        {
    75            "--with_executable",
    76            "/dummy/bin/g++",
    77            "-I./kythe/cxx/extractor",
    78            "./kythe/cxx/extractor/testdata/build_config.cc",
    79        },
    80        {{"KYTHE_BUILD_CONFIG", "test-build-config"},
    81         {"KYTHE_ANALYSIS_TARGET", "//this/is/a/build:target"}},
    82    });
    83    CanonicalizeHashes(&unit);
    84    unit.set_argument(2, "dummy-target");
    85    unit.mutable_details()->erase(
    86        std::remove_if(
    87            unit.mutable_details()->begin(), unit.mutable_details()->end(),
    88            [&](const auto& any) {
    89              // This doesn't match the parsed type url above, but is compatible
    90              // with it.
    91              return any.type_url() != "kythe.io/proto/kythe.proto.BuildDetails";
    92            }),
    93        unit.mutable_details()->end());
    94  
    95    EXPECT_THAT(unit, EquivToProto(ExpectedCompilation()));
    96  }
    97  
    98  }  // namespace
    99  }  // namespace kythe