Bug 1404401 - ignore presence of target_task_config.json in tests; r?ahal draft
authorDustin J. Mitchell <dustin@mozilla.com>
Thu, 12 Oct 2017 17:58:14 +0000
changeset 679630 ff9871612835c79df0e6657e4afb4cf92b84d211
parent 679629 25aad10380b10b6efa50c2b4d97245f078d870a0
child 735655 a8271606d6def403852bc23b1fdd6de70cb2a7b9
push id84282
push userdmitchell@mozilla.com
push dateThu, 12 Oct 2017 22:48:33 +0000
reviewersahal
bugs1404401
milestone58.0a1
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
config/mozunit.py
taskcluster/taskgraph/test/test_decision.py
--- a/config/mozunit.py
+++ b/config/mozunit.py
@@ -130,16 +130,19 @@ class MockedOpen(object):
     Context manager diverting the open builtin such that opening files
     can open "virtual" file instances given when creating a MockedOpen.
 
     with MockedOpen({'foo': 'foo', 'bar': 'bar'}):
         f = open('foo', 'r')
 
     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.
 
     with MockedOpen():
         f = open('foo', 'w')
         f.write('foo')
     self.assertRaises(Exception,f.open('foo', 'r'))
@@ -149,17 +152,20 @@ class MockedOpen(object):
         for name, content in files.iteritems():
             self.files[normcase(os.path.abspath(name))] = content
 
     def __call__(self, name, mode='r'):
         absname = normcase(os.path.abspath(name))
         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:
             file = self.open(name, mode)
         if 'a' in mode:
             file.seek(0, os.SEEK_END)
         return file
 
@@ -178,41 +184,40 @@ class MockedOpen(object):
         import __builtin__
         __builtin__.open = self.open
         os.path.exists = self._orig_path_exists
         os.path.isdir = self._orig_path_isdir
         os.path.isfile = self._orig_path_isfile
 
     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)
 
     def _wrapped_isdir(self, p):
         p = normcase(p)
         p = p if p.endswith(('/', '\\')) else p + os.sep
         if any(f.startswith(p) for f in self.files):
             return True
 
         abspath = normcase(os.path.abspath(p) + os.sep)
         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):
     runwith = kwargs.pop('runwith', 'pytest')
 
     if runwith == 'unittest':
         unittest.main(testRunner=MozTestRunner(), *args, **kwargs)
     else:
--- a/taskcluster/taskgraph/test/test_decision.py
+++ b/taskcluster/taskgraph/test/test_decision.py
@@ -41,58 +41,62 @@ class TestDecision(unittest.TestCase):
         finally:
             if os.path.exists(tmpdir):
                 shutil.rmtree(tmpdir)
             decision.ARTIFACTS_DIR = 'artifacts'
 
 
 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',
             'head_repository': 'https://hg.mozilla.org/mozilla-central',
             'head_rev': 'abcd',
             'head_ref': 'ef01',
             'message': '',
             'project': 'mozilla-central',
             'pushlog_id': 143,
             'pushdate': 1503691511,
             'owner': 'nobody@mozilla.com',
             'level': 3,
         }
 
     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')
         self.assertEqual(params['try_mode'], None)
         self.assertEqual(params['try_options'], None)
         self.assertEqual(params['try_task_config'], None)
 
     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')
         self.assertEqual(params['try_task_config'], None)
 
     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)
             self.assertEqual(params['try_task_config'], ttc)
 
 
 if __name__ == '__main__':
     main()