Skip to content

Commit e6c83d3

Browse files
committed
transpile: snapshot: allow platform-dependent snapshots
we need to support different output on different platforms due to various differences that we can't paper over. for example, macOS defines some builtins with different types than Linux, and macOS libc and glibc have different typedef setups for fixed-size types, resulting in different type alias preambles. support these by appending the operating system to the snapshot name for results that may vary by platform.
1 parent da66496 commit e6c83d3

File tree

11 files changed

+68
-24
lines changed

11 files changed

+68
-24
lines changed

c2rust-transpile/tests/snapshots.rs

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -46,22 +46,7 @@ fn config() -> TranspilerConfig {
4646
}
4747
}
4848

49-
fn transpile(c_path: &Path) {
50-
let parent_dir_name = c_path
51-
.parent()
52-
.and_then(|dir| dir.file_name())
53-
.and_then(|file_name| file_name.to_str())
54-
.unwrap_or_default();
55-
// Some things transpile differently on Linux vs. macOS,
56-
// as they use `unsigned long` and `unsigned long long` differently for builtins.
57-
// This makes snapshot tests trickier, as the output will be OS-dependent.
58-
// We only test Linux here, as that should be sufficient for these specific tests,
59-
// and because cross-compiling with transpilation is not super straightforward,
60-
// so generating the macOS snapshots locally on Linux is annoying.
61-
if parent_dir_name == "linux" && !cfg!(target_os = "linux") {
62-
return;
63-
}
64-
49+
fn transpile(platform: Option<&str>, c_path: &Path) {
6550
let status = Command::new("clang")
6651
.args(&["-c", "-o", "/dev/null"])
6752
.arg(c_path)
@@ -76,7 +61,12 @@ fn transpile(c_path: &Path) {
7661
let rs_path = c_path.with_extension("rs");
7762
let rs = fs::read_to_string(&rs_path).unwrap();
7863
let debug_expr = format!("cat {}", rs_path.display());
79-
insta::assert_snapshot!("transpile", &rs, &debug_expr);
64+
65+
let name = platform
66+
.map(|platform| ["transpile", platform].join("-"))
67+
.unwrap_or("transpile".into());
68+
69+
insta::assert_snapshot!(name, &rs, &debug_expr);
8070

8171
let status = Command::new("rustc")
8272
.args(&["--crate-type", "lib", "--edition", "2021", "-o", "-"])
@@ -87,9 +77,21 @@ fn transpile(c_path: &Path) {
8777

8878
#[test]
8979
fn transpile_all() {
90-
// We need to do this as a single glob,
91-
// as `insta` removes the common prefix to all matches files,
92-
// and if we do this as separate globs (for linux-only files),
93-
// they'll overwrite each other.
94-
insta::glob!("snapshots/**/*.c", transpile);
80+
insta::glob!("snapshots/*.c", |x| transpile(None, x));
81+
82+
// Some things transpile differently on Linux vs. macOS,
83+
// as they use `unsigned long` and `unsigned long long` differently for builtins.
84+
// This makes snapshot tests trickier, as the output will be OS-dependent.
85+
// We handle this by adding OS name to the snapshot result filename.
86+
#[allow(unused)]
87+
let platform = "unknown";
88+
89+
#[cfg(target_os = "linux")]
90+
let platform = "linux";
91+
#[cfg(target_os = "macos")]
92+
let platform = "macos";
93+
94+
insta::with_settings!({snapshot_suffix => platform}, {
95+
insta::glob!("snapshots/platform-specific/*.c", |x| transpile(Some(platform), x));
96+
});
9597
}
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
---
22
source: c2rust-transpile/tests/snapshots.rs
3-
expression: cat tests/snapshots/linux/rotate.rs
4-
input_file: c2rust-transpile/tests/snapshots/linux/rotate.c
3+
assertion_line: 67
4+
expression: cat tests/snapshots/platform-specific/rotate.rs
5+
input_file: c2rust-transpile/tests/snapshots/platform-specific/rotate.c
56
---
67
#![allow(
78
dead_code,
@@ -28,3 +29,4 @@ pub unsafe extern "C" fn rotate_right_64(
2829
.rotate_right(4 as std::ffi::c_int as std::ffi::c_ulong as u32)
2930
as std::ffi::c_ulonglong;
3031
}
32+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
source: c2rust-transpile/tests/snapshots.rs
3+
assertion_line: 67
4+
expression: cat tests/snapshots/platform-specific/rotate.rs
5+
input_file: c2rust-transpile/tests/snapshots/platform-specific/rotate.c
6+
---
7+
#![allow(
8+
dead_code,
9+
mutable_transmutes,
10+
non_camel_case_types,
11+
non_snake_case,
12+
non_upper_case_globals,
13+
unused_assignments,
14+
unused_mut
15+
)]
16+
#[no_mangle]
17+
pub unsafe extern "C" fn rotate_left_64(
18+
mut x: std::ffi::c_ulonglong,
19+
) -> std::ffi::c_ulonglong {
20+
return x.rotate_left(4 as std::ffi::c_int as std::ffi::c_ulonglong as u32);
21+
}
22+
#[no_mangle]
23+
pub unsafe extern "C" fn rotate_right_64(
24+
mut x: std::ffi::c_ulonglong,
25+
) -> std::ffi::c_ulonglong {
26+
return x.rotate_right(4 as std::ffi::c_int as std::ffi::c_ulonglong as u32);
27+
}
28+

c2rust-transpile/tests/snapshots/[email protected]

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
---
22
source: c2rust-transpile/tests/snapshots.rs
3+
assertion_line: 67
34
expression: cat tests/snapshots/atomics.rs
45
input_file: c2rust-transpile/tests/snapshots/atomics.c
56
---
@@ -44,3 +45,4 @@ pub unsafe extern "C" fn c11_atomics(mut x: std::ffi::c_int) -> std::ffi::c_int
4445
fresh1.1;
4546
return x;
4647
}
48+

c2rust-transpile/tests/snapshots/[email protected]

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
---
22
source: c2rust-transpile/tests/snapshots.rs
3+
assertion_line: 67
34
expression: cat tests/snapshots/factorial.rs
45
input_file: c2rust-transpile/tests/snapshots/factorial.c
56
---
@@ -24,3 +25,4 @@ pub unsafe extern "C" fn factorial(mut n: std::ffi::c_ushort) -> std::ffi::c_ush
2425
}
2526
return result;
2627
}
28+

c2rust-transpile/tests/snapshots/[email protected]

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
---
22
source: c2rust-transpile/tests/snapshots.rs
3+
assertion_line: 67
34
expression: cat tests/snapshots/gotos.rs
45
input_file: c2rust-transpile/tests/snapshots/gotos.c
56
---
@@ -22,3 +23,4 @@ pub unsafe extern "C" fn sum(mut count: std::ffi::c_int) -> std::ffi::c_int {
2223
}
2324
return x;
2425
}
26+

c2rust-transpile/tests/snapshots/[email protected]

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
---
22
source: c2rust-transpile/tests/snapshots.rs
3+
assertion_line: 67
34
expression: cat tests/snapshots/insertion.rs
45
input_file: c2rust-transpile/tests/snapshots/insertion.c
56
---
@@ -30,3 +31,4 @@ pub unsafe extern "C" fn insertion_sort(n: std::ffi::c_int, p: *mut std::ffi::c_
3031
i;
3132
}
3233
}
34+

c2rust-transpile/tests/snapshots/[email protected]

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
---
22
source: c2rust-transpile/tests/snapshots.rs
3+
assertion_line: 67
34
expression: cat tests/snapshots/rotate.rs
45
input_file: c2rust-transpile/tests/snapshots/rotate.c
56
---
@@ -40,3 +41,4 @@ pub unsafe extern "C" fn rotate_right_16(
4041
pub unsafe extern "C" fn rotate_right_32(mut x: std::ffi::c_uint) -> std::ffi::c_uint {
4142
return x.rotate_right(3 as std::ffi::c_int as std::ffi::c_uint as u32);
4243
}
44+

0 commit comments

Comments
 (0)