test_shell_utils.py 1.9 KB

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