def test_simple2(self): self.maxDiff = None text = ''' fruits apple berries blueberries strawberries melons watermelon cheeses parmessan asiago ''' expected = node(TDATA('root', 0)) expected.ensure_path([TDATA('fruits', 2), TDATA('apple', 3)]) expected.ensure_path( [TDATA('fruits', 2), TDATA('berries', 4), TDATA('blueberries', 5)]) expected.ensure_path([ TDATA('fruits', 2), TDATA('berries', 4), TDATA('strawberries', 6) ]) expected.ensure_path( [TDATA('fruits', 2), TDATA('melons', 7), TDATA('watermelon', 8)]) expected.ensure_path([TDATA('cheeses', 9), TDATA('parmessan', 10)]) expected.ensure_path([TDATA('cheeses', 9), TDATA('asiago', 11)]) self.assertMultiLineEqual( expected.to_string(data_func=self._data_func), self._parse(text))
def test_indent_tabs(self): text = ''' fruits \t\tapple \t\tberries \t\t\tblueberries \t\t\tstrawberries \t\tmelons \t\t\twatermelon cheeses \t\tparmessan \t\tasiago ''' expected = node(TDATA('root', 0)) expected.ensure_path([TDATA('fruits', 2), TDATA('apple', 3)]) expected.ensure_path( [TDATA('fruits', 2), TDATA('berries', 4), TDATA('blueberries', 5)]) expected.ensure_path([ TDATA('fruits', 2), TDATA('berries', 4), TDATA('strawberries', 6) ]) expected.ensure_path( [TDATA('fruits', 2), TDATA('melons', 7), TDATA('watermelon', 8)]) expected.ensure_path([TDATA('cheeses', 9), TDATA('parmessan', 10)]) expected.ensure_path([TDATA('cheeses', 9), TDATA('asiago', 11)]) self.assertMultiLineEqual(str(expected), self._parse(text))
def test_to_string(self): n = node('root') n.add_child('fruits') fruits = n.find_child_by_data('fruits') fruits.add_child('kiwi') fruits.add_child('strawberry') fruits.add_child('melons') melons = fruits.find_child_by_data('melons') melons.add_child('watermelon') melons.add_child('canteloupe') n.add_child('cheeses') cheeses = n.find_child_by_data('cheeses') cheeses.add_child('gouda') cheeses.add_child('brie') expected='''root fruits kiwi strawberry melons watermelon canteloupe cheeses gouda brie''' self.assertMultiLineEqual( expected, str(n) )
def test_flat_paths(self): r = node('root') r.ensure_path([ 'cheese', 'blue' ]) r.ensure_path([ 'cheese', 'brie' ]) r.ensure_path([ 'cheese', 'gouda' ]) r.ensure_path([ 'foo' ]) r.ensure_path([ 'fruit', 'apple' ]) r.ensure_path([ 'fruit', 'kiwi' ]) r.ensure_path([ 'fruit', 'melon', 'canteloupe' ]) r.ensure_path([ 'fruit', 'melon', 'watermelon' ]) r.ensure_path([ 'wine', 'chianti' ]) r.ensure_path([ 'wine', 'sancere' ]) paths = sorted(r.flat_paths()) self.assertEqual( [ [ 'root', 'cheese', 'blue' ], [ 'root', 'cheese', 'brie' ], [ 'root', 'cheese', 'gouda' ], [ 'root', 'foo' ], [ 'root', 'fruit', 'apple' ], [ 'root', 'fruit', 'kiwi' ], [ 'root', 'fruit', 'melon', 'canteloupe' ], [ 'root', 'fruit', 'melon', 'watermelon' ], [ 'root', 'wine', 'chianti' ], [ 'root', 'wine', 'sancere' ], ], [ x.path for x in paths ] )
def test_simple1(self): self.maxDiff = None text = ''' fruits apple kiwi ''' expected = node(TDATA('root', 0)) expected.ensure_path([TDATA('fruits', 2), TDATA('apple', 3)]) expected.ensure_path([TDATA('fruits', 2), TDATA('kiwi', 4)]) self.assertMultiLineEqual( expected.to_string(data_func=self._data_func), self._parse(text))
def test_strip_quote_in_comments(self): self.maxDiff = None text = ''' fruits apple kiwi ### fprintf(stderr, "Usage: cut.exe args\\n"); ''' expected = node(TDATA('root', 0)) expected.ensure_path([TDATA('fruits', 2), TDATA('apple', 3)]) expected.ensure_path([TDATA('fruits', 2), TDATA('kiwi', 4)]) self.assertMultiLineEqual( expected.to_string(data_func=self._data_func), self._parse(text, strip_comments=True))
def test_strip_comments(self): self.maxDiff = None text = ''' # comment fruits # comment # comment apple # comment kiwi # comment ''' expected = node(TDATA('root', 0)) expected.ensure_path([TDATA('fruits', 3), TDATA('apple', 5)]) expected.ensure_path([TDATA('fruits', 3), TDATA('kiwi', 7)]) self.assertMultiLineEqual( expected.to_string(data_func=self._data_func), self._parse(text, strip_comments=True))
def list_dir(self, remote_dir, recursive, options): 'List entries in a directory.' check.check_string(remote_dir) check.check_bool(recursive) check.check_vfs_file_info_options(options, allow_none=True) remote_dir = vfs_path_util.normalize(remote_dir) options = options or vfs_file_info_options() result = node(self.SEP) local_dir_path = self._make_local_dir_path(remote_dir) self.log.log_d( 'list_dir: remote_dir={} recursive={} local_dir_path={}'.format( remote_dir, recursive, local_dir_path)) if not path.exists(local_dir_path): raise vfs_error('dir does not exist: {}'.format(remote_dir)) if not path.isdir(local_dir_path): raise vfs_error('not a dir: {}'.format(remote_dir)) max_depth = None if recursive else 1 setattr(result, '_remote_filename', self.SEP) setattr(result, '_local_filename', self._local_root_dir) setattr(result, '_is_file', False) num_added = 0 for root, dirs, files in file_find.walk_with_depth(local_dir_path, max_depth=max_depth, follow_links=True): if root == local_dir_path: rel = os.sep else: rel = file_util.ensure_lsep( file_util.remove_head(root, local_dir_path)) self.log.log_d( 'list_dir: next: root={} dirs={} files={} rel={}'.format( root, dirs, files, rel)) files_set = set(files) if not self._should_include_file(rel): self.log.log_d('list_dir: skipping {}'.format(rel)) continue for next_file_or_dir in sorted(files + dirs): if self._should_include_file(next_file_or_dir): self.log.log_d( 'list_dir: rel={} next_file_or_dir={}'.format( rel, next_file_or_dir)) local_filename_rel = path.join(rel, next_file_or_dir) remote_filename = local_filename_rel.replace( os.sep, self.SEP) self.log.log_d( 'list_dir: local_filename_rel={} remote_filename={}'. format(local_filename_rel, remote_filename)) assert local_filename_rel[0] == os.sep assert remote_filename[0] == self.SEP remote_filename = remote_filename[1:] local_filename = path.join(root, next_file_or_dir) parts = remote_filename.split('/') new_node = result.ensure_path(parts) setattr(new_node, '_remote_filename', remote_filename) setattr(new_node, '_local_filename', local_filename) setattr(new_node, '_is_file', next_file_or_dir in files_set) num_added += 1 else: self.log.log_d( 'list_dir: skipping {}'.format(next_file_or_dir)) if num_added == 0: return vfs_file_info_list() fs_tree = self._convert_node_to_fs_tree(result, 0, options) return fs_tree.children
def test___init__(self): n = node('root') self.assertEqual( 0, n.num_children() ) self.assertEqual( 'root', n.data ) self.assertEqual( None, n.find_child_by_data('notthere') )
def _make_tree(self, paths): n = node('root') for p in paths: n.ensure_path(p.split('/')) return n
def test_add_child(self): n = node('root') n.add_child('fruits') self.assertEqual( 1, n.num_children() ) self.assertEqual( node('fruits'), n.find_child_by_data('fruits') )