From f02496276183292cc0ec12bb3343c37461f2b470 Mon Sep 17 00:00:00 2001
From: Danny Hindson <danny.hindson@stfc.ac.uk>
Date: Thu, 19 Dec 2019 14:27:59 +0000
Subject: [PATCH] Updated approach to setting the date modified on temp folders

Use os.utime instead of sleep to make test less flaky
---
 .../test/test_projectrecovery.py              | 26 ++++++++++++++++---
 1 file changed, 23 insertions(+), 3 deletions(-)

diff --git a/qt/applications/workbench/workbench/projectrecovery/test/test_projectrecovery.py b/qt/applications/workbench/workbench/projectrecovery/test/test_projectrecovery.py
index 762f9791736..903acdecb7e 100644
--- a/qt/applications/workbench/workbench/projectrecovery/test/test_projectrecovery.py
+++ b/qt/applications/workbench/workbench/projectrecovery/test/test_projectrecovery.py
@@ -16,6 +16,7 @@ import sys
 import tempfile
 import time
 import unittest
+import datetime
 
 from mantid.api import AnalysisDataService as ADS
 from mantid.kernel import ConfigService
@@ -36,13 +37,23 @@ class ProjectRecoveryTest(unittest.TestCase):
         self.multifileinterpreter = mock.MagicMock()
         self.pr = ProjectRecovery(self.multifileinterpreter)
         self.working_directory = tempfile.mkdtemp()
-        # Make sure there is actually a different modified time on the files by using sleeps
+        # Make sure there is actually a different modified time on the files
         self.firstPath = tempfile.mkdtemp()
-        time.sleep(0.5)
         self.secondPath = tempfile.mkdtemp()
-        time.sleep(0.5)
         self.thirdPath = tempfile.mkdtemp()
 
+        # offset the date modified stamps in the past in case future modified dates
+        # cause any problems
+        finalFileDateTime = datetime.datetime.fromtimestamp(os.path.getmtime(self.thirdPath))
+
+        dateoffset = finalFileDateTime - datetime.timedelta(hours=2)
+        modTime = time.mktime(dateoffset.timetuple())
+        os.utime(self.firstPath, (modTime, modTime))
+        dateoffset = finalFileDateTime - datetime.timedelta(hours=1)
+        modTime = time.mktime(dateoffset.timetuple())
+        os.utime(self.secondPath, (modTime, modTime))
+
+
     def tearDown(self):
         ADS.clear()
         if os.path.exists(self.pr.recovery_directory_hostname):
@@ -51,6 +62,15 @@ class ProjectRecoveryTest(unittest.TestCase):
         if os.path.exists(self.working_directory):
             shutil.rmtree(self.working_directory)
 
+        if os.path.exists(self.firstPath):
+            shutil.rmtree(self.firstPath)
+
+        if os.path.exists(self.secondPath):
+            shutil.rmtree(self.secondPath)
+
+        if os.path.exists(self.thirdPath):
+            shutil.rmtree(self.thirdPath)
+
     def test_constructor_settings_are_set(self):
         # Test the paths set in the constructor that are generated.
         self.assertEqual(self.pr.recovery_directory,
-- 
GitLab