xref: /llvm-project/lldb/test/API/commands/settings/use_source_cache/TestUseSourceCache.py (revision 47d9ca87b0385975e8b14f5df06886ddd6057b17)
1"""
2Tests large source files are not locked on Windows when source cache is disabled
3"""
4
5import lldb
6import os
7from lldbsuite.test.decorators import *
8from lldbsuite.test.lldbtest import *
9from lldbsuite.test import lldbutil
10from shutil import copy
11
12
13class SettingsUseSourceCacheTestCase(TestBase):
14    NO_DEBUG_INFO_TESTCASE = True
15
16    def test_set_use_source_cache_false(self):
17        """Test that after 'set use-source-cache false', files are not locked."""
18        self.set_use_source_cache_and_test(False)
19
20    @skipIf(hostoslist=no_match(["windows"]))
21    def test_set_use_source_cache_true(self):
22        """Test that after 'set use-source-cache true', files are locked."""
23        self.set_use_source_cache_and_test(True)
24
25    def set_use_source_cache_and_test(self, is_cache_enabled):
26        """Common test for both True/False values of use-source-cache."""
27        self.build()
28
29        # Enable/Disable source cache
30        self.runCmd(
31            "settings set use-source-cache " + ("true" if is_cache_enabled else "false")
32        )
33
34        # Get paths for the main source file.
35        src = self.getBuildArtifact("main-copy.cpp")
36        self.assertTrue(src)
37
38        # Make sure source file is bigger than 16K to trigger memory mapping
39        self.assertGreater(os.stat(src).st_size, 4 * 4096)
40
41        target, process, thread, breakpoint = lldbutil.run_to_name_breakpoint(
42            self, "calc"
43        )
44
45        # Show the source file contents to make sure LLDB loads src file.
46        self.runCmd("source list")
47
48        # Try overwriting the source file.
49        is_file_overwritten = self.overwriteFile(src)
50
51        if is_cache_enabled:
52            self.assertFalse(
53                is_file_overwritten,
54                "Source cache is enabled, but writing to file succeeded",
55            )
56
57        if not is_cache_enabled:
58            self.assertTrue(
59                is_file_overwritten,
60                "Source cache is disabled, but writing to file failed",
61            )
62
63    def overwriteFile(self, src):
64        """Write to file and return true iff file was successfully written."""
65        try:
66            f = open(src, "w")
67            f.writelines(["// hello world\n"])
68            f.close()
69            return True
70        except Exception:
71            return False
72