summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Lively <tlively@google.com>2021-06-17 10:30:06 -0700
committerThomas Lively <7121787+tlively@users.noreply.github.com>2021-06-17 14:35:56 -0400
commit8de4349ebdb6ce398ef4939f9f375a7413d99b09 (patch)
treecbe1e20501cee9eabc63683846cee4d466732fd7
parentacdea58d62079c1477eb35f460741908063ec26a (diff)
downloadbinaryen-8de4349ebdb6ce398ef4939f9f375a7413d99b09.tar.gz
binaryen-8de4349ebdb6ce398ef4939f9f375a7413d99b09.tar.bz2
binaryen-8de4349ebdb6ce398ef4939f9f375a7413d99b09.zip
Add a `foreach` script for use in lit tests
The major drawback of lit tests is that so far they have only supported a single module per test file. This commit adds a new utility script that splits an input file into multiple files and runs a command on each of them, giving lit tests a simple way to test multiple modules per file.
-rwxr-xr-xscripts/foreach.py46
-rw-r--r--test/lit/lit.cfg.py9
2 files changed, 51 insertions, 4 deletions
diff --git a/scripts/foreach.py b/scripts/foreach.py
new file mode 100755
index 000000000..536741c22
--- /dev/null
+++ b/scripts/foreach.py
@@ -0,0 +1,46 @@
+#!/usr/bin/env python3
+
+# Copyright 2021 WebAssembly Community Group participants
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import sys
+import subprocess
+
+from test import support
+
+
+# Usage: foreach infile tempfile cmd...
+#
+# Split 'infile', which contains multiple text modules, into separate temp files
+# containing one text module each and named `tempfile`.0, `tempfile`.1, etc. Run
+# `cmd` with the current temp file appended to it on all the temp files in
+# sequence. Exit with code 0 only if all of the subprocesses exited with code 0.
+def main():
+ infile = sys.argv[1]
+ tempfile = sys.argv[2]
+ cmd = sys.argv[3:]
+ returncode = 0
+ for i, (module, asserts) in enumerate(support.split_wast(infile)):
+ tempname = tempfile + '.' + str(i)
+ with open(tempname, 'w') as temp:
+ print(module, file=temp)
+ new_cmd = cmd + [tempname]
+ result = subprocess.run(new_cmd)
+ if result.returncode != 0:
+ returncode = result.returncode
+ sys.exit(returncode)
+
+
+if __name__ == '__main__':
+ main()
diff --git a/test/lit/lit.cfg.py b/test/lit/lit.cfg.py
index 60ba1ced6..bc034b349 100644
--- a/test/lit/lit.cfg.py
+++ b/test/lit/lit.cfg.py
@@ -15,7 +15,8 @@ for tool_file in os.listdir(bin_dir):
tool = tool_file[:-4] if tool_file.endswith('.exe') else tool_file
config.substitutions.append((tool, tool_path))
-# Also make the `not` command available
-not_file = config.binaryen_src_root + '/scripts/not.py'
-python = sys.executable.replace('\\', '/')
-config.substitutions.append(('not', python + ' ' + not_file))
+# Also make the `not` and `foreach` commands available
+for tool in ('not', 'foreach'):
+ tool_file = config.binaryen_src_root + '/scripts/' + tool + '.py'
+ python = sys.executable.replace('\\', '/')
+ config.substitutions.append((tool, python + ' ' + tool_file))