summaryrefslogtreecommitdiff
path: root/tools/Testing.py
blob: 2b9012701fbdb6357a4f28606eff16cbcfe6be57 (plain)
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
# -*- coding: utf-8 -*-

import glob
import os
import tempfile

import Pkg


def _testpath():
    return os.environ.get(
        'TESTPATH',
        os.path.join(os.path.dirname(__file__), "..", "test")
    )


TEST_CONFIG = os.path.join(_testpath(), "test.config")
with open(TEST_CONFIG) as f:
    exec(compile(f.read(), TEST_CONFIG, 'exec'))

currently_testing = 0
output = []


def isTest():
    return currently_testing


def startTest():
    global currently_testing
    global output
    output = []
    currently_testing = 1


def addOutput(s):
    global output
    output.append(s)


def getOutput():
    global output
    return output


def getTestedPath(path):
    return os.path.join(_testpath(), path)


def getTestedPackage(name):
    pkg_path = glob.glob(getTestedPath(name) + "-*.rpm")[0]
    return Pkg.Pkg(pkg_path, tempfile.gettempdir())


def getTestedSpecPackage(name):
    pkg_path = glob.glob(getTestedPath(name) + ".spec")[0]
    return Pkg.FakePkg(pkg_path)


class OutputTest(object):

    check = None
    check_spec = None

    def _rpm_test_output(self, rpm, check=None):
        with getTestedPackage(rpm) as pkg:
            startTest()
            (check or self.check)(pkg)
            return getOutput()

    def _spec_test_output(self, spec, check=None):
        with getTestedSpecPackage(spec) as pkg:
            startTest()
            # call check_spec() directly, as check() doesn't work with
            # getTestedSpecPackage()
            (check or self.check_spec)(pkg, pkg.name)
            return getOutput()