| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright 2016 The LUCI Authors. All rights reserved. | 2 # Copyright 2016 The LUCI Authors. All rights reserved. |
| 3 # Use of this source code is governed under the Apache License, Version 2.0 | 3 # Use of this source code is governed under the Apache License, Version 2.0 |
| 4 # that can be found in the LICENSE file. | 4 # that can be found in the LICENSE file. |
| 5 | 5 |
| 6 import os |
| 6 import sys | 7 import sys |
| 8 import tempfile |
| 7 import unittest | 9 import unittest |
| 8 | 10 |
| 11 from cStringIO import StringIO |
| 12 |
| 9 import test_env | 13 import test_env |
| 10 | 14 |
| 15 import argparse # this is vendored |
| 16 import mock |
| 17 |
| 11 from recipe_engine import autoroll | 18 from recipe_engine import autoroll |
| 19 from recipe_engine import common_args |
| 12 | 20 |
| 13 def process_rejected(rejected_candidates): | 21 def process_rejected(rejected_candidates): |
| 14 return [c.to_dict() for c in rejected_candidates] | 22 return [c.to_dict() for c in rejected_candidates] |
| 15 | 23 |
| 16 class FakeCandidate(object): | 24 class FakeCandidate(object): |
| 17 def __init__(self, data, projects=None): | 25 def __init__(self, data, projects=None): |
| 18 self._projects = projects | 26 self._projects = projects |
| 19 self._data = data | 27 self._data = data |
| 20 | 28 |
| 21 def get_affected_projects(self): | 29 def get_affected_projects(self): |
| (...skipping 13 matching lines...) Expand all Loading... |
| 35 'theother', | 43 'theother', |
| 36 'thing', | 44 'thing', |
| 37 ], | 45 ], |
| 38 process_rejected([ | 46 process_rejected([ |
| 39 FakeCandidate('foobar'), | 47 FakeCandidate('foobar'), |
| 40 FakeCandidate('theother'), | 48 FakeCandidate('theother'), |
| 41 FakeCandidate('thing'), | 49 FakeCandidate('thing'), |
| 42 ])) | 50 ])) |
| 43 | 51 |
| 44 | 52 |
| 53 class TestArgs(unittest.TestCase): |
| 54 def setUp(self): |
| 55 self.p = argparse.ArgumentParser() |
| 56 self.followup = common_args.add_common_args(self.p) |
| 57 subp = self.p.add_subparsers() |
| 58 autoroll.add_subparser(subp) |
| 59 |
| 60 fd, self.tmpfile = tempfile.mkstemp() |
| 61 os.close(fd) |
| 62 |
| 63 def tearDown(self): |
| 64 os.remove(self.tmpfile) |
| 65 |
| 66 @mock.patch('argparse._sys.stderr', new_callable=StringIO) |
| 67 def test_no_fetch(self, stderr): |
| 68 with self.assertRaises(SystemExit): |
| 69 args = self.p.parse_args(['--no-fetch', 'autoroll']) |
| 70 args.postprocess_func(self.p, args) |
| 71 self.assertIn('--no-fetch does not make sense', stderr.getvalue()) |
| 72 |
| 73 @mock.patch('argparse._sys.stderr', new_callable=StringIO) |
| 74 def test_json_flags(self, stderr): |
| 75 with self.assertRaises(SystemExit): |
| 76 args = self.p.parse_args(['autoroll', '--verbose-json']) |
| 77 args.postprocess_func(self.p, args) |
| 78 self.assertIn('without --output-json', stderr.getvalue()) |
| 79 |
| 80 args = self.p.parse_args([ |
| 81 'autoroll', '--verbose-json', '--output-json', self.tmpfile]) |
| 82 args.postprocess_func(self.p, args) |
| 83 |
| 84 |
| 45 if __name__ == '__main__': | 85 if __name__ == '__main__': |
| 46 sys.exit(unittest.main()) | 86 sys.exit(unittest.main()) |
| OLD | NEW |