Skip to content

Commit efa9e4b

Browse files
committed
Demo rust unit test with temp file system
1 parent 76a732c commit efa9e4b

File tree

5 files changed

+694
-2
lines changed

5 files changed

+694
-2
lines changed

rust/Cargo.lock

Lines changed: 198 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,6 @@ default = ["extension-module"]
4343
[dev-dependencies]
4444
parameterized = "2.0.0"
4545
serde_json = "1.0.143"
46+
tempfile = "3.14.0"
47+
filetime = "0.2.26"
48+
map-macro = "0.3.0"

rust/src/graph/builder/mod.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ use read_python_file::read_python_file;
2626

2727
#[derive(Debug, Clone, new)]
2828
pub struct PackageSpec {
29+
#[new(into)]
2930
name: String,
31+
#[new(into)]
3032
directory: PathBuf,
3133
}
3234

@@ -409,3 +411,60 @@ fn calculate_thread_counts() -> ThreadCounts {
409411
module_parsing: num_threads,
410412
}
411413
}
414+
415+
#[cfg(test)]
416+
mod tests {
417+
use super::*;
418+
use crate::test_utils::{DEFAULT_MTIME, TempFileSystemBuilder};
419+
use map_macro::hash_set;
420+
421+
#[test]
422+
fn test_discover_modules_happy_path() {
423+
const SOME_MTIME: i64 = 12340000;
424+
425+
let temp_fs = TempFileSystemBuilder::new(
426+
r#"
427+
mypackage/
428+
__init__.py
429+
not-a-python-file.txt
430+
.hidden
431+
foo/
432+
__init__.py
433+
one.py
434+
two/
435+
__init__.py
436+
green.py
437+
blue.py
438+
"#,
439+
)
440+
.with_file_mtime_map([("mypackage/foo/one.py", SOME_MTIME)])
441+
.build()
442+
.unwrap();
443+
444+
let result = discover_and_parse_modules(
445+
&[PackageSpec::new("mypackage", temp_fs.join("mypackage"))],
446+
None,
447+
)
448+
.unwrap();
449+
450+
assert_eq!(result.len(), 6);
451+
assert_eq!(
452+
result
453+
.iter()
454+
.map(|p| (
455+
p.module.name.as_str(),
456+
p.module.is_package,
457+
p.module.mtime_secs
458+
))
459+
.collect::<HashSet<_>>(),
460+
hash_set! {
461+
("mypackage", true, DEFAULT_MTIME),
462+
("mypackage.foo", true, DEFAULT_MTIME),
463+
("mypackage.foo.one", false, SOME_MTIME),
464+
("mypackage.foo.two", true, DEFAULT_MTIME),
465+
("mypackage.foo.two.green", false, DEFAULT_MTIME),
466+
("mypackage.foo.two.blue", false, DEFAULT_MTIME),
467+
}
468+
);
469+
}
470+
}

rust/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ mod import_scanning;
99
pub mod module_expressions;
1010
mod module_finding;
1111

12+
#[cfg(test)]
13+
pub mod test_utils;
14+
1215
use pyo3::prelude::*;
1316

1417
#[pymodule]

0 commit comments

Comments
 (0)