Skip to content

Commit 8386bd9

Browse files
authored
[test] Simplify test that use shutil.copytree. NFC (emscripten-core#23709)
We cam use the `dirs_exist_ok` param (added in python3.8) to copy directory tree's into the current working directory, avoiding the need to create more sub-directories and use chdir.
1 parent 4204bc3 commit 8386bd9

File tree

3 files changed

+42
-47
lines changed

3 files changed

+42
-47
lines changed

test/common.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ def test_file(*path_components):
101101
return str(Path(TEST_ROOT, *path_components))
102102

103103

104+
def copytree(src, dest):
105+
shutil.copytree(src, dest, dirs_exist_ok=True)
106+
107+
104108
# checks if browser testing is enabled
105109
def has_browser():
106110
return EMTEST_BROWSER != '0'

test/test_browser.py

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import common
2424
from common import BrowserCore, RunnerCore, path_from_root, has_browser, EMTEST_BROWSER, Reporting
2525
from common import create_file, parameterized, ensure_dir, disabled, test_file, WEBIDL_BINDER
26-
from common import read_file, EMRUN, no_wasm64, no_2gb, no_4gb
26+
from common import read_file, EMRUN, no_wasm64, no_2gb, no_4gb, copytree
2727
from common import requires_wasm2js, parameterize, find_browser_test_file, with_all_sjlj
2828
from common import also_with_minimal_runtime, also_with_wasm2js, also_with_asan
2929
from tools import shared
@@ -5522,31 +5522,28 @@ def test_error_reporting(self):
55225522
})
55235523
def test_webpack(self, es6):
55245524
if es6:
5525-
shutil.copytree(test_file('webpack_es6'), 'webpack')
5525+
copytree(test_file('webpack_es6'), '.')
55265526
self.emcc_args += ['-sEXPORT_ES6', '-pthread', '-sPTHREAD_POOL_SIZE=1']
55275527
outfile = 'src/hello.mjs'
55285528
else:
5529-
shutil.copytree(test_file('webpack'), 'webpack')
5529+
copytree(test_file('webpack'), '.')
55305530
outfile = 'src/hello.js'
5531-
with common.chdir('webpack'):
5532-
self.compile_btest('hello_world.c', ['-sEXIT_RUNTIME', '-sMODULARIZE', '-sENVIRONMENT=web,worker', '-o', outfile])
5533-
self.run_process(shared.get_npm_cmd('webpack') + ['--mode=development', '--no-devtool'])
5534-
shutil.copy('webpack/src/hello.wasm', 'webpack/dist/')
5535-
self.run_browser('webpack/dist/index.html', '/report_result?exit:0')
5531+
self.compile_btest('hello_world.c', ['-sEXIT_RUNTIME', '-sMODULARIZE', '-sENVIRONMENT=web,worker', '-o', outfile])
5532+
self.run_process(shared.get_npm_cmd('webpack') + ['--mode=development', '--no-devtool'])
5533+
shutil.copy('src/hello.wasm', 'dist/')
5534+
self.run_browser('dist/index.html', '/report_result?exit:0')
55365535

55375536
def test_vite(self):
5538-
shutil.copytree(test_file('vite'), 'vite')
5539-
with common.chdir('vite'):
5540-
self.compile_btest('hello_world.c', ['-sEXPORT_ES6', '-sEXIT_RUNTIME', '-sMODULARIZE', '-o', 'hello.mjs'])
5541-
self.run_process(shared.get_npm_cmd('vite') + ['build'])
5542-
self.run_browser('vite/dist/index.html', '/report_result?exit:0')
5537+
copytree(test_file('vite'), '.')
5538+
self.compile_btest('hello_world.c', ['-sEXPORT_ES6', '-sEXIT_RUNTIME', '-sMODULARIZE', '-o', 'hello.mjs'])
5539+
self.run_process(shared.get_npm_cmd('vite') + ['build'])
5540+
self.run_browser('dist/index.html', '/report_result?exit:0')
55435541

55445542
def test_rollup(self):
5545-
shutil.copytree(test_file('rollup'), 'rollup')
5546-
with common.chdir('rollup'):
5547-
self.compile_btest('hello_world.c', ['-sEXPORT_ES6', '-sEXIT_RUNTIME', '-sMODULARIZE', '-o', 'hello.mjs'])
5548-
self.run_process(shared.get_npm_cmd('rollup') + ['--config'])
5549-
self.run_browser('rollup/index.html', '/report_result?exit:0')
5543+
copytree(test_file('rollup'), '.')
5544+
self.compile_btest('hello_world.c', ['-sEXPORT_ES6', '-sEXIT_RUNTIME', '-sMODULARIZE', '-o', 'hello.mjs'])
5545+
self.run_process(shared.get_npm_cmd('rollup') + ['--config'])
5546+
self.run_browser('index.html', '/report_result?exit:0')
55505547

55515548

55525549
class emrun(RunnerCore):

test/test_other.py

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
from common import also_with_modularize, also_with_wasmfs, with_all_fs
4242
from common import also_with_minimal_runtime, also_with_wasm_bigint, also_with_wasm64, also_with_asan, flaky
4343
from common import EMTEST_BUILD_VERBOSE, PYTHON, WEBIDL_BINDER
44-
from common import requires_network, parameterize
44+
from common import requires_network, parameterize, copytree
4545
from tools import shared, building, utils, response_file, cache
4646
from tools.utils import read_file, write_file, delete_file, read_binary, MACOS, WINDOWS
4747
import common
@@ -3137,11 +3137,10 @@ def test_dwarf_sourcemap_names(self):
31373137
@with_env_modify({'EMSCRIPTEN_ROOT': path_from_root()})
31383138
def test_scons(self):
31393139
# this test copies the site_scons directory alongside the test
3140-
shutil.copytree(test_file('scons/simple'), 'test')
3141-
shutil.copytree(path_from_root('tools/scons/site_scons'), 'test/site_scons')
3142-
with common.chdir('test'):
3143-
self.run_process(['scons'])
3144-
output = self.run_js('scons_integration.js', assert_returncode=5)
3140+
copytree(test_file('scons/simple'), '.')
3141+
copytree(path_from_root('tools/scons/site_scons'), 'site_scons')
3142+
self.run_process(['scons'])
3143+
output = self.run_js('scons_integration.js', assert_returncode=5)
31453144
self.assertContained('If you see this - the world is all right!', output)
31463145

31473146
@requires_scons
@@ -3152,8 +3151,8 @@ def test_scons(self):
31523151
})
31533152
def test_scons_env(self):
31543153
# this test copies the site_scons directory alongside the test
3155-
shutil.copytree(test_file('scons/env'), 'test')
3156-
shutil.copytree(path_from_root('tools/scons/site_scons'), 'test/site_scons')
3154+
copytree(test_file('scons/env'), '.')
3155+
copytree(path_from_root('tools/scons/site_scons'), 'site_scons')
31573156

31583157
expected_to_propagate = json.dumps({
31593158
'CC': path_from_root('emcc'),
@@ -3166,13 +3165,12 @@ def test_scons_env(self):
31663165
}
31673166
})
31683167

3169-
with common.chdir('test'):
3170-
self.run_process(['scons', '--expected-env', expected_to_propagate])
3168+
self.run_process(['scons', '--expected-env', expected_to_propagate])
31713169

31723170
@requires_scons
31733171
def test_scons_env_no_emscons(self):
3174-
shutil.copytree(test_file('scons/env'), 'test')
3175-
shutil.copytree(path_from_root('tools/scons/site_scons'), 'test/site_scons')
3172+
copytree(test_file('scons/env'), '.')
3173+
copytree(path_from_root('tools/scons/site_scons'), 'site_scons')
31763174

31773175
expected_to_propagate = json.dumps({
31783176
'CC': 'emcc',
@@ -3185,20 +3183,18 @@ def test_scons_env_no_emscons(self):
31853183
}
31863184
})
31873185

3188-
with common.chdir('test'):
3189-
self.run_process(['scons', '--expected-env', expected_to_propagate])
3186+
self.run_process(['scons', '--expected-env', expected_to_propagate])
31903187

31913188
@requires_scons
31923189
def test_emscons(self):
3193-
shutil.copytree(test_file('scons/simple'), 'test')
3194-
with common.chdir('test'):
3195-
self.run_process([path_from_root('emscons'), 'scons'])
3196-
output = self.run_js('scons_integration.js', assert_returncode=5)
3190+
copytree(test_file('scons/simple'), '.')
3191+
self.run_process([path_from_root('emscons'), 'scons'])
3192+
output = self.run_js('scons_integration.js', assert_returncode=5)
31973193
self.assertContained('If you see this - the world is all right!', output)
31983194

31993195
@requires_scons
32003196
def test_emscons_env(self):
3201-
shutil.copytree(test_file('scons/env'), 'test')
3197+
copytree(test_file('scons/env'), '.')
32023198

32033199
building_env = get_building_env()
32043200
expected_to_propagate = json.dumps({
@@ -3212,8 +3208,7 @@ def test_emscons_env(self):
32123208
}
32133209
})
32143210

3215-
with common.chdir('test'):
3216-
self.run_process([path_from_root('emscons'), 'scons', '--expected-env', expected_to_propagate])
3211+
self.run_process([path_from_root('emscons'), 'scons', '--expected-env', expected_to_propagate])
32173212

32183213
def test_embind_fail(self):
32193214
out = self.expect_fail([EMXX, test_file('embind/test_unsigned.cpp')])
@@ -15591,9 +15586,9 @@ def test_embool(self):
1559115586

1559215587
@requires_rust
1559315588
def test_rust_integration_basics(self):
15594-
shutil.copytree(test_file('rust/basics'), 'basics')
15595-
self.run_process(['cargo', 'build', '--target=wasm32-unknown-emscripten'], cwd='basics')
15596-
lib = 'basics/target/wasm32-unknown-emscripten/debug/libbasics.a'
15589+
copytree(test_file('rust/basics'), '.')
15590+
self.run_process(['cargo', 'build', '--target=wasm32-unknown-emscripten'])
15591+
lib = 'target/wasm32-unknown-emscripten/debug/libbasics.a'
1559715592
self.assertExists(lib)
1559815593

1559915594
create_file('main.cpp', '''
@@ -15699,8 +15694,7 @@ def test_late_module_api_assignment(self):
1569915694
self.do_runf(test_file('hello_world.c'), expected, emcc_args=['--post-js=post.js', '-sWASM_ASYNC_COMPILATION=0'], assert_returncode=NON_ZERO)
1570015695

1570115696
def test_rollup(self):
15702-
shutil.copytree(test_file('rollup_node'), 'rollup_node')
15703-
with common.chdir('rollup_node'):
15704-
self.run_process([EMCC, test_file('hello_world.c'), '-sEXPORT_ES6', '-sEXIT_RUNTIME', '-sENVIRONMENT=node', '-sMODULARIZE', '-o', 'hello.mjs'])
15705-
self.run_process(shared.get_npm_cmd('rollup') + ['--config'])
15706-
self.assertContained('hello, world!', self.run_js('bundle.mjs'))
15697+
copytree(test_file('rollup_node'), '.')
15698+
self.run_process([EMCC, test_file('hello_world.c'), '-sEXPORT_ES6', '-sEXIT_RUNTIME', '-sENVIRONMENT=node', '-sMODULARIZE', '-o', 'hello.mjs'])
15699+
self.run_process(shared.get_npm_cmd('rollup') + ['--config'])
15700+
self.assertContained('hello, world!', self.run_js('bundle.mjs'))

0 commit comments

Comments
 (0)