Skip to content

Commit fd5a137

Browse files
committed
Add tests for relative path and Wasm cases on Module.locateFile() in node.js
1 parent 4333ffc commit fd5a137

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
@@ -8121,9 +8121,10 @@ def test_noderawfs_disables_embedding(self):
81218121

81228122
# Tests that Emscripten-compiled applications can be run from a relative path with node command line that is different than the current working directory.
81238123
def test_node_js_run_from_different_directory(self):
8124+
args = ['-O3', '-s', 'WASM=1', '--memory-init-file', '1', '-s', 'BINARYEN_METHOD="asmjs,native-wasm"']
81248125
# Test that .mem.js is loaded up properly even if running the build output from a separate directory.
81258126
os.mkdir('subdir')
8126-
Popen([PYTHON, EMCC, path_from_root('tests', 'hello_world.c'), '-o', os.path.join('subdir', 'a.js'), '-O3']).communicate()
8127+
Popen([PYTHON, EMCC, path_from_root('tests', 'hello_world.c'), '-o', os.path.join('subdir', 'a.js')] + args).communicate()
81278128
ret = Popen(NODE_JS + [os.path.join('subdir', 'a.js')], stdout=PIPE).communicate()[0]
81288129
try_delete('subdir')
81298130
assert 'hello, world!' in ret
@@ -8132,11 +8133,41 @@ def test_node_js_run_from_different_directory(self):
81328133
os.mkdir('subdir')
81338134
open(os.path.join('subdir', 'pre.js'), 'w').write('''
81348135
var Module = {};
8136+
Module.memoryInitializerPrefixURL = 'this_should_be_getting_ignored_since_locateFile_is_specified/';
81358137
Module.locateFile = function(f) { return __dirname + '/' + f; }
81368138
''')
81378139

8138-
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()
8140+
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()
81398141
ret = Popen(NODE_JS + [os.path.join('subdir', 'a.js')], stdout=PIPE).communicate()[0]
81408142
try_delete('subdir')
81418143
assert 'hello, world!' in ret
81428144

8145+
# Test that the build is loaded properly when Module.locateFile is being used, and it returns a relative path.
8146+
os.mkdir('subdir')
8147+
open(os.path.join('subdir', 'pre.js'), 'w').write('''
8148+
var Module = {};
8149+
Module.memoryInitializerPrefixURL = 'this_should_be_getting_ignored_since_locateFile_is_specified/';
8150+
Module.locateFile = function(f) { return 'data/' + f; }
8151+
''')
8152+
8153+
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()
8154+
os.mkdir(os.path.join('subdir', 'data'))
8155+
os.rename(os.path.join('subdir', 'a.js.mem'), os.path.join('subdir', 'data', 'a.js.mem'))
8156+
os.rename(os.path.join('subdir', 'a.asm.js'), os.path.join('subdir', 'data', 'a.asm.js'))
8157+
ret = Popen(NODE_JS + [os.path.join('subdir', 'a.js')], stdout=PIPE).communicate()[0]
8158+
try_delete('subdir')
8159+
assert 'hello, world!' in ret
8160+
8161+
# Test that the build is loaded properly when memoryInitializerPrefixURL is being used, and it returns a relative path.
8162+
os.mkdir('subdir')
8163+
open(os.path.join('subdir', 'pre.js'), 'w').write('''
8164+
var Module = {};
8165+
Module.memoryInitializerPrefixURL = 'data/';
8166+
''')
8167+
8168+
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()
8169+
os.mkdir(os.path.join('subdir', 'data'))
8170+
os.rename(os.path.join('subdir', 'a.js.mem'), os.path.join('subdir', 'data', 'a.js.mem'))
8171+
ret = Popen(NODE_JS + [os.path.join('subdir', 'a.js')], stdout=PIPE).communicate()[0]
8172+
try_delete('subdir')
8173+
assert 'hello, world!' in ret

0 commit comments

Comments
 (0)