qlty_analysis/workspace_entries/
all_source.rs

1use super::workspace_entry::{WorkspaceEntry, WorkspaceEntrySource};
2use crate::ArgsSource;
3use core::fmt;
4use std::{path::PathBuf, sync::Arc};
5
6// qlty-ignore(semgrep/derive-debug): manual Debug impl below
7pub struct AllSource {
8    iter: ArgsSource,
9}
10
11impl fmt::Debug for AllSource {
12    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
13        write!(f, "AllSource[{:?}]", self.iter.root)
14    }
15}
16
17impl Default for AllSource {
18    fn default() -> Self {
19        Self::new(PathBuf::from("."))
20    }
21}
22
23impl AllSource {
24    pub fn new(root: PathBuf) -> Self {
25        Self {
26            iter: ArgsSource::new(root.clone(), vec![root]),
27        }
28    }
29}
30
31impl WorkspaceEntrySource for AllSource {
32    fn entries(&self) -> Arc<Vec<WorkspaceEntry>> {
33        self.iter.entries()
34    }
35}
36
37#[cfg(test)]
38mod test {
39    use super::*;
40    use crate::WorkspaceEntryKind;
41    use itertools::Itertools;
42    use qlty_test_utilities::git::build_sample_project;
43
44    #[test]
45    fn test_all_source_next() {
46        let root = build_sample_project();
47        let source = AllSource::new(root.path().to_path_buf());
48        let mut paths = vec![];
49
50        for workspace_entry in source.entries().iter() {
51            let workspace_entry = workspace_entry.clone();
52            paths.push((workspace_entry.path, workspace_entry.kind));
53        }
54
55        let expected_paths = build_expected_workspace_entries(vec![
56            ("", WorkspaceEntryKind::Directory),
57            (".gitignore", WorkspaceEntryKind::File),
58            ("lib", WorkspaceEntryKind::Directory),
59            ("lib/hello.rb", WorkspaceEntryKind::File),
60            ("lib/tasks", WorkspaceEntryKind::Directory),
61            ("lib/tasks/ops", WorkspaceEntryKind::Directory),
62            ("lib/tasks/ops/deploy.rb", WorkspaceEntryKind::File),
63            ("lib/tasks/ops/setup.rb", WorkspaceEntryKind::File),
64            ("lib/tasks/some.rb", WorkspaceEntryKind::File),
65            ("greetings.rb", WorkspaceEntryKind::File),
66            ("README.md", WorkspaceEntryKind::File),
67        ]);
68
69        assert_eq!(
70            paths
71                .iter()
72                .cloned()
73                .sorted()
74                .collect::<Vec<(PathBuf, WorkspaceEntryKind)>>(),
75            expected_paths
76        );
77    }
78
79    #[test]
80    fn test_all_source_includes_hidden_files() {
81        let root = build_sample_project();
82        // Add a .hidden file to the sample project
83        std::fs::write(root.path().join(".hidden"), "This is a hidden file.").unwrap();
84        let source = AllSource::new(root.path().to_path_buf());
85        let mut paths = vec![];
86
87        for workspace_entry in source.entries().iter() {
88            paths.push(workspace_entry.clone().path);
89        }
90
91        assert!(
92            paths.contains(&PathBuf::from(".hidden")),
93            "Expected .hidden file to be included in the paths, but it wasn't."
94        );
95    }
96
97    fn build_expected_workspace_entries(
98        workspace_entries: Vec<(&str, WorkspaceEntryKind)>,
99    ) -> Vec<(PathBuf, WorkspaceEntryKind)> {
100        workspace_entries
101            .into_iter()
102            .map(|(s, tt)| (PathBuf::from(s), tt))
103            .sorted()
104            .collect()
105    }
106}