-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathtest_cli.py
75 lines (50 loc) · 1.96 KB
/
test_cli.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import contextlib
import io
import pytest
from hdfs_native import Client
from hdfs_native.cli import main as cli_main
def test_cli(minidfs: str):
client = Client(minidfs)
# cat
with client.create("/testfile") as file:
file.write(b"1234")
buf = io.BytesIO()
with contextlib.redirect_stdout(io.TextIOWrapper(buf)):
cli_main(["cat", "/testfile"])
assert buf.getvalue() == b"1234"
with client.create("/testfile2") as file:
file.write(b"5678")
buf = io.BytesIO()
with contextlib.redirect_stdout(io.TextIOWrapper(buf)):
cli_main(["cat", "/testfile", "/testfile2"])
assert buf.getvalue() == b"12345678"
with pytest.raises(FileNotFoundError):
cli_main(["cat", "/nonexistent"])
client.delete("/testfile")
client.delete("/testfile2")
# mkdir
cli_main(["mkdir", "/testdir"])
assert client.get_file_info("/testdir").isdir
with pytest.raises(RuntimeError):
cli_main(["mkdir", "/testdir/nested/dir"])
cli_main(["mkdir", "-p", "/testdir/nested/dir"])
assert client.get_file_info("/testdir/nested/dir").isdir
client.delete("/testdir", True)
# mv
client.create("/testfile").close()
client.mkdirs("/testdir")
cli_main(["mv", "/testfile", "/testfile2"])
client.get_file_info("/testfile2")
with pytest.raises(ValueError):
cli_main(["mv", "/testfile2", "hdfs://badnameservice/testfile"])
with pytest.raises(RuntimeError):
cli_main(["mv", "/testfile2", "/nonexistent/testfile"])
cli_main(["mv", "/testfile2", "/testdir"])
client.get_file_info("/testdir/testfile2")
client.rename("/testdir/testfile2", "/testfile1")
client.create("/testfile2").close()
with pytest.raises(ValueError):
cli_main(["mv", "/testfile1", "/testfile2", "/testfile3"])
cli_main(["mv", "/testfile1", "/testfile2", "/testdir/"])
client.get_file_info("/testdir/testfile1")
client.get_file_info("/testdir/testfile2")