Skip to content

Commit

Permalink
Fs: Fix walk.
Browse files Browse the repository at this point in the history
  • Loading branch information
matan1008 committed Feb 13, 2022
1 parent 0b87329 commit b474e65
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 28 deletions.
12 changes: 4 additions & 8 deletions src/rpcclient/rpcclient/fs.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,15 +293,11 @@ def walk(self, top: str):
""" provides the same results as os.walk(top) """
dirs = []
files = []
for file in self.listdir(top):
filename = file.d_name
if filename in ('.', '..'):
continue
infos = self.stat(posixpath.join(top, filename))
if infos.st_mode & S_IFMT == infos.st_mode & S_IFDIR:
dirs.append(filename)
for entry in self.scandir(top):
if entry.is_dir():
dirs.append(entry.name)
else:
files.append(filename)
files.append(entry.name)

yield top, dirs, files

Expand Down
62 changes: 42 additions & 20 deletions src/rpcclient/tests/test_fs.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,26 +51,16 @@ def test_symlink(client, tmp_path):
assert os.readlink(symlink) == str(file)


def test_stat_sanity(client, tmp_path):
file = (tmp_path / 'temp.txt')
file.write_text('h' * 0x10000)
client_stat = client.fs.stat(file)
path_stat = file.stat()
assert path_stat.st_dev == client_stat.st_dev
assert path_stat.st_ino == client_stat.st_ino
assert path_stat.st_mode == client_stat.st_mode
assert path_stat.st_nlink == client_stat.st_nlink
assert path_stat.st_uid == client_stat.st_uid
assert path_stat.st_gid == client_stat.st_gid
assert path_stat.st_rdev == client_stat.st_rdev
assert path_stat.st_atime == client_stat.st_atime
assert path_stat.st_mtime == client_stat.st_mtime
assert path_stat.st_ctime == client_stat.st_ctime
assert path_stat.st_size == client_stat.st_size
assert path_stat.st_blocks == client_stat.st_blocks
assert path_stat.st_blksize == client_stat.st_blksize
assert path_stat.st_flags == client_stat.st_flags
assert path_stat.st_gen == client_stat.st_gen
def test_link(client, tmp_path):
(tmp_path / 'temp.txt').write_text('hello')
client.fs.link((tmp_path / 'temp.txt'), (tmp_path / 'temp1.txt'))
assert (tmp_path / 'temp1.txt').read_text() == 'hello'


def test_listdir(client, tmp_path):
assert not client.fs.listdir(tmp_path)
(tmp_path / 'temp.txt').touch()
assert client.fs.listdir(tmp_path) == ['temp.txt']


def test_scandir_sanity(client, tmp_path):
Expand Down Expand Up @@ -99,3 +89,35 @@ def test_scandir_context_manager(client, tmp_path):
assert entries[0].is_file()
assert not entries[0].is_dir()
assert not entries[0].is_symlink()


def test_stat_sanity(client, tmp_path):
file = (tmp_path / 'temp.txt')
file.write_text('h' * 0x10000)
client_stat = client.fs.stat(file)
path_stat = file.stat()
assert path_stat.st_dev == client_stat.st_dev
assert path_stat.st_ino == client_stat.st_ino
assert path_stat.st_mode == client_stat.st_mode
assert path_stat.st_nlink == client_stat.st_nlink
assert path_stat.st_uid == client_stat.st_uid
assert path_stat.st_gid == client_stat.st_gid
assert path_stat.st_rdev == client_stat.st_rdev
assert path_stat.st_atime == client_stat.st_atime
assert path_stat.st_mtime == client_stat.st_mtime
assert path_stat.st_ctime == client_stat.st_ctime
assert path_stat.st_size == client_stat.st_size
assert path_stat.st_blocks == client_stat.st_blocks
assert path_stat.st_blksize == client_stat.st_blksize
assert path_stat.st_flags == client_stat.st_flags
assert path_stat.st_gen == client_stat.st_gen


def test_walk(client, tmp_path):
(tmp_path / 'dir_a').mkdir()
(tmp_path / 'dir_a' / 'a1.txt').touch()
(tmp_path / 'dir_a' / 'a2.txt').touch()
(tmp_path / 'dir_b').mkdir()
(tmp_path / 'dir_b' / 'b1.txt').touch()
(tmp_path / 'dir_b' / 'b2.txt').touch()
assert list(os.walk(tmp_path)) == list(client.fs.walk(tmp_path))

0 comments on commit b474e65

Please sign in to comment.