File: test_file_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 (74 lines) | stat: -rw-r--r-- 2,135 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
# 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

from . import autoconfig

from pygccxml import parser

TEST_FILE = os.path.join(autoconfig.data_directory, 'core_cache.hpp')
cache_file = os.path.join(
            autoconfig.data_directory,
            'pygccxml.cache')


def reset_cache():
    if os.path.exists(cache_file) and os.path.isfile(cache_file):
        os.remove(cache_file)


def touch():
    # Need to change file.
    with open(TEST_FILE, "a+") as header:
        header.write("//touch")


def test_update_cache():
    reset_cache()
    config = autoconfig.cxx_parsers_cfg.config.clone()

    # Save the content of the header file for later
    with open(TEST_FILE, "r") as old_header:
        content = old_header.read()

    declarations = parser.parse([TEST_FILE], config)
    cache = parser.file_cache_t(cache_file)
    cache.update(
        source_file=TEST_FILE,
        configuration=config,
        declarations=declarations,
        included_files=[])
    assert declarations == cache.cached_value(
            TEST_FILE,
            config)
    touch()
    assert cache.cached_value(TEST_FILE, config) is None

    # We wrote a //touch in the header file. Just replace the file with the
    # original content. The touched file would be sometimes commited by
    # error as it was modified.
    with open(TEST_FILE, "w") as new_header:
        new_header.write(content)


def test_cache_from_file():
    reset_cache()
    config = autoconfig.cxx_parsers_cfg.config.clone()
    declarations = parser.parse([TEST_FILE], config)
    cache = parser.file_cache_t(cache_file)
    cache.update(
        source_file=TEST_FILE,
        configuration=config,
        declarations=declarations,
        included_files=[])
    assert declarations == cache.cached_value(
            TEST_FILE,
            config)
    cache.flush()
    cache = parser.file_cache_t(cache_file)
    assert declarations == cache.cached_value(
            TEST_FILE,
            config)