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
29 changes: 27 additions & 2 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ pub struct BundleArgs {
/// C++ source file to bundle
pub file: PathBuf,

/// Extra options passed through to the compiler
#[arg(trailing_var_arg = true, allow_hyphen_values = true)]
/// Extra options after `--` passed straight to the compiler
#[arg(last = true, value_name = "COMPILER OPTIONS")]
pub options: Vec<String>,
}

Expand Down Expand Up @@ -90,3 +90,28 @@ pub enum LibraryCommand {
verbose: bool,
},
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn options_after_file_are_parsed_as_risundle_flags() {
let args = BundleArgs::try_parse_from(["risundle", "main.cpp", "-e"]).unwrap();
assert!(args.embed);
assert!(args.options.is_empty());
}

#[test]
fn double_dash_separates_compiler_options() {
let args =
BundleArgs::try_parse_from(["risundle", "main.cpp", "--", "-std=gnu++20", "-O2"])
.unwrap();
assert_eq!(args.options, ["-std=gnu++20", "-O2"]);
}

#[test]
fn hyphen_arguments_before_double_dash_are_rejected() {
assert!(BundleArgs::try_parse_from(["risundle", "main.cpp", "-O2"]).is_err());
}
}
33 changes: 32 additions & 1 deletion tests/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,11 @@ fn run_bundle(sandbox: &Sandbox, args: &[&str]) -> String {
.arg("main.cpp")
.output()
.expect("run bundle");
assert!(output.status.success(), "bundle failed");
assert!(
output.status.success(),
"bundle failed: {}",
String::from_utf8_lossy(&output.stderr)
);
String::from_utf8(output.stdout).expect("utf-8")
}

Expand Down Expand Up @@ -344,6 +348,33 @@ fn bundle_ignores_identifiers_in_comments_and_strings() {
assert_eq!(compile_and_run(&sandbox, &bundled).trim(), "5");
}

#[test]
fn bundle_passes_options_after_double_dash_to_compiler() {
let sandbox = Sandbox::new();
sandbox.write(
"main.cpp",
"#ifdef RISUNDLE_TEST_ANSWER\nint main() { return RISUNDLE_TEST_ANSWER; }\n\
#else\n#error RISUNDLE_TEST_ANSWER is not defined\n#endif\n",
);

// -D が届かなければ #error でプリプロセスごと失敗するので、成功 = 受け渡しの証明。
let output = sandbox
.risundle()
.args(["-k", STD, "main.cpp", "--", "-DRISUNDLE_TEST_ANSWER=0"])
.output()
.expect("run bundle");
assert!(
output.status.success(),
"bundle failed: {}",
String::from_utf8_lossy(&output.stderr)
);
let bundled = String::from_utf8(output.stdout).expect("utf-8");
assert!(
bundled.contains("return 0"),
"-D で定義したマクロが展開されるべき"
);
}

#[test]
fn embed_includes_original_source_as_comment() {
let sandbox = Sandbox::new();
Expand Down
Loading