-
Notifications
You must be signed in to change notification settings - Fork 777
/
Copy pathtest_utils.py
95 lines (76 loc) · 3.15 KB
/
test_utils.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
from tests import unittest
import os
import tempfile
import shutil
from awsshell.utils import FSLayer
from awsshell.utils import InMemoryFSLayer
from awsshell.utils import FileReadError
from awsshell.utils import temporary_file
class TestFSLayer(unittest.TestCase):
# TestFSLayer provides abstractions over the OS.
# It is one of the only exceptions in the AWS Shell
# code where it's ok to test by using actual files.
# All other test code should use FSLayer.
def setUp(self):
self.tempdir = tempfile.mkdtemp()
self.temporary_filename = os.path.join(
self.tempdir, 'tempfilefoo')
self.fslayer = FSLayer()
def tearDown(self):
shutil.rmtree(self.tempdir)
def test_can_read_file_contents(self):
with open(self.temporary_filename, 'w') as f:
f.write('helloworld')
self.assertEqual(
self.fslayer.file_contents(self.temporary_filename),
'helloworld')
self.assertEqual(
self.fslayer.file_contents(self.temporary_filename, binary=True),
b'helloworld')
def test_file_exists(self):
self.assertFalse(self.fslayer.file_exists(self.temporary_filename))
with open(self.temporary_filename, 'w') as f:
pass
self.assertTrue(self.fslayer.file_exists(self.temporary_filename))
def test_file_does_not_exist_error(self):
with self.assertRaises(FileReadError):
self.fslayer.file_contents('/tmp/thisdoesnot-exist.asdf')
class TestInMemoryFSLayer(unittest.TestCase):
def setUp(self):
self.file_mapping = {}
self.fslayer = InMemoryFSLayer(self.file_mapping)
def test_file_exists(self):
self.file_mapping['/my/fake/path'] = 'file contents'
self.assertTrue(self.fslayer.file_exists('/my/fake/path'))
def test_can_read_file_contents(self):
self.file_mapping['/myfile'] = 'helloworld'
self.assertEqual(self.fslayer.file_contents('/myfile'), 'helloworld')
self.assertEqual(self.fslayer.file_contents('/myfile', binary=True),
b'helloworld')
def test_file_does_not_exist_error(self):
with self.assertRaises(FileReadError):
self.fslayer.file_contents('/tmp/thisdoesnot-exist.asdf')
class TestTemporaryFile(unittest.TestCase):
def test_can_use_as_context_manager(self):
with temporary_file('w') as f:
filename = f.name
f.write("foobar")
f.flush()
self.assertEqual(open(filename).read(), "foobar")
def test_is_removed_after_exiting_context(self):
with temporary_file('w') as f:
filename = f.name
f.write("foobar")
f.flush()
self.assertFalse(os.path.isfile(filename))
def test_can_open_in_read(self):
with temporary_file('r') as f:
filename = f.name
assert f.read() == ''
# Verify we can open the file again
# in another file descriptor.
with open(filename, 'w') as f2:
f2.write("foobar")
f.seek(0)
assert f.read() == "foobar"
self.assertFalse(os.path.isfile(filename))