def test_temp_dir_sentinel(): from os.path import join, isdir, exists, abspath basedir = "test_temp_dir_sentinel" fn = join(basedir, "foo", "bar") if exists(basedir): print("Path %s exists, not running test_temp_dir_sentinel()" % basedir) return os.makedirs(basedir) eq_(get_closest_dir(fn)[0], basedir) eq_(get_closest_dir(fn)[1], "foo") sentinel = join(basedir, "foo.inuse") try: with temp_dir(fn, erase_after=True, with_sentinel=True): assert isdir(fn) assert exists(sentinel) # simulate work open(join(fn, "dummy.txt"), "w").close() # work file should be deleted together with directory assert not exists(fn) assert not exists(join(basedir, "foo")) # basedir should still exist, though! assert isdir(basedir) finally: if isdir(basedir): shutil.rmtree(basedir)
def test_temp_dir(): fn = "test_temp_dir" if os.path.exists(fn): print("Path %s exists, not running test_temp_dir()" % fn) return try: assert not os.path.exists(fn) with temp_dir(fn): assert os.path.exists(fn) assert os.path.exists(fn) os.rmdir(fn) assert not os.path.exists(fn) with temp_dir(fn, erase_after=True): assert os.path.exists(fn) assert not os.path.exists(fn) finally: if os.path.isdir(fn): os.rmdir(fn)
def test_temp_dir_pardir(): with temp_dir("../test_temp_dir"): pass