command_test.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. # ~*~ encoding: utf-8 ~*~
  2. import os
  3. import pytest
  4. import six
  5. from compose.cli.command import get_config_path_from_options
  6. from compose.config.environment import Environment
  7. from compose.const import IS_WINDOWS_PLATFORM
  8. from tests import mock
  9. class TestGetConfigPathFromOptions(object):
  10. def test_path_from_options(self):
  11. paths = ['one.yml', 'two.yml']
  12. opts = {'--file': paths}
  13. environment = Environment.from_env_file('.')
  14. assert get_config_path_from_options('.', opts, environment) == paths
  15. def test_single_path_from_env(self):
  16. with mock.patch.dict(os.environ):
  17. os.environ['COMPOSE_FILE'] = 'one.yml'
  18. environment = Environment.from_env_file('.')
  19. assert get_config_path_from_options('.', {}, environment) == ['one.yml']
  20. @pytest.mark.skipif(IS_WINDOWS_PLATFORM, reason='posix separator')
  21. def test_multiple_path_from_env(self):
  22. with mock.patch.dict(os.environ):
  23. os.environ['COMPOSE_FILE'] = 'one.yml:two.yml'
  24. environment = Environment.from_env_file('.')
  25. assert get_config_path_from_options(
  26. '.', {}, environment
  27. ) == ['one.yml', 'two.yml']
  28. @pytest.mark.skipif(not IS_WINDOWS_PLATFORM, reason='windows separator')
  29. def test_multiple_path_from_env_windows(self):
  30. with mock.patch.dict(os.environ):
  31. os.environ['COMPOSE_FILE'] = 'one.yml;two.yml'
  32. environment = Environment.from_env_file('.')
  33. assert get_config_path_from_options(
  34. '.', {}, environment
  35. ) == ['one.yml', 'two.yml']
  36. def test_multiple_path_from_env_custom_separator(self):
  37. with mock.patch.dict(os.environ):
  38. os.environ['COMPOSE_PATH_SEPARATOR'] = '^'
  39. os.environ['COMPOSE_FILE'] = 'c:\\one.yml^.\\semi;colon.yml'
  40. environment = Environment.from_env_file('.')
  41. assert get_config_path_from_options(
  42. '.', {}, environment
  43. ) == ['c:\\one.yml', '.\\semi;colon.yml']
  44. def test_no_path(self):
  45. environment = Environment.from_env_file('.')
  46. assert not get_config_path_from_options('.', {}, environment)
  47. def test_unicode_path_from_options(self):
  48. paths = [b'\xe5\xb0\xb1\xe5\x90\x83\xe9\xa5\xad/docker-compose.yml']
  49. opts = {'--file': paths}
  50. environment = Environment.from_env_file('.')
  51. assert get_config_path_from_options(
  52. '.', opts, environment
  53. ) == ['就吃饭/docker-compose.yml']
  54. @pytest.mark.skipif(six.PY3, reason='Env values in Python 3 are already Unicode')
  55. def test_unicode_path_from_env(self):
  56. with mock.patch.dict(os.environ):
  57. os.environ['COMPOSE_FILE'] = b'\xe5\xb0\xb1\xe5\x90\x83\xe9\xa5\xad/docker-compose.yml'
  58. environment = Environment.from_env_file('.')
  59. assert get_config_path_from_options(
  60. '.', {}, environment
  61. ) == ['就吃饭/docker-compose.yml']