forked from shunfan/porter
/
test.py
171 lines (142 loc) · 4.68 KB
/
test.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#coding=utf-8
from __future__ import with_statement
import os
from porter import mkdir, remove, copy, copy_to, move, \
move_to, archive, archive_to, FileExistsError, \
FileNotFoundError, FileTypeError
from pytest import raises
test_porter = 'test_porter'
dir_mkdir = os.path.join(test_porter, 'dir_mkdir')
dir1 = os.path.join(test_porter, 'dir1')
dir1_tar = os.path.join(test_porter, 'dir1.tar')
dir1_archive_tar = os.path.join(test_porter, 'archive.tar')
dir2 = os.path.join(test_porter, 'dir2')
dir2_dir1_tar = os.path.join(test_porter, 'dir2', 'dir1.tar')
dir2_archive_tar = os.path.join(test_porter, 'dir2', 'archive.tar')
dir2_dir1 = os.path.join(test_porter, 'dir2', 'dir1')
dir1_f1 = os.path.join(test_porter, 'dir1', 'f1.txt')
dir2_f1 = os.path.join(test_porter, 'dir2', 'f1.txt')
dir2_f2 = os.path.join(test_porter, 'dir2', 'f2.txt')
def create(file_path):
with open(file_path, 'w') as f:
f.write('test file')
def init():
mkdir(test_porter, force=True)
mkdir(dir1)
mkdir(dir2)
create(dir1_f1)
create(dir2_f2)
def test_mkdir():
init()
mkdir(dir_mkdir)
mkdir(dir_mkdir, ignore=True)
mkdir(dir_mkdir, force=True)
assert os.path.exists(dir_mkdir) == True
class TestRemove:
def test_file(self):
init()
remove(dir1_f1)
assert os.path.exists(dir1_f1) == False
def test_directory(self):
init()
remove(dir1)
assert os.path.exists(dir1) == False
class TestCopy:
def test_file(self):
init()
copy(dir1_f1, dir2_f1)
copy(dir1_f1, dir2_f1, ignore=True)
copy(dir1_f1, dir2_f1, force=True)
assert os.path.exists(dir1_f1) == True
assert os.path.exists(dir2_f1) == True
def test_directory(self):
init()
copy(dir1, dir2_dir1)
copy(dir1, dir2_dir1, ignore=True)
copy(dir1, dir2_dir1, force=True)
assert os.path.exists(dir1) == True
assert os.path.exists(dir2_dir1) == True
class TestCopyTo:
def test_file(self):
init()
copy_to(dir1_f1, dir2)
copy_to(dir1_f1, dir2, ignore=True)
copy_to(dir1_f1, dir2, force=True)
assert os.path.exists(dir1_f1) == True
assert os.path.exists(dir2_f1) == True
def test_directory(self):
init()
copy_to(dir1, dir2)
copy_to(dir1, dir2, ignore=True)
copy_to(dir1, dir2, force=True)
assert os.path.exists(dir1) == True
assert os.path.exists(dir2_dir1) == True
class TestMove:
def test_file(self):
init()
move(dir1_f1, dir2_f1)
move(dir1_f1, dir2_f1, ignore=True)
create(dir1_f1)
move(dir1_f1, dir2_f1, force=True)
assert os.path.exists(dir1_f1) == False
assert os.path.exists(dir2_f1) == True
def test_directory(self):
init()
move(dir1, dir2_dir1)
move(dir1, dir2_dir1, ignore=True)
mkdir(dir1)
move(dir1, dir2_dir1, force=True)
assert os.path.exists(dir1) == False
assert os.path.exists(dir2_dir1) == True
class TestMoveTo:
def test_file(self):
init()
move_to(dir1_f1, dir2)
move_to(dir1_f1, dir2, ignore=True)
create(dir1_f1)
move_to(dir1_f1, dir2, force=True)
assert os.path.exists(dir1_f1) == False
assert os.path.exists(dir2_f1) == True
def test_directory(self):
init()
move_to(dir1, dir2)
move_to(dir1, dir2, ignore=True)
mkdir(dir1)
move_to(dir1, dir2, force=True)
assert os.path.exists(dir1) == False
assert os.path.exists(dir2_dir1) == True
class TestArchive:
def test_directory(self):
init()
archive(dir1)
archive(dir1, 'archive')
assert os.path.exists(dir1) == True
assert os.path.exists(dir1_tar) == True
assert os.path.exists(dir1_archive_tar) == True
class TestArchiveTo:
def test_directory(self):
init()
archive_to(dir1, dir2)
archive_to(dir1, dir2, 'archive')
assert os.path.exists(dir1) == True
assert os.path.exists(dir2_dir1_tar) == True
assert os.path.exists(dir2_archive_tar) == True
class TestError:
def test_FileExistsError(self):
init()
with raises(FileExistsError):
mkdir(test_porter)
init()
with raises(FileExistsError):
copy(dir1_f1, dir2_f2)
init()
with raises(FileExistsError):
copy(dir1, dir2)
def test_FileNotFoundError(self):
init()
with raises(FileNotFoundError):
move('none', 'none')
def test_FileTypeError(self):
init()
with raises(FileTypeError):
archive(dir1_f1)