Skip to content

Commit 8a48d0a

Browse files
committed
Add tests for relative path and Wasm cases on Module.locateFile() in node.js
1 parent 5d092a1 commit 8a48d0a

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

tests/test_other.py

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8166,9 +8166,10 @@ def test_noderawfs_disables_embedding(self):
81668166

81678167
# Tests that Emscripten-compiled applications can be run from a relative path with node command line that is different than the current working directory.
81688168
def test_node_js_run_from_different_directory(self):
8169+
args = ['-O3', '-s', 'WASM=1', '--memory-init-file', '1', '-s', 'BINARYEN_METHOD="asmjs,native-wasm"']
81698170
# Test that .mem.js is loaded up properly even if running the build output from a separate directory.
81708171
os.mkdir('subdir')
8171-
Popen([PYTHON, EMCC, path_from_root('tests', 'hello_world.c'), '-o', os.path.join('subdir', 'a.js'), '-O3']).communicate()
8172+
Popen([PYTHON, EMCC, path_from_root('tests', 'hello_world.c'), '-o', os.path.join('subdir', 'a.js')] + args).communicate()
81728173
ret = Popen(NODE_JS + [os.path.join('subdir', 'a.js')], stdout=PIPE).communicate()[0]
81738174
try_delete('subdir')
81748175
assert 'hello, world!' in ret
@@ -8177,11 +8178,41 @@ def test_node_js_run_from_different_directory(self):
81778178
os.mkdir('subdir')
81788179
open(os.path.join('subdir', 'pre.js'), 'w').write('''
81798180
var Module = {};
8181+
Module.memoryInitializerPrefixURL = 'this_should_be_getting_ignored_since_locateFile_is_specified/';
81808182
Module.locateFile = function(f) { return __dirname + '/' + f; }
81818183
''')
81828184

8183-
Popen([PYTHON, EMCC, path_from_root('tests', 'hello_world.c'), '-o', os.path.join('subdir', 'a.js'), '--pre-js', os.path.join('subdir', 'pre.js'), '-O3']).communicate()
8185+
Popen([PYTHON, EMCC, path_from_root('tests', 'hello_world.c'), '-o', os.path.join('subdir', 'a.js'), '--pre-js', os.path.join('subdir', 'pre.js')] + args).communicate()
81848186
ret = Popen(NODE_JS + [os.path.join('subdir', 'a.js')], stdout=PIPE).communicate()[0]
81858187
try_delete('subdir')
81868188
assert 'hello, world!' in ret
81878189

8190+
# Test that the build is loaded properly when Module.locateFile is being used, and it returns a relative path.
8191+
os.mkdir('subdir')
8192+
open(os.path.join('subdir', 'pre.js'), 'w').write('''
8193+
var Module = {};
8194+
Module.memoryInitializerPrefixURL = 'this_should_be_getting_ignored_since_locateFile_is_specified/';
8195+
Module.locateFile = function(f) { return 'data/' + f; }
8196+
''')
8197+
8198+
Popen([PYTHON, EMCC, path_from_root('tests', 'hello_world.c'), '-o', os.path.join('subdir', 'a.js'), '--pre-js', os.path.join('subdir', 'pre.js')] + args).communicate()
8199+
os.mkdir(os.path.join('subdir', 'data'))
8200+
os.rename(os.path.join('subdir', 'a.js.mem'), os.path.join('subdir', 'data', 'a.js.mem'))
8201+
os.rename(os.path.join('subdir', 'a.asm.js'), os.path.join('subdir', 'data', 'a.asm.js'))
8202+
ret = Popen(NODE_JS + [os.path.join('subdir', 'a.js')], stdout=PIPE).communicate()[0]
8203+
try_delete('subdir')
8204+
assert 'hello, world!' in ret
8205+
8206+
# Test that the build is loaded properly when memoryInitializerPrefixURL is being used, and it returns a relative path.
8207+
os.mkdir('subdir')
8208+
open(os.path.join('subdir', 'pre.js'), 'w').write('''
8209+
var Module = {};
8210+
Module.memoryInitializerPrefixURL = 'data/';
8211+
''')
8212+
8213+
Popen([PYTHON, EMCC, path_from_root('tests', 'hello_world.c'), '-o', os.path.join('subdir', 'a.js'), '--pre-js', os.path.join('subdir', 'pre.js')] + args).communicate()
8214+
os.mkdir(os.path.join('subdir', 'data'))
8215+
os.rename(os.path.join('subdir', 'a.js.mem'), os.path.join('subdir', 'data', 'a.js.mem'))
8216+
ret = Popen(NODE_JS + [os.path.join('subdir', 'a.js')], stdout=PIPE).communicate()[0]
8217+
try_delete('subdir')
8218+
assert 'hello, world!' in ret

0 commit comments

Comments
 (0)