test_reyaml.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. # -*- coding: utf-8 -*-
  2. import pytest
  3. pytest.importorskip('yaml')
  4. import os
  5. import subprocess
  6. script_path = os.path.realpath(os.path.join(__file__, '..', '..', '..', 'scripts', 'reyaml'))
  7. @pytest.mark.parametrize(('stdin', 'params', 'expected_stdout'), [
  8. [b'a: b\n', [], b'a: b\n'],
  9. [b'{a: b}\n', [], b'a: b\n'],
  10. [b'[a, b, c]\n', [], b'- a\n- b\n- c\n'],
  11. ])
  12. def test_params(stdin, params, expected_stdout):
  13. p = subprocess.Popen(
  14. [script_path],
  15. stdin = subprocess.PIPE,
  16. stdout = subprocess.PIPE,
  17. )
  18. stdout, stderr = p.communicate(stdin)
  19. assert expected_stdout == stdout
  20. def test_file_input(tmpdir):
  21. input_file = tmpdir.join('in')
  22. input_file.write('a: b')
  23. assert b'a: b\n' == subprocess.check_output(
  24. [script_path, '-i', input_file.strpath],
  25. )
  26. def test_file_output(tmpdir):
  27. output_file = tmpdir.join('out')
  28. p = subprocess.Popen(
  29. [script_path, '-o', output_file.strpath],
  30. stdin = subprocess.PIPE,
  31. stdout = subprocess.PIPE,
  32. stderr = subprocess.PIPE,
  33. )
  34. stdout, stderr = p.communicate(b'a: b')
  35. assert stdout == b''
  36. assert stderr == b''
  37. assert 'a: b\n' == output_file.read()
  38. def test_file_input_output(tmpdir):
  39. input_file = tmpdir.join('in')
  40. input_file.write('c: d')
  41. output_file = tmpdir.join('out')
  42. p = subprocess.Popen(
  43. [script_path, '-i', input_file.strpath, '-o', output_file.strpath],
  44. stdout = subprocess.PIPE,
  45. stderr = subprocess.PIPE,
  46. )
  47. stdout, stderr = p.communicate()
  48. assert stdout == b''
  49. assert stderr == b''
  50. assert 'c: d\n' == output_file.read()
  51. def test_file_input_output_same(tmpdir):
  52. io_file = tmpdir.join('io')
  53. io_file.write('{b: 3}')
  54. p = subprocess.Popen(
  55. [script_path, '-i', io_file.strpath, '-o', io_file.strpath],
  56. stdout = subprocess.PIPE,
  57. stderr = subprocess.PIPE,
  58. )
  59. stdout, stderr = p.communicate()
  60. assert stdout == b''
  61. assert stderr == b''
  62. assert 'b: 3\n' == io_file.read()