0

I have a code that is architecturally close to posted below (unfortunately i can't post full version cause it's proprietary). I have an self-updating executable and i'm trying to test this feature. We assume that full path to this file will be in A.some_path after executing input. My problem is that assertion failed, because on second call os.stat still returning the previous file stats (i suppose it thinks that nothing could changed so it's unnecessary). I have tried to launch this manually and self-updating works completely fine and the file is really removing and recreating with stats changing. Is there any guaranteed way to force os.stat re-read file stats by the same path, or alternative option to make it works (except recreating an A object)?

from pathlib import Path
import unittest
import os

class A:
    some_path = Path()

    def __init__(self, _some_path):
        self.some_path = Path(_some_path)
        
    def get_path(self):
        return self.some_path

class TestKit(unittest.TestCase):
    def setUp(self):
        pass

    def check_body(self, a):
        some_path = a.get_path()
        modification_time = os.stat(some_path).st_mtime
        # Launching self-updating executable
        self.assertTrue(modification_time < os.stat(some_path).st_mtime)

    def check(self):
        a = A(input('Enter the file path\n'))
        self.check_body(a)

def Tests():
    suite = unittest.TestSuite()
    suite.addTest(TestKit('check'))
    return suite

def main():
    tests_suite = Tests()
    unittest.TextTestRunner().run(tests_suite)

if __name__ == "__main__":
    main()
k0shinus
  • 81
  • 1
  • 6
  • What OS are you on, and how is the file being modified? If I run `os.stat()` repeatedly against a file being written to, the mtime changes each time. – match Dec 01 '21 at 19:18
  • @match I get this problem on Win10. Well, executable downloading new version from net into temp folder, launching this temporary file, then removing existed but already finished old executable, copying itself to the old path (with renaming if it's needed), launching normaly from old place (but with new executable) and finally removing temporary exe. – k0shinus Dec 01 '21 at 19:31

1 Answers1

0

I have found the origins of the problem: i've tried to launch self-updating via os.system which wait till the process done. But first: during the self-updating we launch several detached proccesses and actually should wait unitl all them have ended, and the second: even the signal that the proccess ends doesn't mean that OS really completely realease the file, and looks like on assertTrue we are not yet done with all our routines. For my task i simply used sleep, but normal solution should analyze the existing proccesses in the system and wait for them to finish, or at least there should be several attempts with awaiting.

k0shinus
  • 81
  • 1
  • 6