OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/python |
| 2 |
| 3 # Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 4 # Use of this source code is governed by a BSD-style license that can be |
| 5 # found in the LICENSE file. |
| 6 |
| 7 """Unittests for loman.""" |
| 8 |
| 9 import os |
| 10 import StringIO |
| 11 import sys |
| 12 import tempfile |
| 13 import unittest |
| 14 |
| 15 import loman |
| 16 |
| 17 _TEST_MANIFEST1 = """<manifest> |
| 18 <project name="foo" path="path/to/foo" workon="True" /> |
| 19 </manifest>""" |
| 20 |
| 21 class LocalManifestTest(unittest.TestCase): |
| 22 |
| 23 def setUp(self): |
| 24 self.utf8 = "<?xml version='1.0' encoding='UTF-8'?>\n" |
| 25 self.tiny_manifest = '<manifest>\n</manifest>' |
| 26 |
| 27 def testSimpleParse(self): |
| 28 ptree = loman.LocalManifest() |
| 29 ptree.Parse() |
| 30 |
| 31 def testParse(self): |
| 32 ptree = loman.LocalManifest(self.tiny_manifest) |
| 33 ptree.Parse() |
| 34 self.assertEqual(ptree.ToString(), self.utf8 + self.tiny_manifest) |
| 35 |
| 36 def testUTF8Parse(self): |
| 37 ptree = loman.LocalManifest(self.utf8 + self.tiny_manifest) |
| 38 ptree.Parse() |
| 39 self.assertEqual(ptree.ToString(), self.utf8 + self.tiny_manifest) |
| 40 |
| 41 def testAddNew(self): |
| 42 ptree = loman.LocalManifest('<manifest>\n</manifest>') |
| 43 ptree.Parse() |
| 44 self.assertTrue(ptree.AddWorkonProject('foo', 'path/to/foo')) |
| 45 self.assertEqual( |
| 46 ptree.ToString(), |
| 47 self.utf8 + '<manifest>\n' |
| 48 '<project name="foo" path="path/to/foo" workon="True" />\n' |
| 49 '</manifest>') |
| 50 |
| 51 def testAddDup(self): |
| 52 ptree = loman.LocalManifest('<manifest>\n</manifest>') |
| 53 ptree.Parse() |
| 54 ptree.AddWorkonProject('foo', 'path/to/foo') |
| 55 self.assertTrue(not ptree.AddWorkonProject('foo', 'path/to/foo')) |
| 56 self.assertTrue(not ptree.AddWorkonProject('foo', 'path/foo')) |
| 57 self.assertTrue(not ptree.AddWorkonProject('foobar', 'path/to/foo')) |
| 58 |
| 59 class MainTest(unittest.TestCase): |
| 60 |
| 61 def setUp(self): |
| 62 self.utf8 = "<?xml version='1.0' encoding='UTF-8'?>\n" |
| 63 self.tiny_manifest = '<manifest>\n</manifest>' |
| 64 self.stderr = sys.stderr |
| 65 sys.stderr = StringIO.StringIO() |
| 66 |
| 67 def tearDown(self): |
| 68 sys.stderr = self.stderr |
| 69 |
| 70 def testNotEnoughArgs(self): |
| 71 err_msg = 'Not enough arguments\n' |
| 72 self.assertRaises(SystemExit, loman.main, ['loman']) |
| 73 self.assertTrue(sys.stderr.getvalue().endswith(err_msg)) |
| 74 |
| 75 def testNotWorkon(self): |
| 76 err_msg = 'Adding of non-workon projects is currently unsupported.\n' |
| 77 self.assertRaises(SystemExit, loman.main, ['loman', 'add', 'foo', 'path']) |
| 78 self.assertTrue(sys.stderr.getvalue().endswith(err_msg)) |
| 79 |
| 80 def testBadCommand(self): |
| 81 err_msg = 'Unsupported command: bad.\n' |
| 82 self.assertRaises(SystemExit, loman.main, ['loman', 'bad', 'foo', 'path']) |
| 83 self.assertTrue(sys.stderr.getvalue().endswith(err_msg)) |
| 84 |
| 85 def testSimpleAdd(self): |
| 86 temp = tempfile.NamedTemporaryFile('w') |
| 87 print >> temp, '<manifest>\n</manifest>' |
| 88 temp.flush() |
| 89 os.fsync(temp.fileno()) |
| 90 loman.main(['loman', 'add', '--workon', '-f', |
| 91 temp.name, 'foo', 'path/to/foo']) |
| 92 self.assertEqual( |
| 93 open(temp.name, 'r').read(), |
| 94 self.utf8 + '<manifest>\n' |
| 95 '<project name="foo" path="path/to/foo" workon="True" />\n' |
| 96 '</manifest>\n') |
| 97 |
| 98 def testAddDup(self): |
| 99 temp = tempfile.NamedTemporaryFile('w') |
| 100 print >> temp, '<manifest>\n</manifest>' |
| 101 temp.flush() |
| 102 os.fsync(temp.fileno()) |
| 103 loman.main(['loman', 'add', '--workon', '-f', |
| 104 temp.name, 'foo', 'path/to/foo']) |
| 105 self.assertRaises(SystemExit, loman.main, |
| 106 ['loman', 'add', '--workon', '-f', |
| 107 temp.name, 'foo', 'path/to/foo']) |
| 108 |
| 109 |
| 110 if __name__ == '__main__': |
| 111 unittest.main() |
OLD | NEW |