|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +#[cfg(test)] |
| 9 | +mod tests { |
| 10 | + use std::collections::BTreeMap; |
| 11 | + use std::collections::BTreeSet; |
| 12 | + |
| 13 | + use lifeguard::find_sources::build_source_db; |
| 14 | + use lifeguard::test_lib::populate_temp_dir; |
| 15 | + |
| 16 | + fn keys(build_map: &BTreeMap<String, String>) -> BTreeSet<&str> { |
| 17 | + build_map.keys().map(|s| s.as_str()).collect() |
| 18 | + } |
| 19 | + |
| 20 | + #[test] |
| 21 | + fn test_seeds_py_files_from_input_dir() { |
| 22 | + let tmp = populate_temp_dir(&[("a.py", ""), ("pkg/__init__.py", ""), ("pkg/b.py", "")]); |
| 23 | + |
| 24 | + let (build_map, seed_count) = build_source_db(tmp.path(), None).unwrap(); |
| 25 | + assert_eq!(seed_count, 3, "all three .py files are seeded"); |
| 26 | + assert_eq!( |
| 27 | + keys(&build_map), |
| 28 | + BTreeSet::from(["a.py", "pkg/__init__.py", "pkg/b.py"]), |
| 29 | + ); |
| 30 | + } |
| 31 | + |
| 32 | + #[test] |
| 33 | + fn test_skips_non_identifier_names() { |
| 34 | + let tmp = populate_temp_dir(&[ |
| 35 | + ("good.py", ""), |
| 36 | + // Dir whose name is not a valid identifier: skipped wholesale. |
| 37 | + (".venv/bad.py", ""), |
| 38 | + // File whose stem is not a valid identifier: skipped. |
| 39 | + ("2024-migration.py", ""), |
| 40 | + ]); |
| 41 | + |
| 42 | + let (build_map, _) = build_source_db(tmp.path(), None).unwrap(); |
| 43 | + assert_eq!(keys(&build_map), BTreeSet::from(["good.py"])); |
| 44 | + } |
| 45 | + |
| 46 | + #[test] |
| 47 | + fn test_follows_imports_into_site_packages() { |
| 48 | + let tmp = populate_temp_dir(&[ |
| 49 | + ("proj/main.py", "import foo\n"), |
| 50 | + ("sp/foo/__init__.py", ""), |
| 51 | + // Unreachable from main.py's imports — must not be pulled in. |
| 52 | + ("sp/foo/helper.py", ""), |
| 53 | + ("sp/unused/__init__.py", ""), |
| 54 | + ]); |
| 55 | + let proj = tmp.path().join("proj"); |
| 56 | + let sp = tmp.path().join("sp"); |
| 57 | + |
| 58 | + let (build_map, seed_count) = build_source_db(&proj, Some(&sp)).unwrap(); |
| 59 | + assert_eq!(seed_count, 1, "only main.py is seeded from the project"); |
| 60 | + assert_eq!( |
| 61 | + keys(&build_map), |
| 62 | + BTreeSet::from(["main.py", "foo/__init__.py"]), |
| 63 | + ); |
| 64 | + } |
| 65 | +} |
0 commit comments