Files
lightgui_rust/crates/lightgui-support/tests/cmake_tests.rs
T
cnmcdee 3d53281388 Add lightgui-debug and lightgui-support: GDB MI debugger + theme/preferences/codeview/cmake ports
lightgui-debug: GdbSession over gdb -q -i=mi2 pipes with hand-written MI
parser (mi.rs), settings/breakpoint/variable types, and the 44-check
C++ harness ported as Rust tests (37 console + 7 GTK, gdb 17.1).
lightgui-support: CodeTheme + C tokenizer/highlighter, FontSettings,
CodeView fold/gutter/inspection widget, CMake project builder.
Tests: debug 59/59, support 23/23 (incl. theme/font/cmake suites).
cargo test clean, zero rustc warnings, clippy clean, cargo fmt clean.
2026-11-29 06:10:11 +00:00

203 lines
7.6 KiB
Rust

// CMake tests — Rust port of tests/cmake_probe.cpp, minus the parts
// that need the code generator (sections 1-2: generate_project lives
// in lightgui-gen, not this crate). Covers: CMakeLists.txt text,
// real configure+build of hand-written console and GTK projects,
// settings persistence, failure reporting, and dirs with spaces.
// Run with: cargo test -p lightgui-support --test cmake_tests
// (the GTK app runs are wrapped in their own `xvfb-run -a`, so no
// outer display is needed for those checks).
use lightgui_support::cmake::{
cmake_build_project, cmake_lists_text, cmake_project_exists, CMakeSettings,
};
use std::path::{Path, PathBuf};
use std::process::Command;
fn base(name: &str) -> PathBuf {
let b = std::env::temp_dir().join(format!("lightgui_cmake_rust_{}", name));
let _ = std::fs::remove_dir_all(&b);
std::fs::create_dir_all(&b).unwrap();
b
}
fn write_file(p: &PathBuf, content: &str) {
if let Some(parent) = p.parent() {
std::fs::create_dir_all(parent).unwrap();
}
std::fs::write(p, content).unwrap();
}
fn read_file(p: &PathBuf) -> String {
std::fs::read_to_string(p).unwrap_or_default()
}
fn run_status(cmd: &str) -> i32 {
// Runs via /bin/sh; returns the exit code (-1 on spawn failure).
match Command::new("sh").arg("-c").arg(cmd).status() {
Ok(s) => s.code().unwrap_or(-1),
Err(_) => -1,
}
}
fn console_project(dir: &Path) {
write_file(
&dir.join("CMakeLists.txt"),
"cmake_minimum_required(VERSION 3.10)\nproject(console_demo CXX)\nadd_executable(app main.cpp)\n",
);
write_file(
&dir.join("main.cpp"),
"#include <cstdio>\nint main() { std::printf(\"cmake-console-ok\\n\"); return 0; }\n",
);
}
#[test]
fn cmakelists_text() {
let sources = vec![
"main.cpp".to_string(),
"MyWindow.cpp".to_string(),
"Button_001.cpp".to_string(),
"Label_001.cpp".to_string(),
"Entry_001.cpp".to_string(),
];
let cm = cmake_lists_text("MyWindow", &sources);
assert!(!cm.is_empty());
assert!(cm.contains("cmake_minimum_required(VERSION 3.10)"));
assert!(cm.contains("project(MyWindow CXX)"));
assert!(cm.contains("find_package(PkgConfig REQUIRED)"));
assert!(cm.contains("pkg_check_modules(GTK3 REQUIRED gtk+-3.0)"));
assert!(cm.contains("add_executable(app"));
for s in &sources {
assert!(cm.contains(s.as_str()), "missing {}", s);
}
assert!(cm.contains("target_include_directories(app PRIVATE ${GTK3_INCLUDE_DIRS})"));
assert!(cm.contains("target_link_libraries(app PRIVATE ${GTK3_LIBRARIES})"));
assert!(cm.contains("CMAKE_CXX_STANDARD 17"));
// project detection follows the file, not the text.
let b = base("text");
assert!(!cmake_project_exists(b.to_str().unwrap()));
write_file(&b.join("CMakeLists.txt"), &cm);
assert!(cmake_project_exists(b.to_str().unwrap()));
}
#[test]
fn cmake_build_handwritten_console() {
let b = base("console");
let dir = b.join("console");
console_project(&dir);
let def = CMakeSettings::default();
let cr = cmake_build_project(dir.to_str().unwrap(), &def);
assert!(cr.ok, "output:\n{}", cr.output);
assert!(cr.configured);
assert_eq!(cr.exe_path, format!("{}/build/app", dir.to_string_lossy()));
assert!(PathBuf::from(&cr.exe_path).is_file());
let rc = run_status(&format!(
"'{}' > '{}' 2>&1",
cr.exe_path,
dir.join("run.log").to_string_lossy()
));
assert_eq!(rc, 0);
assert_eq!(read_file(&dir.join("run.log")), "cmake-console-ok\n");
// Rebuild is a no-op success (idempotent configure+build).
let br2 = cmake_build_project(dir.to_str().unwrap(), &def);
assert!(br2.ok, "output:\n{}", br2.output);
}
#[test]
fn cmake_build_handwritten_gtk() {
let b = base("gtk");
let dir = b.join("gtk");
write_file(
&dir.join("CMakeLists.txt"),
"cmake_minimum_required(VERSION 3.10)\nproject(gtk_demo CXX)\nfind_package(PkgConfig REQUIRED)\npkg_check_modules(GTK3 REQUIRED gtk+-3.0)\nadd_executable(app main.cpp)\ntarget_include_directories(app PRIVATE ${GTK3_INCLUDE_DIRS})\ntarget_link_libraries(app PRIVATE ${GTK3_LIBRARIES})\n",
);
write_file(
&dir.join("main.cpp"),
"#include <gtk/gtk.h>\nint main(int argc, char** argv) {\n gtk_init(&argc, &argv);\n GtkWidget* w = gtk_window_new(GTK_WINDOW_TOPLEVEL);\n gtk_widget_show_all(w);\n gtk_main();\n return 0;\n}\n",
);
let def = CMakeSettings::default();
let gr = cmake_build_project(dir.to_str().unwrap(), &def);
assert!(gr.ok, "output:\n{}", gr.output);
// The built GTK app runs (timeout kills it: exit 124).
let rc = run_status(&format!(
"timeout 3 xvfb-run -a '{}' > /dev/null 2>&1",
gr.exe_path
));
assert_eq!(rc, 124, "expected timeout-kill 124, got {}", rc);
}
#[test]
fn cmake_settings_roundtrip_and_custom_build_dir() {
let b = base("settings");
let s = CMakeSettings {
cmake_path: "cmake".to_string(),
generator: "Unix Makefiles".to_string(),
build_type: "Debug".to_string(),
build_dir: "out".to_string(),
};
let conf = b.join("cmake.conf");
assert!(s.save(conf.to_str().unwrap()));
let mut s2 = CMakeSettings::default();
assert!(s2.load(conf.to_str().unwrap()));
assert_eq!(s2.generator, "Unix Makefiles");
assert_eq!(s2.build_type, "Debug");
assert_eq!(s2.build_dir, "out");
assert_eq!(s2.cmake_path, "cmake");
// Custom build dir + build type are honoured by a real build.
let dir = b.join("console");
console_project(&dir);
let dbg = cmake_build_project(dir.to_str().unwrap(), &s);
assert!(dbg.ok, "output:\n{}", dbg.output);
assert_eq!(dbg.build_dir, format!("{}/out", dir.to_string_lossy()));
assert!(dir.join("out/app").is_file());
// Defaults load: unknown keys ignored, empty file loads as defaults.
let conf2 = b.join("cmake2.conf");
write_file(&conf2, "# comment\nunknown_key=1\n");
let mut s3 = CMakeSettings::default();
assert!(s3.load(conf2.to_str().unwrap()));
assert_eq!(s3.build_type, "Release");
assert_eq!(s3.build_dir, "build");
}
#[test]
fn cmake_failure_reporting() {
let b = base("failures");
let def = CMakeSettings::default();
// Missing CMakeLists.txt.
let nofile = cmake_build_project(b.to_str().unwrap(), &def);
assert!(!nofile.ok && !nofile.configured);
// Broken CMakeLists.txt.
let broken_dir = b.join("broken");
write_file(
&broken_dir.join("CMakeLists.txt"),
"this is not valid cmake (\n",
);
let bad = cmake_build_project(broken_dir.to_str().unwrap(), &def);
assert!(!bad.ok && !bad.configured, "output:\n{}", bad.output);
// Project whose sources fail to compile: configure ok, build fails.
let badsrc_dir = b.join("badsrc");
write_file(
&badsrc_dir.join("CMakeLists.txt"),
"cmake_minimum_required(VERSION 3.10)\nproject(bad CXX)\nadd_executable(app main.cpp)\n",
);
write_file(&badsrc_dir.join("main.cpp"), "int main( { syntax error\n");
let badsrc = cmake_build_project(badsrc_dir.to_str().unwrap(), &def);
assert!(
!badsrc.ok && badsrc.configured,
"output:\n{}",
badsrc.output
);
}
#[test]
fn cmake_dir_with_space_builds() {
let b = base("spaced");
let spaced = b.join("dir with space");
write_file(
&spaced.join("CMakeLists.txt"),
"cmake_minimum_required(VERSION 3.10)\nproject(spaced CXX)\nadd_executable(app main.cpp)\n",
);
write_file(&spaced.join("main.cpp"), "int main() { return 0; }\n");
let sp = cmake_build_project(spaced.to_str().unwrap(), &CMakeSettings::default());
assert!(sp.ok, "output:\n{}", sp.output);
}