1 | # -*- coding: utf-8 -*- |
---|
2 | # |
---|
3 | # Copyright (C) 2005-2007 Christopher Lenz <cmlenz@gmx.de> |
---|
4 | # Copyright (C) 2007-2010 Edgewall Software |
---|
5 | # All rights reserved. |
---|
6 | # |
---|
7 | # This software is licensed as described in the file COPYING, which |
---|
8 | # you should have received as part of this distribution. The terms |
---|
9 | # are also available at http://bitten.edgewall.org/wiki/License. |
---|
10 | |
---|
11 | import os |
---|
12 | import shutil |
---|
13 | import tempfile |
---|
14 | import unittest |
---|
15 | |
---|
16 | from bitten.build.config import Configuration |
---|
17 | from bitten.recipe import Context, Recipe, InvalidRecipeError |
---|
18 | from bitten.util import xmlio |
---|
19 | |
---|
20 | |
---|
21 | class ContextTestCase(unittest.TestCase): |
---|
22 | |
---|
23 | def setUp(self): |
---|
24 | self.basedir = os.path.realpath(tempfile.mkdtemp()) |
---|
25 | |
---|
26 | def tearDown(self): |
---|
27 | shutil.rmtree(self.basedir) |
---|
28 | |
---|
29 | def test_vars_basedir(self): |
---|
30 | config = Configuration(properties={'foo.bar': 'baz'}) |
---|
31 | ctxt = Context('%s/${path}/${foo.bar}' % os.path.realpath('/foo'), |
---|
32 | config, {'path': 'bar'}) |
---|
33 | |
---|
34 | self.assertEquals(os.path.realpath('/foo/bar/baz'), |
---|
35 | os.path.realpath(ctxt.vars['basedir'])) |
---|
36 | if os.name == 'nt': |
---|
37 | # Make sure paths are double-escaped |
---|
38 | self.failUnless('\\\\' in ctxt.vars['basedir']) |
---|
39 | |
---|
40 | def test_run_wrong_arg(self): |
---|
41 | ctxt = Context(self.basedir) |
---|
42 | try: |
---|
43 | ctxt.run(1, 'http://bitten.edgewall.org/tools/sh', 'exec', {'foo':'bar'}) |
---|
44 | self.fail("InvalidRecipeError expected") |
---|
45 | except InvalidRecipeError, e: |
---|
46 | self.failUnless("Unsupported argument 'foo'" in str(e)) |
---|
47 | |
---|
48 | def test_attach_file_config(self): |
---|
49 | # Verify output from attaching a file to a config |
---|
50 | ctxt = Context(self.basedir, Configuration()) |
---|
51 | |
---|
52 | ctxt.attach(file_='config.txt', description='config config', |
---|
53 | resource='config') |
---|
54 | self.assertEquals(1, len(ctxt.output)) |
---|
55 | self.assertEquals(Recipe.ATTACH, ctxt.output[0][0]) |
---|
56 | attach_xml = ctxt.output[0][3] |
---|
57 | self.assertEquals('<file resource="config" ' |
---|
58 | 'description="config config" ' |
---|
59 | 'filename="config.txt"/>', str(attach_xml)) |
---|
60 | |
---|
61 | def test_attach_file_build(self): |
---|
62 | # Verify output from attaching a file to a build |
---|
63 | ctxt = Context(self.basedir, Configuration()) |
---|
64 | |
---|
65 | ctxt.attach(file_='build.txt', description='build build') |
---|
66 | self.assertEquals(1, len(ctxt.output)) |
---|
67 | self.assertEquals(Recipe.ATTACH, ctxt.output[0][0]) |
---|
68 | attach_xml = ctxt.output[0][3] |
---|
69 | self.assertEquals('<file resource="build" ' |
---|
70 | 'description="build build" ' |
---|
71 | 'filename="build.txt"/>', str(attach_xml)) |
---|
72 | |
---|
73 | class RecipeTestCase(unittest.TestCase): |
---|
74 | |
---|
75 | def setUp(self): |
---|
76 | self.basedir = os.path.realpath(tempfile.mkdtemp()) |
---|
77 | |
---|
78 | def tearDown(self): |
---|
79 | shutil.rmtree(self.basedir) |
---|
80 | |
---|
81 | def test_empty_recipe(self): |
---|
82 | xml = xmlio.parse('<build/>') |
---|
83 | recipe = Recipe(xml, basedir=self.basedir) |
---|
84 | self.assertEqual(self.basedir, recipe.ctxt.basedir) |
---|
85 | steps = list(recipe) |
---|
86 | self.assertEqual(0, len(steps)) |
---|
87 | |
---|
88 | def test_empty_step(self): |
---|
89 | xml = xmlio.parse('<build>' |
---|
90 | ' <step id="foo" description="Bar"></step>' |
---|
91 | '</build>') |
---|
92 | recipe = Recipe(xml, basedir=self.basedir) |
---|
93 | steps = list(recipe) |
---|
94 | self.assertEqual(1, len(steps)) |
---|
95 | self.assertEqual('foo', steps[0].id) |
---|
96 | self.assertEqual('Bar', steps[0].description) |
---|
97 | self.assertEqual('fail', steps[0].onerror) |
---|
98 | |
---|
99 | def test_validate_bad_root(self): |
---|
100 | xml = xmlio.parse('<foo></foo>') |
---|
101 | recipe = Recipe(xml, basedir=self.basedir) |
---|
102 | self.assertRaises(InvalidRecipeError, recipe.validate) |
---|
103 | |
---|
104 | def test_validate_no_steps(self): |
---|
105 | xml = xmlio.parse('<build></build>') |
---|
106 | recipe = Recipe(xml, basedir=self.basedir) |
---|
107 | self.assertRaises(InvalidRecipeError, recipe.validate) |
---|
108 | |
---|
109 | def test_validate_child_not_step(self): |
---|
110 | xml = xmlio.parse('<build><foo/></build>') |
---|
111 | recipe = Recipe(xml, basedir=self.basedir) |
---|
112 | self.assertRaises(InvalidRecipeError, recipe.validate) |
---|
113 | |
---|
114 | def test_validate_child_not_step(self): |
---|
115 | xml = xmlio.parse('<build><foo/></build>') |
---|
116 | recipe = Recipe(xml, basedir=self.basedir) |
---|
117 | self.assertRaises(InvalidRecipeError, recipe.validate) |
---|
118 | |
---|
119 | def test_validate_step_without_id(self): |
---|
120 | xml = xmlio.parse('<build><step><cmd/></step></build>') |
---|
121 | recipe = Recipe(xml, basedir=self.basedir) |
---|
122 | self.assertRaises(InvalidRecipeError, recipe.validate) |
---|
123 | |
---|
124 | def test_validate_step_with_empty_id(self): |
---|
125 | xml = xmlio.parse('<build><step id=""><cmd/></step></build>') |
---|
126 | recipe = Recipe(xml, basedir=self.basedir) |
---|
127 | self.assertRaises(InvalidRecipeError, recipe.validate) |
---|
128 | |
---|
129 | def test_validate_step_without_commands(self): |
---|
130 | xml = xmlio.parse('<build><step id="test"/></build>') |
---|
131 | recipe = Recipe(xml, basedir=self.basedir) |
---|
132 | self.assertRaises(InvalidRecipeError, recipe.validate) |
---|
133 | |
---|
134 | def test_validate_step_with_command_children(self): |
---|
135 | xml = xmlio.parse('<build><step id="test">' |
---|
136 | '<somecmd><child1/><child2/></somecmd>' |
---|
137 | '</step></build>') |
---|
138 | recipe = Recipe(xml, basedir=self.basedir) |
---|
139 | self.assertRaises(InvalidRecipeError, recipe.validate) |
---|
140 | |
---|
141 | def test_validate_step_with_duplicate_id(self): |
---|
142 | xml = xmlio.parse('<build>' |
---|
143 | '<step id="test"><somecmd></somecmd></step>' |
---|
144 | '<step id="test"><othercmd></othercmd></step>' |
---|
145 | '</build>') |
---|
146 | recipe = Recipe(xml, basedir=self.basedir) |
---|
147 | self.assertRaises(InvalidRecipeError, recipe.validate) |
---|
148 | |
---|
149 | def test_validate_successful(self): |
---|
150 | xml = xmlio.parse('<build>' |
---|
151 | '<step id="foo"><somecmd></somecmd></step>' |
---|
152 | '<step id="bar"><othercmd></othercmd></step>' |
---|
153 | '</build>') |
---|
154 | recipe = Recipe(xml, basedir=self.basedir) |
---|
155 | recipe.validate() |
---|
156 | |
---|
157 | def test_onerror_defaults(self): |
---|
158 | xml = xmlio.parse('<build onerror="continue">' |
---|
159 | ' <step id="foo" description="Bar"></step>' |
---|
160 | '</build>') |
---|
161 | recipe = Recipe(xml, basedir=self.basedir) |
---|
162 | steps = list(recipe) |
---|
163 | self.assertEqual(1, len(steps)) |
---|
164 | self.assertEqual('foo', steps[0].id) |
---|
165 | self.assertEqual('Bar', steps[0].description) |
---|
166 | self.assertEqual('continue', steps[0].onerror) |
---|
167 | |
---|
168 | |
---|
169 | def test_onerror_override(self): |
---|
170 | xml = xmlio.parse('<build onerror="ignore">' |
---|
171 | ' <step id="foo" description="Bar" onerror="continue"></step>' |
---|
172 | '</build>') |
---|
173 | recipe = Recipe(xml, basedir=self.basedir) |
---|
174 | steps = list(recipe) |
---|
175 | self.assertEqual(1, len(steps)) |
---|
176 | self.assertEqual('foo', steps[0].id) |
---|
177 | self.assertEqual('Bar', steps[0].description) |
---|
178 | self.assertEqual('continue', steps[0].onerror) |
---|
179 | |
---|
180 | |
---|
181 | def suite(): |
---|
182 | suite = unittest.TestSuite() |
---|
183 | suite.addTest(unittest.makeSuite(ContextTestCase, 'test')) |
---|
184 | suite.addTest(unittest.makeSuite(RecipeTestCase, 'test')) |
---|
185 | return suite |
---|
186 | |
---|
187 | if __name__ == '__main__': |
---|
188 | unittest.main(defaultTest='suite') |
---|