ctb_formats_dceutils/
cli.rs

1// SPDX-License-Identifier: AGPL-3.0-or-later
2/*
3This file is part of Collective Toolbox, a database and document workspace and utilities.
4Copyright (C) 2026 Collective Toolbox Developers
5Contact: info@collectivetoolbox.com
6
7This program is free software: you can redistribute it and/or modify it under
8the terms of the GNU Affero General Public License as published by the Free
9Software Foundation, either version 3 of the License, or (at your option) any
10later version.
11
12This program is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more details.
15
16You should have received a copy of the GNU Affero General Public License along
17with this program.  If not, see <https://www.gnu.org/licenses/>.
18*/
19
20//! CLI execution helpers for DCE utilities.
21
22#[allow(
23    unused_imports,
24    clippy::wildcard_imports,
25    reason = "Standard workspace module prelude"
26)]
27use crate::utilities::*;
28
29use std::path::Path;
30
31pub fn php_to_csv(php_file: &Path) -> Result<ToolResult> {
32    crate::to_csv::php_file_to_csv_files(php_file)?;
33    Ok(ToolResult::immediate_ok(Vec::new()))
34}
35
36#[cfg(test)]
37#[allow(
38    clippy::panic,
39    clippy::expect_used,
40    clippy::unwrap_used,
41    clippy::unwrap_in_result,
42    clippy::panic_in_result_fn,
43    clippy::indexing_slicing,
44    clippy::arithmetic_side_effects,
45    reason = "Standard repository test boilerplate"
46)]
47mod tests {
48    use super::*;
49    #[crate::ctb_test]
50    fn test_dceutils_php_to_csv_command() {
51        let temp_dir = tempfile::tempdir().expect("Create temp dir");
52        let random_num = std::time::SystemTime::now()
53            .duration_since(std::time::UNIX_EPOCH)
54            .unwrap_or_default()
55            .as_nanos();
56        let php_filename = format!("test_cmd_{random_num}.php");
57        let php_path = temp_dir.path().join(&php_filename);
58
59        let php_content = r"<?php
60$my_test_array = array('a' => '1', 'b' => '2');
61?>";
62        std::fs::write(&php_path, php_content).expect("Write temp PHP file");
63
64        let expected_csv_name = format!("{php_filename}-my_test_array.csv");
65        let expected_csv_path = std::path::Path::new(&expected_csv_name);
66
67        if expected_csv_path.exists() {
68            let _ = std::fs::remove_file(expected_csv_path);
69        }
70
71        let result = super::php_to_csv(&php_path).expect("Run php_to_csv");
72        match result {
73            ToolResult::Immediate { .. } => {
74                assert!(expected_csv_path.exists());
75                let csv_content = std::fs::read_to_string(expected_csv_path)
76                    .expect("Read CSV content");
77                assert_eq!(csv_content, "a,1\nb,2\n");
78            }
79            _ => panic!("Expected Immediate ToolResult"),
80        }
81
82        let _ = std::fs::remove_file(expected_csv_path);
83    }
84}