Skip to content

Commit 9dc68f4

Browse files
committed
Refactor find_app
1 parent 2810b60 commit 9dc68f4

File tree

1 file changed

+34
-64
lines changed

1 file changed

+34
-64
lines changed

test/test-manager/src/package.rs

+34-64
Original file line numberDiff line numberDiff line change
@@ -70,72 +70,42 @@ fn find_app(
7070
let current_dir = std::env::current_dir().expect("Unable to get current directory");
7171
let packages_dir = package_folder.unwrap_or(&current_dir);
7272
std::fs::create_dir_all(packages_dir)?;
73-
let mut dir = std::fs::read_dir(packages_dir.clone()).context("Failed to list packages")?;
74-
75-
let mut matches = vec![];
76-
77-
while let Some(Ok(entry)) = dir.next() {
78-
let path = entry.path();
79-
if !path.is_file() {
80-
continue;
81-
}
82-
83-
// Filter out irrelevant platforms
84-
if !e2e_bin {
85-
let ext = get_ext(package_type);
86-
87-
// Skip file if wrong file extension
88-
if !path
73+
let dir = std::fs::read_dir(packages_dir.clone()).context("Failed to list packages")?;
74+
75+
dir
76+
.filter_map(|entry| entry.ok())
77+
.map(|entry| entry.path())
78+
.filter(|entry| entry.is_file())
79+
.filter(|path| {
80+
e2e_bin ||
81+
path
8982
.extension()
90-
.map(|m_ext| m_ext.eq_ignore_ascii_case(ext))
83+
.map(|m_ext| m_ext.eq_ignore_ascii_case(get_ext(package_type)))
9184
.unwrap_or(false)
92-
{
93-
continue;
94-
}
95-
}
96-
97-
let mut u8_path = path.as_os_str().to_string_lossy().into_owned();
98-
u8_path.make_ascii_lowercase();
99-
100-
// Skip non-UI-e2e binaries or vice versa
101-
if e2e_bin ^ u8_path.contains("app-e2e-tests") {
102-
continue;
103-
}
104-
105-
// Filter out irrelevant platforms
106-
if e2e_bin && !u8_path.contains(get_os_name(package_type)) {
107-
continue;
108-
}
109-
110-
// Skip file if it doesn't match the architecture
111-
if let Some(arch) = package_type.2 {
112-
// Skip for non-e2e bin on non-Linux, because there's only one package
113-
if (e2e_bin || package_type.0 == OsType::Linux)
114-
&& !arch.get_identifiers().iter().any(|id| u8_path.contains(id))
115-
{
116-
continue;
117-
}
118-
}
119-
120-
if u8_path.contains(&app) {
121-
matches.push(path);
122-
}
123-
}
124-
125-
// TODO: Search for package in git repository if not found
126-
127-
// Take the shortest match
128-
matches.sort_unstable_by_key(|path| path.as_os_str().len());
129-
matches.into_iter().next().context(if e2e_bin {
130-
format!(
131-
"Could not find UI/e2e test for package: {app}.\n\
132-
Expecting a binary named like `app-e2e-tests-{app}_ARCH` to exist in {package_dir}/\n\
133-
Example ARCH: `amd64-unknown-linux-gnu`, `x86_64-unknown-linux-gnu`",
134-
package_dir = packages_dir.display()
135-
)
136-
} else {
137-
format!("Could not find package for app: {app}")
138-
})
85+
}) // Filter out irrelevant platforms
86+
.map(|path| {
87+
let u8_path = path.as_os_str().to_string_lossy().to_ascii_lowercase();
88+
(path, u8_path)
89+
})
90+
.filter(|(_path, u8_path)| !(e2e_bin ^ u8_path.contains("app-e2e-tests"))) // Skip non-UI-e2e binaries or vice versa
91+
.filter(|(_path, u8_path)| !e2e_bin || u8_path.contains(get_os_name(package_type))) // Filter out irrelevant platforms
92+
.filter(|(_path, u8_path)| {
93+
let linux = e2e_bin || package_type.0 == OsType::Linux;
94+
let matching_ident = package_type.2.map(|arch| arch.get_identifiers().iter().any(|id| u8_path.contains(id))).unwrap_or(true);
95+
// Skip for non-Linux, because there's only one package
96+
!linux || matching_ident
97+
}) // Skip file if it doesn't match the architecture
98+
.find(|(_path, u8_path)| u8_path.contains(&app)) // Find match
99+
.map(|(path, _)| path).context(if e2e_bin {
100+
format!(
101+
"Could not find UI/e2e test for package: {app}.\n\
102+
Expecting a binary named like `app-e2e-tests-{app}_ARCH` to exist in {package_dir}/\n\
103+
Example ARCH: `amd64-unknown-linux-gnu`, `x86_64-unknown-linux-gnu`",
104+
package_dir = packages_dir.display()
105+
)
106+
} else {
107+
format!("Could not find package for app: {app}")
108+
})
139109
}
140110

141111
fn get_ext(package_type: (OsType, Option<PackageType>, Option<Architecture>)) -> &'static str {

0 commit comments

Comments
 (0)