OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """Unit tests for subprocess2.py.""" | 6 """Unit tests for subprocess2.py.""" |
7 | 7 |
8 import logging | 8 import logging |
9 import optparse | 9 import optparse |
10 import os | 10 import os |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
70 def _fake_Popen(self): | 70 def _fake_Popen(self): |
71 """Mocks the whole subprocess2.Popen class.""" | 71 """Mocks the whole subprocess2.Popen class.""" |
72 results = {} | 72 results = {} |
73 class fake_Popen(object): | 73 class fake_Popen(object): |
74 returncode = -8 | 74 returncode = -8 |
75 def __init__(self, args, **kwargs): | 75 def __init__(self, args, **kwargs): |
76 assert not results | 76 assert not results |
77 results.update(kwargs) | 77 results.update(kwargs) |
78 results['args'] = args | 78 results['args'] = args |
79 @staticmethod | 79 @staticmethod |
80 def communicate(input=None, timeout=None): # pylint: disable=W0622 | 80 # pylint: disable=W0622 |
| 81 def communicate(input=None, timeout=None, nag_timer=None): |
81 return None, None | 82 return None, None |
82 self.mock(subprocess2, 'Popen', fake_Popen) | 83 self.mock(subprocess2, 'Popen', fake_Popen) |
83 return results | 84 return results |
84 | 85 |
85 def _fake_subprocess_Popen(self): | 86 def _fake_subprocess_Popen(self): |
86 """Mocks the base class subprocess.Popen only.""" | 87 """Mocks the base class subprocess.Popen only.""" |
87 results = {} | 88 results = {} |
88 def __init__(self, args, **kwargs): | 89 def __init__(self, args, **kwargs): |
89 assert not results | 90 assert not results |
90 results.update(kwargs) | 91 results.update(kwargs) |
(...skipping 493 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
584 pass | 585 pass |
585 def blow(_): | 586 def blow(_): |
586 raise Blow() | 587 raise Blow() |
587 proc = subprocess2.Popen(self.exe + ['--stdout'], stdout=blow) | 588 proc = subprocess2.Popen(self.exe + ['--stdout'], stdout=blow) |
588 try: | 589 try: |
589 proc.communicate() | 590 proc.communicate() |
590 self.fail() | 591 self.fail() |
591 except Blow: | 592 except Blow: |
592 self.assertNotEquals(0, proc.returncode) | 593 self.assertNotEquals(0, proc.returncode) |
593 | 594 |
| 595 def test_nag_timer(self): |
| 596 w = [] |
| 597 l = logging.getLogger() |
| 598 class _Filter(logging.Filter): |
| 599 def filter(self, record): |
| 600 if record.levelno == logging.WARNING: |
| 601 w.append(record.getMessage().lstrip()) |
| 602 return 0 |
| 603 f = _Filter() |
| 604 l.addFilter(f) |
| 605 proc = subprocess2.Popen( |
| 606 self.exe + ['--stdout', '--sleep_first'], stdout=PIPE) |
| 607 res = proc.communicate(nag_timer=3), proc.returncode |
| 608 l.removeFilter(f) |
| 609 self._check_res(res, 'A\nBB\nCCC\n', None, 0) |
| 610 expected = ['No output for 3 seconds from command:', proc.cmd_str, |
| 611 'No output for 6 seconds from command:', proc.cmd_str, |
| 612 'No output for 9 seconds from command:', proc.cmd_str] |
| 613 self.assertEquals(w, expected) |
594 | 614 |
| 615 |
595 def child_main(args): | 616 def child_main(args): |
596 if sys.platform == 'win32': | 617 if sys.platform == 'win32': |
597 # Annoying, make sure the output is not translated on Windows. | 618 # Annoying, make sure the output is not translated on Windows. |
598 # pylint: disable=E1101,F0401 | 619 # pylint: disable=E1101,F0401 |
599 import msvcrt | 620 import msvcrt |
600 msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY) | 621 msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY) |
601 msvcrt.setmode(sys.stderr.fileno(), os.O_BINARY) | 622 msvcrt.setmode(sys.stderr.fileno(), os.O_BINARY) |
602 | 623 |
603 parser = optparse.OptionParser() | 624 parser = optparse.OptionParser() |
604 parser.add_option( | 625 parser.add_option( |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
650 return options.return_value | 671 return options.return_value |
651 | 672 |
652 | 673 |
653 if __name__ == '__main__': | 674 if __name__ == '__main__': |
654 logging.basicConfig(level= | 675 logging.basicConfig(level= |
655 [logging.WARNING, logging.INFO, logging.DEBUG][ | 676 [logging.WARNING, logging.INFO, logging.DEBUG][ |
656 min(2, sys.argv.count('-v'))]) | 677 min(2, sys.argv.count('-v'))]) |
657 if len(sys.argv) > 1 and sys.argv[1] == '--child': | 678 if len(sys.argv) > 1 and sys.argv[1] == '--child': |
658 sys.exit(child_main(sys.argv[2:])) | 679 sys.exit(child_main(sys.argv[2:])) |
659 unittest.main() | 680 unittest.main() |
OLD | NEW |