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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
use clap::{Arg, ArgAction, ArgGroup, ArgMatches};
use serde::{Deserialize, Serialize};
use std::collections::{BTreeMap, HashMap};
use std::convert::TryFrom;
use std::fs::File;
use std::io::Write;
use std::path::PathBuf;
use std::rc::Rc;
use walkdir::DirEntry;

use validate::validate_path;

use crate::command::Command;
use crate::commands::files::{
    alpabetical, get_files_with_filter, iterate_over, last_modified, read_file_content,
    regular_ordering,
};
use crate::commands::{
    validate, ALPHABETICAL, DIRECTORY, DIRECTORY_ONLY, LAST_MODIFIED, RULES_AND_TEST_FILE,
    RULES_FILE, TEST, TEST_DATA, VERBOSE,
};
use crate::rules::errors::Error;
use crate::rules::eval::eval_rules_file;
use crate::rules::exprs::RulesFile;
use crate::rules::path_value::PathAwareValue;
use crate::rules::Status::SKIP;
use crate::rules::{NamedStatus, RecordType, Result, Status};
use crate::utils::reader::Reader;
use crate::utils::writer::Writer;

#[derive(Clone, Copy, Eq, PartialEq)]
pub struct Test {}

#[allow(clippy::new_without_default)]
impl Test {
    pub fn new() -> Self {
        Test {}
    }
}

impl Command for Test {
    fn name(&self) -> &'static str {
        TEST
    }

    fn command(&self) -> clap::Command {
        clap::Command::new(TEST)
            .about(
                r#"Built in unit testing capability to validate a Guard rules file against
unit tests specified in YAML format to determine each individual rule's success
or failure testing.
"#,
            )
            .arg(
                Arg::new(RULES_FILE.0)
                    .long(RULES_FILE.0)
                    .short(RULES_FILE.1)
                    .action(ArgAction::Set)
                    .help("Provide a rules file"),
            )
            .arg(
                Arg::new(TEST_DATA.0)
                    .long(TEST_DATA.0)
                    .short(TEST_DATA.1)
                    .action(ArgAction::Set)
                    .help("Provide a file or dir for data files in JSON or YAML"),
            )
            .arg(
                Arg::new(DIRECTORY.0)
                    .long(DIRECTORY.0)
                    .short(DIRECTORY.1)
                    .action(ArgAction::Set)
                    .help("Provide the root directory for rules"),
            )
            .group(
                ArgGroup::new(RULES_AND_TEST_FILE)
                    .requires_all([RULES_FILE.0, TEST_DATA.0])
                    .conflicts_with(DIRECTORY_ONLY),
            )
            .group(
                ArgGroup::new(DIRECTORY_ONLY)
                    .args(["dir"])
                    .requires_all([DIRECTORY.0])
                    .conflicts_with(RULES_AND_TEST_FILE),
            )
            .arg(
                Arg::new(ALPHABETICAL.0)
                    .long(ALPHABETICAL.0)
                    .short(ALPHABETICAL.1)
                    .action(ArgAction::SetTrue)
                    .help("Sort alphabetically inside a directory"),
            )
            .arg(
                Arg::new(LAST_MODIFIED.0)
                    .long(LAST_MODIFIED.0)
                    .short(LAST_MODIFIED.1)
                    .action(ArgAction::SetTrue)
                    .conflicts_with(ALPHABETICAL.0)
                    .help("Sort by last modified times within a directory"),
            )
            .arg(
                Arg::new(VERBOSE.0)
                    .long(VERBOSE.0)
                    .short(VERBOSE.1)
                    .action(ArgAction::SetTrue)
                    .help("Verbose logging"),
            )
            .arg_required_else_help(true)
    }

    fn execute(&self, app: &ArgMatches, writer: &mut Writer, _: &mut Reader) -> Result<i32> {
        let mut exit_code = 0;
        let cmp = if app.get_flag(ALPHABETICAL.0) {
            alpabetical
        } else if app.get_flag(LAST_MODIFIED.0) {
            last_modified
        } else {
            regular_ordering
        };

        let verbose = app.get_flag(VERBOSE.0);

        if app.contains_id(DIRECTORY_ONLY) {
            struct GuardFile {
                prefix: String,
                file: DirEntry,
                test_files: Vec<DirEntry>,
            }
            let dir = app.get_one::<String>(DIRECTORY.0).unwrap();
            validate_path(dir)?;
            let walk = walkdir::WalkDir::new(dir);
            let mut non_guard: Vec<DirEntry> = vec![];
            let mut ordered_guard_files: BTreeMap<String, Vec<GuardFile>> = BTreeMap::new();
            for file in walk
                .follow_links(true)
                .sort_by_file_name()
                .into_iter()
                .flatten()
            {
                if file.path().is_file() {
                    let name = file
                        .file_name()
                        .to_str()
                        .map_or("".to_string(), |s| s.to_string());
                    if name.ends_with(".guard") || name.ends_with(".ruleset") {
                        let prefix = name
                            .strip_suffix(".guard")
                            .or_else(|| name.strip_suffix(".ruleset"))
                            .unwrap()
                            .to_string();
                        ordered_guard_files
                            .entry(
                                file.path()
                                    .parent()
                                    .map_or("".to_string(), |p| format!("{}", p.display())),
                            )
                            .or_default()
                            .push(GuardFile {
                                prefix,
                                file,
                                test_files: vec![],
                            });
                        continue;
                    } else {
                        non_guard.push(file);
                    }
                }
            }
            for file in non_guard {
                let name = file
                    .file_name()
                    .to_str()
                    .map_or("".to_string(), |s| s.to_string());
                if name.ends_with(".yaml")
                    || name.ends_with(".yml")
                    || name.ends_with(".json")
                    || name.ends_with(".jsn")
                {
                    let parent = file.path().parent();
                    if parent.map_or(false, |p| p.ends_with("tests")) {
                        if let Some(candidates) = parent.unwrap().parent().and_then(|grand| {
                            let grand = format!("{}", grand.display());
                            ordered_guard_files.get_mut(&grand)
                        }) {
                            for guard_file in candidates {
                                if name.starts_with(&guard_file.prefix) {
                                    guard_file.test_files.push(file);
                                    break;
                                }
                            }
                        }
                    }
                }
            }

            for (_dir, guard_files) in ordered_guard_files {
                for each_rule_file in guard_files {
                    if each_rule_file.test_files.is_empty() {
                        writeln!(
                            writer,
                            "Guard File {} did not have any tests associated, skipping.",
                            each_rule_file.file.path().display()
                        )?;
                        writeln!(writer, "---")?;
                        continue;
                    }
                    writeln!(
                        writer,
                        "Testing Guard File {}",
                        each_rule_file.file.path().display()
                    )?;
                    let rule_file = File::open(each_rule_file.file.path())?;
                    let content = read_file_content(rule_file)?;
                    let span =
                        crate::rules::parser::Span::new_extra(&content, &each_rule_file.prefix);
                    match crate::rules::parser::rules_file(span) {
                        Err(e) => {
                            writeln!(writer, "Parse Error on ruleset file {e}",)?;
                            exit_code = 1;
                        }
                        Ok(Some(rules)) => {
                            let data_test_files = each_rule_file
                                .test_files
                                .iter()
                                .map(|de| de.path().to_path_buf())
                                .collect::<Vec<PathBuf>>();
                            let test_exit_code =
                                test_with_data(&data_test_files, &rules, verbose, writer)?;
                            exit_code = if exit_code == 0 {
                                test_exit_code
                            } else {
                                exit_code
                            }
                        }
                        Ok(None) => {}
                    }
                    writeln!(writer, "---")?;
                }
            }
        } else {
            let file = app.get_one::<String>(RULES_FILE.0).unwrap();
            let data = app.get_one::<String>(TEST_DATA.0).unwrap();

            validate_path(file)?;
            validate_path(data)?;

            let data_test_files = get_files_with_filter(data, cmp, |entry| {
                entry
                    .file_name()
                    .to_str()
                    .map(|name| {
                        name.ends_with(".json")
                            || name.ends_with(".yaml")
                            || name.ends_with(".JSON")
                            || name.ends_with(".YAML")
                            || name.ends_with(".yml")
                            || name.ends_with(".jsn")
                    })
                    .unwrap_or(false)
            })?;

            let path = PathBuf::from(file);

            let rule_file = File::open(&path)?;
            if !rule_file.metadata()?.is_file() {
                return Err(Error::IoError(std::io::Error::from(
                    std::io::ErrorKind::InvalidInput,
                )));
            }

            let ruleset = vec![path];
            for rules in iterate_over(&ruleset, |content, file| {
                Ok((content, file.to_str().unwrap_or("").to_string()))
            }) {
                match rules {
                    Err(e) => {
                        write!(writer, "Unable to read rule file content {e}")?;
                        exit_code = 1;
                    }
                    Ok((context, path)) => {
                        let span = crate::rules::parser::Span::new_extra(&context, &path);
                        match crate::rules::parser::rules_file(span) {
                            Err(e) => {
                                writeln!(writer, "Parse Error on ruleset file {e}")?;
                                exit_code = 1;
                            }
                            Ok(Some(rules)) => {
                                let curr_exit_code =
                                    test_with_data(&data_test_files, &rules, verbose, writer)?;
                                if curr_exit_code != 0 {
                                    exit_code = curr_exit_code;
                                }
                            }
                            Ok(None) => {}
                        }
                    }
                }
            }
        }

        Ok(exit_code)
    }
}

#[derive(Serialize, Deserialize, Debug)]
struct TestExpectations {
    rules: HashMap<String, String>,
}

#[derive(Serialize, Deserialize, Debug)]
struct TestSpec {
    name: Option<String>,
    input: serde_yaml::Value,
    expectations: TestExpectations,
}

#[allow(clippy::never_loop)]
fn test_with_data(
    test_data_files: &[PathBuf],
    rules: &RulesFile<'_>,
    verbose: bool,
    writer: &mut Writer,
) -> Result<i32> {
    let mut exit_code = 0;
    let mut test_counter = 1;
    for specs in iterate_over(test_data_files, |data, path| {
        match serde_yaml::from_str::<Vec<TestSpec>>(&data) {
            Ok(spec) => Ok(spec),
            Err(_) => match serde_json::from_str::<Vec<TestSpec>>(&data) {
                Ok(specs) => Ok(specs),
                Err(e) => Err(Error::ParseError(format!(
                    "Unable to process data in file {}, Error {},",
                    path.display(),
                    e
                ))),
            },
        }
    }) {
        match specs {
            Err(e) => {
                writeln!(writer, "Error processing {e}")?;
                exit_code = 1;
            }
            Ok(specs) => {
                for each in specs {
                    writeln!(writer, "Test Case #{test_counter}")?;
                    if each.name.is_some() {
                        writeln!(writer, "Name: {}", each.name.unwrap())?;
                    }

                    let by_result = {
                        let mut by_result = HashMap::new();
                        let root = PathAwareValue::try_from(each.input)?;
                        let mut root_scope =
                            crate::rules::eval_context::root_scope(rules, Rc::new(root.clone()))?;
                        eval_rules_file(rules, &mut root_scope, None)?; // we never use data file name in the output
                        let top = root_scope.reset_recorder().extract();

                        let by_rules: HashMap<&str, Vec<&Option<RecordType<'_>>>> =
                            top.children.iter().fold(HashMap::new(), |mut acc, rule| {
                                if let Some(RecordType::RuleCheck(NamedStatus { name, .. })) =
                                    rule.container
                                {
                                    acc.entry(name).or_default().push(&rule.container)
                                }
                                acc
                            });

                        for (rule_name, rule) in by_rules {
                            let expected = match each.expectations.rules.get(rule_name) {
                                Some(exp) => Status::try_from(exp.as_str())?,
                                None => {
                                    writeln!(
                                        writer,
                                        "  No Test expectation was set for Rule {rule_name}"
                                    )?;
                                    continue;
                                }
                            };

                            let mut statues: Vec<Status> = Vec::with_capacity(rule.len());
                            let matched = 'matched: loop {
                                let mut all_skipped = 0;

                                for each in rule.iter().copied().flatten() {
                                    if let RecordType::RuleCheck(NamedStatus {
                                        status: got_status,
                                        ..
                                    }) = each
                                    {
                                        match expected {
                                            SKIP => {
                                                if *got_status == SKIP {
                                                    all_skipped += 1;
                                                }
                                            }

                                            rest => {
                                                if *got_status == rest {
                                                    break 'matched Some(expected);
                                                }
                                            }
                                        }
                                        statues.push(*got_status)
                                    }
                                }
                                if expected == SKIP && all_skipped == rule.len() {
                                    break 'matched Some(expected);
                                }
                                break 'matched None;
                            };

                            match matched {
                                Some(status) => {
                                    by_result
                                        .entry(String::from("PASS"))
                                        .or_insert_with(indexmap::IndexSet::new)
                                        .insert(format!("{rule_name}: Expected = {status}"));
                                }

                                None => {
                                    by_result
                                        .entry(String::from("FAIL"))
                                        .or_insert_with(indexmap::IndexSet::new)
                                        .insert(format!(
                                            "{rule_name}: Expected = {expected}, Evaluated = {statues:?}"
                                        ));
                                    exit_code = 7;
                                }
                            }
                        }

                        if verbose {
                            validate::print_verbose_tree(&top, writer);
                        }
                        by_result
                    };
                    print_test_case_report(&by_result, writer);
                    test_counter += 1;
                }
            }
        }
    }
    Ok(exit_code)
}

pub(crate) fn print_test_case_report(
    by_result: &HashMap<String, indexmap::IndexSet<String>>,
    writer: &mut Writer,
) {
    use itertools::Itertools;
    let mut results = by_result.keys().cloned().collect_vec();

    results.sort(); // Deterministic order of results

    for result in &results {
        writeln!(writer, "  {result} Rules:").expect("Unable to write to the output");
        for each_case in by_result.get(result).unwrap() {
            writeln!(writer, "    {}", *each_case).expect("Unable to write to the output");
        }
    }
    writeln!(writer).expect("Unable to write to the output");
}