subprocess2_test_script.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #!/usr/bin/env python
  2. # Copyright (c) 2019 The Chromium Authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. """Script used to test subprocess2."""
  6. import optparse
  7. import os
  8. import sys
  9. import time
  10. if sys.platform == 'win32':
  11. # Annoying, make sure the output is not translated on Windows.
  12. # pylint: disable=no-member,import-error
  13. import msvcrt
  14. msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
  15. msvcrt.setmode(sys.stderr.fileno(), os.O_BINARY)
  16. parser = optparse.OptionParser()
  17. parser.add_option(
  18. '--fail',
  19. dest='return_value',
  20. action='store_const',
  21. default=0,
  22. const=64)
  23. parser.add_option(
  24. '--crlf', action='store_const', const='\r\n', dest='eol', default='\n')
  25. parser.add_option(
  26. '--cr', action='store_const', const='\r', dest='eol')
  27. parser.add_option('--stdout', action='store_true')
  28. parser.add_option('--stderr', action='store_true')
  29. parser.add_option('--read', action='store_true')
  30. options, args = parser.parse_args()
  31. if args:
  32. parser.error('Internal error')
  33. def do(string):
  34. if options.stdout:
  35. sys.stdout.write(string.upper())
  36. sys.stdout.write(options.eol)
  37. if options.stderr:
  38. sys.stderr.write(string.lower())
  39. sys.stderr.write(options.eol)
  40. do('A')
  41. do('BB')
  42. do('CCC')
  43. if options.read:
  44. assert options.return_value == 0
  45. try:
  46. while sys.stdin.read(1):
  47. options.return_value += 1
  48. except OSError:
  49. pass
  50. sys.exit(options.return_value)