use std::path::Path;
use crate::deps::graph::FileGraph;
use crate::walk::{TEST_DIRS, is_test_file};
use super::reading::graphed_language;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Role {
Source,
Test,
Other,
}
pub fn role(path: &Path) -> Role {
let in_test_dir = path
.components()
.filter_map(|c| c.as_os_str().to_str())
.any(|c| TEST_DIRS.contains(&c));
let is_script = graphed_language(path) == Some("Elixir Script");
let named = |name: &str| path.file_name().is_some_and(|file| file == name);
if is_test_file(path) || named("tests.py") {
Role::Test
} else if in_test_dir || is_script || named("conftest.py") {
Role::Other
} else {
Role::Source
}
}
pub fn roles_of(graph: &FileGraph) -> Vec<Role> {
let named_tests = |path: &Path| path.file_stem().is_some_and(|stem| stem == "tests");
let holding = |file: usize| match role(&graph.files[file]) {
Role::Other if graph.own_tests[file] => Role::Test,
Role::Source if graph.own_tests[file] && named_tests(&graph.files[file]) => Role::Test,
role => role,
};
(0..graph.files.len()).map(holding).collect()
}
pub fn own_tests(graph: &FileGraph, roles: &[Role], referring: &mut [Vec<usize>]) {
for file in (0..roles.len()).filter(|&f| roles[f] == Role::Source && graph.own_tests[f]) {
referring[file].push(file);
}
}