diff --git a/lib/iris/tests/__init__.py b/lib/iris/tests/__init__.py index 1d4d6583a0..9da98d9d6f 100644 --- a/lib/iris/tests/__init__.py +++ b/lib/iris/tests/__init__.py @@ -669,8 +669,6 @@ def check_graphic(self, tol=None): unique_id = self._unique_id() - figure = plt.gcf() - repo_fname = os.path.join(os.path.dirname(__file__), 'results', 'imagerepo.json') with open(repo_fname, 'rb') as fi: repo = json.load(codecs.getreader('utf-8')(fi)) @@ -702,13 +700,17 @@ def check_graphic(self, tol=None): if err.errno != 17: raise - figure.savefig(result_fname) + def save_figure_hash(): + figure = plt.gcf() + figure.savefig(result_fname) - # XXX: Deal with a new test result i.e. it's not in the repo + # hash the created image using sha1 + with open(result_fname, 'rb') as res_file: + sha1 = hashlib.sha1(res_file.read()) + return sha1 + + sha1 = save_figure_hash() - # hash the created image using sha1 - with open(result_fname, 'rb') as res_file: - sha1 = hashlib.sha1(res_file.read()) if unique_id not in repo: msg = 'Image comparison failed: Created image {} for test {}.' raise ValueError(msg.format(result_fname, unique_id)) @@ -719,6 +721,12 @@ def check_graphic(self, tol=None): expected = [os.path.splitext(os.path.basename(uri))[0] for uri in uris] + if sha1.hexdigest() not in expected: + # This can be an accidental failure, unusual, but it occurs + # https://github.com/SciTools/iris/issues/2195 + # retry once, in case it passes second time round. + sha1 = save_figure_hash() + if sha1.hexdigest() not in expected: emsg = 'Actual SHA1 {} not in expected {} for test {}.' emsg = emsg.format(sha1.hexdigest(), expected, unique_id)