Skip to content

Commit 6ec6fb5

Browse files
Add a test command to build.py and also use it in the CI (#283)
1 parent afbbb2e commit 6ec6fb5

File tree

3 files changed

+47
-5
lines changed

3 files changed

+47
-5
lines changed

.github/workflows/rust.yml

+1-4
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,7 @@ jobs:
7878
override: true
7979

8080
- name: Run cargo test
81-
uses: actions-rs/cargo@v1
82-
with:
83-
command: test
84-
args: -Zbuild-std=std --target x86_64-unknown-linux-gnu --features=exts
81+
run: uefi-test-runner/build.py test
8582

8683
lints:
8784
name: Lints

uefi-test-runner/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Available commands:
3030
- `run`: (re)build and run
3131
- `doc`: generate documentation
3232
- `clippy`: run Clippy
33+
- `test`: run tests and doctests
3334

3435
Available options:
3536

uefi-test-runner/build.py

+45-1
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,48 @@ def doc():
128128
'--package', 'uefi-services',
129129
], check=True)
130130

131+
def get_rustc_cfg():
132+
'Run and parse "rustc --print=cfg" as key, val pairs.'
133+
output = sp.run([
134+
'rustc', '--print=cfg'
135+
], check=True, capture_output=True, text=True).stdout
136+
for line in output.splitlines():
137+
parts = line.split('=', maxsplit=1)
138+
# Only interested in the lines that look like this: key="val"
139+
if len(parts) == 2:
140+
key = parts[0]
141+
val = parts[1]
142+
# Strip the quotes
143+
if val.startswith('"') and val.endswith('"'):
144+
val = val[1:-1]
145+
yield key, val
146+
147+
def get_host_target():
148+
'Get the host target, e.g. "x86_64-unknown-linux-gnu".'
149+
cfg = dict(get_rustc_cfg())
150+
arch = cfg['target_arch']
151+
vendor = cfg['target_vendor']
152+
os = cfg['target_os']
153+
env = cfg['target_env']
154+
return f'{arch}-{vendor}-{os}-{env}'
155+
156+
def test():
157+
'Run tests and doctests using the host target.'
158+
repo_dir = Path(__file__).resolve().parent.parent
159+
sp.run([
160+
'cargo', 'test',
161+
# Specifying the manifest path allows this command to
162+
# run successfully regardless of the CWD.
163+
'--manifest-path', repo_dir / 'Cargo.toml',
164+
'-Zbuild-std=std',
165+
'--target', get_host_target(),
166+
'--features', 'exts',
167+
'--package', 'uefi',
168+
'--package', 'uefi-macros',
169+
# Don't test uefi-services (or the packages that depend on it)
170+
# as it has lang items that conflict with `std`.
171+
], check=True)
172+
131173
def ovmf_files(ovmf_dir):
132174
'Returns the tuple of paths to the OVMF code and vars firmware files, given the directory'
133175
if SETTINGS['arch'] == 'x86_64':
@@ -348,7 +390,7 @@ def main():
348390
parser = argparse.ArgumentParser(description=desc)
349391

350392
parser.add_argument('verb', help='command to run', type=str,
351-
choices=['build', 'run', 'doc', 'clippy'])
393+
choices=['build', 'run', 'doc', 'clippy', 'test'])
352394

353395
parser.add_argument('--target', help='target to build for (default: %(default)s)', type=str,
354396
choices=['x86_64', 'aarch64'], default='x86_64')
@@ -382,6 +424,8 @@ def main():
382424
clippy()
383425
elif verb == 'doc':
384426
doc()
427+
elif verb == 'test':
428+
test()
385429
elif verb == 'run' or verb is None or opts.verb == '':
386430
# Run the program, by default.
387431
run_qemu()

0 commit comments

Comments
 (0)