1# Copyright (C) 2012, 2016 Canonical Ltd 2# 3# This program is free software; you can redistribute it and/or modify 4# it under the terms of the GNU General Public License as published by 5# the Free Software Foundation; either version 2 of the License, or 6# (at your option) any later version. 7# 8# This program is distributed in the hope that it will be useful, 9# but WITHOUT ANY WARRANTY; without even the implied warranty of 10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11# GNU General Public License for more details. 12# 13# You should have received a copy of the GNU General Public License 14# along with this program; if not, write to the Free Software 15# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 16 17from breezy import ( 18 errors, 19 tests, 20 ) 21from breezy.tests.per_tree import TestCaseWithTree 22 23 24class Path2IdTests(TestCaseWithTree): 25 26 def setUp(self): 27 super(Path2IdTests, self).setUp() 28 work_a = self.make_branch_and_tree('wta') 29 if not work_a.supports_setting_file_ids(): 30 self.skipTest("working tree does not support setting file ids") 31 self.build_tree(['wta/bla', 'wta/dir/', 'wta/dir/file']) 32 work_a.add(['bla', 'dir', 'dir/file'], 33 [b'bla-id', b'dir-id', b'file-id']) 34 work_a.commit('add files') 35 self.tree_a = self.workingtree_to_test_tree(work_a) 36 37 def test_path2id(self): 38 self.assertEqual(b'bla-id', self.tree_a.path2id('bla')) 39 self.assertEqual(b'dir-id', self.tree_a.path2id('dir')) 40 self.assertIs(None, self.tree_a.path2id('idontexist')) 41 42 def test_path2id_list(self): 43 self.assertEqual(b'bla-id', self.tree_a.path2id(['bla'])) 44 self.assertEqual(b'dir-id', self.tree_a.path2id(['dir'])) 45 self.assertEqual(b'file-id', self.tree_a.path2id(['dir', 'file'])) 46 self.assertEqual(self.tree_a.path2id(''), 47 self.tree_a.path2id([])) 48 self.assertIs(None, self.tree_a.path2id(['idontexist'])) 49 self.assertIs(None, self.tree_a.path2id(['dir', 'idontexist'])) 50 51 def test_id2path(self): 52 self.addCleanup(self.tree_a.lock_read().unlock) 53 self.assertEqual('bla', self.tree_a.id2path(b'bla-id')) 54 self.assertEqual('dir', self.tree_a.id2path(b'dir-id')) 55 self.assertEqual('dir/file', self.tree_a.id2path(b'file-id')) 56 self.assertRaises(errors.NoSuchId, self.tree_a.id2path, b'nonexistant') 57 58 def skip_if_no_reference(self, tree): 59 if not getattr(tree, 'supports_tree_reference', lambda: False)(): 60 raise tests.TestNotApplicable('Tree references not supported') 61 62 def create_nested(self): 63 work_tree = self.make_branch_and_tree('wt') 64 with work_tree.lock_write(): 65 self.skip_if_no_reference(work_tree) 66 subtree = self.make_branch_and_tree('wt/subtree') 67 self.build_tree(['wt/subtree/a']) 68 subtree.add(['a']) 69 subtree.commit('foo') 70 work_tree.add_reference(subtree) 71 tree = self._convert_tree(work_tree) 72 self.skip_if_no_reference(tree) 73 return tree, subtree 74 75 def test_path2id_nested_tree(self): 76 tree, subtree = self.create_nested() 77 self.assertIsNot(None, tree.path2id('subtree')) 78 self.assertIsNot(None, tree.path2id('subtree/a')) 79 self.assertEqual('subtree', tree.id2path(tree.path2id('subtree'))) 80 self.assertEqual('subtree/a', tree.id2path(tree.path2id('subtree/a'))) 81 self.assertIsNot('subtree/a', tree.id2path(tree.path2id('subtree/a'), recurse='down')) 82 self.assertRaises(errors.NoSuchId, tree.id2path, tree.path2id('subtree/a'), recurse='none') 83 84 85class Path2IdsTests(TestCaseWithTree): 86 87 def test_paths2ids_recursive(self): 88 work_tree = self.make_branch_and_tree('tree') 89 self.build_tree(['tree/dir/', 'tree/dir/file']) 90 work_tree.add(['dir', 'dir/file']) 91 if not work_tree.supports_setting_file_ids(): 92 raise tests.TestNotApplicable( 93 "test not applicable on non-inventory tests") 94 tree = self._convert_tree(work_tree) 95 tree.lock_read() 96 self.addCleanup(tree.unlock) 97 self.assertEqual({tree.path2id('dir'), tree.path2id('dir/file')}, 98 tree.paths2ids(['dir'])) 99 100 def test_paths2ids_forget_old(self): 101 work_tree = self.make_branch_and_tree('tree') 102 self.build_tree(['tree/file']) 103 work_tree.add('file') 104 work_tree.commit('commit old state') 105 work_tree.remove('file') 106 if not work_tree.supports_setting_file_ids(): 107 raise tests.TestNotApplicable( 108 "test not applicable on non-inventory tests") 109 tree = self._convert_tree(work_tree) 110 tree.lock_read() 111 self.addCleanup(tree.unlock) 112 self.assertEqual(set([]), tree.paths2ids(['file'], 113 require_versioned=False)) 114