command_test.py 2.4 KB

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