1
1
const Board = require ( './micropython.js' )
2
2
const assert = require ( 'assert' )
3
3
const fs = require ( 'fs' )
4
+ const path = require ( 'path' )
4
5
5
6
function sleep ( millis ) {
6
7
return new Promise ( ( resolve , reject ) => {
@@ -102,6 +103,7 @@ const testCases = {
102
103
return Promise . resolve ( )
103
104
} ,
104
105
"run code after stop" : async ( board ) => {
106
+ debugger
105
107
board . run (
106
108
`from time import sleep\nfor i in range(0, 10):\n print('.')\n sleep(1)\n`
107
109
) . catch ( e => {
@@ -110,7 +112,7 @@ const testCases = {
110
112
await ( new Promise ( ( r ) => setTimeout ( r , 100 ) ) )
111
113
await board . stop ( )
112
114
await ( new Promise ( ( r ) => setTimeout ( r , 100 ) ) )
113
-
115
+ await board . get_prompt ( )
114
116
const output = await board . run ( 'print(123)' )
115
117
assert . equal ( output , 'OK123\r\n\x04\x04>' )
116
118
@@ -127,12 +129,103 @@ const testCases = {
127
129
assert . equal ( output , 'OK123\r\n\x04\x04>' )
128
130
129
131
} ,
132
+ "upload file" : async ( board ) => {
133
+ const diskFilePath = path . resolve ( './examples/test.py' )
134
+ const serialFilePath = '/test.py'
135
+ await board . fs_put ( diskFilePath , serialFilePath )
136
+ const diskFileContent = fs . readFileSync ( diskFilePath )
137
+ const boardFileContent = await board . fs_cat ( serialFilePath )
138
+ assert . equal ( diskFileContent . toString ( ) , boardFileContent )
139
+ await board . fs_rm ( serialFilePath )
140
+ } ,
141
+ "upload big file" : async ( board ) => {
142
+ const diskFilePath = path . resolve ( './examples/big_file.py' )
143
+ const serialFilePath = '/big_file.py'
144
+ await board . fs_put (
145
+ diskFilePath , serialFilePath ,
146
+ ( e ) => console . log ( 'uploading big file' , e )
147
+ )
148
+ const diskFileContent = fs . readFileSync ( diskFilePath )
149
+ const boardFileContent = await board . fs_cat ( serialFilePath )
150
+ assert . equal ( diskFileContent . toString ( ) , boardFileContent )
151
+ await board . fs_rm ( serialFilePath )
152
+ } ,
153
+ "create folder" : async ( board ) => {
154
+ const folderPath = '/test_folder'
155
+ await board . fs_mkdir ( folderPath )
156
+ const ls = await board . fs_ils ( '/' )
157
+ const folder = ls . find ( f => f [ 0 ] === 'test_folder' && f [ 1 ] === 16384 )
158
+ assert . ok ( folder )
159
+ await board . fs_rmdir ( folderPath )
160
+ } ,
161
+ "list files and folders" : async ( board ) => {
162
+ const file = [ 'test.py' , 32768 ]
163
+ const test_folder = 'test_folder'
164
+ await board . fs_put ( path . resolve ( 'examples' , file [ 0 ] ) , '/' + file [ 0 ] )
165
+ await board . fs_mkdir ( '/' + test_folder )
166
+ const ls = await board . fs_ils ( '/' )
167
+ const createdFile = ls . find ( f => f [ 0 ] === file [ 0 ] && f [ 1 ] === file [ 1 ] )
168
+ const createdFolder = ls . find ( f => f [ 0 ] === test_folder && f [ 1 ] === 16384 )
169
+ assert . ok ( createdFile )
170
+ assert . ok ( createdFolder )
171
+ } ,
172
+ "check if file exists" : async ( board ) => {
173
+ const filePath = '/test_exist' + parseInt ( Math . random ( ) * 99999 )
174
+ await board . fs_save ( '.' , filePath )
175
+ const fileExists = await board . fs_exists ( filePath )
176
+ assert ( fileExists )
177
+ const fileDoesNotExist = await board . fs_exists ( '/xxx' + parseInt ( Math . random ( ) * 99999 ) )
178
+ assert . ok ( ! fileDoesNotExist )
179
+ await board . fs_rm ( filePath )
180
+ } ,
181
+ "save file content" : async ( board ) => {
182
+ const filePath = '/test.py'
183
+ const content = `.`
184
+ await board . fs_save ( content , filePath )
185
+ const boardContent = await board . fs_cat ( filePath )
186
+ assert . equal ( content , boardContent )
187
+ } ,
188
+ "save big file content" : async ( board ) => {
189
+ const filePath = '/test.py'
190
+ const content = fs . readFileSync ( path . resolve ( './examples/big_file.py' ) )
191
+ await board . fs_save ( content . toString ( ) , filePath )
192
+ const boardContent = await board . fs_cat ( filePath )
193
+ assert . equal ( content , boardContent )
194
+ } ,
195
+ "get file" : async ( board ) => {
196
+ const filePath = '/test.py'
197
+ const content = `.`
198
+ await board . fs_save ( content , filePath )
199
+ const boardContent = await board . fs_cat ( filePath )
200
+ assert . equal ( content , boardContent )
201
+ } ,
202
+ "remove file" : async ( board ) => {
203
+ const filePath = '/test_remove' + parseInt ( Math . random ( ) * 99999 )
204
+ await board . fs_save ( '.' , filePath )
205
+ const fileExists = await board . fs_exists ( filePath )
206
+ assert ( fileExists )
207
+ await board . fs_rm ( filePath )
208
+ const fileDoesNotExist = await board . fs_exists ( filePath )
209
+ assert . ok ( ! fileDoesNotExist )
210
+ } ,
211
+ "remove folder" : async ( board ) => {
212
+ const folderPath = '/test_remove' + parseInt ( Math . random ( ) * 99999 )
213
+ await board . fs_mkdir ( folderPath )
214
+ const ls = await board . fs_ils ( '/' )
215
+ const foundFolder = ls . find ( f => f [ 0 ] === folderPath . slice ( 1 ) && f [ 1 ] === 16384 )
216
+ assert . ok ( foundFolder )
217
+ await board . fs_rmdir ( folderPath )
218
+ const notFoundFolder = ls . find ( f => f [ 0 ] === folderPath . slice ( 1 ) && f [ 1 ] === 16384 )
219
+ assert . ok ( ! notFoundFolder )
220
+ } ,
130
221
// "foo": async (board) => Promise.reject()
131
222
}
132
223
133
224
// SKIP LONG RUNNERS
134
225
delete testCases [ 'execute raw big' ]
135
226
delete testCases [ 'run big code' ]
227
+ delete testCases [ 'upload big file' ]
228
+ delete testCases [ 'save big file content' ]
136
229
137
230
async function main ( ) {
138
231
let errors = [ ]
@@ -164,90 +257,4 @@ async function main() {
164
257
}
165
258
}
166
259
167
-
168
-
169
- // REFERENCE OF A FULL FEATURE TEST:
170
- // async function main() {
171
- // const board = new Board()
172
- // await board.open(process.env.PORT || '/dev/ttyACM0')
173
- // console.log('connected')
174
- //
175
- // await board.get_prompt()
176
- // console.log('has prompt')
177
- //
178
- // const fn = async () => {
179
- // let o = await board.serial.read()
180
- // console.log('DATA', o.toString())
181
- // }
182
- // board.serial.on('readable', fn)
183
- // await board.eval('pri')
184
- // await sleep(10)
185
- // await board.eval('nt(1')
186
- // await sleep(10)
187
- // await board.eval('23)')
188
- // await board.eval('\r')
189
- // await sleep(10)
190
- // board.serial.removeListener('readable', fn)
191
- //
192
- // await board.enter_raw_repl()
193
- // console.log('in raw repl')
194
- //
195
- // const code = `from time import sleep\nfor i in range(0, 10):\n print('.')\n sleep(0.1)\n`
196
- // let i = 0
197
- // out = await board.exec_raw(code, async (d) => {
198
- // console.log('->', d)
199
- // // i += 1; if (i > 3) await board.stop()
200
- // })
201
- // console.log('executed', out)
202
- //
203
- // await board.exit_raw_repl()
204
- // console.log('out raw repl')
205
- //
206
- // out = await board.fs_exists('boot.py')
207
- // console.log('boot.py exists', out)
208
- // out = await board.fs_exists('this_is_not_a_file.py')
209
- // console.log('nope.py exists', out)
210
- //
211
- // out = await board.fs_ls('./')
212
- // console.log('root files', out)
213
- // out = await board.fs_ls('./lib')
214
- // console.log('lib files', out)
215
- //
216
- // out = await board.fs_ils('./')
217
- // console.log('root files', out)
218
- // out = await board.fs_ils('./lib')
219
- // console.log('lib files', out)
220
- //
221
- // out = await board.fs_put(
222
- // './examples/big_file.py', 'test.py', (d) => console.log('progress', d)
223
- // )
224
- // console.log('send file to board', out)
225
- //
226
- // out = await board.fs_cat('test.py')
227
- // console.log('get test.py content', out)
228
- //
229
- // out = await board.fs_save(
230
- // '# overrides test file', 'test.py', (d) => console.log('progress', d)
231
- // )
232
- // console.log('save test.py content', out)
233
- //
234
- // out = await board.fs_cat('test.py')
235
- // console.log('get test.py content', out)
236
- //
237
- // out = await board.fs_rm('test.py')
238
- // console.log('removing test.py', out)
239
- //
240
- // out = await board.fs_mkdir('test_dir')
241
- // console.log('created test_dir', out)
242
- // out = await board.fs_ils()
243
- // console.log('files at ./', out)
244
- //
245
- // out = await board.fs_rmdir('test_dir')
246
- // console.log('removed test_dir', out)
247
- // out = await board.fs_ils()
248
- // console.log('files at ./', out)
249
- //
250
- // board.close()
251
- // }
252
-
253
260
main ( )
0 commit comments