Skip to content

Commit 1f38517

Browse files
author
Juho Hovila
committed
Fix tests
1 parent 208a021 commit 1f38517

File tree

1 file changed

+28
-28
lines changed

1 file changed

+28
-28
lines changed

test/non_hardware/test_flash.py

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@
2020
# pylint:disable=unused-argument
2121

2222
import logging
23-
import unittest
2423
import os
24+
import platform
25+
import unittest
26+
2527
import mock
2628

2729
from mbed_flasher.common import FlashError
@@ -115,44 +117,42 @@ def test_raises_with_bad_file_extension(self):
115117
self.assertEqual(cm.exception.return_code, EXIT_CODE_DAPLINK_USER_ERROR)
116118

117119
# 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)
124127

125128
def test_copy_file_unable_to_read(self):
126129
flasher = FlasherMbed()
127130
with self.assertRaises(FlashError):
128131
flasher.copy_file("not-existing-file", "target")
129132

130133
# 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):
132149
flasher = FlasherMbed()
133150
with open("empty_file", 'a'):
134-
os.utime("empty_file", None)
151+
pass
152+
135153
flasher.copy_file("empty_file", "target")
136154
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")
156156

157157

158158
class FlasherMbedRetry(unittest.TestCase):

0 commit comments

Comments
 (0)