1 # test_run.py -- Tests for selftest.run
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.run."""
27 from selftest
.run
import (
29 expand_environment_strings
,
33 run_testsuite_command
,
36 from selftest
.tests
import TestCase
39 class ExpandEnvironmentStringsTests(TestCase
):
41 def test_no_vars(self
):
42 self
.assertEquals("foo bar", expand_environment_strings("foo bar", {}))
44 def test_simple(self
):
45 self
.assertEquals("foo bar",
46 expand_environment_strings("foo $BLA", {"BLA": "bar"}))
48 def test_unknown(self
):
49 self
.assertEquals("foo $BLA",
50 expand_environment_strings("foo $BLA", {}))
53 class ExpandCommandListTests(TestCase
):
55 def test_no_list(self
):
56 self
.assertIs(None, expand_command_list("test bla"))
59 self
.assertEquals("test --list", expand_command_list("test $LISTOPT"))
62 class ExpandCommandRunTests(TestCase
):
64 def test_idlist(self
):
65 self
.assertEquals(("test foo bar", None),
66 expand_command_run("test", False, True, subtests
=["foo", "bar"]))
68 def test_idlist_all(self
):
69 self
.assertEquals(("test", None),
70 expand_command_run("test", False, True))
72 def test_loadlist(self
):
73 (cmd
, tmpf
) = expand_command_run("test $LOADLIST", True, False,
74 subtests
=["foo", "bar"])
75 self
.addCleanup(os
.remove
, tmpf
)
78 self
.assertEquals(f
.read(), "foo\nbar\n")
81 self
.assertEquals("test --load-list=%s" % tmpf
, cmd
)
83 def test_loadlist_all(self
):
84 self
.assertEquals(("test ", None),
85 expand_command_run("test $LOADLIST", True, False))
88 class ExportedEnvvarsStrTests(TestCase
):
90 def test_no_vars(self
):
91 self
.assertEquals("", exported_envvars_str({}, ["foo", "bar"]))
94 self
.assertEquals("foo=1\n",
95 exported_envvars_str({"foo": "1"}, ["foo", "bar"]))
97 def test_vars_unknown(self
):
98 self
.assertEquals("foo=1\n",
99 exported_envvars_str({"foo": "1", "bla": "2"}, ["foo", "bar"]))
103 class NowTests(TestCase
):
105 def test_basic(self
):
106 self
.assertIsInstance(now(), datetime
.datetime
)
107 self
.assertIsNot(now().tzinfo
, None)
110 class MockSubunitOps(object):
115 def start_testsuite(self
, name
):
116 self
.calls
.append(("start-testsuite", name
))
118 def progress(self
, count
, whence
):
119 self
.calls
.append(("progress", count
, whence
))
122 self
.calls
.append(("time", ))
124 def end_testsuite(self
, name
, result
, details
=None):
125 self
.calls
.append(("end-testsuite", name
, result
, details
))
128 class RunTestsuiteCommandTests(TestCase
):
130 def test_success_no_env(self
):
131 outf
= tempfile
.TemporaryFile()
132 subunit_ops
= MockSubunitOps()
133 exit_code
= run_testsuite_command("thetestsuitename", "echo doing something", subunit_ops
, outf
=outf
)
135 ("start-testsuite", "thetestsuitename"),
136 ("progress", None, subunit
.PROGRESS_PUSH
),
139 ("progress", None, subunit
.PROGRESS_POP
),
140 ("end-testsuite", "thetestsuitename", "success", None),
141 ], subunit_ops
.calls
)
142 self
.assertEquals(0, exit_code
)
144 self
.assertEquals("""\
146 command: echo doing something
147 expanded command: echo doing something
150 def test_failure(self
):
151 outf
= tempfile
.TemporaryFile()
152 subunit_ops
= MockSubunitOps()
153 exit_code
= run_testsuite_command("thetestsuitename", "exit 3", subunit_ops
, outf
=outf
)
155 ("start-testsuite", "thetestsuitename"),
156 ("progress", None, subunit
.PROGRESS_PUSH
),
159 ("progress", None, subunit
.PROGRESS_POP
),
160 ("end-testsuite", "thetestsuitename", "failure", "Exit code was 3"),
161 ], subunit_ops
.calls
)
162 self
.assertEquals(3, exit_code
)
164 self
.assertEquals("""\
166 expanded command: exit 3
169 def test_error(self
):
170 outf
= tempfile
.TemporaryFile()
171 subunit_ops
= MockSubunitOps()
172 exit_code
= run_testsuite_command("thetestsuitename",
173 "thisisacommandthatdoesnotexist 2>/dev/null", subunit_ops
, outf
=outf
)
175 ("start-testsuite", "thetestsuitename"),
176 ("progress", None, subunit
.PROGRESS_PUSH
),
179 ("progress", None, subunit
.PROGRESS_POP
),
180 ("end-testsuite", "thetestsuitename", "failure", "Exit code was 127"),
181 ], subunit_ops
.calls
)
182 self
.assertEquals(127, exit_code
)
184 self
.assertEquals("""\
185 command: thisisacommandthatdoesnotexist 2>/dev/null
186 expanded command: thisisacommandthatdoesnotexist 2>/dev/null