fsvalidator 0.3.0

A file structure validator
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
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
464
465
466
467
468
469
use anyhow::Result;
use fsvalidator::model::{DirNode, FileNode, NodeName};
use std::fs;

#[test]
fn test_literal_file_validation_exists() -> Result<()> {
    // Create a test file
    let test_dir = tempfile::tempdir()?;
    let file_path = test_dir.path().join("test_file.txt");
    fs::write(&file_path, "test content")?;

    // Create a file node to validate
    let file_node = FileNode::new(NodeName::Literal("test_file.txt".to_string()), true);

    // Test basic validation
    assert!(file_node.validate(&file_path).is_ok());

    Ok(())
}

#[test]
fn test_literal_file_validation_missing_required() -> Result<()> {
    // Create an empty test directory
    let test_dir = tempfile::tempdir()?;
    let missing_file_path = test_dir.path().join("test_file.txt");

    // Create a file node to validate
    let file_node = FileNode::new(NodeName::Literal("test_file.txt".to_string()), true);

    // Test validation (should fail because file is missing but required)
    let result = file_node.validate(&missing_file_path);
    assert!(result.is_err());
    if let Err(validation_error) = result {
        assert!(validation_error.message.contains("Missing required file"));
        assert!(matches!(validation_error.category, fsvalidator::ErrorCategory::Missing));
    }

    Ok(())
}

#[test]
fn test_literal_file_validation_missing_optional() -> Result<()> {
    // Create an empty test directory
    let test_dir = tempfile::tempdir()?;
    let missing_file_path = test_dir.path().join("test_file.txt");

    // Create a file node to validate (not required)
    let file_node = FileNode::new(NodeName::Literal("test_file.txt".to_string()), false);

    // Test validation (should pass because file is optional)
    assert!(file_node.validate(&missing_file_path).is_ok());

    Ok(())
}

#[test]
fn test_pattern_file_validation() -> Result<()> {
    // Create a test file
    let test_dir = tempfile::tempdir()?;
    let file_path = test_dir.path().join("test_file.txt");
    fs::write(&file_path, "test content")?;

    // Create a file node with pattern to validate
    let file_node = FileNode::new(NodeName::Pattern("test_.*\\.txt".to_string()), true);

    // Test validation with direct path to file
    assert!(file_node.validate(&file_path).is_ok());

    Ok(())
}

#[test]
fn test_pattern_file_validation_no_match() -> Result<()> {
    // Create a test file that doesn't match the pattern
    let test_dir = tempfile::tempdir()?;
    let file_path = test_dir.path().join("wrong_file.txt");
    fs::write(&file_path, "test content")?;

    // Create a file node with pattern to validate
    let file_node = FileNode::new(NodeName::Pattern("test_.*\\.txt".to_string()), true);

    // Test validation (should fail because filename doesn't match pattern)
    let result = file_node.validate(&file_path);
    assert!(result.is_err());
    if let Err(validation_error) = result {
        assert!(validation_error.message.contains("doesn't match expected pattern"));
        assert!(matches!(validation_error.category, fsvalidator::ErrorCategory::NameMismatch));
    }

    Ok(())
}

#[test]
fn test_literal_dir_validation() -> Result<()> {
    // Create a test directory
    let test_dir = tempfile::tempdir()?;
    let sub_dir = test_dir.path().join("sub_dir");
    fs::create_dir(&sub_dir)?;

    // Create a dir node to validate
    let dir_node = DirNode::new(
        NodeName::Literal("sub_dir".to_string()),
        vec![],
        true,
        false,
        vec![],
    );

    // Test validation with direct path to directory
    assert!(dir_node.validate(&sub_dir).is_ok());

    Ok(())
}

#[test]
fn test_pattern_dir_validation() -> Result<()> {
    // Create a test directory
    let test_dir = tempfile::tempdir()?;
    let sub_dir = test_dir.path().join("test_dir_123");
    fs::create_dir(&sub_dir)?;

    // Create a dir node with pattern to validate
    let dir_node = DirNode::new(
        NodeName::Pattern("test_dir_\\d+".to_string()),
        vec![],
        true,
        false,
        vec![],
    );

    // Test validation with direct path to directory
    assert!(dir_node.validate(&sub_dir).is_ok());

    Ok(())
}

#[test]
fn test_dir_with_children() -> Result<()> {
    // Create a test directory structure
    let test_dir = tempfile::tempdir()?;
    let sub_dir = test_dir.path().join("sub_dir");
    fs::create_dir(&sub_dir)?;

    let file_in_sub_dir = sub_dir.join("test_file.txt");
    fs::write(&file_in_sub_dir, "test content")?;

    // Create a file node for the test file
    let file_node = FileNode::new(NodeName::Literal("test_file.txt".to_string()), true);

    // Create a dir node with the file as a child
    let dir_node = DirNode::new(
        NodeName::Literal("sub_dir".to_string()),
        vec![file_node],
        true,
        false,
        vec![],
    );

    // Test validation with direct path to directory
    assert!(dir_node.validate(&sub_dir).is_ok());

    Ok(())
}

#[test]
fn test_dir_with_missing_child() -> Result<()> {
    // Create a test directory without the required child file
    let test_dir = tempfile::tempdir()?;
    let sub_dir = test_dir.path().join("sub_dir");
    fs::create_dir(&sub_dir)?;

    // Create a file node for a required file that doesn't exist
    let file_node = FileNode::new(NodeName::Literal("test_file.txt".to_string()), true);

    // Create a dir node with the file as a child
    let dir_node = DirNode::new(
        NodeName::Literal("sub_dir".to_string()),
        vec![file_node],
        true,
        false,
        vec![],
    );

    // Test validation with direct path to directory (should fail because required file is missing)
    let result = dir_node.validate(&sub_dir);
    assert!(result.is_err());
    if let Err(validation_error) = result {
        assert!(!validation_error.children.is_empty());
        // Check that the error contains information about the missing file
        assert!(validation_error.to_string().contains("test_file.txt"));
        // Directory validation errors have category "Other" at the parent level
        assert!(matches!(validation_error.category, fsvalidator::ErrorCategory::Other));
        // But child errors should be "Missing"
        if !validation_error.children.is_empty() {
            assert!(matches!(validation_error.children[0].category, fsvalidator::ErrorCategory::Missing));
        }
    }

    Ok(())
}

#[test]
fn test_allow_defined_only() -> Result<()> {
    // Create a test directory with an extra unexpected file
    let test_dir = tempfile::tempdir()?;
    let sub_dir = test_dir.path().join("sub_dir");
    fs::create_dir(&sub_dir)?;

    // Create expected file
    let expected_file = sub_dir.join("expected.txt");
    fs::write(&expected_file, "test content")?;

    // Create unexpected file
    let unexpected_file = sub_dir.join("unexpected.txt");
    fs::write(&unexpected_file, "test content")?;

    // Create a file node for the expected file
    let file_node = FileNode::new(NodeName::Literal("expected.txt".to_string()), true);

    // Create a dir node with allow_defined_only=true
    let dir_node = DirNode::new(
        NodeName::Literal("sub_dir".to_string()),
        vec![file_node],
        true,
        true, // Only allow defined entries
        vec![],
    );

    // Test validation with direct path to directory (should fail due to unexpected file)
    let result = dir_node.validate(&sub_dir);
    assert!(result.is_err());
    if let Err(validation_error) = result {
        assert!(validation_error.to_string().contains("Unexpected entry"));
        
        // Check for Unexpected category in child errors
        let has_unexpected = validation_error.children.iter()
            .any(|err| matches!(err.category, fsvalidator::ErrorCategory::Unexpected));
        assert!(has_unexpected);
    }

    Ok(())
}

#[test]
fn test_mixed_pattern_and_literal() -> Result<()> {
    // Create a test directory structure
    let test_dir = tempfile::tempdir()?;

    // Create sub directories
    let sub_dir1 = test_dir.path().join("config");
    fs::create_dir(&sub_dir1)?;

    let sub_dir2 = test_dir.path().join("config_backup");
    fs::create_dir(&sub_dir2)?;

    // Create files
    fs::write(sub_dir1.join("settings.json"), "{}")?;
    fs::write(sub_dir2.join("settings.json"), "{}")?;

    // Define validation structure for directories with a settings.json file
    let config_dir_pattern = NodeName::Pattern("config.*".to_string());

    // Test first directory
    let config_dir1 = DirNode::new(
        config_dir_pattern.clone(),
        vec![FileNode::new(
            NodeName::Literal("settings.json".to_string()),
            true,
        )],
        true,
        true,
        vec![],
    );
    assert!(config_dir1.validate(&sub_dir1).is_ok());

    // Test second directory
    let config_dir2 = DirNode::new(
        config_dir_pattern,
        vec![FileNode::new(
            NodeName::Literal("settings.json".to_string()),
            true,
        )],
        true,
        true,
        vec![],
    );
    assert!(config_dir2.validate(&sub_dir2).is_ok());

    Ok(())
}

#[cfg(feature = "toml")]
#[test]
fn test_from_toml() -> Result<()> {
    // Create a temporary toml file
    let test_dir = tempfile::tempdir()?;
    let toml_path = test_dir.path().join("test_config.toml");

    let toml_content = r#"[root]
type = "dir"
name = "test_dir"
required = true

[[root.children]]
type = "file"
name = "test_file.txt"
required = true

[template]
# Empty but required
"#;

    fs::write(&toml_path, toml_content)?;

    // Create the validation target
    let target_dir = test_dir.path().join("test_dir");
    fs::create_dir(&target_dir)?;
    fs::write(target_dir.join("test_file.txt"), "content")?;

    // Parse and validate directly against target directory
    let node = fsvalidator::from_toml(&toml_path)?;
    
    // Test validation
    assert!(node.validate(&target_dir).is_ok());

    Ok(())
}

#[cfg(feature = "json")]
#[test]
fn test_from_json() -> Result<()> {
    // Create a temporary json file
    let test_dir = tempfile::tempdir()?;
    let json_path = test_dir.path().join("test_config.json");

    let json_content = r#"{
  "root": {
    "type": "dir",
    "name": "test_dir",
    "required": true,
    "children": [
      {
        "type": "file",
        "name": "test_file.txt",
        "required": true
      }
    ]
  },
  "template": {}
}
"#;

    fs::write(&json_path, json_content)?;

    // Create the validation target
    let target_dir = test_dir.path().join("test_dir");
    fs::create_dir(&target_dir)?;
    fs::write(target_dir.join("test_file.txt"), "content")?;

    // Parse and validate directly against target directory
    let node = fsvalidator::from_json(&json_path)?;
    
    // Test validation
    assert!(node.validate(&target_dir).is_ok());

    Ok(())
}

#[test]
fn test_invalid_path_type() -> Result<()> {
    // Create a test file that should be a directory
    let test_dir = tempfile::tempdir()?;
    let file_path = test_dir.path().join("not_a_dir");
    fs::write(&file_path, "test content")?;

    // Create a dir node expecting a directory
    let dir_node = DirNode::new(
        NodeName::Literal("not_a_dir".to_string()),
        vec![],
        true,
        false,
        vec![],
    );

    // Test basic validation with direct path (should fail because path exists but is not a directory)
    let result = dir_node.validate(&file_path);
    assert!(result.is_err());
    assert!(result.unwrap_err().to_string().contains("not a directory"));
    
    Ok(())
}

#[test]
fn test_dir_path_is_file() -> Result<()> {
    // Create a test file when code expects a directory
    let test_dir = tempfile::tempdir()?;
    let file_path = test_dir.path().join("file_not_dir");
    fs::write(&file_path, "test content")?;

    // Create a dir node with a child (which attempts to use the file as a directory)
    let child_file_node = FileNode::new(NodeName::Literal("some_child.txt".to_string()), true);
    let dir_node = DirNode::new(
        NodeName::Literal("file_not_dir".to_string()),
        vec![child_file_node],
        true,
        false,
        vec![],
    );

    // Test validation with direct path (should fail because path exists but is not a directory)
    let result = dir_node.validate(&file_path);
    assert!(result.is_err());
    if let Err(validation_error) = result {
        assert!(validation_error.message.contains("not a directory"));
        assert!(matches!(validation_error.category, fsvalidator::ErrorCategory::WrongType));
    }

    Ok(())
}

#[test]
fn test_multiple_validation_errors() -> Result<()> {
    // Create a test directory with multiple issues
    let test_dir = tempfile::tempdir()?;
    let sub_dir = test_dir.path().join("test_dir");
    fs::create_dir(&sub_dir)?;
    
    // Create a file that doesn't match our expected pattern
    fs::write(sub_dir.join("wrong.txt"), "content")?;
    
    // Define a validation structure that expects two specific files
    let dir_node = DirNode::new(
        NodeName::Literal("test_dir".to_string()),
        vec![
            FileNode::new(NodeName::Literal("required1.txt".to_string()), true),
            FileNode::new(NodeName::Literal("required2.txt".to_string()), true),
        ],
        true,
        true, // Only allow defined entries
        vec![],
    );
    
    // Test validation with multiple errors
    let result = dir_node.validate(&sub_dir);
    assert!(result.is_err());
    
    if let Err(validation_error) = result {
        // Should have multiple errors - missing required files and unexpected file
        assert!(validation_error.children.len() >= 3, 
               "Expected at least 3 errors, got {}", validation_error.children.len());
        
        // Check the error message contains information about all issues
        let error_string = validation_error.to_string();
        assert!(error_string.contains("required1.txt"), "Missing error for required1.txt");
        assert!(error_string.contains("required2.txt"), "Missing error for required2.txt");
        assert!(error_string.contains("Unexpected entry"), "Missing error for unexpected file");
        
        // Check error categories
        let has_missing = validation_error.children.iter()
            .any(|err| matches!(err.category, fsvalidator::ErrorCategory::Missing));
        let has_unexpected = validation_error.children.iter()
            .any(|err| matches!(err.category, fsvalidator::ErrorCategory::Unexpected));
            
        assert!(has_missing, "Missing errors should have Missing category");
        assert!(has_unexpected, "Unexpected entry errors should have Unexpected category");
    }
    
    Ok(())
}