-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
180 lines (153 loc) · 6.09 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
172
173
174
175
176
177
178
179
180
import unittest, tempfile, os, contextlib, io
import mvi
class TestCase(unittest.TestCase):
def setUp(self):
ctx = tempfile.TemporaryDirectory()
path = ctx.__enter__()
self.addCleanup(ctx.__exit__, None, None, None)
oldwd = os.getcwd()
os.chdir(path)
self.addCleanup(os.chdir, oldwd)
def assertFileEquals(self, path, contents):
with open(path) as f:
self.assertEqual(f.read(), contents)
@contextlib.contextmanager
def assertStdout(self, output):
with contextlib.redirect_stdout(io.StringIO()) as f:
yield
self.assertEqual(f.getvalue(), output)
class display_and_verify(TestCase):
def test(self):
sources = ['a', 'b', 'c']
destinations = ['a', 'newb', 'c']
with self.assertStdout('Line 2: b -> newb\n'):
mvi.display_and_verify(sources, destinations)
def test_too_short(self):
sources = ['a', 'b', 'c']
destinations = ['a', 'newb']
with (self.assertStdout('Line 2: b -> newb\nLine 3: c -> ?\n'),
self.assertRaisesRegex(ValueError, 'list of destination names is too short')):
mvi.display_and_verify(sources, destinations)
def test_too_long(self):
sources = ['a', 'b', 'c']
destinations = ['a', 'newb', 'c', 'd']
with (self.assertStdout('Line 2: b -> newb\nLine 4: ? -> d\n'),
self.assertRaisesRegex(ValueError, 'list of destination names is too long')):
mvi.display_and_verify(sources, destinations)
def test_empty(self):
sources = ['a', 'b', 'c']
destinations = ['a', 'newb', '']
with (self.assertStdout('Line 2: b -> newb\nLine 3: c -> \n'),
self.assertRaisesRegex(ValueError, 'filenames must be at least one character long')):
mvi.display_and_verify(sources, destinations)
def test_collision(self):
sources = ['a', 'b', 'c']
destinations = ['x', 'b', 'x']
with (self.assertStdout('Line 1: a -> x\nLine 3: c -> x\n'),
self.assertRaisesRegex(ValueError, 'both a and c want to move to x')):
mvi.display_and_verify(sources, destinations)
def test_cycle(self):
sources = ['a', 'b', 'c']
destinations = ['b', 'c', 'a']
with self.assertStdout('Line 1: a -> b\nLine 2: b -> c\nLine 3: c -> a\n'):
mvi.display_and_verify(sources, destinations)
def test_exists(self):
with open('newb', 'w') as f:
f.write('foo')
sources = ['a', 'b', 'c']
destinations = ['a', 'newb', 'c']
with (self.assertStdout('Line 2: b -> newb\n'),
self.assertRaisesRegex(ValueError, 'destination exists: newb')):
mvi.display_and_verify(sources, destinations)
def test_not_exists(self):
with open('b', 'w') as f:
f.write('foo')
with open('c', 'w') as f:
f.write('bar')
sources = ['a', 'b', 'c']
destinations = ['b', 'c', 'newc']
with self.assertStdout('Line 1: a -> b\nLine 2: b -> c\nLine 3: c -> newc\n'):
mvi.display_and_verify(sources, destinations)
class move(TestCase):
def test_move_file(self):
with open('a', 'w') as f:
f.write('foo')
mvi.move('a', 'b')
self.assertEqual(os.listdir(), ['b'])
self.assertFileEquals('b', 'foo')
def test_move_to_dir(self):
with open('a', 'w') as f:
f.write('foo')
mvi.move('a', 'dir/a')
self.assertFileEquals('dir/a', 'foo')
def test_exists(self):
with open('a', 'w') as f:
f.write('foo')
with open('b', 'w') as f:
f.write('bar')
with self.assertRaises(FileExistsError):
mvi.move('a', 'b')
class move_all(TestCase):
def test_move_files(self):
with open('a', 'w') as f:
f.write('foo')
with open('b', 'w') as f:
f.write('bar')
sources = ['a', 'b']
destinations = ['newa', 'newb']
mvi.move_all(sources, destinations)
self.assertEqual(sources, destinations)
self.assertEqual(set(os.listdir()), {'newa', 'newb'})
self.assertFileEquals('newa', 'foo')
self.assertFileEquals('newb', 'bar')
def test_move_to_dir(self):
with open('a', 'w') as f:
f.write('foo')
with open('b', 'w') as f:
f.write('bar')
sources = ['a', 'b']
destinations = ['dira/a', 'dirb/b']
mvi.move_all(sources, destinations)
self.assertEqual(sources, destinations)
self.assertEqual(set(os.listdir()), {'dira', 'dirb'})
self.assertFileEquals('dira/a', 'foo')
self.assertFileEquals('dirb/b', 'bar')
def test_exists(self):
with open('a', 'w') as f:
f.write('foo')
with open('b', 'w') as f:
f.write('bar')
sources = ['a']
destinations = ['b']
with self.assertRaises(FileExistsError):
mvi.move_all(sources, destinations)
def test_swap(self):
with open('a', 'w') as f:
f.write('foo')
with open('b', 'w') as f:
f.write('bar')
with open('c', 'w') as f:
f.write('baz')
sources = ['a', 'b', 'c']
destinations = ['b', 'c', 'a']
mvi.move_all(sources, destinations)
self.assertEqual(sources, destinations)
self.assertEqual(set(os.listdir()), {'a', 'b', 'c'})
self.assertFileEquals('a', 'baz')
self.assertFileEquals('b', 'foo')
self.assertFileEquals('c', 'bar')
def test_swap_and_rename(self):
with open('a', 'w') as f:
f.write('foo')
with open('b', 'w') as f:
f.write('bar')
with open('c', 'w') as f:
f.write('baz')
sources = ['a', 'b', 'c']
destinations = ['c', 'newb', 'a']
mvi.move_all(sources, destinations)
self.assertEqual(sources, destinations)
self.assertEqual(set(os.listdir()), {'a', 'newb', 'c'})
self.assertFileEquals('a', 'baz')
self.assertFileEquals('newb', 'bar')
self.assertFileEquals('c', 'foo')