command_test.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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({}, environment) == ['one.yml', 'two.yml']
  24. @pytest.mark.skipif(not IS_WINDOWS_PLATFORM, reason='windows separator')
  25. def test_multiple_path_from_env_windows(self):
  26. with mock.patch.dict(os.environ):
  27. os.environ['COMPOSE_FILE'] = 'one.yml;two.yml'
  28. environment = Environment.from_env_file('.')
  29. assert get_config_path_from_options({}, environment) == ['one.yml', 'two.yml']
  30. def test_multiple_path_from_env_custom_separator(self):
  31. with mock.patch.dict(os.environ):
  32. os.environ['COMPOSE_PATH_SEPARATOR'] = '^'
  33. os.environ['COMPOSE_FILE'] = 'c:\\one.yml^.\\semi;colon.yml'
  34. environment = Environment.from_env_file('.')
  35. assert get_config_path_from_options({}, environment) == ['c:\\one.yml', '.\\semi;colon.yml']
  36. def test_no_path(self):
  37. environment = Environment.from_env_file('.')
  38. assert not get_config_path_from_options({}, environment)
  39. def test_unicode_path_from_options(self):
  40. paths = [b'\xe5\xb0\xb1\xe5\x90\x83\xe9\xa5\xad/docker-compose.yml']
  41. opts = {'--file': paths}
  42. environment = Environment.from_env_file('.')
  43. assert get_config_path_from_options(opts, environment) == ['就吃饭/docker-compose.yml']