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
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,
20 """Tests for selftest.testlist."""
25 from samba
.tests
import TestCase
27 from selftest
.testlist
import (
28 RestrictedTestManager
,
36 from cStringIO
import StringIO
39 class FindInListTests(TestCase
):
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")
70 self
.assertEquals([('foo', 'bar', 'bla', None)],
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")
77 self
.assertEquals([('foo', 'bar', 'bla', None)],
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')
117 f
.write('data\nbla\n')
120 f
= open_file_or_pipe(p
, 'r')
122 self
.assertEquals("data\nbla\n", f
.read())
127 f
= open_file_or_pipe('echo foo|', 'r')
129 self
.assertEquals("foo\n", f
.read())
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')
141 f
.write('noise\n-- TEST --\ndata\nenv\ncmd\n')
146 [('data', 'env', 'cmd', None)],
147 list(read_testlist_file(p
, outf
)))
148 self
.assertEquals("noise\n", outf
.getvalue())