import py import dirat, os def setup_module(mod): mod.udir = py.path.local.make_numbered_dir(prefix='usession-', keep=3) mod.original_cwd = os.getcwd() def teardown_function(fn): os.chdir(original_cwd) def test_opencwd(): original_cwd = os.getcwd() dir = dirat.opencwd() assert str(dir) == original_cwd os.chdir('/tmp') assert str(dir) == original_cwd assert str(dirat.opencwd()) == '/tmp' def test_opencwd_robustness(): p1 = udir.join('dir1') p1.ensure(dir=1) os.chdir(str(p1)) a1 = dirat.opencwd() assert str(a1) == str(p1) p2 = p1.dirpath().join('dir2') p1.rename(str(p2)) assert str(a1) == str(p2) def test_opendir(): dir = dirat.opendir('/tmp') assert str(dir) == '/tmp' py.test.raises(OSError, dirat.opendir, __file__) py.test.raises(OSError, dirat.opendir, 'some/nonexistent/path') def test_join(): a1 = dirat.opendir('/') for name in str(udir).split('/'): a1 = a1.join(name) assert str(a1) == str(udir) def test_parentdir(): a1 = dirat.opendir(str(udir)) a2 = a1.parentdir() assert str(a2) == str(udir.dirpath()) def test_eq(): a1 = dirat.opendir(str(udir)) a2 = a1.parentdir() assert a2 != a1 assert not (a2 == a1) a3 = a1.parentdir() assert a2 == a3 assert hash(a2) == hash(a3) assert not (a2 != a3)