From 0d5ca56ea5432e85719cd30a5d0ba0c68405e08a Mon Sep 17 00:00:00 2001 From: "Dustin J. Mitchell" Date: Thu, 12 Oct 2017 17:58:14 +0000 Subject: [PATCH] Bug 1404401 - ignore presence of target_task_config.json in tests; r=ahal I *think* the modifications to MockedOpen are correct, but I'm not sure.. MozReview-Commit-ID: 6vTZBtdQ1dz --HG-- extra : rebase_source : 2d2008f87640747ef831d000bf13a4b1b7fcd338 --- config/mozunit.py | 17 +++++++++++------ taskcluster/taskgraph/test/test_decision.py | 14 +++++++++----- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/config/mozunit.py b/config/mozunit.py index 246a37b952f5..201172d19b13 100644 --- a/config/mozunit.py +++ b/config/mozunit.py @@ -135,6 +135,9 @@ class MockedOpen(object): will thus open the virtual file instance for the file 'foo' to f. + If the content of a file is given as None, then that file will be + represented as not existing (even if it does, actually, exist). + MockedOpen also masks writes, so that creating or replacing files doesn't touch the file system, while subsequently opening the file will return the recorded content. @@ -154,7 +157,10 @@ class MockedOpen(object): if 'w' in mode: file = MockedFile(self, absname) elif absname in self.files: - file = MockedFile(self, absname, self.files[absname]) + content = self.files[absname] + if content is None: + raise IOError(2, 'No such file or directory') + file = MockedFile(self, absname, content) elif 'a' in mode: file = MockedFile(self, absname, self.open(name, 'r').read()) else: @@ -183,17 +189,16 @@ class MockedOpen(object): def _wrapped_exists(self, p): return (self._wrapped_isfile(p) or - self._wrapped_isdir(p) or - self._orig_path_exists(p)) + self._wrapped_isdir(p)) def _wrapped_isfile(self, p): p = normcase(p) if p in self.files: - return True + return self.files[p] is not None abspath = normcase(os.path.abspath(p)) if abspath in self.files: - return True + return self.files[abspath] is not None return self._orig_path_isfile(p) @@ -207,7 +212,7 @@ class MockedOpen(object): if any(f.startswith(abspath) for f in self.files): return True - return self._orig_path_exists(p) + return self._orig_path_isdir(p) def main(*args, **kwargs): diff --git a/taskcluster/taskgraph/test/test_decision.py b/taskcluster/taskgraph/test/test_decision.py index bf4216879608..749e3b773c86 100644 --- a/taskcluster/taskgraph/test/test_decision.py +++ b/taskcluster/taskgraph/test/test_decision.py @@ -46,6 +46,8 @@ class TestDecision(unittest.TestCase): class TestGetDecisionParameters(unittest.TestCase): + ttc_file = os.path.join(os.getcwd(), 'try_task_config.json') + def setUp(self): self.options = { 'base_repository': 'https://hg.mozilla.org/mozilla-unified', @@ -61,7 +63,8 @@ class TestGetDecisionParameters(unittest.TestCase): } def test_simple_options(self): - params = decision.get_decision_parameters(self.options) + with MockedOpen({self.ttc_file: None}): + params = decision.get_decision_parameters(self.options) self.assertEqual(params['pushlog_id'], 143) self.assertEqual(params['build_date'], 1503691511) self.assertEqual(params['moz_build_date'], '20170825200511') @@ -71,13 +74,15 @@ class TestGetDecisionParameters(unittest.TestCase): def test_no_email_owner(self): self.options['owner'] = 'ffxbld' - params = decision.get_decision_parameters(self.options) + with MockedOpen({self.ttc_file: None}): + params = decision.get_decision_parameters(self.options) self.assertEqual(params['owner'], 'ffxbld@noreply.mozilla.org') def test_try_options(self): self.options['message'] = 'try: -b do -t all' self.options['project'] = 'try' - params = decision.get_decision_parameters(self.options) + with MockedOpen({self.ttc_file: None}): + params = decision.get_decision_parameters(self.options) self.assertEqual(params['try_mode'], 'try_option_syntax') self.assertEqual(params['try_options']['build_types'], 'do') self.assertEqual(params['try_options']['unittests'], 'all') @@ -85,9 +90,8 @@ class TestGetDecisionParameters(unittest.TestCase): def test_try_task_config(self): ttc = {'tasks': ['a', 'b'], 'templates': {}} - ttc_file = os.path.join(os.getcwd(), 'try_task_config.json') self.options['project'] = 'try' - with MockedOpen({ttc_file: json.dumps(ttc)}): + with MockedOpen({self.ttc_file: json.dumps(ttc)}): params = decision.get_decision_parameters(self.options) self.assertEqual(params['try_mode'], 'try_task_config') self.assertEqual(params['try_options'], None)