class TestProjectDetection(TestCase): def setUp(self): self.fs = MemoryFS() self.root = MemoryPath(fs=self.fs, path=("test-dir", )) self.root.createDirectory() def test_it_detects_git_repositories(self): self.root.child(".git").createDirectory() self.assertEqual( project.from_path(self.root), project.GitPath( git_dir=self.root.child(".git"), path=self.root, ), ) def test_it_detects_hg_repositories(self): self.root.child(".hg").createDirectory() self.assertEqual( project.from_path(self.root), project.HgPath(hg_dir=self.root.child(".hg")), ) def test_it_detects_normal_directories(self): self.assertEqual(project.from_path(self.root), self.root)
class TestProjectDetection(TestCase): def setUp(self): self.fs = MemoryFS() self.root = MemoryPath(fs=self.fs, path=("test-dir",)) self.root.createDirectory() def test_it_detects_git_repositories(self): self.root.child(".git").createDirectory() self.assertEqual( project.from_path(self.root), project.GitPath( git_dir=self.root.child(".git"), path=self.root, ), ) def test_it_detects_hg_repositories(self): self.root.child(".hg").createDirectory() self.assertEqual( project.from_path(self.root), project.HgPath(hg_dir=self.root.child(".hg")), ) def test_it_detects_normal_directories(self): self.assertEqual(project.from_path(self.root), self.root)
class TestOutputters(TestCase): def setUp(self): self.fs = MemoryFS() self.root = MemoryPath(fs=self.fs, path=("test-dir",)) self.root.createDirectory() def assertOutputs(self, result, **kwargs): stdout = BytesIO() cli.run(stdout=stdout, **kwargs) self.assertEqual(stdout.getvalue(), dedent(result)) def children(self, *new, **kwargs): of = kwargs.pop("of", self.root) assert not kwargs of.createDirectory() for child in new: path = of.child(child) path.setContent("") yield path def test_it_lists_directories(self): foo, bar = self.children("foo", "bar") self.assertOutputs( output=core.columnized, paths=[self.root], result="bar foo\n", ) def test_it_lists_multiple_directories(self): one = self.root.child("one") two, four = self.children("two", "four", of=one) three, = self.children("three") self.assertOutputs( output=core.columnized, paths=[self.root, one], result="""\ /mem/test-dir: one three /mem/test-dir/one: four two """, ) def test_group_directories_first(self): self.root.child("one").createDirectory() self.root.child("two").createDirectory() three, four = self.children("three", "four") self.assertOutputs( output=core.columnized, sort_by=core.group_directories_first, paths=[self.root], result="one two four three\n", ) def test_group_directories_first_one_per_line(self): self.root.child("one").createDirectory() self.root.child("two").createDirectory() three, four = self.children("three", "four") self.assertOutputs( output=core.one_per_line, sort_by=core.group_directories_first, paths=[self.root], result="""\ one two four three """, ) def test_group_directories_first_multiple_one_per_line(self): one = self.root.child("one") self.root.child("two").createDirectory() three, four = self.children("three", "four") five, six = self.children("five", "six", of=one) one.child("seven").createDirectory() self.assertOutputs( output=core.one_per_line, sort_by=core.group_directories_first, paths=[self.root, one], result="""\ /mem/test-dir/one /mem/test-dir/one/seven /mem/test-dir/two /mem/test-dir/four /mem/test-dir/one/five /mem/test-dir/one/six /mem/test-dir/three """, ) def test_it_ignores_hidden_files_by_default(self): foo, hidden = self.children("foo", ".hidden") self.assertOutputs( output=core.columnized, paths=[self.root], result="foo\n", ) def test_it_ignores_hidden_files_by_default_for_multiple_directories(self): one = self.root.child("one") two, four = self.children(".two", "four", of=one) three, = self.children(".three") self.assertOutputs( output=core.columnized, paths=[self.root, one], result="""\ /mem/test-dir: one /mem/test-dir/one: four """, ) def test_it_can_list_almost_everything(self): one = self.root.child("one") two, four = self.children(".two", "four", of=one) three, = self.children(".three") self.assertOutputs( ls=core.ls_almost_all, output=core.columnized, paths=[self.root, one], result="""\ /mem/test-dir: .three one /mem/test-dir/one: .two four """, ) def test_it_can_list_everything(self): one = self.root.child("one") two, four = self.children(".two", "four", of=one) three, = self.children(".three") self.assertOutputs( ls=core.ls_all, output=core.columnized, paths=[self.root, one], result="""\ /mem/test-dir: . .. .three one /mem/test-dir/one: . .. .two four """, ) def test_it_can_list_everything_recursively(self): one = self.root.child("one") two, four = self.children(".two", "four", of=one) three, = self.children(".three") self.assertOutputs( ls=core.ls_all, output=core.columnized, recurse=core.recurse, paths=[self.root], result="""\ /mem/test-dir: . .. .three one /mem/test-dir/one: . .. .two four """, ) def test_it_lists_directories_one_per_line(self): foo, bar = self.children("foo", "bar") self.assertOutputs( output=core.one_per_line, paths=[self.root], result="bar\nfoo\n", ) def test_it_lists_multiple_absolute_directories_one_per_line(self): one = self.root.child("one") two, four = self.children("two", "four", of=one) three, = self.children("three") self.assertOutputs( output=core.one_per_line, paths=[self.root, one], result="""\ /mem/test-dir/one /mem/test-dir/one/four /mem/test-dir/one/two /mem/test-dir/three """, ) def test_it_lists_directories_recursively(self): one = self.root.child("one") two, four = self.children("two", "four", of=one) three, = self.children("three") self.assertOutputs( output=core.columnized, recurse=core.recurse, paths=[self.root], result="""\ /mem/test-dir: one three /mem/test-dir/one: four two """, ) def test_it_lists_directories_recursively_one_per_line(self): one = self.root.child("one") two, four = self.children("two", "four", of=one) three, = self.children("three") self.assertOutputs( output=core.one_per_line, recurse=core.recurse, paths=[self.root], result="""\ /mem/test-dir/one /mem/test-dir/one/four /mem/test-dir/one/two /mem/test-dir/three """, ) def test_it_lists_flat_trees(self): foo, bar = self.children("foo", "bar") self.assertOutputs( output=core.as_tree, paths=[self.root], result="""\ /mem/test-dir ├── bar └── foo """, ) def test_it_lists_multiple_flat_directories_as_trees(self): one, two = self.children("one", "two") foo, bar = self.children("foo", "bar", of=one) baz, quux = self.children("baz", "quux", of=two) self.assertOutputs( output=core.as_tree, paths=[one, two], result="""\ /mem/test-dir/one ├── bar └── foo /mem/test-dir/two ├── baz └── quux """, ) def test_group_directories_first_as_tree(self): self.root.child("one").createDirectory() self.root.child("two").createDirectory() three, four = self.children("three", "four") self.assertOutputs( output=core.as_tree, sort_by=core.group_directories_first, paths=[self.root], result="""\ /mem/test-dir ├── one ├── two ├── four └── three """, ) def test_it_lists_empty_directories(self): self.assertOutputs( output=core.columnized, paths=[self.root], result="", ) def test_it_lists_empty_directories_one_per_line(self): self.assertOutputs( output=core.one_per_line, paths=[self.root], result="", ) def test_it_lists_empty_directories_as_tree(self): self.assertOutputs( output=core.as_tree, paths=[self.root], result="/mem/test-dir\n", ) def test_it_lists_multiple_empty_directories(self): self.assertOutputs( output=core.columnized, paths=[self.root, self.root], result="/mem/test-dir:\n\n\n/mem/test-dir:\n\n", ) def test_it_lists_multiple_empty_directories_one_per_line(self): self.assertOutputs( output=core.one_per_line, paths=[self.root, self.root], result="", ) def test_it_lists_multiple_empty_directories_as_tree(self): self.assertOutputs( output=core.as_tree, paths=[self.root, self.root], result="/mem/test-dir\n" * 2, )
class MemoryPathTestCase(AbstractFilePathTestCase): def subdir(self, *dirname): for head in heads(dirname): self.fs._dirs.add(head) self.fs._dirs.add(dirname) def subfile(self, *dirname): for head in heads(dirname): self.fs._dirs.add(head) return self.fs.open(dirname) def setUp(self): self.fs = MemoryFS() AbstractFilePathTestCase.setUp(self) self.path = MemoryPath(fs=self.fs) self.root = self.path self.all = self.fs._dirs | set(self.fs._store.keys()) self.all = set(format_memory_path(p, "/") for p in self.all) def test_removeDirectory(self): """ L{MemoryPath.remove} on a L{MemoryPath} that refers to a directory will recursively delete its contents. """ self.assertTrue(self.path.isdir()) self.assertTrue(self.path.exists()) foo = self.path.child("foo") foo.setContent("Hi!") baz = self.path.descendant(["bar", "baz"]) baz.setContent("Bye!") self.path.remove() self.assertFalse(self.path.exists()) self.assertFalse(foo.exists()) self.assertFalse(baz.exists()) def test_removeFile(self): """ L{MemoryPath.remove} on a L{MemoryPath} that refers to a file simply deletes the file. """ path = self.path.child("file") path.setContent("Hello!") self.assertTrue(path.isfile()) self.assertTrue(path.exists()) path.remove() self.assertFalse(path.exists()) def test_removeNonExistant(self): """ L{MemoryPath.remove} on a L{MemoryPath} that does not exist raises an error. """ path = self.path.child("file") self.assertFalse(path.exists()) with self.assertRaises(PathError) as e: path.remove() self.assertEqual(e.exception.errno, errno.ENOENT)
class TestShow(TestCase): def setUp(self): self.fs = MemoryFS() self.root = MemoryPath(fs=self.fs, path=("test-dir",)) self.root.createDirectory() def assertShows(self, result, **kwargs): self.assertEqual( core.show(**kwargs), dedent(result).strip("\n") + "\n", ) def children(self, *new, **kwargs): of = kwargs.pop("of", self.root) assert not kwargs of.createDirectory() for child in new: path = of.child(child) path.setContent("") yield path def test_it_lists_directories(self): foo, bar = self.children("foo", "bar") self.assertShows(paths=[self.root], result="bar foo") def test_it_lists_multiple_directories(self): one = self.root.child("one") two, four = self.children("two", "four", of=one) three, = self.children("three") self.assertShows( paths=[self.root, one], result=""" /mem/test-dir: one three /mem/test-dir/one: four two """, ) def test_it_ignores_hidden_files_by_default(self): foo, hidden = self.children("foo", ".hidden") self.assertShows(paths=[self.root], result="foo") def test_it_ignores_hidden_files_by_default_for_multiple_directories(self): one = self.root.child("one") two, four = self.children(".two", "four", of=one) three, = self.children(".three") self.assertShows( paths=[self.root, one], result=""" /mem/test-dir: one /mem/test-dir/one: four """, )
class MemoryPathTestCase(AbstractFilePathTestCase): def subdir(self, *dirname): for head in heads(dirname): self.fs._dirs.add(head) self.fs._dirs.add(dirname) def subfile(self, *dirname): for head in heads(dirname): self.fs._dirs.add(head) return self.fs.open(dirname) def setUp(self): self.fs = MemoryFS() AbstractFilePathTestCase.setUp(self) self.path = MemoryPath(fs=self.fs) self.root = self.path self.all = self.fs._dirs | set(self.fs._store.keys()) self.all = set(format_memory_path(p, "/") for p in self.all) def test_removeDirectory(self): """ L{MemoryPath.remove} on a L{MemoryPath} that refers to a directory will recursively delete its contents. """ self.assertTrue(self.path.isdir()) self.assertTrue(self.path.exists()) foo = self.path.child("foo") foo.setContent("Hi!") bar = self.path.child("bar") bar.createDirectory() baz = bar.child("baz") baz.setContent("Bye!") self.path.remove() self.assertFalse(self.path.exists()) self.assertFalse(foo.exists()) self.assertFalse(baz.exists()) def test_removeFile(self): """ L{MemoryPath.remove} on a L{MemoryPath} that refers to a file simply deletes the file. """ path = self.path.child("file") path.setContent("Hello!") self.assertTrue(path.isfile()) self.assertTrue(path.exists()) path.remove() self.assertFalse(path.exists()) def test_removeNonExistant(self): """ L{MemoryPath.remove} on a L{MemoryPath} that does not exist raises an error. """ path = self.path.child("file") self.assertFalse(path.exists()) with self.assertRaises(PathError) as e: path.remove() self.assertEqual(e.exception.errno, errno.ENOENT) def test_getUserID(self): """ L{MemoryPath.getUserID} on a L{MemoryPath} returns the current user's ID by default. """ self.assertEqual(self.path.getUserID(), os.getuid()) def test_getUserID_after_set(self): self.fs.chown(self.path, uid=1234, gid=-1) self.assertEqual(self.path.getUserID(), 1234)