|
1 | | -#![allow(dead_code)] |
2 | | -use std::fmt; |
3 | | -use std::path::PathBuf; |
4 | | - |
5 | | -use globset::{Glob, GlobSetBuilder}; |
6 | | -use ignore::WalkBuilder; |
7 | | - |
8 | 1 | pub mod cli; |
9 | | - |
10 | | -#[derive(Debug, PartialEq, Eq, Clone)] |
11 | | -pub enum Size { |
12 | | - UnknownSize, |
13 | | - KnownSize(u64), |
14 | | -} |
15 | | - |
16 | | -impl fmt::Display for Size { |
17 | | - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
18 | | - match self { |
19 | | - Size::UnknownSize => write!(f, "unknown"), |
20 | | - Size::KnownSize(bytes) => { |
21 | | - if *bytes < 1024 { |
22 | | - write!(f, "{} B", bytes) |
23 | | - } else if *bytes < 1024 * 1024 { |
24 | | - write!(f, "{:.1} KB", *bytes as f64 / 1024.0) |
25 | | - } else if *bytes < 1024 * 1024 * 1024 { |
26 | | - write!(f, "{:.1} MB", *bytes as f64 / (1024.0 * 1024.0)) |
27 | | - } else { |
28 | | - write!(f, "{:.1} GB", *bytes as f64 / (1024.0 * 1024.0 * 1024.0)) |
29 | | - } |
30 | | - } |
31 | | - } |
32 | | - } |
33 | | -} |
34 | | - |
35 | | -#[derive(Debug, PartialEq, Eq)] |
36 | | -pub struct ArtifactCandidate { |
37 | | - pub path: PathBuf, |
38 | | - pub size: Size, |
39 | | -} |
40 | | - |
41 | | -impl ArtifactCandidate { |
42 | | - fn new(path: PathBuf) -> Self { |
43 | | - ArtifactCandidate { |
44 | | - path, |
45 | | - size: Size::UnknownSize, |
46 | | - } |
47 | | - } |
48 | | -} |
49 | | - |
50 | | -impl Ord for ArtifactCandidate { |
51 | | - fn cmp(&self, other: &Self) -> std::cmp::Ordering { |
52 | | - self.path.cmp(&other.path) |
53 | | - } |
54 | | -} |
55 | | - |
56 | | -impl PartialOrd for ArtifactCandidate { |
57 | | - fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> { |
58 | | - Some(self.cmp(other)) |
59 | | - } |
60 | | -} |
61 | | - |
62 | | -pub fn fetch_artifacts(root_path: &PathBuf) -> Vec<ArtifactCandidate> { |
63 | | - let mut builder = GlobSetBuilder::new(); |
64 | | - // FIX-ME hardcoded pattern for Rust |
65 | | - builder.add(Glob::new("**/target").unwrap()); |
66 | | - |
67 | | - let globset = builder.build().unwrap(); |
68 | | - |
69 | | - WalkBuilder::new(root_path) |
70 | | - .git_ignore(false) |
71 | | - .build() |
72 | | - .filter_map(|entry_result| match entry_result { |
73 | | - Err(_) => None, |
74 | | - Ok(entry) if !entry.path().is_dir() => None, |
75 | | - Ok(entry) => { |
76 | | - let path = entry.into_path(); |
77 | | - let rel_path = path.strip_prefix(root_path).ok()?; |
78 | | - match globset.is_match(&rel_path) { |
79 | | - true => Some(ArtifactCandidate::new(path)), |
80 | | - false => None, |
81 | | - } |
82 | | - } |
83 | | - }) |
84 | | - .collect() |
85 | | -} |
86 | | - |
87 | | -#[cfg(test)] |
88 | | -mod tests { |
89 | | - |
90 | | - use assert_fs::prelude::*; |
91 | | - use assert_fs::{ |
92 | | - fixture::{ChildPath, PathChild}, |
93 | | - TempDir, |
94 | | - }; |
95 | | - |
96 | | - use super::{fetch_artifacts, ArtifactCandidate}; |
97 | | - |
98 | | - fn mk_subpath(base: &TempDir, rel_path: &str) -> ChildPath { |
99 | | - let sub = base.child(rel_path); |
100 | | - sub.create_dir_all().unwrap(); |
101 | | - sub |
102 | | - } |
103 | | - |
104 | | - fn mk_rust_project<P: PathChild>(base: &P) { |
105 | | - base.child("target").create_dir_all().unwrap(); |
106 | | - base.child("Cargo.toml").touch().unwrap(); |
107 | | - } |
108 | | - |
109 | | - #[test] |
110 | | - fn test_simple_rust_project_being_scanned_folder() { |
111 | | - // given |
112 | | - let temp = TempDir::new().unwrap(); |
113 | | - mk_rust_project(&temp); |
114 | | - |
115 | | - // when |
116 | | - let results = fetch_artifacts(&temp.to_path_buf()); |
117 | | - |
118 | | - // then |
119 | | - assert_eq!(results.len(), 1, "Expected exactly one artifact"); |
120 | | - |
121 | | - let expected_path = temp.child("target").path().to_path_buf(); |
122 | | - let expected = ArtifactCandidate::new(expected_path); |
123 | | - assert_eq!(&results[0], &expected); |
124 | | - |
125 | | - temp.close().unwrap(); |
126 | | - } |
127 | | - |
128 | | - #[test] |
129 | | - fn test_where_scanned_folder_has_three_rust_projects() { |
130 | | - // given |
131 | | - let temp = TempDir::new().unwrap(); |
132 | | - let project1 = mk_subpath(&temp, "project1"); |
133 | | - mk_rust_project(&project1); |
134 | | - let project2 = mk_subpath(&temp, "project2"); |
135 | | - mk_rust_project(&project2); |
136 | | - let project3 = mk_subpath(&temp, "work/project3"); |
137 | | - mk_rust_project(&project3); |
138 | | - |
139 | | - // when |
140 | | - let mut results = fetch_artifacts(&temp.to_path_buf()); |
141 | | - |
142 | | - // then |
143 | | - assert_eq!(results.len(), 3, "Expected exactly three artifacts"); |
144 | | - results.sort(); |
145 | | - |
146 | | - let expected_path_1 = |
147 | | - temp.child("project1").child("target").path().to_path_buf(); |
148 | | - let expected_1 = ArtifactCandidate::new(expected_path_1); |
149 | | - assert_eq!(&results[0], &expected_1); |
150 | | - |
151 | | - let expected_path_2 = |
152 | | - temp.child("project2").child("target").path().to_path_buf(); |
153 | | - let expected_2 = ArtifactCandidate::new(expected_path_2); |
154 | | - assert_eq!(&results[1], &expected_2); |
155 | | - |
156 | | - let expected_path_3 = temp |
157 | | - .child("work") |
158 | | - .child("project3") |
159 | | - .child("target") |
160 | | - .path() |
161 | | - .to_path_buf(); |
162 | | - let expected_3 = ArtifactCandidate::new(expected_path_3); |
163 | | - assert_eq!(&results[2], &expected_3); |
164 | | - |
165 | | - temp.close().unwrap(); |
166 | | - } |
167 | | -} |
| 2 | +pub mod fetcher; |
| 3 | +pub mod types; |
0 commit comments