From 9717d5d928df88280c57f0b782c9cca508e2ea1f Mon Sep 17 00:00:00 2001 From: Antoine Catton Date: Tue, 30 Dec 2014 22:05:08 -0700 Subject: [PATCH 1/2] Add test case: what if there's a directory in defaults The goal of the directory name is to be before foo and bar --- cdist/test/cdist_type/__init__.py | 7 +++++++ .../__directory_in_default/parameter/default/a/.gitkeep | 0 .../fixtures/__directory_in_default/parameter/default/bar | 0 .../fixtures/__directory_in_default/parameter/default/foo | 0 .../fixtures/__directory_in_default/parameter/optional | 2 ++ 5 files changed, 9 insertions(+) create mode 100644 cdist/test/cdist_type/fixtures/__directory_in_default/parameter/default/a/.gitkeep create mode 100644 cdist/test/cdist_type/fixtures/__directory_in_default/parameter/default/bar create mode 100644 cdist/test/cdist_type/fixtures/__directory_in_default/parameter/default/foo create mode 100644 cdist/test/cdist_type/fixtures/__directory_in_default/parameter/optional diff --git a/cdist/test/cdist_type/__init__.py b/cdist/test/cdist_type/__init__.py index 36a524b4..ccbf6742 100644 --- a/cdist/test/cdist_type/__init__.py +++ b/cdist/test/cdist_type/__init__.py @@ -153,3 +153,10 @@ class TypeTestCase(test.CdistTestCase): self.assertFalse('optional2' in cdist_type.parameter_defaults) self.assertEqual(cdist_type.parameter_defaults['optional1'], 'value1') + def test_directory_in_default(self): + base_path = fixtures + cdist_type = core.CdistType(base_path, '__directory_in_default') + self.assertEqual( + list(sorted(cdist_type.parameter_defaults.keys())), + ['bar', 'foo'] + ) diff --git a/cdist/test/cdist_type/fixtures/__directory_in_default/parameter/default/a/.gitkeep b/cdist/test/cdist_type/fixtures/__directory_in_default/parameter/default/a/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/cdist/test/cdist_type/fixtures/__directory_in_default/parameter/default/bar b/cdist/test/cdist_type/fixtures/__directory_in_default/parameter/default/bar new file mode 100644 index 00000000..e69de29b diff --git a/cdist/test/cdist_type/fixtures/__directory_in_default/parameter/default/foo b/cdist/test/cdist_type/fixtures/__directory_in_default/parameter/default/foo new file mode 100644 index 00000000..e69de29b diff --git a/cdist/test/cdist_type/fixtures/__directory_in_default/parameter/optional b/cdist/test/cdist_type/fixtures/__directory_in_default/parameter/optional new file mode 100644 index 00000000..3bd1f0e2 --- /dev/null +++ b/cdist/test/cdist_type/fixtures/__directory_in_default/parameter/optional @@ -0,0 +1,2 @@ +foo +bar From 2318c792d7cdcd6ffd5502e9747f95e38f3a7fe8 Mon Sep 17 00:00:00 2001 From: Antoine Catton Date: Tue, 30 Dec 2014 22:06:01 -0700 Subject: [PATCH 2/2] Fix test cdist_type.TypeTestCase.test_directory_in_default --- cdist/core/cdist_type.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/cdist/core/cdist_type.py b/cdist/core/cdist_type.py index ff1ebaec..d5b4ea3e 100644 --- a/cdist/core/cdist_type.py +++ b/cdist/core/cdist_type.py @@ -204,11 +204,13 @@ class CdistType(object): try: defaults_dir = os.path.join(self.absolute_path, "parameter", "default") for name in os.listdir(defaults_dir): - with open(os.path.join(defaults_dir, name)) as fd: - defaults[name] = fd.read().strip() + try: + with open(os.path.join(defaults_dir, name)) as fd: + defaults[name] = fd.read().strip() + except EnvironmentError: + pass # Swallow errors raised by open() or read() except EnvironmentError: - # error ignored - pass + pass # Swallow error raised by os.listdir() finally: self.__parameter_defaults = defaults return self.__parameter_defaults