summaryrefslogtreecommitdiffstats
path: root/testsuite/Testtools/__init__.py
blob: 993938e07fcab0a7911157e5873584dc98c87538 (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import os
import re
import sys

# add all parent testsuite directories to sys.path to allow (most)
# relative imports in python 2.4
path = os.path.dirname(__file__)
while path != "/":
    if os.path.basename(path).lower().startswith("test"):
        sys.path.append(path)
    if os.path.basename(path) == "testsuite":
        break
    path = os.path.dirname(path)
from common import *

TOOLSDIR = os.path.abspath(os.path.join(os.path.dirname(__file__),
                                        "..", "..", "tools"))

class TestToolsDocs(Bcfg2TestCase):
    blankline = re.compile(r'^\s*$')

    @skipUnless(os.path.exists(TOOLSDIR),
                "%s does not exist, skipping tools/ tests" % TOOLSDIR)
    def tools_in_README(self, toolsdir=None):
        if toolsdir is None:
            toolsdir = TOOLSDIR
        script = None
        desc = None
        started = False
        rv = dict()
        for line in open(os.path.join(toolsdir, "README")).readlines():
            if not started:
                # skip up to the first blank line
                if self.blankline.match(line):
                    started = True
            elif not self.blankline.match(line):
                match = re.match(r'^(\S+)', line)
                if match:
                    script = match.group(1)
                    desc = ''
                else:
                    match = re.match(r'^\s+(?:-\s+)?(.*)$', line)
                    if match:
                        desc += match.group(1)
            else:
                # blank line
                if script and desc:
                    rv[script] = desc
        if script and desc:
            rv[script] = desc
        return rv

    @skipUnless(os.path.exists(TOOLSDIR),
                "%s does not exist, skipping tools/ tests" % TOOLSDIR)
    def test_all_scripts_in_README(self, prefix=''):
        toolsdir = os.path.join(TOOLSDIR, prefix)
        tools = self.tools_in_README(toolsdir=toolsdir)
        for fname in os.listdir(toolsdir):
            if fname == 'README':
                continue
            dname = os.path.join(prefix, fname) # display name
            fpath = os.path.join(toolsdir, fname)
            if os.path.isfile(fpath):
                self.assertIn(fname, tools,
                              msg="%s has no entry in README" % dname)
                self.assertNotRegexpMatches(tools[fname], r'^(\s|\?)*$',
                                    msg="%s has an empty entry in README" %
                                    dname)

    @skipUnless(os.path.exists(TOOLSDIR),
                "%s does not exist, skipping tools/ tests" % TOOLSDIR)
    def test_no_extras_in_README(self, prefix=''):
        toolsdir = os.path.join(TOOLSDIR, prefix)
        tools = self.tools_in_README(toolsdir=toolsdir)
        for fname in tools.keys():
            dname = os.path.join(prefix, fname) # display name
            fpath = os.path.join(toolsdir, fname)
            self.assertTrue(os.path.exists(fpath),
                            msg="%s is listed in README but does not exist" %
                            dname)

    @skipUnless(os.path.exists(TOOLSDIR),
                "%s does not exist, skipping tools/ tests" % TOOLSDIR)
    def test_upgrade_scripts_documented(self):
        upgrade = os.path.join(TOOLSDIR, "upgrade")
        for udir in os.listdir(upgrade):
            upath = os.path.join(upgrade, udir)
            dname = os.path.join("upgrade", udir) # display name
            self.assertTrue(os.path.isdir(upath),
                            msg="Unexpected script %s found in %s" % (udir,
                                                                      dname))
            self.assertTrue(os.path.exists(os.path.join(upath, 'README')),
                            msg="%s has no README" % dname)
            self.test_all_scripts_in_README(dname)