fix code format

This commit is contained in:
2026-03-14 17:56:12 +08:00
parent a0e4fdbacf
commit dd996b92d1
5 changed files with 68 additions and 53 deletions

View File

@@ -126,7 +126,11 @@ fn parse_d_file(content: &str) -> Vec<String> {
let chars: Vec<char> = text.chars().collect();
for i in 0..chars.len() {
if chars[i] == ':' {
if i == 1 && chars[0].is_ascii_alphabetic() && i + 1 < chars.len() && (chars[i+1] == '\\' || chars[i+1] == '/') {
if i == 1
&& chars[0].is_ascii_alphabetic()
&& i + 1 < chars.len()
&& (chars[i + 1] == '\\' || chars[i + 1] == '/')
{
continue; // Windows drive letter
}
target_end = i + 1;
@@ -134,7 +138,11 @@ fn parse_d_file(content: &str) -> Vec<String> {
}
}
let deps_str = if target_end > 0 { &text[target_end..] } else { &text };
let deps_str = if target_end > 0 {
&text[target_end..]
} else {
&text
};
let mut current_path = String::new();
let mut in_escape = false;
@@ -162,7 +170,6 @@ fn parse_d_file(content: &str) -> Vec<String> {
deps
}
#[cfg(test)]
mod tests {
use super::*;
@@ -214,7 +221,10 @@ mod tests {
let src = dir.join("test.c");
fs::write(&src, "int main() {}").unwrap();
let obj = dir.join("test.o");
assert!(needs_rebuild(&src.to_string_lossy(), &obj.to_string_lossy()));
assert!(needs_rebuild(
&src.to_string_lossy(),
&obj.to_string_lossy()
));
}
#[test]
@@ -226,7 +236,10 @@ mod tests {
// Sleep briefly to ensure mtime difference
std::thread::sleep(std::time::Duration::from_millis(50));
fs::write(&obj, "").unwrap();
assert!(!needs_rebuild(&src.to_string_lossy(), &obj.to_string_lossy()));
assert!(!needs_rebuild(
&src.to_string_lossy(),
&obj.to_string_lossy()
));
}
#[test]
@@ -237,7 +250,10 @@ mod tests {
fs::write(&obj, "").unwrap();
std::thread::sleep(std::time::Duration::from_millis(50));
fs::write(&src, "int main() { return 1; }").unwrap();
assert!(needs_rebuild(&src.to_string_lossy(), &obj.to_string_lossy()));
assert!(needs_rebuild(
&src.to_string_lossy(),
&obj.to_string_lossy()
));
}
#[test]
@@ -381,15 +397,25 @@ mod tests {
std::thread::sleep(std::time::Duration::from_millis(50));
fs::write(&obj, "").unwrap();
let d_content = format!("{}: {} {}", obj.to_string_lossy(), src.to_string_lossy(), header.to_string_lossy());
let d_content = format!(
"{}: {} {}",
obj.to_string_lossy(),
src.to_string_lossy(),
header.to_string_lossy()
);
fs::write(&d_file, d_content).unwrap();
assert!(!needs_rebuild(&src.to_string_lossy(), &obj.to_string_lossy()), "should be fresh");
assert!(
!needs_rebuild(&src.to_string_lossy(), &obj.to_string_lossy()),
"should be fresh"
);
std::thread::sleep(std::time::Duration::from_millis(50));
fs::write(&header, "#define A 2").unwrap(); // modify header
assert!(needs_rebuild(&src.to_string_lossy(), &obj.to_string_lossy()), "changed header should trigger rebuild");
assert!(
needs_rebuild(&src.to_string_lossy(), &obj.to_string_lossy()),
"changed header should trigger rebuild"
);
}
}

View File

@@ -218,7 +218,8 @@ fn build_objects(
for line in stdout.lines() {
if let Some(stripped) = line.strip_prefix("Note: including file:") {
headers.push(stripped.trim().to_string());
} else if let Some(stripped) = line.strip_prefix("Примечание: включение файла:") {
} else if let Some(stripped) = line.strip_prefix("Примечание: включение файла:")
{
headers.push(stripped.trim().to_string());
} else {
clean_stdout.push_str(line);
@@ -233,7 +234,10 @@ fn build_objects(
} else {
let trimmed_out = clean_stdout.trim();
let trimmed_err = stderr.trim();
let src_filename = Path::new(source).file_name().and_then(|v| v.to_str()).unwrap_or("");
let src_filename = Path::new(source)
.file_name()
.and_then(|v| v.to_str())
.unwrap_or("");
if !trimmed_out.is_empty() && trimmed_out != src_filename {
print!("{}", clean_stdout);
}

View File

@@ -588,4 +588,3 @@ mod tests {
assert!(config.check(), "valid config should return true");
}
}

View File

@@ -30,10 +30,7 @@ mod tests {
#[test]
fn bin_path_release() {
assert_eq!(
bin_path("release", "hello", None),
"./target/release/hello"
);
assert_eq!(bin_path("release", "hello", None), "./target/release/hello");
}
#[test]
@@ -75,4 +72,3 @@ mod tests {
);
}
}

View File

@@ -122,10 +122,7 @@ fn run_without_toml_fails() {
fn clean_without_toml_fails() {
let dir = unique_sandbox_dir("clean_no_toml");
let out = run_dcr(&["clean"], &dir);
assert!(
!out.status.success(),
"clean without dcr.toml should fail"
);
assert!(!out.status.success(), "clean without dcr.toml should fail");
}
#[test]
@@ -148,7 +145,10 @@ fn init_nonempty_dir_fails() {
// Put a file in the dir so it's not empty
std::fs::write(dir.join("dummy.txt"), "x").expect("failed to write");
let out = run_dcr(&["init"], &dir);
assert!(!out.status.success(), "dcr init in non-empty dir should fail");
assert!(
!out.status.success(),
"dcr init in non-empty dir should fail"
);
let stdout = String::from_utf8_lossy(&out.stdout);
assert!(
stdout.contains("not empty") || stdout.contains("error"),
@@ -163,10 +163,7 @@ fn build_unknown_flag_fails() {
let out = run_dcr(&["init"], &dir);
assert!(out.status.success(), "init should succeed");
let out = run_dcr(&["build", "--foobar"], &dir);
assert!(
!out.status.success(),
"build with unknown flag should fail"
);
assert!(!out.status.success(), "build with unknown flag should fail");
}
#[test]
@@ -183,18 +180,12 @@ fn run_library_project_fails() {
// Edit dcr.toml to set kind = "staticlib"
let toml_path = dir.join("dcr.toml");
let toml = std::fs::read_to_string(&toml_path).expect("failed to read dcr.toml");
let updated = toml.replace(
"kind = \"bin\"",
"kind = \"staticlib\"",
);
let updated = toml.replace("kind = \"bin\"", "kind = \"staticlib\"");
std::fs::write(&toml_path, updated).expect("failed to write dcr.toml");
let envs = [("DCR_COMPILER", compiler)];
let out = run_dcr_env(&["run"], &dir, &envs);
assert!(
!out.status.success(),
"dcr run on staticlib should fail"
);
assert!(!out.status.success(), "dcr run on staticlib should fail");
let stdout = String::from_utf8_lossy(&out.stdout);
assert!(
stdout.contains("library") || stdout.contains("Cannot run"),
@@ -231,10 +222,7 @@ fn clean_specific_profile() {
fn new_no_name_fails() {
let dir = unique_sandbox_dir("new_noname");
let out = run_dcr(&["new"], &dir);
assert!(
!out.status.success(),
"dcr new without name should fail"
);
assert!(!out.status.success(), "dcr new without name should fail");
let stdout = String::from_utf8_lossy(&out.stdout);
assert!(
stdout.contains("not specified") || stdout.contains("error"),
@@ -266,11 +254,7 @@ fn staticlib_build() {
.map(|entries| {
entries
.filter_map(|e| e.ok())
.any(|e| {
e.file_name()
.to_string_lossy()
.ends_with(".a")
})
.any(|e| e.file_name().to_string_lossy().ends_with(".a"))
})
.unwrap_or(false);
assert!(has_lib, "staticlib should produce a .a file");
@@ -302,6 +286,12 @@ fn version_contains_version_string() {
assert!(out.status.success());
let stdout = String::from_utf8_lossy(&out.stdout);
// Should contain version like "dcr 0.2.8 (target)"
assert!(stdout.contains("dcr "), "version output should start with 'dcr '");
assert!(stdout.contains('.'), "version output should contain a version number");
assert!(
stdout.contains("dcr "),
"version output should start with 'dcr '"
);
assert!(
stdout.contains('.'),
"version output should contain a version number"
);
}