blob: 4168b58903a2bddef2ae4f2f27dbcc10d7d2d755 [file] [edit]
// Copyright 2026 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#include "testing/test_file_utils.h"
#include <fstream>
#include <memory>
#include <sstream>
#include <string>
#include <utility>
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/string_view.h"
#include "riegeli/bytes/cfile_reader.h"
#include "riegeli/bytes/reader.h"
#include "tools/cpp/runfiles/runfiles.h"
namespace credentio_testing {
namespace {
std::string GetAbsoluteFilePath(absl::string_view file_path) {
std::string error;
std::unique_ptr<bazel::tools::cpp::runfiles::Runfiles> runfiles(
bazel::tools::cpp::runfiles::Runfiles::CreateForTest(&error));
if (runfiles != nullptr) {
return runfiles->Rlocation(std::string(file_path));
}
return std::string(file_path);
}
} // namespace
absl::StatusOr<std::string> GetContents(absl::string_view file_path) {
std::string actual_path = GetAbsoluteFilePath(file_path);
std::ifstream f(actual_path, std::ios::binary);
if (!f.is_open()) {
return absl::NotFoundError("Failed to open file: " + actual_path);
}
std::stringstream buffer;
buffer << f.rdbuf();
return buffer.str();
}
absl::StatusOr<std::unique_ptr<riegeli::Reader>> GetFileReader(
absl::string_view file_path) {
std::string actual_path = GetAbsoluteFilePath(file_path);
std::unique_ptr<riegeli::CFileReader<>> reader =
std::make_unique<riegeli::CFileReader<>>(actual_path);
if (!reader->ok()) {
return reader->status();
}
return std::move(reader);
}
} // namespace credentio_testing