Files
carbon-lang/toolchain/install/install_paths_test_helpers.cpp
T
Chandler Carruth 2e509e9103 Port //toolchain/install to new filesystem library (#5905)
This removes a bunch of manual filesystem helpers and complexity that
are directly provided by the new library.

It also moves all of the install paths detection to use
`std::filesystem::path` instead of the LLVM path library. The goal is to
consolidate all our logic onto a single stack, and the standard one
seems the best for that purpose. This does give up some of the
optimizations of this code to avoid memory allocation, but in practice
that likely isn't a critical issue. And with the new filesystem library
we can likely do more to avoid that by using directory-object-relative
filesystem access. However, that will have to wait for moving more parts
of the toolchain over to use this set of filesystem abstractions. There
is a related TODO left in the manifest handling code.
2025-08-12 02:20:22 +00:00

34 lines
1.2 KiB
C++

// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include "toolchain/install/install_paths_test_helpers.h"
#include <memory>
#include <utility>
#include "testing/base/global_exe_path.h"
namespace Carbon::Testing {
// Prepares the VFS with prelude files from the real filesystem. Primarily for
// tests.
auto AddPreludeFilesToVfs(
const InstallPaths& install_paths,
llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem>& vfs) -> void {
// Load the prelude into the test VFS.
auto real_fs = llvm::vfs::getRealFileSystem();
auto prelude = install_paths.ReadPreludeManifest();
CARBON_CHECK(prelude.ok(), "{0}", prelude.error());
for (const auto& path : *prelude) {
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> file =
real_fs->getBufferForFile(path);
CARBON_CHECK(file, "Error getting file: {0}", file.getError().message());
bool added = vfs->addFile(path, /*ModificationTime=*/0, std::move(*file));
CARBON_CHECK(added, "Duplicate file: {0}", path);
}
}
} // namespace Carbon::Testing