Bill Richardson | 856e072 | 2011-02-07 15:39:45 -0800 | [diff] [blame] | 1 | #!/usr/bin/python -tt |
Bill Richardson | 794d4d4 | 2011-02-10 19:13:10 -0800 | [diff] [blame^] | 2 | # |
| 3 | # Copyright (c) 2011 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. |
Bill Richardson | 856e072 | 2011-02-07 15:39:45 -0800 | [diff] [blame] | 6 | |
| 7 | """Unit tests for bmpblk_utility. |
| 8 | """ |
| 9 | |
| 10 | import os |
| 11 | import sys |
| 12 | import subprocess |
| 13 | import unittest |
| 14 | |
| 15 | def runprog(*args): |
| 16 | """Runs specified program and args, returns (exitcode, stdout, stderr).""" |
| 17 | p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 18 | out, err = p.communicate() |
| 19 | return (p.returncode, out, err) |
| 20 | |
| 21 | |
| 22 | class TestBmpBlock(unittest.TestCase): |
| 23 | |
| 24 | def testNoArgs(self): |
| 25 | """Running with no args should print usage and fail.""" |
| 26 | rc, out, err = runprog(prog) |
| 27 | self.assertNotEqual(0, rc) |
Bill Richardson | 794d4d4 | 2011-02-10 19:13:10 -0800 | [diff] [blame^] | 28 | self.assertTrue(err.count("missing BMPBLOCK name")) |
| 29 | self.assertTrue(out.count("bmpblk_utility")) |
Bill Richardson | 856e072 | 2011-02-07 15:39:45 -0800 | [diff] [blame] | 30 | |
| 31 | def testMissingBmp(self): |
| 32 | """Missing a bmp specified in the yaml is an error.""" |
Bill Richardson | 794d4d4 | 2011-02-10 19:13:10 -0800 | [diff] [blame^] | 33 | rc, out, err = runprog(prog, '-c', 'case_nobmp.yaml', 'FOO') |
Bill Richardson | 856e072 | 2011-02-07 15:39:45 -0800 | [diff] [blame] | 34 | self.assertNotEqual(0, rc) |
| 35 | self.assertTrue(err.count("No such file or directory")) |
| 36 | |
| 37 | def testInvalidBmp(self): |
| 38 | """A .bmp file that isn't really a BMP should fail.""" |
Bill Richardson | 794d4d4 | 2011-02-10 19:13:10 -0800 | [diff] [blame^] | 39 | rc, out, err = runprog(prog, '-c', 'case_badbmp.yaml', 'FOO') |
Bill Richardson | 856e072 | 2011-02-07 15:39:45 -0800 | [diff] [blame] | 40 | self.assertNotEqual(0, rc) |
| 41 | self.assertTrue(err.count("Unsupported image format")) |
| 42 | |
| 43 | |
| 44 | # Run these tests |
| 45 | if __name__ == '__main__': |
| 46 | varname = 'BMPBLK' |
| 47 | if varname not in os.environ: |
| 48 | print('You must specify the path to bmpblk_utility in the $%s ' |
| 49 | 'environment variable.' % varname) |
| 50 | sys.exit(1) |
| 51 | prog = os.environ[varname] |
| 52 | print "Testing prog...", prog |
| 53 | unittest.main() |
| 54 | |