use super::render_main_test_go;
#[test]
fn mock_server_bootstrap_imports_are_gofmt_sorted() {
let out = render_main_test_go("testing_data", true, false, &Default::default());
let import_paths: Vec<&str> = out
.lines()
.skip_while(|l| *l != "import (")
.skip(1)
.take_while(|l| *l != ")")
.map(|l| l.trim().trim_matches('"'))
.collect();
let mut sorted = import_paths.clone();
sorted.sort_unstable();
assert_eq!(
import_paths, sorted,
"mock-server bootstrap import block must already be gofmt-sorted; got:\n{out}"
);
}
#[test]
fn harness_spawn_imports_are_gofmt_sorted() {
let out = render_main_test_go("testing_data", false, true, &Default::default());
let import_paths: Vec<&str> = out
.lines()
.skip_while(|l| *l != "import (")
.skip(1)
.take_while(|l| *l != ")")
.map(|l| l.trim().trim_matches('"'))
.collect();
let mut sorted = import_paths.clone();
sorted.sort_unstable();
assert_eq!(
import_paths, sorted,
"harness-spawn import block must already be gofmt-sorted; got:\n{out}"
);
}
#[test]
fn plain_path_imports_are_unchanged_by_sorting() {
let out = render_main_test_go("testing_data", false, false, &Default::default());
let import_paths: Vec<&str> = out
.lines()
.skip_while(|l| *l != "import (")
.skip(1)
.take_while(|l| *l != ")")
.map(|l| l.trim().trim_matches('"'))
.collect();
assert_eq!(
import_paths,
vec!["os", "path/filepath", "runtime", "testing"],
"got:\n{out}"
);
}
#[test]
fn err_check_panics_are_never_single_line() {
let out = render_main_test_go("testing_data", true, false, &Default::default());
assert!(
!out.contains("{ panic(err) }"),
"if-err-panic must be split across lines, not emitted as a one-liner; got:\n{out}"
);
}
#[test]
fn drain_goroutine_is_never_single_line() {
let out = render_main_test_go("testing_data", true, false, &Default::default());
assert!(
!out.contains("go func() { for scanner.Scan() { } }()"),
"drain goroutine must be split across lines, not emitted as a one-liner; got:\n{out}"
);
}
#[test]
fn env_concat_has_no_spaces_around_plus() {
let mut env = std::collections::BTreeMap::new();
env.insert("SOME_VAR".to_string(), "1".to_string());
let out = render_main_test_go("testing_data", true, false, &env);
assert!(
out.contains("\"SOME_VAR=\"+v"),
"env concatenation must have no spaces around `+`; got:\n{out}"
);
assert!(
!out.contains("\"SOME_VAR=\" + v"),
"env concatenation must not have spaces around `+`; got:\n{out}"
);
}
#[test]
fn rendered_main_test_go_matches_gofmt_when_available() {
let mut env = std::collections::BTreeMap::new();
env.insert("SOME_VAR".to_string(), "1".to_string());
for out in [
render_main_test_go("testing_data", true, false, &env),
render_main_test_go("testing_data", false, true, &Default::default()),
render_main_test_go("testing_data", false, false, &Default::default()),
] {
assert_gofmt_no_op(&out);
}
}
fn assert_gofmt_no_op(code: &str) {
use std::io::Write as _;
let Ok(mut child) = std::process::Command::new("gofmt")
.stdin(std::process::Stdio::piped())
.stdout(std::process::Stdio::piped())
.spawn()
else {
return;
};
child
.stdin
.take()
.expect("gofmt stdin")
.write_all(code.as_bytes())
.expect("write Go source");
let output = child.wait_with_output().expect("wait for gofmt");
assert!(output.status.success(), "{}", String::from_utf8_lossy(&output.stderr));
assert_eq!(
String::from_utf8(output.stdout).expect("gofmt output is UTF-8"),
code,
"render_main_test_go output must already be gofmt-canonical"
);
}