def test_dict_key(self): "Should be able to dict(Path()=5)" mydict = {Path('/foo'): 1} self.assertEqual(1, mydict[Path('/foo')]) self.assertEqual(1, mydict['/foo']) mydict['/foo'] = 2 self.assertEqual(2, mydict['/foo'])
def test_ls_nonexistant(self): "Should raise if we don't exist" nopath = tempfile.mkdtemp() rmdir(nopath) p = Path(nopath) with self.assertRaises(exceptions.DoesNotExistError): p.ls()
def test_truncate(self): "Should truncate a file" p = Path(tempfile.mkdtemp()) + 'testfile.txt' p << "Contentz" self.assertTrue(p.size > 0) p.truncate() self.assertEqual(0, p.size)
def test_split(self): "Split should ignore leading /" ap = Path('/foo/bar') p = Path('foo/bar') expected = ['foo', 'bar'] self.assertEqual(expected, ap._split) self.assertEqual(expected, p._split)
def test_mkdir_child(self): "Should create child directories" p = Path(self.tdir) self.assertFalse(os.path.isdir(self.tdir + 'one')) self.assertFalse(os.path.isdir(self.tdir + 'two')) p.mkdir('one', 'two') self.assertTrue(os.path.isdir(self.tdir + '/one')) self.assertTrue(os.path.isdir(self.tdir + '/two'))
def test_touch_child(self): "Touch children" p = Path(self.tdir) self.assertFalse(os.path.isfile(self.tdir + 'one.txt')) self.assertFalse(os.path.isfile(self.tdir + 'two.txt')) p.touch('one.txt', 'two.txt') self.assertTrue(os.path.isfile(self.tdir + '/one.txt')) self.assertTrue(os.path.isfile(self.tdir + '/two.txt'))
def test_cp_dir(self): "Intuits Directory, recursive" p = Path(self.tdir) p.mkdir('somedir') pd = p + 'somedir' pd.cp(p + 'otherdir') self.assertTrue(os.path.isdir(pd)) self.assertTrue(os.path.isdir(p + 'otherdir'))
def test_as_csv(self): "Csv contextmanager" p = Path(self.tmpath) p << '1,2,3,4' with p.csv() as csv: for i, row in enumerate(csv): self.assertEqual(0, i) self.assertEqual('1 2 3 4'.split(), row)
def test_ls_glob(self): "only return the globbed contents" p = Path(self.tdir) p.touch('one.txt', 'two.txt', 'three.csv') contents = p.ls('*.txt') self.assertEqual(2, len(contents)) self.assertTrue(all([isinstance(i, Path) for i in contents])) for p in [self.tdir + '/one.txt', self.tdir + '/two.txt']: self.assertIn(p, contents)
def test_mv_file(self): "Should move a file" p = Path(self.tdir) + 'some.txt' p << 'contents' self.assertTrue(p.is_file) p2 = p.mv(p.parent + 'some2.txt') self.assertTrue(p2.is_file) self.assertFalse(p.is_file) self.assertEqual('contents', open(p2).read())
def test_ls_ispath(self): "Should return a list of paths" p = Path(self.tdir) p.touch('one.txt', 'two.txt') contents = p.ls() self.assertEqual(2, len(contents)) self.assertTrue(all([isinstance(i, Path) for i in contents])) for p in [self.tdir + '/one.txt', self.tdir + '/two.txt']: self.assertIn(p, contents)
def test_readline(self): "Should ducktype as a file and readline()" nopath = tempfile.mkdtemp() p = Path(nopath) + 'testfile.txt' p << "Frist\nNext\nLast" self.assertEqual("Frist\n", p.readline()) self.assertEqual("Next\n", p.readline()) self.assertEqual("Last", p.readline()) self.assertEqual("", p.readline())
def test_mv_dir(self): "Should move a directory" p = Path(self.tdir) p.mkdir('somedir') pd = p + 'somedir' self.assertTrue(p.is_dir) p2 = p.mv(p.parent + 'some2') self.assertTrue(p2.is_dir) self.assertFalse(p.is_dir)
def test_open_mkpath(self): "If we open a path that doesn't exist yet, make it" nopath = tempfile.mkdtemp() rmdir(nopath) p = Path(nopath) + 'really/doesnt/exist.txt' filename = nopath + '/really/doesnt/exist.txt' with p.open('w') as fh: self.assertIsInstance(fh, FileKlass) self.assertEqual(filename, fh.name) pass
def test_contains(self): "Test x in Path syntax" p = Path('/foo/bar/baz') self.assertTrue('foo' in p) self.assertTrue('/foo' in p) self.assertTrue('/foo/bar' in p) self.assertFalse('oo/bar' in p) self.assertFalse('az' in p) self.assertFalse('/bar/baz' in p) rp = Path('my/rel/file.txt') self.assertTrue('my/rel' in rp)
def test_as_csv_header(self): "Csv contextmanager with header" p = Path(self.tmpath) p << 'a,b,c,d\n' p << '1,2,3,4' with p.csv(header=True) as csv: row = csv.next() self.assertEqual('1', row.a) self.assertEqual('2', row.b) self.assertEqual('3', row.c) self.assertEqual('4', row.d)
def test_with_tmp_contents(self): "Should kill directory contents" with Path.temp() as p: val = str(p) touch(p + 'my.txt') self.assertTrue(os.path.exists(str(p + 'my.txt'))) self.assertFalse(os.path.exists(val))
def test_contextmanager_dir(self): "With dir should change directory" with Path(self.tdir): cwd = os.getcwd() if sys.platform == 'darwin': cwd = cwd.replace('/private', '') self.assertEqual(self.tdir, cwd)
def test_lshift_notstring(self): "Should raise TypeError. Can only write strings" cases = [123, 12.3, {'hai': 'bai'}, object()] p = Path() for case in cases: with self.assertRaises(TypeError): p << case
def test_here(self): """ Should return a path instance representing the directory of the calling site. """ expected = os.path.dirname(__file__) self.assertEqual(expected, Path.here())
def test_blacklisted(self): "Inappropriate methods of strings should be overriden" blacklist = _path_blacklists._strblacklist p = Path() for method in blacklist: with self.assertRaises(AttributeError): getattr(p, method)
def test_setitem_indexerror(self): "Paths should always raise on item assignment" p = Path('/foo/bar/baz.txt') with self.assertRaises(TypeError): p[3] = 'car' with self.assertRaises(TypeError): p[4] = 'car'
def test_iter_raises(self): "Iterate through lines in a file" nopath = tempfile.mkdtemp() rmdir(nopath) p = Path(nopath) with self.assertRaises(exceptions.DoesNotExistError): for branch in p: raise AssertionError("Shouldn't get this far")
def test_iter_dir(self): "Iterate through lines in a file" p = Path(self.tdir) touch(p + 'foo.txt') touch(p + 'bar.txt') i = ['foo.txt', 'bar.txt'] for branch in p: self.assertIn(branch, i)
def test_tempfile_contextmanager(self): "should yeild a file that exists" with Path.tempfile() as p: val = str(p) self.assertTrue(os.path.exists(val)) self.assertTrue(os.path.isfile(val)) self.assertFalse(os.path.isfile(val)) self.assertFalse(os.path.exists(val))
def test_lshift_unicode(self): "Make sure we can accept unicode strings" p = Path(self.tmpath) if sys.version_info < (3, 0): p << unicode("Hello Beautiful") else: p << "Hello Beautiful" contents = open(self.tmpath).read() self.assertEqual("Hello Beautiful", contents)
def test_getitem(self): "Make paths slicable" p = Path('/foo/bar/baz') self.assertEqual(p[0], 'foo') self.assertEqual(p[1:], 'bar/baz') self.assertEqual(p[:1], '/foo') self.assertEqual(p[0:2], '/foo/bar') self.assertEqual(p[1:3], 'bar/baz') self.assertEqual(p[-1], 'baz')
def test_add_emptycoll(self): "adding an empty coll is a no-op" p = Path('/foo') p = p + [] self.assertEqual('/foo', p) self.assertIsInstance(p, Path) p = p + tuple() self.assertEqual('/foo', p) self.assertIsInstance(p, Path)
def test_iter_file(self): "Iterate through lines in a file" with open(self.tmpath, 'w') as tf: tf.write("foo\nbar\nbaz\n") p = Path(self.tmpath) i = ['foo\n', 'bar\n', 'baz\n'] try: fn = itertools.izip except AttributeError: fn = zip for branch, expected in fn(p, i): self.assertEqual(expected, branch)
def test_abspath_tilde(self): "If *nix, expand ~" if not sys.platform.startswith('win'): home = 'home' if sys.platform == 'darwin': home = 'Users' user = getpass.getuser() expected = '/{0}/{1}/.emacs'.format(home, user) if user == 'travis': expected = '/' p = Path('~/.emacs') self.assertEqual(expected, p.abspath)
def test_loads(self): "Can load a json file" p = Path(self.tmpath) jsonified = json.dumps(dict(foo=1)) p << jsonified self.assertEqual(dict(foo=1), p.json_load())
def test_tmpfile_function(self): "Should leave the tempdir" p = Path.newfile() self.assertTrue(os.path.exists(p)) self.assertTrue(os.path.isfile(p))
def test_ls(self): "Should list dir contents" p = Path(self.tdir) self.assertEqual([], p.ls())
def test_mv_nonexistent(self): "Should raise" p = Path(self.tdir) + 'nonexistant' with self.assertRaises(exceptions.DoesNotExistError): p.mv(self.tdir)
def test_lsfile(self): "Just returns the name" p = Path(self.tmpath) self.assertEqual(self.tmpath, p.ls())
def test_touch(self): "Should touch it" p = Path(self.tdir) + 'notyet.txt' self.assertFalse(os.path.isfile(str(p))) p.touch() self.assertTrue(os.path.isfile(str(p)))
def test_cp_nonexistant(self): "Should raise" with self.assertRaises(exceptions.DoesNotExistError): p = Path('does/not/exist/here') p.cp('will/not/exist/there')
def test_cp_file(self): "Copy self to dest" p = Path(self.tdir) + 'some.txt' p << 'contents' p.cp(p.parent + 'some2.txt') self.assertTrue(filecmp.cmp(p, p.parent + 'some2.txt'))
def test_mkdir_parents(self): "Make the parents as well" p = Path(self.tdir) self.assertFalse(os.path.isdir(self.tdir + '/somedir/one')) p.mkdir('somedir/one/two') self.assertTrue(os.path.isdir(self.tdir + '/somedir/one'))
def test_ls_returns_dir(self): "Should return a dictionary" p = Path(self.tdir) p.touch('one.txt', 'two.txt') contents = p.ls() self.assertIsInstance(contents, Pset)
def test_mkdir(self): "Should make the file." p = Path(self.tdir) + 'foo' self.assertFalse(os.path.isdir(self.tdir + '/foo')) p.mkdir() self.assertTrue(os.path.isdir(self.tdir + '/foo'))
def test_mkdir_file(self): "Should raise TypeError" p = Path(self.tmpath) with self.assertRaises(TypeError): p.mkdir()
def test_touch_child_tree(self): "Should imply the tree" p = Path(self.tdir) p.touch('that/theother.txt') self.assertTrue(os.path.isfile(self.tdir + '/that/theother.txt'))
def test_touch_child_no_self(self): "Should imply self" p = Path(self.tdir) + 'that' p.touch('theother.txt') self.assertTrue(os.path.isfile(self.tdir + '/that/theother.txt'))