~jamesh/archive-index/app-install

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
#!/usr/bin/env python3
import os.path
import sys
import unittest
import subprocess
sys.path.insert(0, "..")

from src.ContentsIndex import ContentsIndex

CONTENTS_PATH = "archive/dists/test/Contents-bar.gz"
INDEX_PATHS = ("usr/share/applications", "/usr/lib", "/usr/share/")
CONTENTS_LINES, INDEX_LINES = 18, (3, 1, 8)

SEARCH_EXACT_PAIRS = {
  # regular items
  "/usr/bin/no-icon-app": "no-icon-app",
  "/usr/share/menu/no-icon": "no-icon-app",
  # relative symlink item
  "usr/share/icons/symlink-to-usrlibsym/symlink-icon-app.png":
      "symlink-icon-app",
  # absolute symlink item
  "usr/share/link_dir/subdir/needle.txt":
      "abs-symlink-app",
  # failure case
  "/red/herring/path": None}

SEARCH_PAIRS = {
  (".*symlink-icon-app", "usr/lib"): [("symlink-icon-app",
   "usr/lib/sym/symlink-icon-app.png")],
  (".*link.*\.desktop$", "/usr/share/applications"): [("symlink-icon-app",
   "usr/share/applications/symlink-icon-app.desktop")],
  (".*no-icon$", "usr/share/"): [("no-icon-app",
   "usr/share/menu/no-icon")]}


class ContentsIndexTest(unittest.TestCase):
    """
    Tests the proper operation of ContentsIndex, which handles regex
    searches an archive mapping package names to file paths.
    """

    @classmethod
    def setUpClass(self):
        self.index = ContentsIndex(CONTENTS_PATH, INDEX_PATHS)

    def test_contents_length(self):
        """archive cache stores its length in lines"""
        self.assertTrue(hasattr(self.index, "__len__"))
        self.assertEqual(len(self.index), CONTENTS_LINES)

    def test_index_length(self):
        """archive cache indexes store their length in lines"""
        for i, path in enumerate(INDEX_PATHS):
            self.assertEqual(self.index.__len__(path), INDEX_LINES[i])

    def test_index_checking(self):
        """archive cache allows idiomatic checking of index existance"""
        self.assertTrue(hasattr(self.index, "__contains__"))
        self.assertTrue(hasattr(self.index, "prefixes"))
        for path in INDEX_PATHS:
            self.assertTrue(path in self.index)

    def test_search_exact(self):
        """archive cache can find an exact path from contents"""
        for arg, result in SEARCH_EXACT_PAIRS.items():
            self.assertEqual(self.index.search_exact(arg), result)

    def test_search_regex(self):
        """archive cache can find a regex result from contents"""
        for args, results in SEARCH_PAIRS.items():
            self.assertEqual(set(self.index.search(*args)), set(results))

    def test_search_exact_symlinked(self):
        """archive cache should return partial path matches for symlinks"""
        # Contents.gz interprets symlinks as files. If we search for an exact
        # file via a symlinked directory, it should still work.
        result = self.index.search_exact("usr/share/icons/symlink-to-usrlibsym"
                                         "/symlink-icon-app.png")
        self.assertEqual(result, "symlink-icon-app")

    def test_search_no_memory(self):
        """archive cache search should work without memory caching"""
        old_index = self.index
        self.index = ContentsIndex(CONTENTS_PATH, INDEX_PATHS, memory=0)
        self.test_search_exact()
        self.test_search_regex()
        self.test_search_exact_symlinked()
        self.index = old_index

if __name__ == "__main__":
    if not os.path.exists(CONTENTS_PATH):
        subprocess.call("./build_test_archive")
    unittest.main()