From 6e4a954dbd4f899b15d8cc621175463c56d6ac70 Mon Sep 17 00:00:00 2001 From: Mazz Mosley Date: Thu, 2 Jul 2015 15:31:38 +0100 Subject: [PATCH] 'file' key can be omitted from extends If the 'file' key is not set in the extends_options dict then we look for the 'service' from within the same file. Fixes this issue: https://github.com/docker/compose/issues/1237 Signed-off-by: Mazz Mosley --- compose/config.py | 7 +++++- tests/fixtures/extends/no-file-specified.yml | 9 ++++++++ tests/unit/config_test.py | 24 ++++++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 tests/fixtures/extends/no-file-specified.yml diff --git a/compose/config.py b/compose/config.py index a5fe5b3ef..cf831f312 100644 --- a/compose/config.py +++ b/compose/config.py @@ -163,7 +163,12 @@ class ServiceLoader(object): if self.working_dir is None: raise Exception("No working_dir passed to ServiceLoader()") - other_config_path = expand_path(self.working_dir, extends_options['file']) + try: + extends_from_filename = extends_options['file'] + except KeyError: + extends_from_filename = os.path.split(self.filename)[1] + + other_config_path = expand_path(self.working_dir, extends_from_filename) other_working_dir = os.path.dirname(other_config_path) other_already_seen = self.already_seen + [self.signature(service_dict['name'])] other_loader = ServiceLoader( diff --git a/tests/fixtures/extends/no-file-specified.yml b/tests/fixtures/extends/no-file-specified.yml new file mode 100644 index 000000000..40e43c4bf --- /dev/null +++ b/tests/fixtures/extends/no-file-specified.yml @@ -0,0 +1,9 @@ +myweb: + extends: + service: web + environment: + - "BAR=1" +web: + image: busybox + environment: + - "BAZ=3" diff --git a/tests/unit/config_test.py b/tests/unit/config_test.py index 5a61ad528..4f0f1893d 100644 --- a/tests/unit/config_test.py +++ b/tests/unit/config_test.py @@ -447,6 +447,30 @@ class ExtendsTest(unittest.TestCase): dictionary['extends']['what'] = 'is this' self.assertRaisesRegexp(config.ConfigurationError, 'what', load_config) + def test_extends_file_defaults_to_self(self): + """ + Test not specifying a file in our extends options that the + config is valid and correctly extends from itself. + """ + service_dicts = config.load('tests/fixtures/extends/no-file-specified.yml') + self.assertEqual(service_dicts, [ + { + 'name': 'myweb', + 'image': 'busybox', + 'environment': { + "BAR": "1", + "BAZ": "3", + } + }, + { + 'name': 'web', + 'image': 'busybox', + 'environment': { + "BAZ": "3", + } + } + ]) + def test_blacklisted_options(self): def load_config(): return config.make_service_dict('myweb', {