def test_move_to_empty_dir_set_mtime(src, tmp_path): dst = tmp_path.joinpath("dst") move_atomic(src, dst) assert dst.joinpath("test").exists() _dst = str(dst) # set the mtime via stamp timestamp1 = "2020-01-08T11:05:50.832123Z" _set_file_mtime(_dst, _parse_timestamp(timestamp1)) assert timestamp1 == _get_file_mtimestamp(_dst) # reset the mtime using an offset stamp timestamp2 = "2010-02-12T12:05:50.832123+01:00" _set_file_mtime(_dst, _parse_timestamp(timestamp2)) assert _get_file_mtimestamp(_dst) == "2010-02-12T11:05:50.832123Z"
def generate_import_root(rootdir, filelist): if os.path.exists(rootdir): return for (path, typesymbol, content) in filelist: if typesymbol == "F": (dirnames, filename) = os.path.split(path) os.makedirs(os.path.join(rootdir, dirnames), exist_ok=True) fullpath = os.path.join(rootdir, dirnames, filename) with open(fullpath, "wt") as f: f.write(content) # set file mtime to arbitrary _set_file_mtime(fullpath, _parse_timestamp(TIMESTAMP)) elif typesymbol == "D": os.makedirs(os.path.join(rootdir, path), exist_ok=True) elif typesymbol == "S": (dirnames, filename) = os.path.split(path) os.makedirs(os.path.join(rootdir, dirnames), exist_ok=True) os.symlink(content, os.path.join(rootdir, path))
def test_move_to_empty_dir_set_mtime(src, tmp_path): # Skip this test if we do not have support for subsecond precision mtimes # if not have_subsecond_mtime(str(tmp_path)): pytest.skip( "Filesystem does not support subsecond mtime precision: {}".format( str(tmp_path))) dst = tmp_path.joinpath("dst") move_atomic(src, dst) assert dst.joinpath("test").exists() _dst = str(dst) # set the mtime via stamp timestamp1 = "2020-01-08T11:05:50.832123Z" _set_file_mtime(_dst, _parse_timestamp(timestamp1)) assert timestamp1 == _get_file_mtimestamp(_dst) # reset the mtime using an offset stamp timestamp2 = "2010-02-12T12:05:50.832123+01:00" _set_file_mtime(_dst, _parse_timestamp(timestamp2)) assert _get_file_mtimestamp(_dst) == "2010-02-12T11:05:50.832123Z"
def generate_random_root(rootno, directory): # By seeding the random number generator, we ensure these tests # will be repeatable, at least until Python changes the random # number algorithm. random.seed(RANDOM_SEED + rootno) rootname = "root{}".format(rootno) rootdir = os.path.join(directory, "content", rootname) if os.path.exists(rootdir): return things = [] locations = ["."] os.makedirs(rootdir) for i in range(0, 100): location = random.choice(locations) thingname = "node{}".format(i) thing = random.choice(["dir", "link", "file"]) if thing == "dir": thingname = "dir" + thingname target = os.path.join(rootdir, location, thingname) if thing == "dir": os.makedirs(target) locations.append(os.path.join(location, thingname)) elif thing == "file": with open(target, "wt") as f: f.write("This is node {}\n".format(i)) _set_file_mtime(target, _parse_timestamp(TIMESTAMP)) elif thing == "link": symlink_type = random.choice(["absolute", "relative", "broken"]) if symlink_type == "broken" or not things: os.symlink("/broken", target) elif symlink_type == "absolute": symlink_destination = random.choice(things) os.symlink(symlink_destination, target) else: symlink_destination = random.choice(things) relative_link = os.path.relpath(symlink_destination, start=location) os.symlink(relative_link, target) things.append(os.path.join(location, thingname))