kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/cxx/extractor/bazel_artifact_reader_test.cc (about) 1 /* 2 * Copyright 2020 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 #include "kythe/cxx/extractor/bazel_artifact_reader.h" 17 18 #include <sstream> 19 #include <utility> 20 #include <vector> 21 22 #include "absl/strings/str_cat.h" 23 #include "gmock/gmock.h" 24 #include "google/protobuf/io/zero_copy_stream_impl.h" 25 #include "google/protobuf/util/delimited_message_util.h" 26 #include "gtest/gtest.h" 27 #include "kythe/cxx/extractor/bazel_artifact.h" 28 #include "kythe/cxx/extractor/bazel_event_reader.h" 29 #include "third_party/bazel/src/main/java/com/google/devtools/build/lib/buildeventstream/proto/build_event_stream.pb.h" 30 31 namespace kythe { 32 namespace { 33 using ::google::protobuf::io::IstreamInputStream; 34 using ::google::protobuf::util::SerializeDelimitedToOstream; 35 using ::testing::ElementsAre; 36 37 template <typename T, typename U> 38 auto Artifact(T&& t, U&& u) { 39 return ::testing::AllOf( 40 ::testing::Field(&BazelArtifact::label, std::forward<T>(t)), 41 ::testing::Field(&BazelArtifact::files, std::forward<U>(u))); 42 } 43 44 template <typename T, typename U> 45 auto File(T&& path, U&& uri) { 46 return ::testing::AllOf( 47 ::testing::Field(&BazelArtifactFile::local_path, std::forward<T>(path)), 48 ::testing::Field(&BazelArtifactFile::uri, std::forward<U>(uri))); 49 } 50 51 TEST(BazelArtifactReaderTest, ReadsExpectedArtifacts) { 52 static constexpr int kArtifactCount = 5; 53 std::stringstream stream; 54 for (int i = 0; i < kArtifactCount; i++) { 55 build_event_stream::BuildEvent event; 56 auto* id = event.mutable_id()->mutable_action_completed(); 57 id->set_primary_output("local/path/to/file.kzip"); 58 id->set_label(absl::StrCat("//id/label:", i)); 59 auto* payload = event.mutable_action(); 60 payload->set_success(true); 61 payload->set_type("extra_action"); 62 payload->mutable_primary_output()->set_uri( 63 absl::StrCat("file:///dummy/", i, ".kzip")); 64 ASSERT_TRUE(SerializeDelimitedToOstream(event, &stream)); 65 } 66 67 IstreamInputStream input(&stream); 68 BazelEventReader events(&input); 69 BazelArtifactReader reader(&events); 70 std::vector<BazelArtifact> artifacts; 71 for (; !reader.Done(); reader.Next()) { 72 artifacts.push_back(std::move(reader.Ref())); 73 } 74 ASSERT_TRUE(reader.status().ok()) << reader.status(); 75 EXPECT_THAT( 76 artifacts, 77 ElementsAre( 78 Artifact("//id/label:0", ElementsAre(File("local/path/to/file.kzip", 79 "file:///dummy/0.kzip"))), 80 Artifact("//id/label:1", ElementsAre(File("local/path/to/file.kzip", 81 "file:///dummy/1.kzip"))), 82 Artifact("//id/label:2", ElementsAre(File("local/path/to/file.kzip", 83 "file:///dummy/2.kzip"))), 84 Artifact("//id/label:3", ElementsAre(File("local/path/to/file.kzip", 85 "file:///dummy/3.kzip"))), 86 Artifact("//id/label:4", ElementsAre(File("local/path/to/file.kzip", 87 "file:///dummy/4.kzip"))))); 88 } 89 90 } // namespace 91 } // namespace kythe