@@ -1992,3 +1992,34 @@ def test_wget(self):
1992
1992
with open (os .path .join (self .get_dir (), 'test.txt' ), 'w' ) as f :
1993
1993
f .write ('emscripten' )
1994
1994
self .btest (path_from_root ('tests' , 'test_wget.c' ), expected = '1' , args = ['-s' , 'ASYNCIFY=1' ])
1995
+
1996
+ def test_locate_file (self ):
1997
+ self .clear ()
1998
+ open ('src.cpp' , 'w' ).write (self .with_report_result (r'''
1999
+ #include <stdio.h>
2000
+ #include <string.h>
2001
+ #include <assert.h>
2002
+ int main() {
2003
+ FILE *f = fopen("data.txt", "r");
2004
+ assert(f && "could not open file");
2005
+ char buf[100];
2006
+ int num = fread(buf, 1, 20, f);
2007
+ assert(num == 20 && "could not read 20 bytes");
2008
+ buf[20] = 0;
2009
+ fclose(f);
2010
+ int result = !strcmp("load me right before", buf);
2011
+ printf("|%s| : %d\n", buf, result);
2012
+ REPORT_RESULT();
2013
+ return 0;
2014
+ }
2015
+ ''' ))
2016
+ open ('data.txt' , 'w' ).write ('load me right before...' )
2017
+ open ('pre.js' , 'w' ).write ('Module.locateFile = function(x) { return "sub/" + x };' )
2018
+ Popen ([PYTHON , FILE_PACKAGER , 'test.data' , '--preload' , 'data.txt' ], stdout = open ('data.js' , 'w' )).communicate ()
2019
+ # put pre.js first, then the file packager data, so locateFile is there for the file loading code
2020
+ Popen ([PYTHON , EMCC , 'src.cpp' , '-O2' , '-g' , '--pre-js' , 'pre.js' , '--pre-js' , 'data.js' , '-o' , 'page.html' ]).communicate ()
2021
+ os .mkdir ('sub' )
2022
+ shutil .move ('page.html.mem' , os .path .join ('sub' , 'page.html.mem' ))
2023
+ shutil .move ('test.data' , os .path .join ('sub' , 'test.data' ))
2024
+ self .run_browser ('page.html' , None , '/report_result?1' )
2025
+
0 commit comments