File: TestSwiftMetadataCache.py

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (110 lines) | stat: -rw-r--r-- 4,227 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
"""
Test the swift metadata cache works correctly
"""
import glob
import io
import os
import lldb
from lldbsuite.test.lldbtest import *
from lldbsuite.test.decorators import *
import lldbsuite.test.lldbutil as lldbutil
import unittest2


class TestSwiftMetadataCache(TestBase):

    def setUp(self):
        # Call super's setUp().
        TestBase.setUp(self)

        cache_dir = self.getBuildArtifact('swift-metadata-cache')
        # Set the lldb module cache directory to a directory inside the build
        # artifacts directory so no other tests are interfered with.
        self.runCmd('settings set symbols.swift-metadata-cache-path "%s"' % cache_dir)
        self.runCmd('settings set symbols.enable-swift-metadata-cache true')

    def check_strings_in_log(self, log_path, strings):
        types_log = io.open(log_path, "r", encoding='utf-8')
        for line in types_log:
            # copy to remove from original
            copy = strings[:]
            for s in copy:
                if s in line:
                    try:
                        strings.remove(s)
                    # In case the string shows up more than once
                    except ValueError:
                        pass

            if len(strings) == 0:
                return True
        return False
            
        
    @swiftTest
    def test_swift_metadata_cache(self):
        """Test the swift metadata cache."""
        
        # This test runs does three runs:

        # In the first run, we make sure we build the cache.

        # In the second run, we make sure we use the cache.

        # In the third run, we emulate a source file modification by building 
        # main_modified.swift instead of main.swift. We then make sure that
        # we invalidate the modified cache and rebuild it.

        # Build with the "original" main file.
        self.build(dictionary={'SWIFT_SOURCES': 'main.swift'})

        types_log = self.getBuildDir() + "/first_run_types.txt"
        self.runCmd('log enable lldb types -v -f "%s"' % types_log)
        target, process, thread, bkpt = lldbutil.run_to_source_breakpoint(
            self, 'Set breakpoint here', lldb.SBFileSpec('main.swift'))

        # Run frame variable for the first time to build the cache.
        self.expect('v v', substrs=['SomeTypeWeWillLookUp'])

        # Check that we wrote the cache for the main module.
        self.assertTrue(self.check_strings_in_log(types_log, [
            'Cache file written for module a.out.']))
        # Finish run.
        self.runCmd('c')


        types_log = self.getBuildDir() + "/second_run_types.txt"
        self.runCmd('log enable lldb types -v -f "%s"' % types_log)
        target, process, thread, bkpt = lldbutil.run_to_source_breakpoint(
            self, 'Set breakpoint here', lldb.SBFileSpec('main.swift'))

        # Run frame variable for the second time to check if the cache was queried.
        self.expect('v v', substrs=['SomeTypeWeWillLookUp'])

        # Check that we have a cache and that we found the type of 'v' in it.
        self.assertTrue(self.check_strings_in_log(types_log, [
            'Loaded cache for module a.out.',
            'Returning field descriptor for mangled name 1a20SomeTypeWeWillLookUpV']))
        
        # Finish run.
        self.runCmd('c')

        
        # Rebuild the "modified" program. This should have the exact same path 
        # as the original one.
        self.build(dictionary={'SWIFT_SOURCES': 'main_modified.swift'})
        
        types_log = self.getBuildDir() + "/third_run_types.txt"
        self.runCmd('log enable lldb types -v -f "%s"' % types_log)
        target, process, thread, bkpt = lldbutil.run_to_source_breakpoint(
            self, 'Set breakpoint here', lldb.SBFileSpec('main_modified.swift'))
         

        # Run frame variable for the third time to check that the cache is invalidated
        # and that we rebuild it.
        self.expect('v v', substrs=['SomeTypeWeWillLookUp'])

        # Check that we found the type of 'v' in the cache.
        self.assertTrue(self.check_strings_in_log(types_log, [
            'Module UUID mismatch for a.out.',
            'Cache file written for module a.out.']))