selftest.run: Factor out read_testlist_file and open_file_or_pipe.
[samba.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 import os
23 import tempfile
24
25 from selftest.tests import TestCase
26
27 from selftest.testlist import (
28     RestrictedTestManager,
29     find_in_list,
30     open_file_or_pipe,
31     read_test_regexes,
32     read_testlist,
33     read_testlist_file,
34     )
35
36 from cStringIO import StringIO
37
38
39 class FindInListTests(TestCase):
40
41     def test_empty(self):
42         self.assertIs(None, find_in_list([], "foo.test"))
43
44     def test_no_reason(self):
45         self.assertEquals("because",
46             find_in_list([("foo.*bar", "because")], "foo.bla.bar"))
47
48
49 class ReadTestRegexesTests(TestCase):
50
51     def test_comment(self):
52         f = StringIO("# I am a comment\n # I am also a comment\n")
53         self.assertEquals([], list(read_test_regexes(f)))
54
55     def test_no_reason(self):
56         f = StringIO(" foo\n")
57         self.assertEquals([("foo", None)], list(read_test_regexes(f)))
58
59     def test_reason(self):
60         f = StringIO(" foo # because\nbar\n")
61         self.assertEquals([("foo", "because"), ("bar", None)],
62             list(read_test_regexes(f)))
63
64
65 class ReadTestlistTests(TestCase):
66
67     def test_read_list(self):
68         inf = StringIO("-- TEST --\nfoo\nbar\nbla\n")
69         outf = StringIO()
70         self.assertEquals([('foo', 'bar', 'bla', False, False)],
71                 list(read_testlist(inf, outf)))
72         self.assertEquals("", outf.getvalue())
73
74     def test_read_list_passes_through(self):
75         inf = StringIO("MORENOISE\n-- TEST --\nfoo\nbar\nbla\nNOISE\n")
76         outf = StringIO()
77         self.assertEquals([('foo', 'bar', 'bla', False, False)],
78                 list(read_testlist(inf, outf)))
79         self.assertEquals("MORENOISE\nNOISE\n", outf.getvalue())
80
81
82
83 class RestrictedTestManagerTests(TestCase):
84
85     def test_unused(self):
86         mgr = RestrictedTestManager(["foo.bar"])
87         self.assertEquals(["foo.bar"], list(mgr.iter_unused()))
88
89     def test_run_testsuite(self):
90         mgr = RestrictedTestManager(["foo.bar"])
91         self.assertEquals(None, mgr.should_run_testsuite("foo.bar"))
92
93     def test_run_subtest(self):
94         mgr = RestrictedTestManager(["foo.bar.bla"])
95         self.assertEquals(["bla"], mgr.should_run_testsuite("foo.bar"))
96
97     def test_run_subtest_after_testsuite(self):
98         mgr = RestrictedTestManager(["foo.bar", "foo.bar.bla"])
99         self.assertEquals(None, mgr.should_run_testsuite("foo.bar"))
100
101     def test_run_multiple_subtests(self):
102         mgr = RestrictedTestManager(["foo.bar.blie", "foo.bar.bla"])
103         self.assertEquals(["blie", "bla"], mgr.should_run_testsuite("foo.bar"))
104
105     def test_run_nomatch(self):
106         mgr = RestrictedTestManager(["foo.bar"])
107         self.assertEquals([], mgr.should_run_testsuite("foo.blie.bla"))
108
109
110 class OpenFileOrPipeTests(TestCase):
111
112     def test_regular_file(self):
113         (fd, p) = tempfile.mkstemp()
114         self.addCleanup(os.remove, p)
115         f = os.fdopen(fd, 'w')
116         try:
117             f.write('data\nbla\n')
118         finally:
119             f.close()
120         f = open_file_or_pipe(p, 'r')
121         try:
122             self.assertEquals("data\nbla\n", f.read())
123         finally:
124             f.close()
125
126     def test_pipe(self):
127         f = open_file_or_pipe('echo foo|', 'r')
128         try:
129             self.assertEquals("foo\n", f.read())
130         finally:
131             f.close()
132
133
134 class ReadTestListFileTests(TestCase):
135
136     def test_regular_file(self):
137         (fd, p) = tempfile.mkstemp()
138         self.addCleanup(os.remove, p)
139         f = os.fdopen(fd, 'w')
140         try:
141             f.write('noise\n-- TEST --\ndata\nenv\ncmd\n')
142         finally:
143             f.close()
144         outf = StringIO()
145         self.assertEquals(
146             [('data', 'env', 'cmd', False, False)],
147             list(read_testlist_file(p, outf)))
148         self.assertEquals("noise\n", outf.getvalue())