def test_git_config(self): """Unit test git_config with invalid config name... """ self.enable_unit_test() import utils self.assertTrue(utils.scratch_dir is None) # Call create_scratch_dir, and verify that no warning is printed # on stderr, by redirecting it to a StringIO file. real_stderr = sys.stderr new_stderr = StringIO() sys.stderr = new_stderr utils.create_scratch_dir() sys.stderr = real_stderr rmtree(utils.scratch_dir) self.assertEqual(new_stderr.getvalue(), '', new_stderr.getvalue()) new_stderr.close() # Call create_scratch_dir again, and verify that a warning is # printed, this time. new_stderr = StringIO() sys.stderr = new_stderr utils.create_scratch_dir() sys.stderr = real_stderr rmtree(utils.scratch_dir) self.assertTrue( 'Unexpected second call to create_scratch_dir' in new_stderr.getvalue(), new_stderr.getvalue()) new_stderr.close()
if update_cls is None: # Report an error. We could look more precisely into what # might be the reason behind this error, and print more precise # diagnostics, but it does not seem like this would be worth # the effort: It requires some pretty far-fetched scenarios # for this to trigger; so, this should happen only very seldomly, # and when a user does something very unusual. raise InvalidUpdate("This type of update (%s,%s) is not valid." % (ref_name, get_object_type(new_rev))) with FileLock('git-hooks::update.token'): update_cls.validate() maybe_update_hook(ref_name, old_rev, new_rev) if __name__ == "__main__": args = parse_command_line() try: init_all_globals( OrderedDict([(args.ref_name, (args.old_rev, args.new_rev))])) create_scratch_dir() check_update(args.ref_name, args.old_rev, args.new_rev) except InvalidUpdate, E: # The update was rejected. Print the rejection reason, and # exit with a nonzero status. warn(*E) sys.exit(1) finally: # Delete our scratch directory. if utils.scratch_dir is not None: rmtree(utils.scratch_dir)
from shutil import rmtree import os # Modules provided by git-hooks import utils assert utils.scratch_dir is None print("DEBUG: Calling utils.create_scratch_dir (call #1)...") utils.create_scratch_dir() print("DEBUG: Deleting scratch dir...") rmtree(utils.scratch_dir) print("DEBUG: Calling utils.create_scratch_dir (call #2)...") print(" (this call is expected to generate a warning)") utils.create_scratch_dir() print("DEBUG: Deleting crash dir again...") rmtree(utils.scratch_dir)
REMARKS This function assumes that scratch_dir has been initialized. """ debug('check_update(ref_name=%s, old_rev=%s, new_rev=%s)' % (ref_name, old_rev, new_rev), level=2) update_cls = new_update(ref_name, old_rev, new_rev, git_show_ref(), submitter_email=None) if update_cls is None: raise InvalidUpdate( "This type of update (%s,%s) is currently unsupported." % (ref_name, get_object_type(new_rev))) with FileLock('git-hooks::update.token'): update_cls.validate() if __name__ == "__main__": args = parse_command_line() try: create_scratch_dir() check_update(args.ref_name, args.old_rev, args.new_rev) except InvalidUpdate, E: # The update was rejected. Print the rejection reason, and # exit with a nonzero status. warn(*E) sys.exit(1) finally: # Delete our scratch directory. if utils.scratch_dir is not None: rmtree(utils.scratch_dir)