caxe 0.3.8

A modern C/C++ project manager that cuts through build system complexity. Zero config, smart dependencies, and parallel builds.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
//! Test runner for C/C++ unit tests.
//!
//! This module provides the `cx test` command which compiles and runs
//! test files from the `tests/` directory.
//!
//! ## Features
//!
//! - Auto-links project sources for testing internals
//! - Parallel test compilation
//! - Test filtering with `--filter`

use super::utils::{get_compiler, get_std_flag_gcc, get_std_flag_msvc, load_config};
use crate::config::CxConfig;
use anyhow::Result;
use colored::*;
use indicatif::{ProgressBar, ProgressStyle};
use rayon::prelude::*;
use std::fs;
use std::path::Path;
use std::process::Command;
use walkdir::WalkDir;

pub fn run_tests(filter: Option<String>) -> Result<()> {
    // Load config or default
    let config = load_config().unwrap_or_else(|_| CxConfig {
        package: crate::config::PackageConfig {
            name: "test_runner".into(),
            version: "0.0.0".into(),
            edition: "c++20".into(),
        },
        ..Default::default()
    });

    let test_dir_str = config
        .test
        .as_ref()
        .and_then(|t| t.source_dir.clone())
        .unwrap_or_else(|| "tests".to_string());
    let test_dir = Path::new(&test_dir_str);

    if !test_dir.exists() {
        println!("{} No {}/ directory found.", "!".yellow(), test_dir_str);
        return Ok(());
    }

    let mut include_paths = Vec::new();
    let mut extra_cflags = Vec::new();
    let mut dep_libs = Vec::new();

    if let Some(deps) = &config.dependencies
        && !deps.is_empty()
    {
        let (paths, cflags, libs) = crate::deps::fetch_dependencies(deps)?;
        include_paths = paths;
        extra_cflags = cflags;
        dep_libs = libs;
    }

    println!("{} Running tests...", "๐Ÿงช".magenta());
    if let Some(f) = &filter {
        println!("   Filter: {}", f.cyan());
    }
    fs::create_dir_all("build/tests")?;

    // Collect Project Object Files (excluding main)
    // We assume the project was built in 'debug' mode for tests
    let obj_dir = Path::new("build/debug/obj");
    let mut project_objs = Vec::new();
    if obj_dir.exists() {
        for entry in WalkDir::new(obj_dir).into_iter().filter_map(|e| e.ok()) {
            let path = entry.path();
            if path.extension().is_some_and(|e| e == "o" || e == "obj") {
                // Exclude main.o / main.obj to avoid multiple entry points
                let stem = path.file_stem().unwrap_or_default().to_string_lossy();
                if stem != "main" {
                    project_objs.push(path.to_path_buf());
                }
            }
        }
    } else {
        println!(
            "{} Warning: Project not built. Running tests without linking project sources.",
            "!".yellow()
        );
        println!("   Run 'cx build' first to link project code.");
    }

    let mut test_files = Vec::new();
    for entry in WalkDir::new(test_dir).into_iter().filter_map(|e| e.ok()) {
        let path = entry.path().to_path_buf();
        let is_cpp = path
            .extension()
            .and_then(|ext| ext.to_str())
            .is_some_and(|ext| ["cpp", "cc", "cxx"].contains(&ext));
        let is_c = path.extension().is_some_and(|ext| ext == "c");

        if is_cpp || is_c {
            // Apply Filter
            if let Some(f) = &filter {
                let name = path.file_stem().unwrap_or_default().to_string_lossy();
                if !name.contains(f) {
                    continue;
                }
            }
            test_files.push((path, is_cpp));
        }
    }

    if test_files.is_empty() {
        println!("{} No tests found.", "!".yellow());
        return Ok(());
    }

    // Check for Single Binary Mode
    // If enabled, we compile ALL test sources into ONE executable (runner)
    let single_binary = config
        .test
        .as_ref()
        .and_then(|t| t.single_binary)
        .unwrap_or(false);

    if single_binary {
        println!("{} Building single test runner...", "๐Ÿ”จ".cyan());
        let runner_name = "test_runner";
        let output_bin = format!("build/tests/{}", runner_name); // Linux/Mac

        let compiler = get_compiler(&config, true); // Assume C++ for tests generally
        let is_msvc = compiler.contains("cl.exe") || compiler == "cl";

        let mut cmd = Command::new(&compiler);
        let mut args = Vec::new();

        if is_msvc {
            args.push("/nologo".to_string());
            args.push("/EHsc".to_string());
            args.push(format!("/Fe{}", output_bin));
            args.push(get_std_flag_msvc(&config.package.edition));

            // Includes
            for p in &include_paths {
                args.push(format!("/I{}", p.display()));
            }
            args.push("/Isrc".to_string());

            // Sources
            for (path, _) in &test_files {
                args.push(path.to_string_lossy().to_string());
            }
            // Project Objects
            for obj in &project_objs {
                args.push(obj.to_string_lossy().to_string());
            }

            // Libs
            args.push("/link".to_string());
            for lib in &dep_libs {
                args.push(lib.clone());
            }
        } else {
            args.push(get_std_flag_gcc(&config.package.edition));
            args.push("-o".to_string());
            args.push(output_bin.clone());

            // Includes
            for p in &include_paths {
                args.push(format!("-I{}", p.display()));
            }
            args.push("-Isrc".to_string());

            // Sources
            for (path, _) in &test_files {
                args.push(path.to_string_lossy().to_string());
            }
            // Project Objects
            for obj in &project_objs {
                args.push(obj.to_string_lossy().to_string());
            }

            // Libs
            for lib in &dep_libs {
                args.push(lib.clone());
            }
            if let Some(cfg) = &config.build
                && let Some(libs) = &cfg.libs
            {
                for lib in libs {
                    args.push(format!("-l{}", lib));
                }
            }
        }

        cmd.args(&args);

        // Execute Compilation
        let start = std::time::Instant::now();
        let output = cmd.output()?;
        if !output.status.success() {
            println!("{} Test Runner Compilation Failed:", "x".red());
            println!("{}", String::from_utf8_lossy(&output.stdout));
            println!("{}", String::from_utf8_lossy(&output.stderr));
            return Ok(());
        }
        println!("   {} Compiled in {:.2?}s", "โœ“".green(), start.elapsed());

        // Run It
        println!("{} Running tests...", "๐Ÿš€".cyan());
        let run_path = if cfg!(target_os = "windows") {
            format!("{}.exe", output_bin)
        } else {
            format!("./{}", output_bin)
        };

        let mut run_cmd = Command::new(&run_path);
        // Pass filter as argument if present (standard for Catch2/GTest/doctest)
        if let Some(f) = &filter {
            run_cmd.arg(f);
        }

        let status = run_cmd.status()?;
        if status.success() {
            println!("{}", "TESTS PASSED".green().bold());
        } else {
            println!("{}", "TESTS FAILED".red().bold());
        }
        return Ok(());
    }

    let pb = ProgressBar::new((test_files.len() * 2) as u64);
    pb.set_style(
        ProgressStyle::default_bar()
            .template("{bar:40.green/black} {pos:>3}/{len:3} [{elapsed_precise}] {msg}")
            .unwrap_or_else(|_| ProgressStyle::default_bar())
            .progress_chars("โ—โ—‹ยท"),
    );

    // Phase 1: Parallel Compilation
    let compiled_results: Vec<(String, Option<String>)> = test_files
        .par_iter()
        .map(|(path, is_cpp)| {
            let test_name = path
                .file_stem()
                .unwrap_or_default()
                .to_string_lossy()
                .to_string();
            let output_bin = format!("build/tests/{}", test_name);

            // Caching Check: Compare mtime of test source vs test binary
            // In a real system, we'd check dependencies too, but this is a start.
            let bin_path = if cfg!(target_os = "windows") {
                format!("{}.exe", output_bin)
            } else {
                output_bin.clone()
            };

            let skip_compile = if Path::new(&bin_path).exists() {
                let src_mtime = fs::metadata(path)
                    .and_then(|m| m.modified())
                    .unwrap_or(std::time::SystemTime::UNIX_EPOCH);
                let bin_mtime = fs::metadata(&bin_path)
                    .and_then(|m| m.modified())
                    .unwrap_or(std::time::SystemTime::UNIX_EPOCH);
                // If source is older than binary, we *might* skip.
                // Ideally we also check project object mtimes, but let's keep it simple for now or check one level deep.
                src_mtime < bin_mtime
            } else {
                false
            };

            if skip_compile {
                pb.inc(1); // Skip compile step
                return (test_name, Some(output_bin));
            }

            pb.set_message(format!("Compiling {}", test_name));

            let compiler = get_compiler(&config, *is_cpp);
            let is_msvc = compiler.contains("cl.exe") || compiler == "cl";
            let mut cmd = Command::new(&compiler);

            if is_msvc {
                cmd.arg("/nologo");
                cmd.arg("/EHsc");
                cmd.arg(path);
                cmd.arg(format!("/Fe{}", output_bin)); // Output exe name
                cmd.arg(get_std_flag_msvc(&config.package.edition));

                // Includes
                for p in &include_paths {
                    cmd.arg(format!("/I{}", p.display()));
                }
                // Include "src" so tests can "#include <main.hpp>" easily
                cmd.arg("/Isrc");
            } else {
                cmd.arg(path);
                cmd.arg("-o").arg(&output_bin);
                cmd.arg(get_std_flag_gcc(&config.package.edition));

                // Includes
                for p in &include_paths {
                    cmd.arg(format!("-I{}", p.display()));
                }
                cmd.arg("-Isrc");
            }

            cmd.args(&extra_cflags);

            if let Some(build_cfg) = &config.build
                && let Some(flags) = build_cfg.get_flags()
            {
                cmd.args(flags);
            }

            // Link Libs & Project Objects
            if is_msvc {
                cmd.arg("/link");
            }
            cmd.args(&dep_libs);

            // Link Project Objects
            for obj in &project_objs {
                cmd.arg(obj);
            }

            if let Some(build_cfg) = &config.build
                && let Some(libs) = &build_cfg.libs
            {
                for lib in libs {
                    if is_msvc {
                        cmd.arg(format!("{}.lib", lib));
                    } else {
                        cmd.arg(format!("-l{}", lib));
                    }
                }
            }

            let output = cmd.output();
            let success = match output {
                Ok(out) => {
                    if !out.status.success() {
                        pb.suspend(|| {
                            println!("{} COMPILE FAIL: {}", "x".red(), test_name.bold());
                            println!("{}", String::from_utf8_lossy(&out.stdout));
                            println!("{}", String::from_utf8_lossy(&out.stderr));
                        });
                        false
                    } else {
                        true
                    }
                }
                Err(e) => {
                    pb.suspend(|| {
                        println!("{} COMPILER ERROR: {} ({})", "x".red(), test_name.bold(), e);
                    });
                    false
                }
            };

            pb.inc(1);
            if success {
                (test_name, Some(output_bin))
            } else {
                (test_name, None)
            }
        })
        .collect();

    // Phase 2: Sequential Execution (Running Tests)
    let mut passed_tests = 0;
    let mut total_tests = 0;

    for (test_name, bin_path) in compiled_results {
        total_tests += 1;

        if let Some(output_bin) = bin_path {
            pb.set_message(format!("Running {}", test_name));

            let run_path = if cfg!(target_os = "windows") {
                format!("{}.exe", output_bin)
            } else {
                format!("./{}", output_bin)
            };

            let run_status = Command::new(&run_path).status();

            match run_status {
                Ok(status) => {
                    if status.success() {
                        pb.suspend(|| {
                            println!(
                                "   {} TEST {} ... {}",
                                "โœ“".green(),
                                test_name.bold(),
                                "PASS".green()
                            )
                        });
                        passed_tests += 1;
                    } else {
                        pb.suspend(|| {
                            println!(
                                "   {} TEST {} ... {}",
                                "x".red(),
                                test_name.bold(),
                                "FAIL".red()
                            )
                        });
                    }
                }
                Err(_) => {
                    pb.suspend(|| {
                        println!(
                            "   {} TEST {} ... {}",
                            "x".red(),
                            test_name.bold(),
                            "EXEC FAIL".red()
                        )
                    });
                }
            }
        }

        pb.inc(1);
    }

    pb.finish_and_clear();

    println!("\nTest Result: {}/{} passed.", passed_tests, total_tests);
    if total_tests > 0 && passed_tests == total_tests {
        println!("{}", "ALL TESTS PASSED โœจ".green().bold());
    } else if total_tests > 0 {
        println!("{}", "SOME TESTS FAILED ๐Ÿ’€".red().bold());
    }

    Ok(())
}