How to use ensure_empty_dir method in tox

Best Python code snippet using tox_python

build.py

Source:build.py Github

copy

Full Screen

...28# Preconditions.29def ensure_remove_dir(path):30 if os.path.exists(path):31 shutil.rmtree(path)32def ensure_empty_dir(path):33 ensure_remove_dir(path)34 os.mkdir(path)35def compile_wasm_interpreter():36 print("Recompiling the wasm interpreter...")37 result = call('make', '-C', INTERPRETER_DIR, 'clean', 'default')38 if result != 0:39 print("Couldn't recompile wasm spec interpreter")40 sys.exit(1)41 print("Done!")42def ensure_wasm_executable(path_to_wasm):43 """44 Ensure we have built the wasm spec interpreter.45 """46 result = call(path_to_wasm, '-v', '-e', '')47 if result != 0:48 print('Unable to run the wasm executable')49 sys.exit(1)50# JS harness.51def convert_one_wast_file(inputs):52 wast_file, js_file = inputs53 print('Compiling {} to JS...'.format(wast_file))54 return run(WASM_EXEC, wast_file, '-h', '-o', js_file)55def convert_wast_to_js(out_js_dir):56 """Compile all the wast files to JS and store the results in the JS dir."""57 inputs = []58 for wast_file in glob.glob(os.path.join(WAST_TESTS_DIR, '*.wast')):59 # Don't try to compile tests that are supposed to fail.60 if '.fail.' in wast_file:61 continue62 js_filename = os.path.basename(wast_file) + '.js'63 js_file = os.path.join(out_js_dir, js_filename)64 inputs.append((wast_file, js_file))65 pool = mp.Pool(processes=8)66 for result in pool.imap_unordered(convert_one_wast_file, inputs):67 if result.returncode != 0:68 print('Error when compiling to JS:')69 print(result.args)70 if result.stdout:71 # stderr is piped to stdout via `run`, so we only need to72 # worry about stdout73 print(result.stdout)74 return [js_file for (wast_file, js_file) in inputs]75def copy_harness_files(out_js_dir, include_harness):76 harness_dir = os.path.join(out_js_dir, 'harness')77 ensure_empty_dir(harness_dir)78 print('Copying JS test harness to the JS out dir...')79 for js_file in glob.glob(os.path.join(HARNESS_DIR, '*')):80 if os.path.basename(js_file) in HARNESS_FILES and not include_harness:81 continue82 shutil.copy(js_file, harness_dir)83def build_js(out_js_dir):84 print('Building JS...')85 convert_wast_to_js(out_js_dir)86 copy_harness_files(out_js_dir, False)87 print('Done building JS.')88# HTML harness.89HTML_HEADER = """<!doctype html>90<html>91 <head>92 <meta charset="UTF-8">93 <title>WebAssembly Web Platform Test</title>94 </head>95 <body>96 <script src={WPT_PREFIX}/testharness.js></script>97 <script src={WPT_PREFIX}/testharnessreport.js></script>98 <script src={PREFIX}/{JS_HARNESS}></script>99 <div id=log></div>100"""101HTML_BOTTOM = """102 </body>103</html>104"""105def wrap_single_test(js_file):106 test_func_name = os.path.basename(js_file).replace('.', '_').replace('-', '_')107 content = "(function {}() {{\n".format(test_func_name)108 with open(js_file, 'r') as f:109 content += f.read()110 content += "reinitializeRegistry();\n})();\n"111 with open(js_file, 'w') as f:112 f.write(content)113def build_html_js(out_dir):114 ensure_empty_dir(out_dir)115 copy_harness_files(out_dir, True)116 tests = convert_wast_to_js(out_dir)117 for js_file in tests:118 wrap_single_test(js_file)119 return tests120def build_html_from_js(tests, html_dir, use_sync):121 for js_file in tests:122 js_filename = os.path.basename(js_file)123 html_filename = js_filename + '.html'124 html_file = os.path.join(html_dir, html_filename)125 js_harness = "sync_index.js" if use_sync else "async_index.js"126 with open(html_file, 'w+') as f:127 content = HTML_HEADER.replace('{PREFIX}', './js/harness') \128 .replace('{WPT_PREFIX}', './js/harness') \129 .replace('{JS_HARNESS}', js_harness)130 content += " <script src=./js/{SCRIPT}></script>".replace('{SCRIPT}', js_filename)131 content += HTML_BOTTOM132 f.write(content)133def build_html(html_dir, js_dir, use_sync):134 print("Building HTML tests...")135 js_html_dir = os.path.join(html_dir, 'js')136 tests = build_html_js(js_html_dir)137 print('Building WPT tests from JS tests...')138 build_html_from_js(tests, html_dir, use_sync)139 print("Done building HTML tests.")140# Front page harness.141def build_front_page(out_dir, js_dir, use_sync):142 print('Building front page containing all the HTML tests...')143 js_out_dir = os.path.join(out_dir, 'js')144 tests = build_html_js(js_out_dir)145 front_page = os.path.join(out_dir, 'index.html')146 js_harness = "sync_index.js" if use_sync else "async_index.js"147 with open(front_page, 'w+') as f:148 content = HTML_HEADER.replace('{PREFIX}', './js/harness') \149 .replace('{WPT_PREFIX}', './js/harness')\150 .replace('{JS_HARNESS}', js_harness)151 for js_file in tests:152 filename = os.path.basename(js_file)153 content += " <script src=./js/{SCRIPT}></script>\n".replace('{SCRIPT}', filename)154 content += HTML_BOTTOM155 f.write(content)156 print('Done building front page!')157# Main program.158def process_args():159 parser = argparse.ArgumentParser(description="Helper tool to build the\160 multi-stage cross-browser test suite for WebAssembly.")161 parser.add_argument('--js',162 dest="js_dir",163 help="Relative path to the output directory for the pure JS tests.",164 type=str)165 parser.add_argument('--html',166 dest="html_dir",167 help="Relative path to the output directory for the Web Platform tests.",168 type=str)169 parser.add_argument('--front',170 dest="front_dir",171 help="Relative path to the output directory for the front page.",172 type=str)173 parser.add_argument('--dont-recompile',174 action="store_const",175 dest="compile",176 help="Don't recompile the wasm spec interpreter (by default, it is)",177 const=False,178 default=True)179 parser.add_argument('--use-sync',180 action="store_const",181 dest="use_sync",182 help="Let the tests use the synchronous JS API (by default, it does not)",183 const=True,184 default=False)185 return parser.parse_args(), parser186if __name__ == '__main__':187 args, parser = process_args()188 js_dir = args.js_dir189 html_dir = args.html_dir190 front_dir = args.front_dir191 if front_dir is None and js_dir is None and html_dir is None:192 print('At least one mode must be selected.\n')193 parser.print_help()194 sys.exit(1)195 if args.compile:196 compile_wasm_interpreter()197 ensure_wasm_executable(WASM_EXEC)198 if js_dir is not None:199 ensure_empty_dir(js_dir)200 build_js(js_dir)201 if html_dir is not None:202 ensure_empty_dir(html_dir)203 build_html(html_dir, js_dir, args.use_sync)204 if front_dir is not None:205 ensure_empty_dir(front_dir)206 build_front_page(front_dir, js_dir, args.use_sync)...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run tox automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful