Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions c2rust-refactor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ rand = "0.7"
fs-err = "3.3.0"
insta = "1.46.3"
c2rust-rust-tools = { path = "../c2rust-rust-tools", version = "0.22.1" }
itertools = "0.14.0"

[features]
default = []
Expand Down
61 changes: 55 additions & 6 deletions c2rust-refactor/tests/snapshots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ use c2rust_rust_tools::rustc;
use c2rust_rust_tools::rustfmt;
use c2rust_rust_tools::EDITION;
use insta::assert_snapshot;
use itertools::Itertools;
use std::path::Path;

#[must_use]
struct RefactorTest<'a> {
command: &'a str,
command_args: &'a [&'a str],
path: Option<&'a str>,
old_expect_format_error: bool,
new_expect_format_error: bool,
Expand All @@ -22,6 +24,7 @@ struct RefactorTest<'a> {
fn refactor(command: &str) -> RefactorTest {
RefactorTest {
command,
command_args: &[],
path: None,
old_expect_format_error: false,
new_expect_format_error: false,
Expand All @@ -31,6 +34,14 @@ fn refactor(command: &str) -> RefactorTest {
}

impl<'a> RefactorTest<'a> {
#[allow(unused)] // TODO remove, will be used soon
pub fn command_args(self, command_args: &'a [&'a str]) -> Self {
Self {
command_args,
..self
}
}

pub fn named(self, path: &'a str) -> Self {
Self {
path: Some(path),
Expand Down Expand Up @@ -80,6 +91,7 @@ impl<'a> RefactorTest<'a> {
let Self {
command,
path,
command_args,
old_expect_format_error,
new_expect_format_error,
old_expect_compile_error,
Expand All @@ -95,6 +107,7 @@ impl<'a> RefactorTest<'a> {
};
test_refactor(
command,
command_args,
path,
old_expect_format_error,
new_expect_format_error,
Expand All @@ -104,8 +117,25 @@ impl<'a> RefactorTest<'a> {
}
}

/// Replace all non-alphanumeric characters and `-_.` with `_`s
/// so that we have a sanitized, idiomatic file name that excludes weird characters,
/// even if they're technically allowed in a file name.
fn sanitize_file_name(file_name: &str) -> String {
file_name
.chars()
.map(|c| {
if c.is_ascii_alphanumeric() || c == '-' || c == '_' || c == '.' {
c
} else {
'_'
}
})
.collect()
}

fn test_refactor(
command: &str,
command_args: &[&str],
path: &str,
old_expect_format_error: bool,
new_expect_format_error: bool,
Expand All @@ -132,7 +162,11 @@ fn test_refactor(
rewrite_modes: vec![OutputMode::Alongside],
commands: vec![RefactorCommand {
name: command.to_owned(),
args: vec![],
args: command_args
.iter()
.copied()
.map(|arg| arg.to_owned())
.collect(),
}],
rustc_args: RustcArgSource::CmdLine(rustc_args.map(|arg| arg.to_owned()).to_vec()),
cursors: Default::default(),
Expand All @@ -153,13 +187,28 @@ fn test_refactor(

let new_rs = fs_err::read_to_string(&new_path).unwrap();

let snapshot_name = if Some(command) == path.strip_suffix(".rs") {
format!("refactor-{path}")
let snapshot_parts_no_cmd;
let snapshot_parts_with_cmd;
let snapshot_name_parts = if Some(command) == path.strip_suffix(".rs") {
snapshot_parts_no_cmd = ["refactor", path];
&snapshot_parts_no_cmd[..]
} else {
format!("refactor-{command}-{path}")
snapshot_parts_with_cmd = ["refactor", command, path];
&snapshot_parts_with_cmd[..]
};
let rustc_args = shlex::try_join(rustc_args).unwrap();
let debug_expr = format!("c2rust-refactor {command} --rewrite-mode alongside -- {rustc_args}");

let snapshot_name = [snapshot_name_parts, command_args]
Comment thread
kkysen marked this conversation as resolved.
.into_iter()
.flatten()
.join("-");
let snapshot_name = sanitize_file_name(&snapshot_name);
let cli_args = [
&["c2rust-refactor", command],
command_args,
&["--rewrite-mode", "alongside", "--"],
&rustc_args,
];
let debug_expr = shlex::try_join(cli_args.into_iter().flatten().copied()).unwrap();

assert_snapshot!(snapshot_name, new_rs, &debug_expr);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: c2rust-refactor/tests/snapshots.rs
expression: c2rust-refactor rename_unnamed --rewrite-mode alongside -- tests/snapshots/rename_unnamed.rs --edition 2021
expression: c2rust-refactor rename_unnamed --rewrite-mode alongside -- tests/snapshots/rename_unnamed.rs --edition 2021
---
pub mod bar {
pub mod bar_h {
Expand Down