MCPcopy Create free account
hub / github.com/emscripten-core/emscripten / test_preload_file

Method test_preload_file

test/test_browser.py:316–458  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

314
315 @also_with_wasmfs
316 def test_preload_file(self):
317 create_file('somefile.txt', 'load me right before running the code please')
318 create_file('.somefile.txt', 'load me right before running the code please')
319 create_file('some@file.txt', 'load me right before running the code please')
320
321 absolute_src_path = os.path.abspath('somefile.txt')
322
323 def make_main(path):
324 print('make main at', path)
325 path = path.replace('\\', '\\\\').replace('"', '\\"') # Escape tricky path name for use inside a C string.
326 # TODO: change this when wasmfs supports relative paths.
327 if self.get_setting('WASMFS'):
328 path = "/" + path
329 create_file('main.c', r'''
330 #include <assert.h>
331 #include <stdio.h>
332 #include <string.h>
333 #include <emscripten.h>
334 int main() {
335 FILE *f = fopen("%s", "r");
336 char buf[100];
337 fread(buf, 1, 20, f);
338 buf[20] = 0;
339 fclose(f);
340 printf("|%%s|\n", buf);
341
342 assert(strcmp("load me right before", buf) == 0);
343 return 0;
344 }
345 ''&#x27; % path)
346
347 test_cases = [
348 # (source preload-file string, file on target FS to load)
349 ("somefile.txt", "somefile.txt"),
350 (".somefile.txt@somefile.txt", "somefile.txt"),
351 ("./somefile.txt", "somefile.txt"),
352 ("somefile.txt@file.txt", "file.txt"),
353 ("./somefile.txt@file.txt", "file.txt"),
354 ("./somefile.txt@./file.txt", "file.txt"),
355 ("somefile.txt@/file.txt", "file.txt"),
356 ("somefile.txt@/", "somefile.txt"),
357 (absolute_src_path + "@file.txt", "file.txt"),
358 (absolute_src_path + "@/file.txt", "file.txt"),
359 (absolute_src_path + "@/", "somefile.txt"),
360 ("somefile.txt@/directory/file.txt", "/directory/file.txt"),
361 ("somefile.txt@/directory/file.txt", "directory/file.txt"),
362 (absolute_src_path + "@/directory/file.txt", "directory/file.txt"),
363 ("some@@file.txt@other.txt", "other.txt"),
364 ("some@@file.txt@some@@otherfile.txt", "some@otherfile.txt")]
365
366 for srcpath, dstpath in test_cases:
367 print('Testing', srcpath, dstpath)
368 make_main(dstpath)
369 self.btest_exit('main.c', cflags=['--preload-file', srcpath])
370 if WINDOWS:
371 # On Windows, the following non-alphanumeric non-control code ASCII characters are supported.
372 # The characters <, >, ", |, ?, * are not allowed, because the Windows filesystem doesn't support those.
373 tricky_filename = '!#$%&\'()+,-. ;=@[]^_`{}~.txt&#x27;

Callers

nothing calls this directly

Calls 8

create_fileFunction · 0.90
delete_dirFunction · 0.90
ensure_dirFunction · 0.90
btest_exitMethod · 0.80
get_settingMethod · 0.80
compile_btestMethod · 0.80
run_browserMethod · 0.80
printFunction · 0.50

Tested by

no test coverage detected