1- //! The module for working with and loading the content from `sql` files
1+ //! The module for working with and loading the content from `sql` files
2+ use std:: fs;
3+ use std:: io;
4+ use std:: path:: { Path , PathBuf } ;
5+
6+ pub struct SqlFiles {
7+ sql_files : Vec < PathBuf > ,
8+ }
9+
10+ impl SqlFiles {
11+ pub fn new < P : AsRef < Path > > ( path : P ) -> io:: Result < SqlFiles > {
12+ let recursive_scan = recursive_dir_scan ( path. as_ref ( ) ) ?;
13+ Ok ( SqlFiles {
14+ sql_files : recursive_scan,
15+ } )
16+ }
17+ }
18+
19+ fn recursive_dir_scan ( path : & Path ) -> io:: Result < Vec < PathBuf > > {
20+ let mut sql_files = Vec :: new ( ) ;
21+ for entry in fs:: read_dir ( path) ? {
22+ let entry = entry?;
23+ let path = entry. path ( ) ;
24+ if path. is_file ( ) && path. extension ( ) . unwrap ( ) == "sql" {
25+ sql_files. push ( path) ;
26+ } else if path. is_dir ( ) {
27+ let nested = recursive_dir_scan ( & path) ?;
28+ sql_files. extend ( nested) ;
29+ }
30+ }
31+ Ok ( sql_files)
32+ }
33+
34+ pub struct Content {
35+ raw_sql : String ,
36+ }
37+
38+ // impl Content {
39+ // pub fn new(files: SqlFiles) -> Content {
40+
41+ // }
42+ // }
43+
44+ #[ cfg( test) ]
45+ mod tests {
46+ use super :: * ;
47+ use std:: env;
48+ use std:: fs;
49+
50+ #[ test]
51+ fn test_recursive_scan_finds_only_sql_files_recursively ( ) {
52+ // Create a unique temporary base directory
53+ let base = env:: temp_dir ( ) . join ( "recursive_scan_test" ) ;
54+ // Clean up any previous leftovers
55+ let _ = fs:: remove_dir_all ( & base) ;
56+ fs:: create_dir_all ( & base) . unwrap ( ) ;
57+
58+ // Create a nested subdirectory
59+ let sub = base. join ( "subdir" ) ;
60+ fs:: create_dir_all ( & sub) . unwrap ( ) ;
61+
62+ // Paths for SQL files
63+ let file1 = base. join ( "one.sql" ) ;
64+ let file2 = sub. join ( "two.sql" ) ;
65+
66+ // Non-SQL files (should be ignored)
67+ let non_sql1 = base. join ( "ignore.txt" ) ;
68+ let non_sql2 = sub. join ( "README.md" ) ;
69+
70+ // Create the files to be tested
71+ fs:: File :: create ( & file1) . unwrap ( ) ;
72+ fs:: File :: create ( & file2) . unwrap ( ) ;
73+ fs:: File :: create ( & non_sql1) . unwrap ( ) ;
74+ fs:: File :: create ( & non_sql2) . unwrap ( ) ;
75+
76+ // Call the function under test
77+ let mut found = recursive_dir_scan ( base. as_path ( ) ) . unwrap ( ) ;
78+
79+ // Sort so order doesn't matter
80+ found. sort ( ) ;
81+
82+ let mut expected = vec ! [ file1, file2] ;
83+ expected. sort ( ) ;
84+
85+ assert_eq ! ( found, expected) ;
86+
87+ // Optional cleanup
88+ let _ = fs:: remove_dir_all ( & base) ;
89+ }
90+ }
0 commit comments