|
1 | 1 | # -*- coding: utf-8 -*- |
2 | 2 | # |
3 | | -# Unit test for the sketchcreator module. |
| 3 | +# Unit test for the Sketch Creator module module. |
4 | 4 | # |
5 | | -# Copyright (c) 2015 carlosperate https://github.com/carlosperate/ |
| 5 | +# Copyright (c) 2017 carlosperate https://github.com/carlosperate/ |
6 | 6 | # Licensed under the Apache License, Version 2.0 (the "License"): |
7 | 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
8 | 8 | # |
|
27 | 27 |
|
28 | 28 | class SketchCreatorTestCase(unittest.TestCase): |
29 | 29 | """ |
30 | | - Tests for SketchCreator class |
| 30 | + Tests for SketchCreator class. |
| 31 | + Rather than mocking around with os module it creates 'safe' folder inside |
| 32 | + the test directory where it can create and delete files. |
31 | 33 | """ |
32 | 34 |
|
33 | 35 | # |
34 | | - # File creation without input code |
| 36 | + # Test fixtures |
35 | 37 | # |
36 | | - def test_create_sketch(self): |
37 | | - """ Tests to see if an Arduino Sketch is created in a new location. """ |
38 | | - # First test with the default name |
39 | | - sketch_dir = os.getcwd() |
40 | | - final_ino_path = os.path.join( |
41 | | - sketch_dir, |
42 | | - sketchcreator.default_sketch_name, |
| 38 | + @classmethod |
| 39 | + def setUpClass(cls): |
| 40 | + """Create a temporary folder to play round.""" |
| 41 | + cls.temp_folder = os.path.join( |
| 42 | + os.path.abspath(os.path.dirname(__file__)), |
| 43 | + 'TestTemp_sketchcreator') |
| 44 | + if os.path.isdir(cls.temp_folder): |
| 45 | + raise Exception('Directory %s already exists.' % cls.temp_folder) |
| 46 | + os.makedirs(cls.temp_folder) |
| 47 | + # Put together the default sketch path |
| 48 | + cls.default_sketch_path = os.path.join( |
| 49 | + cls.temp_folder, sketchcreator.default_sketch_name, |
43 | 50 | '%s.ino' % sketchcreator.default_sketch_name) |
44 | | - # Should be safe to create and delete the ino file in the test folder |
45 | | - if os.path.exists(final_ino_path): |
46 | | - os.remove(final_ino_path) |
47 | | - self.assertFalse(os.path.isfile(final_ino_path)) |
48 | | - # Checks the file is saved, and saved to the right location |
49 | | - created_sketch_path = sketchcreator.create_sketch(sketch_dir) |
50 | | - self.assertEqual(final_ino_path, created_sketch_path) |
51 | | - self.assertTrue(os.path.isfile(final_ino_path)) |
52 | 51 |
|
53 | | - # Now test with a given sketch name and a unicode path |
54 | | - sketch_name = 'TestTemp_Sketch' |
55 | | - sketch_dir_unicode = os.path.join(os.getcwd(), 'TestTemp_ろΓαζςÂé') |
56 | | - final_ino_path = os.path.join(sketch_dir_unicode, |
57 | | - sketch_name, |
58 | | - sketch_name + '.ino') |
59 | | - |
60 | | - # Test directory should be a safe place to create this unicode folder |
61 | | - if not os.path.exists(sketch_dir_unicode): |
62 | | - os.mkdir(sketch_dir_unicode) |
63 | | - else: |
64 | | - # We count on this specific folder name to be too unlikely |
65 | | - # created by a user and not this unit test, so remove contents |
66 | | - shutil.rmtree(sketch_dir_unicode) |
67 | | - os.mkdir(sketch_dir_unicode) |
68 | | - |
69 | | - self.assertFalse(os.path.isfile(final_ino_path)) |
70 | | - |
71 | | - # Checks the file is saved, and saved to the right location |
| 52 | + @classmethod |
| 53 | + def tearDownClass(cls): |
| 54 | + """Deletes the previously created temporary folder.""" |
| 55 | + if os.path.isdir(cls.temp_folder): |
| 56 | + shutil.rmtree(cls.temp_folder) |
| 57 | + |
| 58 | + def setUp(self): |
| 59 | + """Ensure the temp folder exists.""" |
| 60 | + if not os.path.isdir(self.__class__.temp_folder): |
| 61 | + os.makedirs(self.__class__.temp_folder) |
| 62 | + |
| 63 | + def tearDown(self): |
| 64 | + """Delete any files created inside the temp directory""" |
| 65 | + if os.path.isdir(self.__class__.temp_folder): |
| 66 | + shutil.rmtree(self.__class__.temp_folder) |
| 67 | + |
| 68 | + # |
| 69 | + # Tests for file creation |
| 70 | + # |
| 71 | + def test_sketch_name_default(self): |
| 72 | + """Test default sketch has created the file correctly.""" |
| 73 | + sketch_path = sketchcreator.create_sketch(self.temp_folder) |
| 74 | + |
| 75 | + self.assertEqual(sketch_path, self.default_sketch_path) |
| 76 | + self.assertTrue(os.path.isfile(self.default_sketch_path)) |
| 77 | + |
| 78 | + def test_sketch_name_non_default(self): |
| 79 | + """Tests to see if an Arduino Sketch is created in a new location.""" |
| 80 | + filename_unicode = 'TestTemp_ろΓαζςÂé' |
| 81 | + final_ino_path = os.path.join( |
| 82 | + self.temp_folder, filename_unicode, filename_unicode + '.ino') |
| 83 | + |
72 | 84 | created_sketch_path = sketchcreator.create_sketch( |
73 | | - sketch_dir_unicode, sketch_name=sketch_name) |
| 85 | + self.temp_folder, sketch_name=filename_unicode) |
| 86 | + |
74 | 87 | self.assertEqual(final_ino_path, created_sketch_path) |
75 | 88 | self.assertTrue(os.path.isfile(final_ino_path)) |
76 | 89 |
|
77 | | - # Remove created unicode dir |
78 | | - shutil.rmtree(sketch_dir_unicode) |
79 | | - |
80 | | - def test_create_sketch_invalid(self): |
81 | | - """ |
82 | | - Test for invalid inputs in the create_sketch method. |
83 | | - """ |
84 | | - # Test for failure on invalid sketch path |
85 | | - random_invalid_path = os.path.join(os.getcwd(), 'raNd_dIr') |
86 | | - self.assertFalse(os.path.isdir(random_invalid_path)) |
87 | | - created_sketch_path = sketchcreator.create_sketch(random_invalid_path) |
88 | | - self.assertIsNone(created_sketch_path) |
89 | | - self.assertFalse(os.path.isdir(random_invalid_path)) |
90 | | - |
91 | | - # Test for failure on invalid sketch code |
92 | | - sketch_path = os.getcwd() |
93 | | - sketch_folder_path = os.path.join( |
94 | | - sketch_path, sketchcreator.default_sketch_name) |
95 | | - if os.path.isdir(sketch_folder_path): |
96 | | - shutil.rmtree(sketch_folder_path) |
97 | | - self.assertFalse(os.path.isdir(sketch_folder_path)) |
98 | | - invalid_sketch_code = True |
| 90 | + def test_sketch_name_invalid(self): |
| 91 | + """Test for invalid inputs in the create_sketch method.""" |
| 92 | + self.assertFalse(os.path.isdir(self.default_sketch_path)) |
| 93 | + invalid_sketch_name = True |
| 94 | + |
99 | 95 | created_sketch_path = sketchcreator.create_sketch( |
100 | | - sketch_path, sketch_code=invalid_sketch_code) |
| 96 | + self.temp_folder, sketch_name=invalid_sketch_name) |
| 97 | + |
| 98 | + self.assertIsNone(created_sketch_path) |
| 99 | + self.assertFalse(os.path.isdir(self.default_sketch_path)) |
| 100 | + |
| 101 | + def test_sketch_path_invalid(self): |
| 102 | + """Test for invalid inputs in the create_sketch method.""" |
| 103 | + invalid_path = os.path.join(self.temp_folder, 'raNd_dIr') |
| 104 | + self.assertFalse(os.path.isdir(invalid_path)) |
| 105 | + |
| 106 | + created_sketch_path = sketchcreator.create_sketch(invalid_path) |
| 107 | + |
101 | 108 | self.assertIsNone(created_sketch_path) |
102 | | - self.assertFalse(os.path.isdir(sketch_folder_path)) |
| 109 | + self.assertFalse(os.path.isdir(invalid_path)) |
103 | 110 |
|
104 | 111 | # |
105 | | - # File creation with code |
| 112 | + # Tests for code content |
106 | 113 | # |
107 | | - def test_create_sketch_with_code(self): |
108 | | - sketch_dir = os.getcwd() |
109 | | - sketch_ino_location = os.path.join( |
110 | | - sketch_dir, |
111 | | - sketchcreator.default_sketch_name, |
112 | | - '%s.ino' % sketchcreator.default_sketch_name) |
113 | | - sketch_code_write = 'Unicode test (ろΓαζςÂaé) on: %s' % \ |
114 | | - time.strftime("%Y-%m-%d %H:%M:%S") |
115 | | - |
116 | | - sketch_return_location = sketchcreator.create_sketch( |
117 | | - sketch_dir, sketch_code=sketch_code_write) |
118 | | - self.assertEqual(sketch_return_location, sketch_ino_location) |
119 | | - |
120 | | - arduino_sketch = codecs.open( |
121 | | - sketch_return_location, 'r', encoding='utf-8') |
122 | | - sketch_code_read = arduino_sketch.read() |
123 | | - arduino_sketch.close() |
124 | | - self.assertEqual(sketch_code_write, sketch_code_read) |
| 114 | + def test_sketch_code_default(self): |
| 115 | + """Test default sketch has filled the sketch contents correctly.""" |
| 116 | + sketch_path = sketchcreator.create_sketch(self.temp_folder) |
| 117 | + |
| 118 | + with codecs.open(sketch_path, 'r', encoding='utf-8') as sketch: |
| 119 | + sketch_content = sketch.read() |
| 120 | + self.assertEqual(sketch_content, sketchcreator.default_sketch_code) |
| 121 | + |
| 122 | + def test_sketch_code_non_default(self): |
| 123 | + """Test sketch is created correctly with the given code.""" |
| 124 | + sketch_code = 'Unicode test (ろΓαζςÂaé) on: %s' % \ |
| 125 | + time.strftime("%Y-%m-%d %H:%M:%S") |
| 126 | + |
| 127 | + sketch_path = sketchcreator.create_sketch( |
| 128 | + self.temp_folder, sketch_code=sketch_code) |
| 129 | + |
| 130 | + with codecs.open(sketch_path, 'r', encoding='utf-8') as sketch: |
| 131 | + sketch_code_read = sketch.read() |
| 132 | + self.assertEqual(sketch_code_read, sketch_code) |
| 133 | + |
| 134 | + def test_sketch_code_invalid(self): |
| 135 | + """Test for invalid inputs in the create_sketch method.""" |
| 136 | + self.assertFalse(os.path.isdir(self.default_sketch_path)) |
| 137 | + invalid_sketch_code = True |
| 138 | + |
| 139 | + created_sketch_path = sketchcreator.create_sketch( |
| 140 | + self.temp_folder, sketch_code=invalid_sketch_code) |
| 141 | + |
| 142 | + self.assertIsNone(created_sketch_path) |
| 143 | + self.assertFalse(os.path.isdir(self.default_sketch_path)) |
125 | 144 |
|
126 | 145 |
|
127 | 146 | if __name__ == '__main__': |
|
0 commit comments