summaryrefslogtreecommitdiff
path: root/rules
diff options
context:
space:
mode:
authorOliver Reiche <oliver.reiche@huawei.com>2022-12-07 14:50:53 +0100
committerOliver Reiche <oliver.reiche@huawei.com>2022-12-09 13:13:21 +0100
commit72843d795d967b67148ddf9ded23d241f63c53d3 (patch)
tree099ec6fef4c1524b6610eda20db2313d167197ee /rules
parent7d260287328a38fbc1430bed56cbdaeaebd58efd (diff)
downloadjustbuild-72843d795d967b67148ddf9ded23d241f63c53d3.tar.gz
rules: Add argument support for test binaries
Diffstat (limited to 'rules')
-rw-r--r--rules/CC/test/RULES41
-rwxr-xr-xrules/CC/test/test_runner.py69
-rw-r--r--rules/CC/test/test_runner.sh50
3 files changed, 101 insertions, 59 deletions
diff --git a/rules/CC/test/RULES b/rules/CC/test/RULES
index b42b6c4f..44cf1c41 100644
--- a/rules/CC/test/RULES
+++ b/rules/CC/test/RULES
@@ -7,6 +7,7 @@
, "target_fields": ["srcs", "private-hdrs", "private-deps", "data"]
, "string_fields":
[ "name"
+ , "args"
, "stage"
, "pure C"
, "private-defines"
@@ -25,13 +26,14 @@
, "CC_TEST_LAUNCHER"
]
, "implicit":
- {"defaults": [["./", "..", "defaults"]], "runner": ["test_runner.sh"]}
+ {"defaults": [["./", "..", "defaults"]], "runner": ["test_runner.py"]}
, "field_doc":
{ "name":
[ "The name of the test"
, ""
, "Used to name the test binary as well as for staging the test result"
]
+ , "args": ["Command line arguments for the test binary"]
, "srcs": ["The sources of the test binary"]
, "private-hdrs":
[ "Any additional header files that need to be present when compiling"
@@ -198,7 +200,7 @@
}
, "body":
{ "type": "singleton_map"
- , "key": "runner.sh"
+ , "key": "runner"
, "value": {"type": "var", "name": "runner"}
}
}
@@ -206,6 +208,31 @@
}
}
]
+ , [ "test-args"
+ , { "type": "singleton_map"
+ , "key": "test-args.json"
+ , "value":
+ { "type": "BLOB"
+ , "data":
+ { "type": "json_encode"
+ , "$1": {"type": "FIELD", "name": "args", "default": []}
+ }
+ }
+ }
+ ]
+ , [ "test-launcher"
+ , { "type": "singleton_map"
+ , "key": "test-launcher.json"
+ , "value":
+ { "type": "BLOB"
+ , "data":
+ { "type": "json_encode"
+ , "$1":
+ {"type": "var", "name": "CC_TEST_LAUNCHER", "default": []}
+ }
+ }
+ }
+ ]
, [ "data"
, { "type": "disjoint_map_union"
, "msg": "Data runfiles may not conflict"
@@ -234,16 +261,12 @@
, "$1": {"type": "var", "name": "data"}
}
, {"type": "var", "name": "runner"}
+ , {"type": "var", "name": "test-args"}
+ , {"type": "var", "name": "test-launcher"}
, {"type": "var", "name": "staged test binary"}
]
}
- , "cmd":
- { "type": "++"
- , "$1":
- [ ["sh", "./runner.sh"]
- , {"type": "var", "name": "CC_TEST_LAUNCHER", "default": []}
- ]
- }
+ , "cmd": ["./runner"]
, "env":
{ "type": "var"
, "name": "TEST_ENV"
diff --git a/rules/CC/test/test_runner.py b/rules/CC/test/test_runner.py
new file mode 100755
index 00000000..0647621f
--- /dev/null
+++ b/rules/CC/test/test_runner.py
@@ -0,0 +1,69 @@
+#!/usr/bin/env python3
+# Copyright 2022 Huawei Cloud Computing Technology Co., Ltd.
+#
+# 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 json
+import os
+import subprocess
+import time
+
+time_start = time.time()
+time_stop = 0
+result = "UNKNOWN"
+stderr = ""
+stdout = ""
+
+
+def dump_results():
+ with open("result", "w") as f:
+ f.write("%s\n" % (result, ))
+ with open("time-start", "w") as f:
+ f.write("%d\n" % (time_start, ))
+ with open("time-stop", "w") as f:
+ f.write("%d\n" % (time_stop, ))
+ with open("stdout", "w") as f:
+ f.write("%s\n" % (stdout, ))
+ with open("stderr", "w") as f:
+ f.write("%s\n" % (stderr, ))
+
+
+dump_results()
+
+TEMP_DIR = os.path.realpath("scratch")
+os.makedirs(TEMP_DIR, exist_ok=True)
+
+WORK_DIR = os.path.realpath("work")
+os.makedirs(WORK_DIR, exist_ok=True)
+
+ENV = dict(os.environ, TEST_TMPDIR=TEMP_DIR)
+
+with open('test-launcher.json') as f:
+ test_launcher = json.load(f)
+
+with open('test-args.json') as f:
+ test_args = json.load(f)
+
+ret = subprocess.run(test_launcher + ["../test"] + test_args,
+ cwd=WORK_DIR,
+ env=ENV,
+ capture_output=True)
+
+time_stop = time.time()
+result = "PASS" if ret.returncode == 0 else "FAIL"
+stdout = ret.stdout.decode("utf-8")
+stderr = ret.stderr.decode("utf-8")
+
+dump_results()
+
+if result != "PASS": exit(1)
diff --git a/rules/CC/test/test_runner.sh b/rules/CC/test/test_runner.sh
deleted file mode 100644
index 84b9072f..00000000
--- a/rules/CC/test/test_runner.sh
+++ /dev/null
@@ -1,50 +0,0 @@
-#!/bin/sh
-# Copyright 2022 Huawei Cloud Computing Technology Co., Ltd.
-#
-# 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.
-
-
-# ensure all required outputs are present
-touch stdout
-touch stderr
-RESULT=UNKNOWN
-echo "${RESULT}" > result
-echo UNKNOWN > time-start
-echo UNKNOWN > time-stop
-
-mkdir scratch
-export TEST_TMPDIR=$(realpath scratch)
-# Change to the working directory; note: the test might not
-# have test data, so we have to ensure the presence of the work
-# directory.
-
-mkdir -p work
-cd work
-
-date +%s > ../time-start
-# TODO:
-# - proper wrapping with timeout
-# - test arguments to select specific test cases
-if "$@" ../test > ../stdout 2> ../stderr
-then
- RESULT=PASS
-else
- RESULT=FAIL
-fi
-date +%s > ../time-stop
-echo "${RESULT}" > ../result
-
-if [ "${RESULT}" '!=' PASS ]
-then
- exit 1;
-fi