test_shell_utils.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. from __future__ import division, absolute_import, print_function
  2. import pytest
  3. import subprocess
  4. import os
  5. import json
  6. import sys
  7. from numpy.distutils import _shell_utils
  8. argv_cases = [
  9. [r'exe'],
  10. [r'path/exe'],
  11. [r'path\exe'],
  12. [r'\\server\path\exe'],
  13. [r'path to/exe'],
  14. [r'path to\exe'],
  15. [r'exe', '--flag'],
  16. [r'path/exe', '--flag'],
  17. [r'path\exe', '--flag'],
  18. [r'path to/exe', '--flag'],
  19. [r'path to\exe', '--flag'],
  20. # flags containing literal quotes in their name
  21. [r'path to/exe', '--flag-"quoted"'],
  22. [r'path to\exe', '--flag-"quoted"'],
  23. [r'path to/exe', '"--flag-quoted"'],
  24. [r'path to\exe', '"--flag-quoted"'],
  25. ]
  26. @pytest.fixture(params=[
  27. _shell_utils.WindowsParser,
  28. _shell_utils.PosixParser
  29. ])
  30. def Parser(request):
  31. return request.param
  32. @pytest.fixture
  33. def runner(Parser):
  34. if Parser != _shell_utils.NativeParser:
  35. pytest.skip('Unable to run with non-native parser')
  36. if Parser == _shell_utils.WindowsParser:
  37. return lambda cmd: subprocess.check_output(cmd)
  38. elif Parser == _shell_utils.PosixParser:
  39. # posix has no non-shell string parsing
  40. return lambda cmd: subprocess.check_output(cmd, shell=True)
  41. else:
  42. raise NotImplementedError
  43. @pytest.mark.parametrize('argv', argv_cases)
  44. def test_join_matches_subprocess(Parser, runner, argv):
  45. """
  46. Test that join produces strings understood by subprocess
  47. """
  48. # invoke python to return its arguments as json
  49. cmd = [
  50. sys.executable, '-c',
  51. 'import json, sys; print(json.dumps(sys.argv[1:]))'
  52. ]
  53. joined = Parser.join(cmd + argv)
  54. json_out = runner(joined).decode()
  55. assert json.loads(json_out) == argv
  56. @pytest.mark.parametrize('argv', argv_cases)
  57. def test_roundtrip(Parser, argv):
  58. """
  59. Test that split is the inverse operation of join
  60. """
  61. try:
  62. joined = Parser.join(argv)
  63. assert argv == Parser.split(joined)
  64. except NotImplementedError:
  65. pytest.skip("Not implemented")