File: test_declarations_cache.py

package info (click to toggle)
pygccxml 3.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,444 kB
  • sloc: xml: 29,841; python: 13,914; cpp: 2,671; makefile: 163; ansic: 59
file content (144 lines) | stat: -rw-r--r-- 5,198 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# Copyright 2014-2017 Insight Software Consortium.
# Copyright 2004-2009 Roman Yakovenko.
# Distributed under the Boost Software License, Version 1.0.
# See http://www.boost.org/LICENSE_1_0.txt

import os
import os.path

from . import autoconfig

from pygccxml.parser.config import xml_generator_configuration_t
from pygccxml.parser import declarations_cache


def test_file_signature():
    file1 = os.path.join(autoconfig.data_directory, 'decl_cache_file1.txt')
    file1_dup = os.path.join(
        autoconfig.data_directory,
        'decl_cache_file1_duplicate.txt')
    file2 = os.path.join(autoconfig.data_directory, 'decl_cache_file2.txt')
    sig1 = declarations_cache.file_signature(file1)
    sig1_dup = declarations_cache.file_signature(file1_dup)
    sig2 = declarations_cache.file_signature(file2)
    assert sig1 == sig1_dup
    assert sig1 != sig2


def test_config_signature():
    diff_cfg_list = build_differing_cfg_list()
    def_cfg = diff_cfg_list[0]
    def_sig = declarations_cache.configuration_signature(def_cfg)

    # Test changes that should cause sig changes
    for cfg in diff_cfg_list[1:]:
        assert declarations_cache.configuration_signature(cfg) != def_sig

    # Test changes that should not cause sig changes
    no_changes = def_cfg.clone()
    assert declarations_cache.configuration_signature(no_changes) == def_sig

    ignore_changed = def_cfg.clone()
    ignore_changed.ignore_gccxml_output = True
    assert (
        declarations_cache.configuration_signature(ignore_changed) == def_sig
        )


def test_cache_interface():
    cache_file = os.path.join(
        autoconfig.build_directory,
        'decl_cache_test.test_cache_read.cache')
    file1 = os.path.join(autoconfig.data_directory, 'decl_cache_file1.txt')
    file1_dup = os.path.join(
        autoconfig.data_directory,
        'decl_cache_file1_duplicate.txt')
    file2 = os.path.join(autoconfig.data_directory, 'decl_cache_file2.txt')
    diff_cfg_list = build_differing_cfg_list()
    def_cfg = diff_cfg_list[0]

    if os.path.exists(cache_file):
        os.remove(cache_file)

    cache = declarations_cache.file_cache_t(cache_file)
    assert len(cache._file_cache_t__cache) == 0

    # test creating new entries for differing files
    cache.update(file1, def_cfg, 1, [])
    assert len(cache._file_cache_t__cache) == 1
    cache.update(file1_dup, def_cfg, 2, [])
    assert len(cache._file_cache_t__cache) == 1
    cache.update(file2, def_cfg, 3, [])
    assert len(cache._file_cache_t__cache) == 2

    assert cache.cached_value(file1, def_cfg) == 2
    assert cache.cached_value(file2, def_cfg) == 3

    # Test reading again
    cache.flush()
    cache = declarations_cache.file_cache_t(cache_file)
    assert len(cache._file_cache_t__cache) == 2
    assert cache.cached_value(file1, def_cfg) == 2
    assert cache.cached_value(file2, def_cfg) == 3

    # Test flushing doesn't happen if we don't touch the cache
    cache = declarations_cache.file_cache_t(cache_file)
    assert cache.cached_value(file1, def_cfg) == 2  # Read from cache
    cache.flush()    # should not actually flush
    cache = declarations_cache.file_cache_t(cache_file)
    assert len(cache._file_cache_t__cache) == 2

    # Test flush culling
    cache = declarations_cache.file_cache_t(cache_file)
    cache.update(file1_dup, def_cfg, 4, [])    # Modify cache
    cache.flush()    # should cull off one entry
    cache = declarations_cache.file_cache_t(cache_file)
    assert len(cache._file_cache_t__cache) == 1


def build_differing_cfg_list():
    """ Return a list of configurations that all differ. """
    cfg_list = []
    def_cfg = xml_generator_configuration_t(
        "xml_generator_path",
        '.', ['tmp'], ['sym'], ['unsym'], None, False, "")
    cfg_list.append(def_cfg)

    # Test changes that should cause sig changes
    gccxml_changed = def_cfg.clone()
    gccxml_changed.xml_generator_path = "other_path"
    cfg_list.append(gccxml_changed)

    wd_changed = def_cfg.clone()
    wd_changed.working_directory = "other_dir"
    cfg_list.append(wd_changed)

    # inc_changed = def_cfg.clone()
    # inc_changed.include_paths = ["/var/tmp"]
    # assert configuration_signature(inc_changed) != def_sig)
    inc_changed = xml_generator_configuration_t(
        "xml_generator_path", '.', ['/var/tmp'], ['sym'], ['unsym'],
        None, False, "")
    cfg_list.append(inc_changed)

    # def_changed = def_cfg.clone()
    # def_changed.define_symbols = ["symbol"]
    # assert configuration_signature(def_changed) != def_sig)
    def_changed = xml_generator_configuration_t(
        "xml_generator_path", '.', ['/var/tmp'], ['new-sym'], ['unsym'],
        None, False, "")
    cfg_list.append(def_changed)

    # undef_changed = def_cfg.clone()
    # undef_changed.undefine_symbols = ["symbol"]
    # assert configuration_signature(undef_changed) != def_sig)
    undef_changed = xml_generator_configuration_t(
        "xml_generator_path", '.', ['/var/tmp'], ['sym'], ['new-unsym'],
        None, False, "")
    cfg_list.append(undef_changed)

    cflags_changed = def_cfg.clone()
    cflags_changed.cflags = "new flags"
    cfg_list.append(cflags_changed)

    return cfg_list