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