def test_create_file(self): store_tree = StoreTree() tt = TreeTransform(store_tree) with self.assertRaises(NotPending): tt.create_file('name1', 'parent', ['hello']) with tt: parent_id = tt.acquire_existing_id('.') file_id = tt.create_file('name1', parent_id, [b'hello']) source = tt._new_contents.full_path(file_id) target = tt.get_final_path(file_id) self.assertEqual(tt.generate_renames(), [(source, target)]) self.assertEqual(b'hello', b''.join(store_tree.read_content('name1')))
def test_subtrees(self): store_tree = StoreTree() tt = TreeTransform(store_tree) self.assertIs(None, tt._temp_tree) with tt: relative_root = os.path.relpath(tt._temp_tree.tree_root, store_tree.tree_root) self.assertNotIn('..', relative_root) tt._temp_tree.write_content('file1', 0o600, [b'hello']) full_path = os.path.join(relative_root, 'file1') self.assertEqual(b'hello', b''.join(store_tree.read_content(full_path))) self.assertIs(None, tt._temp_tree) with self.assertRaises(NoSuchFile): store_tree.read_content(full_path)
def test_make_new_id(self): tt = TreeTransform(StoreTree(), write=False) with self.assertRaises(NotPending): tt.make_new_id('foo') with tt: self.assertEqual(tt.make_new_id('foo'), 'n-0-foo') self.assertEqual(tt.make_new_id('foo'), 'n-1-foo')
def test__tree_id_to_path(self): tt = TreeTransform(StoreTree()) self.assertEqual('hello', tt._tree_id_to_path('e-hello')) with self.assertRaisesRegexp(ValueError, 'Invalid id.'): tt._tree_id_to_path('ehello') with self.assertRaisesRegexp(ValueError, 'Invalid id.'): tt._tree_id_to_path('-hello')
def test__tree_path_to_id(self): tt = TreeTransform(StoreTree()) self.assertEqual('e-.', tt._tree_path_to_id('.')) self.assertEqual('e-.', tt._tree_path_to_id('foo/..')) with self.assertRaisesRegexp(ValueError, 'Path outside tree.'): tt._tree_path_to_id('..') with self.assertRaisesRegexp(ValueError, 'Path outside tree.'): tt._tree_path_to_id('foo/../..')
def test_generate_renames(self): store_tree = StoreTree() tt = TreeTransform(store_tree, write=False) with tt: file1 = tt.acquire_existing_id('file1') dir1 = tt._tree_path_to_id('dir1') file1_path = tt._new_contents.full_path(file1) tt.set_name_info(file1, dir1, 'file2') self.assertEqual([('file1', file1_path), (file1_path, 'dir1/file2')], tt.generate_renames())
def test_get_final_path(self): store_tree = StoreTree() tt = TreeTransform(store_tree, write=False) with self.assertRaises(NotPending): tt.get_final_path('file1') with tt: file1 = tt.acquire_existing_id('file1') dir1 = tt._tree_path_to_id('dir1') self.assertEqual(tt.get_final_path(file1), 'file1') tt.set_name_info(file1, dir1, 'file2') self.assertEqual(tt.get_final_path(file1), 'dir1/file2')
def test_get_parent(self): store_tree = StoreTree() tt = TreeTransform(store_tree, write=False) with self.assertRaises(NotPending): parent = tt.get_parent('e-file1') with tt: file1 = tt.acquire_existing_id('file1') parent = tt.get_parent(file1) self.assertEqual(parent, tt._tree_path_to_id('.')) root = tt.acquire_existing_id('.') with self.assertRaises(KeyError): parent = tt.get_parent(root)
def test_get_name(self): store_tree = StoreTree() tt = TreeTransform(store_tree, write=False) with self.assertRaises(NotPending): tt.get_name('file1') with tt: file1 = tt.acquire_existing_id('file1') name = tt.get_name(file1) self.assertEqual(name, 'file1') root = tt.acquire_existing_id('.') with self.assertRaises(KeyError): tt.get_name(root)
def test_generate_renames_dir_swap(self): store_tree = StoreTree() tt = TreeTransform(store_tree, write=False) with tt: dir1 = tt._tree_path_to_id('dir1') dir2 = tt._tree_path_to_id('dir1/dir2') root = tt._tree_path_to_id('.') tt.set_name_info(dir1, dir2, 'dir1') tt.set_name_info(dir2, root, 'dir2') dir1_path = tt._new_contents.full_path(dir1) dir2_path = tt._new_contents.full_path(dir2) self.assertEqual([('dir1/dir2', dir2_path), ('dir1', dir1_path), (dir2_path, 'dir2'), (dir1_path, 'dir2/dir1')], tt.generate_renames())
def test_with(self): store_tree = StoreTree() store_tree.write_content('file1', 0o600, [b'hello']) store_tree.mkdir('dir1', 0o700) tt = TreeTransform(store_tree) self.assertIs(InactiveTransform, type(tt._name_info)) self.assertIs(InactiveTransform, type(tt.id_counter)) self.assertIs(None, tt._new_contents) with tt: self.assertEqual({}, tt._name_info) file1 = tt.acquire_existing_id('file1') dir1 = tt._tree_path_to_id('dir1') self.assertEqual(tt.get_final_path(file1), 'file1') tt.set_name_info(file1, dir1, 'file2') self.assertIs(InactiveTransform, type(tt._name_info)) self.assertIs(InactiveTransform, type(tt.id_counter)) self.assertEqual(b'hello', b''.join(store_tree.read_content('dir1/file2')))
def test_with_exception(self): store_tree = StoreTree() store_tree.write_content('file1', 0o600, [b'hello']) class SentryException(Exception): pass with self.assertRaises(SentryException): with TreeTransform(store_tree) as tt: file1 = tt.acquire_existing_id('file1') dir1 = tt._tree_path_to_id('dir1') tt.set_name_info(file1, dir1, 'file2') raise SentryException with self.assertRaises(NoSuchFile): store_tree.read_content('dir1/file2')
def actual_tree(self, tree): return StoreTree(file_store=OverlayFileStore(tree.readonly_version()))
def test_get_existing_id(self): store_tree = StoreTree() tt = TreeTransform(store_tree, write=False) with tt: self.assertEqual('e-file1', tt.acquire_existing_id('file1'))
def setup_tree(self): base = StoreTree().readonly_version() yield StoreTree(file_store=OverlayFileStore(base))
def test_delete(self): store_tree = StoreTree() store_tree.write_content('foo', 0o600, [b'hello']) store_tree.mkdir('bar', 0o700) tt = TreeTransform(store_tree) with self.assertRaises(NotPending): tt.delete('e-foo') with tt: tt.delete(tt.acquire_existing_id('foo')) tt.delete(tt.acquire_existing_id('bar')) store_tree.read_content('foo') with self.assertRaises(IsDirectory): store_tree.read_content('bar') with self.assertRaises(NotPending): tt.delete('e-foo') with self.assertRaises(NoSuchFile): store_tree.read_content('foo') with self.assertRaises(NoSuchFile): store_tree.read_content('bar')
def setup_tree(self): yield StoreTree()