import os import tarfile import unittest from unittest import mock TEMP_FILENAME = "temp.file" TEMP_TAR_FILENAME = "temp.tar" ORIG_LSTAT = os.lstat def intercept_lstat(arg): res = ORIG_LSTAT(arg) if arg == TEMP_FILENAME: return os.stat_result( (res.st_mode, res.st_ino, res.st_dev, res.st_nlink, res.st_uid, res.st_gid, res.st_size+1, res.st_atime, res.st_mtime, res.st_ctime) ) return res class TestClass(unittest.TestCase): def setUp(self): self._create_empty_file(TEMP_FILENAME) @staticmethod def _create_empty_file(filename): with open(filename, "w+") as _: pass def tearDown(self): os.remove(TEMP_FILENAME) os.remove(TEMP_TAR_FILENAME) @mock.patch("tarfile.os.lstat", side_effect=intercept_lstat) def test_stat(self, _): with tarfile.open(TEMP_TAR_FILENAME, "w:gz") as tar: tar.add(TEMP_FILENAME)