subprocess2_test_script.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #!/usr/bin/env python3
  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('--fail',
  18. dest='return_value',
  19. action='store_const',
  20. default=0,
  21. const=64)
  22. parser.add_option('--crlf',
  23. action='store_const',
  24. const='\r\n',
  25. dest='eol',
  26. default='\n')
  27. parser.add_option('--cr', action='store_const', const='\r', dest='eol')
  28. parser.add_option('--stdout', action='store_true')
  29. parser.add_option('--stderr', action='store_true')
  30. parser.add_option('--read', action='store_true')
  31. options, args = parser.parse_args()
  32. if args:
  33. parser.error('Internal error')
  34. def do(string):
  35. if options.stdout:
  36. sys.stdout.buffer.write(string.upper().encode('utf-8'))
  37. sys.stdout.buffer.write(options.eol.encode('utf-8'))
  38. if options.stderr:
  39. sys.stderr.buffer.write(string.lower().encode('utf-8'))
  40. sys.stderr.buffer.write(options.eol.encode('utf-8'))
  41. sys.stderr.flush()
  42. do('A')
  43. do('BB')
  44. do('CCC')
  45. if options.read:
  46. assert options.return_value == 0
  47. try:
  48. while sys.stdin.read(1):
  49. options.return_value += 1
  50. except OSError:
  51. pass
  52. sys.exit(options.return_value)