Skip to content

Commit fc81b05

Browse files
authored
Do not enable cfg(test) in main package for integration tests (#1334)
1 parent 555b958 commit fc81b05

File tree

2 files changed

+59
-12
lines changed

2 files changed

+59
-12
lines changed

scarb/src/ops/resolve.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,16 @@ fn cairo_compilation_unit_for_target(
258258
let cairo_plugins = solution.cairo_plugins.as_ref().unwrap();
259259

260260
let cfg_set = build_cfg_set(&member_target);
261+
let no_test_cfg_set = cfg_set
262+
.iter()
263+
.filter(|cfg| **cfg != Cfg::name("test"))
264+
.cloned()
265+
.collect();
266+
let no_test_cfg_set = if no_test_cfg_set != cfg_set {
267+
Some(no_test_cfg_set)
268+
} else {
269+
None
270+
};
261271

262272
let props: TestTargetProps = member_target.props()?;
263273
let is_integration_test = props.test_type == TestTargetType::Integration;
@@ -304,17 +314,7 @@ fn cairo_compilation_unit_for_target(
304314
enabled_features,
305315
)?
306316
} else {
307-
let component_cfg_set = cfg_set
308-
.iter()
309-
.filter(|cfg| **cfg != Cfg::name("test"))
310-
.cloned()
311-
.collect();
312-
313-
if component_cfg_set != cfg_set {
314-
Some(component_cfg_set)
315-
} else {
316-
None
317-
}
317+
no_test_cfg_set.clone()
318318
}
319319
};
320320

@@ -341,7 +341,7 @@ fn cairo_compilation_unit_for_target(
341341
components.push(CompilationUnitComponent::try_new(
342342
member.clone(),
343343
vec![target],
344-
None,
344+
no_test_cfg_set,
345345
)?);
346346

347347
// Set test package as main package for this compilation unit.

scarb/tests/build_targets.rs

+47
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,53 @@ fn compile_test_target() {
360360
assert_eq!(tests.len(), 0);
361361
}
362362

363+
#[test]
364+
fn integration_tests_do_not_enable_cfg_in_main_package() {
365+
let t = TempDir::new().unwrap();
366+
ProjectBuilder::start()
367+
.name("hello")
368+
.lib_cairo(indoc! {r#"
369+
#[cfg(test)]
370+
fn f() -> felt252 { 42 }
371+
"#})
372+
.build(&t);
373+
t.child("tests").create_dir_all().unwrap();
374+
t.child("tests/test1.cairo")
375+
.write_str(indoc! {r#"
376+
#[cfg(test)]
377+
mod tests {
378+
use hello::f;
379+
#[test]
380+
fn it_works() {
381+
assert(f() == 42, 'it works!');
382+
}
383+
}
384+
"#})
385+
.unwrap();
386+
387+
Scarb::quick_snapbox()
388+
.arg("build")
389+
.arg("--test")
390+
.current_dir(&t)
391+
.assert()
392+
.failure()
393+
.stdout_matches(indoc! {r#"
394+
[..]Compiling test(hello_unittest) hello v1.0.0 ([..]Scarb.toml)
395+
[..]Compiling test(hello_integrationtest) hello_integrationtest v1.0.0 ([..]Scarb.toml)
396+
error: Identifier not found.
397+
--> [..]test1.cairo:3:16
398+
use hello::f;
399+
^
400+
401+
error: Type annotations needed. Failed to infer ?0.
402+
--> [..]test1.cairo:6:16
403+
assert(f() == 42, 'it works!');
404+
^*******^
405+
406+
error: could not compile `hello_integrationtest` due to previous error
407+
"#});
408+
}
409+
363410
#[test]
364411
fn detect_single_file_test_targets() {
365412
let t = TempDir::new().unwrap();

0 commit comments

Comments
 (0)