s3-libsmb: Fix possible comparsion problems.
[Samba/bb.git] / selftest / tests / test_testlist.py
blob4474d0aa07a097bfee8523ed8dc789f9d181d258
1 # test_testlist.py -- The tests for selftest testlist code
2 # Copyright (C) 2012 Jelmer Vernooij <jelmer@samba.org>
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.
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.
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.
20 """Tests for selftest.testlist."""
22 import os
23 import tempfile
25 from selftest.tests import TestCase
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,
36 from cStringIO import StringIO
39 class FindInListTests(TestCase):
41 def test_empty(self):
42 self.assertIs(None, find_in_list([], "foo.test"))
44 def test_no_reason(self):
45 self.assertEquals("because",
46 find_in_list([("foo.*bar", "because")], "foo.bla.bar"))
49 class ReadTestRegexesTests(TestCase):
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)))
55 def test_no_reason(self):
56 f = StringIO(" foo\n")
57 self.assertEquals([("foo", None)], list(read_test_regexes(f)))
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)))
65 class ReadTestlistTests(TestCase):
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())
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())
83 class RestrictedTestManagerTests(TestCase):
85 def test_unused(self):
86 mgr = RestrictedTestManager(["foo.bar"])
87 self.assertEquals(["foo.bar"], list(mgr.iter_unused()))
89 def test_run_testsuite(self):
90 mgr = RestrictedTestManager(["foo.bar"])
91 self.assertEquals(None, mgr.should_run_testsuite("foo.bar"))
93 def test_run_subtest(self):
94 mgr = RestrictedTestManager(["foo.bar.bla"])
95 self.assertEquals(["bla"], mgr.should_run_testsuite("foo.bar"))
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"))
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"))
105 def test_run_nomatch(self):
106 mgr = RestrictedTestManager(["foo.bar"])
107 self.assertEquals([], mgr.should_run_testsuite("foo.blie.bla"))
110 class OpenFileOrPipeTests(TestCase):
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()
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()
134 class ReadTestListFileTests(TestCase):
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())