test_files.py
1 # Adafruit MicroPython Tool - File Operation Tests 2 # Author: Tony DiCola 3 # Copyright (c) 2016 Adafruit Industries 4 # 5 # Permission is hereby granted, free of charge, to any person obtaining a copy 6 # of this software and associated documentation files (the "Software"), to deal 7 # in the Software without restriction, including without limitation the rights 8 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 # copies of the Software, and to permit persons to whom the Software is 10 # furnished to do so, subject to the following conditions: 11 # 12 # The above copyright notice and this permission notice shall be included in all 13 # copies or substantial portions of the Software. 14 # 15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 # SOFTWARE. 22 import tempfile 23 import sys 24 import unittest 25 # Try importing python 3 mock library, then fall back to python 2 (external module). 26 try: 27 import unittest.mock as mock 28 except ImportError: 29 import mock 30 31 import ampy.files as files 32 from ampy.pyboard import PyboardError 33 34 35 class TestFiles(unittest.TestCase): 36 37 def raisesRegex(self, *args, **kwargs): 38 # Wrapper to work with the different names for assertRaisesRegex vs. 39 # assertRaisesRegexp in Python 3 vs. 2 (ugh). 40 if sys.version_info >= (3,2): 41 return self.assertRaisesRegex(*args, **kwargs) 42 else: 43 return self.assertRaisesRegexp(*args, **kwargs) 44 45 def test_ls_multiple_files(self): 46 pyboard = mock.Mock() 47 pyboard.exec_ = mock.Mock(return_value=b"['boot.py', 'main.py', 'foo.txt']") 48 board_files = files.Files(pyboard) 49 result = board_files.ls() 50 self.assertListEqual(result, ['boot.py', 'main.py', 'foo.txt']) 51 52 def test_ls_no_files(self): 53 pyboard = mock.Mock() 54 pyboard.exec_ = mock.Mock(return_value=b"[]") 55 board_files = files.Files(pyboard) 56 result = board_files.ls() 57 self.assertListEqual(result, []) 58 59 def test_ls_bad_directory(self): 60 pyboard = mock.Mock() 61 pyboard.exec_ = mock.Mock(side_effect=PyboardError('exception', b'', b'Traceback (most recent call last):\r\n File "<stdin>", line 3, in <module>\r\nOSError: [Errno 2] ENOENT\r\n')) 62 with self.raisesRegex(RuntimeError, 'No such directory: /foo'): 63 board_files = files.Files(pyboard) 64 result = board_files.ls('/foo') 65 66 def test_get_with_data(self): 67 pyboard = mock.Mock() 68 pyboard.exec_ = mock.Mock(return_value=b"hello world") 69 board_files = files.Files(pyboard) 70 result = board_files.get('foo.txt') 71 self.assertEqual(result, b"hello world") 72 73 def test_get_bad_file(self): 74 pyboard = mock.Mock() 75 pyboard.exec_ = mock.Mock(side_effect=PyboardError('exception', b'', b'Traceback (most recent call last):\r\n File "<stdin>", line 3, in <module>\r\nOSError: [Errno 2] ENOENT\r\n')) 76 with self.raisesRegex(RuntimeError, 'No such file: foo.txt'): 77 board_files = files.Files(pyboard) 78 result = board_files.get('foo.txt') 79 80 def test_put(self): 81 pyboard = mock.Mock() 82 pyboard.exec_ = mock.Mock(return_value=b"") 83 board_files = files.Files(pyboard) 84 board_files.put('foo.txt', 'hello world') 85 86 def test_rm(self): 87 pyboard = mock.Mock() 88 pyboard.exec_ = mock.Mock(return_value=b"") 89 board_files = files.Files(pyboard) 90 board_files.rm('foo.txt') 91 92 def test_rm_file_doesnt_exist(self): 93 pyboard = mock.Mock() 94 pyboard.exec_ = mock.Mock(side_effect=PyboardError('exception', b'', b'Traceback (most recent call last):\r\n File "<stdin>", line 3, in <module>\r\nOSError: [Errno 2] ENOENT\r\n')) 95 with self.raisesRegex(RuntimeError, 'No such file/directory: foo.txt'): 96 board_files = files.Files(pyboard) 97 result = board_files.rm('foo.txt') 98 99 def test_rm_directory_not_empty(self): 100 pyboard = mock.Mock() 101 pyboard.exec_ = mock.Mock(side_effect=PyboardError('exception', b'', b'Traceback (most recent call last):\r\n File "<stdin>", line 3, in <module>\r\nOSError: [Errno 13] EACCES\r\n')) 102 with self.raisesRegex(RuntimeError, 'Directory is not empty: foo'): 103 board_files = files.Files(pyboard) 104 result = board_files.rm('foo') 105 106 def test_rmdir(self): 107 pyboard = mock.Mock() 108 pyboard.exec_ = mock.Mock(return_value=b"") 109 board_files = files.Files(pyboard) 110 board_files.rmdir('foo') 111 112 def test_rmdir_folder_doesnt_exist(self): 113 pyboard = mock.Mock() 114 pyboard.exec_ = mock.Mock(side_effect=PyboardError('exception', b'', b'Traceback (most recent call last):\r\n File "<stdin>", line 3, in <module>\r\nOSError: [Errno 2] ENOENT\r\n')) 115 with self.raisesRegex(RuntimeError, 'No such directory: foo'): 116 board_files = files.Files(pyboard) 117 result = board_files.rmdir('foo') 118 119 def test_run_with_output(self): 120 pyboard = mock.Mock() 121 pyboard.execfile = mock.Mock(return_value=b"Hello world") 122 board_files = files.Files(pyboard) 123 with tempfile.NamedTemporaryFile() as program: 124 program.write(b'print("Hello world")') 125 program.flush() 126 output = board_files.run(program.name) 127 self.assertEqual(output, b"Hello world") 128 129 def test_run_without_output(self): 130 pyboard = mock.Mock() 131 pyboard.exec_raw_no_follow = mock.Mock() 132 board_files = files.Files(pyboard) 133 with tempfile.NamedTemporaryFile() as program: 134 program.write(b'print("Hello world")') 135 program.flush() 136 output = board_files.run(program.name, wait_output=False) 137 self.assertEqual(output, None) 138 pyboard.exec_raw_no_follow.assert_called_once_with(b'print("Hello world")') 139 140 def test_mkdir_no_error(self): 141 pyboard = mock.Mock() 142 pyboard.exec_ = mock.Mock(return_value=b"") 143 board_files = files.Files(pyboard) 144 board_files.mkdir('/foo') 145 146 def test_mkdir_directory_already_exists(self): 147 pyboard = mock.Mock() 148 pyboard.exec_ = mock.Mock(side_effect=PyboardError('exception', b'', b'Traceback (most recent call last):\r\n File "<stdin>", line 3, in <module>\r\nOSError: [Errno 17] EEXIST\r\n')) 149 with self.raisesRegex(files.DirectoryExistsError, 'Directory already exists: /foo'): 150 board_files = files.Files(pyboard) 151 board_files.mkdir('/foo')