29
41
BaseTwistedTestCase.setUp(self)
30
42
self.basedir = self.mktemp('test_root')
43
self.testfile = os.path.join(self.basedir, "test_file")
44
open(self.testfile, 'w').close()
32
46
def tearDown(self):
33
47
self.rmtree(self.basedir)
34
48
BaseTwistedTestCase.tearDown(self)
36
def test_set_readonly(self):
37
"""Linux specific test for set_readonly."""
38
set_readonly(self.basedir)
50
def test_set_dir_readonly(self):
51
"""Test for set_dir_readonly."""
52
set_dir_readonly(self.basedir)
39
53
self.assertRaises(OSError, os.mkdir, os.path.join(self.basedir, 'foo'))
41
def test_set_readwrite(self):
42
"""Linux specific test for set_readwrite."""
43
set_readonly(self.basedir)
55
def test_set_dir_readwrite(self):
56
"""Test for set_dir_readwrite."""
57
set_dir_readonly(self.basedir)
44
58
self.assertRaises(OSError, os.mkdir, os.path.join(self.basedir, 'foo'))
45
set_readwrite(self.basedir)
59
set_dir_readwrite(self.basedir)
46
60
os.mkdir(os.path.join(self.basedir, 'foo'))
48
62
def test_allow_writes(self):
49
"""Linux specific tests for allow_writes."""
50
set_readonly(self.basedir)
63
"""Test for allow_writes."""
64
set_dir_readonly(self.basedir)
51
65
self.assertRaises(OSError, os.mkdir, os.path.join(self.basedir, 'foo'))
52
66
with allow_writes(self.basedir):
53
67
os.mkdir(os.path.join(self.basedir, 'foo'))
69
def test_set_file_readonly(self):
70
"""Test for set_file_readonly."""
71
set_file_readonly(self.testfile)
72
self.assertRaises(IOError, open, self.testfile, 'w')
74
def test_set_file_readwrite(self):
75
"""Test for set_file_readwrite."""
76
set_file_readonly(self.testfile)
77
self.assertRaises(IOError, open, self.testfile, 'w')
78
set_file_readwrite(self.testfile)
79
open(self.testfile, 'w')
81
def test_path_file_exist_yes(self):
82
"""Test that the file exists."""
83
self.assertTrue(path_exists(self.testfile))
85
def test_path_file_exist_no(self):
86
"""Test that the file doesn't exist."""
87
os.remove(self.testfile)
88
self.assertFalse(path_exists(self.testfile))
90
def test_path_dir_exist_yes(self):
91
"""Test that the dir exists."""
92
self.assertTrue(path_exists(self.basedir))
94
def test_path_dir_exist_no(self):
95
"""Test that the dir doesn't exist."""
96
os.remove(self.testfile)
97
self.assertFalse(path_exists(os.path.join(self.basedir, 'nodir')))
99
def test_remove_file(self):
100
"""Test the remove file."""
101
remove_file(self.testfile)
102
self.assertFalse(os.path.exists(self.testfile))
104
def test_remove_dir(self):
105
"""Test the remove dir."""
106
testdir = os.path.join(self.basedir, 'foodir')
108
assert os.path.exists(testdir)
110
self.assertFalse(path_exists(testdir))
112
def test_make_dir_one(self):
113
"""Test the make dir with one dir."""
114
testdir = os.path.join(self.basedir, 'foodir')
115
assert not os.path.exists(testdir)
117
self.assertTrue(os.path.exists(testdir))
119
def test_make_dir_already_there(self):
120
"""Test the make dir with one dir that exists."""
121
self.assertRaises(OSError, make_dir, self.basedir)
123
def test_make_dir_recursive_no(self):
124
"""Test the make dir with some dirs, not recursive explicit."""
125
testdir = os.path.join(self.basedir, 'foo', 'bar')
126
assert not os.path.exists(testdir)
127
self.assertRaises(OSError, make_dir, testdir)
129
def test_make_dir_recursive_yes(self):
130
"""Test the make dir with some dirs, recursive."""
131
testdir = os.path.join(self.basedir, 'foo', 'bar')
132
assert not os.path.exists(testdir)
133
make_dir(testdir, recursive=True)
134
self.assertTrue(os.path.exists(testdir))
136
def test_open_file_not_there(self):
137
"""Open a file that does not exist."""
138
self.assertRaises(IOError, open_file, os.path.join(self.basedir, 'no'))
140
def test_open_file_gets_a_fileobject(self):
141
"""Open a file, and get a file object."""
142
testfile = os.path.join(self.basedir, 'testfile')
143
open(testfile, 'w').close()
144
f = open_file(testfile)
145
self.assertTrue(isinstance(f, file))
147
def test_open_file_read(self):
148
"""Open a file, and read."""
149
testfile = os.path.join(self.basedir, 'testfile')
150
with open(testfile, 'w') as fh:
152
f = open_file(testfile, 'r')
153
self.assertTrue(f.read(), "foo")
155
def test_open_file_write(self):
156
"""Open a file, and write."""
157
testfile = os.path.join(self.basedir, 'testfile')
158
with open_file(testfile, 'w') as fh:
161
self.assertTrue(f.read(), "foo")
163
def test_rename_not_there(self):
164
"""Rename something that does not exist."""
165
self.assertRaises(OSError, rename,
166
os.path.join(self.basedir, 'no'), 'foo')
168
def test_rename_file(self):
170
testfile1 = os.path.join(self.basedir, 'testfile1')
171
testfile2 = os.path.join(self.basedir, 'testfile2')
172
open(testfile1, 'w').close()
173
rename(testfile1, testfile2)
174
self.assertFalse(os.path.exists(testfile1))
175
self.assertTrue(os.path.exists(testfile2))
177
def test_rename_dir(self):
179
testdir1 = os.path.join(self.basedir, 'testdir1')
180
testdir2 = os.path.join(self.basedir, 'testdir2')
182
rename(testdir1, testdir2)
183
self.assertFalse(os.path.exists(testdir1))
184
self.assertTrue(os.path.exists(testdir2))