• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Achiefs / fim / 14275166216

04 Apr 2025 10:13PM UTC coverage: 84.711% (-0.4%) from 85.125%
14275166216

push

github

web-flow
Merge pull request #194 from Achiefs/177-db-hash

Added Hash diff scanner

353 of 433 new or added lines in 12 files covered. (81.52%)

4 existing lines in 1 file now uncovered.

1435 of 1694 relevant lines covered (84.71%)

1.52 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

75.0
/src/hashscanner/test.rs
1
use super::*;
2
use std::fs::{create_dir_all, remove_dir_all, File};
3
use serial_test::serial;
4
use std::path::Path;
5
use std::io::Write;
6
use tokio_test::block_on;
7
use std::fs;
8

9
use crate::db::DB;
10

11
// ----------------------------------------------------------------------------
12

13
fn remove_db(path: &str) {
1✔
14
    use std::fs::remove_file;
15

16
    if Path::new(path).exists() {
1✔
17
        match remove_file(path){
1✔
18
            Ok(_v) => (),
NEW
19
            Err(e) => println!("Error deleting db, {}", e)
×
20
        };
21
    };
22
}
23

24
// ----------------------------------------------------------------------------
25

26
#[test]
27
#[serial]
28
#[cfg(target_family = "unix")]
29
/// Check dir scan result, DB should contains a DBFile definition with the given filepath
30
fn test_scan_path_unix() {
31
    let cfg = AppConfig::new(&utils::get_os(), None);
32
    let tdb = DB::new(&cfg.hashscanner_file);
33
    let scan_dir = String::from("./tmp/test_scan_path");
34
    let filepath = format!("{}/{}", scan_dir.clone(), "test_scan_path.txt");
35
    let _ = create_dir_all(scan_dir.clone());
36

37
    let _file = match File::create(&filepath) {
38
        Ok(file) => file,
39
        Err(e) => panic!("Could not create test file, error: '{}'", e)
40
    };
41

42
    remove_db(&cfg.hashscanner_file);
43
    tdb.create_table();
44
    scan_path(cfg.clone(), scan_dir.clone());
45

46
    let dbfile = tdb.get_file_by_path(filepath.clone()).unwrap();
47
    assert_eq!(dbfile.path, filepath);
48
    assert_eq!(dbfile.size, utils::get_file_size(&filepath));
49

50
    let _ = remove_dir_all(scan_dir);
51
}
52

53
// ----------------------------------------------------------------------------
54

55
#[test]
56
#[serial]
57
#[cfg(target_family = "unix")]
58
/// Check that modify a file is reflected in DB, it should modify the size and hash of DBFile
59
fn test_check_path() {
60
    let cfg = AppConfig::new(&utils::get_os(), None);
61
    let tdb = DB::new(&cfg.hashscanner_file);
62
    let scan_dir = String::from("./tmp/test_check_path");
63
    let filepath = format!("{}/{}", scan_dir.clone(), "test_check_path.txt");
64
    let _ = create_dir_all(scan_dir.clone());
65

66
    let mut _file = match File::create(&filepath) {
67
        Ok(file) => file,
68
        Err(e) => panic!("Could not create test file, error: '{}'", e)
69
    };
70

71
    remove_db(&cfg.hashscanner_file);
72
    tdb.create_table();
73
    scan_path(cfg.clone(), scan_dir.clone());
74
    let _result = writeln!(_file, "{}", "This is an additional line.");
75
    block_on(check_path(cfg.clone(), scan_dir.clone(), true));
76

77
    let dbfile = tdb.get_file_by_path(filepath.clone()).unwrap();
78
    assert_eq!(dbfile.path, filepath);
79
    assert_eq!(dbfile.size, utils::get_file_size(&filepath));
80

81
    let _ = remove_dir_all(scan_dir);
82
}
83

84
// ----------------------------------------------------------------------------
85

86
#[test]
87
#[serial]
88
#[cfg(target_family = "unix")]
89
#[should_panic(expected = "DBFileNotFoundError")]
90
/// Check file deletion of filesystem and DB, it should panic with DBFileNotFoundError
91
fn test_update_db() {
92
    let cfg = AppConfig::new(&utils::get_os(), None);
93
    let tdb = DB::new(&cfg.hashscanner_file);
94
    let scan_dir = String::from("./tmp/test_update_db");
95
    let filepath = format!("{}/{}", scan_dir.clone(), "test_update_db.txt");
96
    let filepath2 = format!("{}/{}", scan_dir.clone(), "test_update_db2.txt");
97
    let _ = create_dir_all(scan_dir.clone());
98

99
    let mut _file = match File::create(&filepath) {
100
        Ok(file) => file,
101
        Err(e) => panic!("Could not create test file, error: '{}'", e)
102
    };
103
    let mut _file2 = match File::create(&filepath2) {
104
        Ok(file) => file,
105
        Err(e) => panic!("Could not create test file, error: '{}'", e)
106
    };
107

108
    remove_db(&cfg.hashscanner_file);
109
    tdb.create_table();
110
    scan_path(cfg.clone(), scan_dir.clone());
111
    fs::remove_file(filepath.clone()).unwrap();
112
    block_on(update_db(cfg, scan_dir.clone(), true));
113

114
    let dbfile = tdb.get_file_by_path(filepath2.clone()).unwrap();
115
    assert_eq!(dbfile.path, filepath2);
116
    assert_eq!(dbfile.size, utils::get_file_size(&filepath2));
117
    let _ = remove_dir_all(scan_dir);
118

119
    // Panic line
120
    let _dbfile = tdb.get_file_by_path(filepath.clone()).unwrap();
121
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc