Skip to content

Commit d33dad4

Browse files
authored
Speed-up unit tests (#6)
First unit test compiles libcc2rs and rules in cargo-target, then all unit-tests use the precompiled libraries from cargo-target instead of building them each time.
1 parent 63e59b1 commit d33dad4

2 files changed

Lines changed: 18 additions & 17 deletions

File tree

tests/lit/lit/formats/Cpp2RustTest.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ def read_rust_version():
1313
with open(toolchain_path, 'rb') as f:
1414
return tomli.load(f)['toolchain']['channel']
1515

16+
def shared_target_dir():
17+
return os.path.abspath(os.path.join(
18+
os.path.dirname(__file__),
19+
'../../../../build/tmp/cargo-target'))
20+
21+
def cargo_env():
22+
return dict(os.environ, CARGO_TARGET_DIR=os.path.abspath(shared_target_dir()))
23+
1624
class Cpp2RustTest(TestFormat):
1725
def __init__(self):
1826
self.regex_xfail = re.compile(r"//\s*XFAIL:\s*(.*)")
@@ -120,24 +128,31 @@ def fail(str, code = fail_code):
120128
fromfile='expected', tofile='generated'))
121129
return fail('different output\n' + diff)
122130

131+
pkg_name = "test_" + re.sub(r'[^a-zA-Z0-9_]', '_', os.path.basename(tmp_dir))
132+
123133
# Check if we can compile the rust file
124134
with open(tmp_dir + "/rust-toolchain.toml", 'w') as f:
125135
f.write(f'[toolchain]\nchannel = "{self.rust_version}"\n')
126136
with open(tmp_dir + "/Cargo.toml", 'w') as f:
127137
f.write(f"""
128138
[package]
129-
name = "test"
139+
name = "{pkg_name}"
130140
version = "0.1.0"
131141
edition = "2021"
132142
rust-version = "{self.rust_version}"
133143
144+
[[bin]]
145+
name = "{pkg_name}"
146+
path = "src/main.rs"
147+
134148
[dependencies]
135149
libc = "0.2.169"
136150
libcc2rs = {{ path = "../../../libcc2rs" }}
151+
rules = {{ path = "../../../rules" }}
137152
""")
138153

139154
cmd = ['cargo', 'build', '--release', '--quiet']
140-
_, err, returncode = lit.util.executeCommand(cmd, tmp_dir)
155+
_, err, returncode = lit.util.executeCommand(cmd, tmp_dir, env=cargo_env())
141156
if should_not_compile:
142157
if returncode != 0:
143158
shutil.rmtree(tmp_dir, True)
@@ -146,7 +161,7 @@ def fail(str, code = fail_code):
146161
if returncode != 0:
147162
return fail('cargo failed\n' + err)
148163

149-
rust_bin = tmp_dir + "/target/release/test"
164+
rust_bin = os.path.join(shared_target_dir(), "release", pkg_name)
150165

151166
if not skip_run:
152167
if should_panic:

tests/unit/Cargo.toml

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)