selftest.testlist: Add manager for restricted test lists.
[obnox/samba/samba-obnox.git] / selftest / tests / test_testlist.py
1 # test_testlist.py -- The tests for selftest testlist code
2 # Copyright (C) 2012 Jelmer Vernooij <jelmer@samba.org>
3 #
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; version 3
7 # of the License or (at your option) any later version of
8 # the License.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18 # MA  02110-1301, USA.
19
20 """Tests for selftest.testlist."""
21
22 from selftest.testlist import (
23     RestrictedTestManager,
24     find_in_list,
25     read_test_regexes,
26     read_testlist,
27     )
28
29 from cStringIO import StringIO
30
31 import unittest
32
33
34 class FindInListTests(unittest.TestCase):
35
36     def test_empty(self):
37         self.assertIs(None, find_in_list([], "foo.test"))
38
39     def test_no_reason(self):
40         self.assertEquals("because",
41             find_in_list([("foo.*bar", "because")], "foo.bla.bar"))
42
43
44 class ReadTestRegexesTests(unittest.TestCase):
45
46     def test_comment(self):
47         f = StringIO("# I am a comment\n # I am also a comment\n")
48         self.assertEquals([], list(read_test_regexes(f)))
49
50     def test_no_reason(self):
51         f = StringIO(" foo\n")
52         self.assertEquals([("foo", None)], list(read_test_regexes(f)))
53
54     def test_reason(self):
55         f = StringIO(" foo # because\nbar\n")
56         self.assertEquals([("foo", "because"), ("bar", None)],
57             list(read_test_regexes(f)))
58
59
60 class ReadTestlistTests(unittest.TestCase):
61
62     def test_read_list(self):
63         inf = StringIO("-- TEST --\nfoo\nbar\nbla\n")
64         outf = StringIO()
65         self.assertEquals([('foo', 'bar', 'bla', False, False)],
66                 list(read_testlist(inf, outf)))
67         self.assertEquals("", outf.getvalue())
68
69     def test_read_list_passes_through(self):
70         inf = StringIO("MORENOISE\n-- TEST --\nfoo\nbar\nbla\nNOISE\n")
71         outf = StringIO()
72         self.assertEquals([('foo', 'bar', 'bla', False, False)],
73                 list(read_testlist(inf, outf)))
74         self.assertEquals("MORENOISE\nNOISE\n", outf.getvalue())
75
76
77
78 class RestrictedTestManagerTests(unittest.TestCase):
79
80     def test_unused(self):
81         mgr = RestrictedTestManager(["foo.bar"])
82         self.assertEquals(["foo.bar"], list(mgr.iter_unused()))
83
84     def test_run_testsuite(self):
85         mgr = RestrictedTestManager(["foo.bar"])
86         self.assertEquals(None, mgr.should_run_testsuite("foo.bar"))
87
88     def test_run_subtest(self):
89         mgr = RestrictedTestManager(["foo.bar.bla"])
90         self.assertEquals(["bla"], mgr.should_run_testsuite("foo.bar"))
91
92     def test_run_subtest_after_testsuite(self):
93         mgr = RestrictedTestManager(["foo.bar", "foo.bar.bla"])
94         self.assertEquals(None, mgr.should_run_testsuite("foo.bar"))
95
96     def test_run_multiple_subtests(self):
97         mgr = RestrictedTestManager(["foo.bar.blie", "foo.bar.bla"])
98         self.assertEquals(["blie", "bla"], mgr.should_run_testsuite("foo.bar"))
99
100     def test_run_nomatch(self):
101         mgr = RestrictedTestManager(["foo.bar"])
102         self.assertEquals([], mgr.should_run_testsuite("foo.blie.bla"))