Skip to content

Commit

Permalink
compose_name is taken from all docker-compose files
Browse files Browse the repository at this point in the history
check compose_name property for uniqueness

Signed-off-by: Alexey Merezhin <estarter@gmail.com>
  • Loading branch information
estarter committed Nov 11, 2017
1 parent cfaa20c commit b28b10c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
18 changes: 17 additions & 1 deletion compose/cli/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from . import errors
from . import verbose_proxy
from .. import config
from ..config import ConfigurationError
from ..config.environment import Environment
from ..const import API_VERSIONS
from ..project import Project
Expand Down Expand Up @@ -85,9 +86,10 @@ def get_project(project_dir, config_path=None, project_name=None, verbose=False,
if not environment:
environment = Environment.from_env_file(project_dir)
config_details = config.find(project_dir, config_path, environment, override_dir)
config_project_name = get_config_project_name(config_details.config_files)
config_data = config.load(config_details)
project_name = get_project_name(
config_details.working_dir, project_name, config_data.project_name, environment
config_details.working_dir, project_name, config_project_name, environment
)

api_version = environment.get(
Expand All @@ -103,6 +105,20 @@ def get_project(project_dir, config_path=None, project_name=None, verbose=False,
return Project.from_config(project_name, config_data, client)


def get_config_project_name(config_files):
config_project_names = set([config_file.project_name
for config_file in config_files
if config_file.project_name])
if len(config_project_names) > 1:
raise ConfigurationError("project_name has multiple values: {0}"
.format(", ".join(str(e) for e in config_project_names)))
config_project_name = None
if len(config_project_names) > 0:
config_project_name = config_project_names.pop()

return config_project_name


def get_project_name(working_dir, project_name=None, config_project_name=None, environment=None):
def normalize_name(name):
return re.sub(r'[^a-z0-9]', '', name.lower())
Expand Down
26 changes: 26 additions & 0 deletions tests/unit/cli/command_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
import six

from compose.cli.command import get_config_path_from_options
from compose.cli.command import get_config_project_name
from compose.config import ConfigurationError
from compose.config.config import ConfigFile
from compose.config.environment import Environment
from compose.const import IS_WINDOWS_PLATFORM
from tests import mock
Expand Down Expand Up @@ -74,3 +77,26 @@ def test_unicode_path_from_env(self):
assert get_config_path_from_options(
'.', {}, environment
) == ['就吃饭/docker-compose.yml']


class TestGetConfigProjectName(object):

def test_single_config_file(self):
config_files = [ConfigFile(None, {'project_name': 'myproject'})]
assert get_config_project_name(config_files) == 'myproject'

def test_undefined_project_name(self):
config_files = [ConfigFile(None, {})]
assert get_config_project_name(config_files) is None

def test_ambiguous_project_name(self):
config_files = [ConfigFile(None, {'project_name': 'myproject'}),
ConfigFile(None, {'project_name': 'another_name'})]
with pytest.raises(ConfigurationError) as excinfo:
get_config_project_name(config_files)
assert 'project_name has multiple values: another_name, myproject' in str(excinfo)

def test_duplicated_project_name(self):
config_files = [ConfigFile(None, {'project_name': 'myproject'}),
ConfigFile(None, {'project_name': 'myproject'})]
assert get_config_project_name(config_files) == 'myproject'

0 comments on commit b28b10c

Please sign in to comment.