|
20 | 20 | # pylint:disable=unused-argument
|
21 | 21 |
|
22 | 22 | import logging
|
23 |
| -import unittest |
24 | 23 | import os
|
| 24 | +import platform |
| 25 | +import unittest |
| 26 | + |
25 | 27 | import mock
|
26 | 28 |
|
27 | 29 | from mbed_flasher.common import FlashError
|
@@ -115,44 +117,42 @@ def test_raises_with_bad_file_extension(self):
|
115 | 117 | self.assertEqual(cm.exception.return_code, EXIT_CODE_DAPLINK_USER_ERROR)
|
116 | 118 |
|
117 | 119 | # pylint: disable=no-self-use
|
118 |
| - def test_copy_file_with_spaces(self): |
119 |
| - try: |
120 |
| - flasher = FlasherMbed() |
121 |
| - flasher.copy_file(__file__, "tar get") |
122 |
| - finally: |
123 |
| - os.remove("tar get") |
| 120 | + @unittest.skipIf(platform.system() != 'Windows', 'require windows') |
| 121 | + @mock.patch('subprocess.check_call') |
| 122 | + def test_copy_file_with_spaces(self, mock_check_call): |
| 123 | + flasher = FlasherMbed() |
| 124 | + flasher.copy_file(__file__, "tar get") |
| 125 | + should_be = ["cmd", "/c", "copy", __file__, "tar get"] |
| 126 | + mock_check_call.assert_called_with(should_be) |
124 | 127 |
|
125 | 128 | def test_copy_file_unable_to_read(self):
|
126 | 129 | flasher = FlasherMbed()
|
127 | 130 | with self.assertRaises(FlashError):
|
128 | 131 | flasher.copy_file("not-existing-file", "target")
|
129 | 132 |
|
130 | 133 | # pylint: disable=no-self-use
|
131 |
| - def test_copy_empty_file(self): |
| 134 | + @unittest.skipIf(platform.system() != 'Windows', 'require windows') |
| 135 | + @mock.patch('subprocess.check_call') |
| 136 | + def test_copy_empty_file_windows(self, mock_system): |
| 137 | + flasher = FlasherMbed() |
| 138 | + file_path = os.path.join(os.getcwd(), "empty_file") |
| 139 | + with open(file_path, 'a'): |
| 140 | + os.utime(file_path, None) |
| 141 | + flasher.copy_file(file_path, "target") |
| 142 | + os.remove(file_path) |
| 143 | + should_be = ["cmd", "/c", "copy", file_path, "target"] |
| 144 | + mock_system.assert_called_once_with(should_be) |
| 145 | + |
| 146 | + @unittest.skipIf(platform.system() != 'Linux', 'require linux') |
| 147 | + @mock.patch('mbed_flasher.flashers.FlasherMbed.FlasherMbed._copy_file') |
| 148 | + def test_copy_empty_file_linux(self, mock_copy_file): |
132 | 149 | flasher = FlasherMbed()
|
133 | 150 | with open("empty_file", 'a'):
|
134 |
| - os.utime("empty_file", None) |
| 151 | + pass |
| 152 | + |
135 | 153 | flasher.copy_file("empty_file", "target")
|
136 | 154 | os.remove("empty_file")
|
137 |
| - os.remove("target") |
138 |
| - |
139 |
| - # pylint: disable=no-self-use |
140 |
| - def test_copy_file_write_success(self): |
141 |
| - flasher = FlasherMbed() |
142 |
| - with open("source_file", 'wb') as source_file: |
143 |
| - source_file.write(b"test data") |
144 |
| - |
145 |
| - flasher.copy_file("source_file", "destination") |
146 |
| - |
147 |
| - with open("destination", "rb") as destination: |
148 |
| - result = destination.read() |
149 |
| - |
150 |
| - # remove file before assert to clean environment |
151 |
| - os.remove("source_file") |
152 |
| - os.remove("destination") |
153 |
| - |
154 |
| - # make sure file.write() really worked |
155 |
| - self.assertEqual(result, b"test data", "copy file failed") |
| 155 | + mock_copy_file.assert_called_once_with(b"", "target") |
156 | 156 |
|
157 | 157 |
|
158 | 158 | class FlasherMbedRetry(unittest.TestCase):
|
|
0 commit comments