xref: /llvm-project/clang-tools-extra/clangd/unittests/FSTests.cpp (revision 35ce741ef3e3dd9db1da3ea0a06c565cb90f665a)
1 //===-- FSTests.cpp - File system related tests -----------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "FS.h"
10 #include "TestFS.h"
11 #include "gmock/gmock.h"
12 #include "gtest/gtest.h"
13 
14 namespace clang {
15 namespace clangd {
16 namespace {
17 
TEST(FSTests,PreambleStatusCache)18 TEST(FSTests, PreambleStatusCache) {
19   llvm::StringMap<std::string> Files;
20   Files["x"] = "";
21   Files["y"] = "";
22   Files["main"] = "";
23   auto FS = buildTestFS(Files);
24 
25   PreambleFileStatusCache StatCache(testPath("main"));
26   auto ProduceFS = StatCache.getProducingFS(FS);
27   EXPECT_TRUE(ProduceFS->openFileForRead("x"));
28   EXPECT_TRUE(ProduceFS->status("y"));
29   EXPECT_TRUE(ProduceFS->status("main"));
30 
31   EXPECT_TRUE(StatCache.lookup(testPath("x")).has_value());
32   EXPECT_TRUE(StatCache.lookup(testPath("y")).has_value());
33   // Main file is not cached.
34   EXPECT_FALSE(StatCache.lookup(testPath("main")).has_value());
35 
36   llvm::vfs::Status S("fake", llvm::sys::fs::UniqueID(123, 456),
37                       std::chrono::system_clock::now(), 0, 0, 1024,
38                       llvm::sys::fs::file_type::regular_file,
39                       llvm::sys::fs::all_all);
40   StatCache.update(*FS, S, "real");
41   auto ConsumeFS = StatCache.getConsumingFS(FS);
42   EXPECT_FALSE(ConsumeFS->status(testPath("fake")));
43   auto Cached = ConsumeFS->status(testPath("real"));
44   EXPECT_TRUE(Cached);
45   EXPECT_EQ(Cached->getName(), testPath("real"));
46   EXPECT_EQ(Cached->getUniqueID(), S.getUniqueID());
47 
48   // real and temp/../real should hit the same cache entry.
49   // However, the Status returned reflects the actual path requested.
50   auto CachedDotDot = ConsumeFS->status(testPath("temp/../real"));
51   EXPECT_TRUE(CachedDotDot);
52   EXPECT_EQ(CachedDotDot->getName(), testPath("temp/../real"));
53   EXPECT_EQ(CachedDotDot->getUniqueID(), S.getUniqueID());
54 }
55 
56 } // namespace
57 } // namespace clangd
58 } // namespace clang
59