Fix the regex matche issue and create a unit tests for the `search_for` utilty function

This commit is contained in:
Anas Elgarhy 2023-02-08 22:15:21 +02:00
parent c71992fc52
commit eb3715f4cb
No known key found for this signature in database
GPG Key ID: 0501802A1D496528
8 changed files with 96 additions and 7 deletions

View File

@ -3,6 +3,7 @@
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/tests" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />

View File

@ -2,7 +2,7 @@
<project version="4">
<component name="DBNavigator.Project.DataEditorManager">
<record-view-column-sorting-type value="BY_INDEX" />
<value-preview-text-wrapping value="true" />
<value-preview-text-wrapping value="false" />
<value-preview-pinned value="false" />
</component>
<component name="DBNavigator.Project.DatabaseEditorStateManager">
@ -11,6 +11,9 @@
<component name="DBNavigator.Project.DatabaseFileManager">
<open-files />
</component>
<component name="DBNavigator.Project.ExecutionManager">
<retain-sticky-names value="false" />
</component>
<component name="DBNavigator.Project.Settings">
<connections />
<browser-settings>

1
Cargo.lock generated
View File

@ -208,6 +208,7 @@ dependencies = [
"clap",
"lrc",
"notify-rust",
"regex",
"test-context",
"typed-builder",
]

View File

@ -8,6 +8,7 @@ edition = "2021"
[dependencies]
lrc = { version = "0.1.7", optional = true }
notify-rust = { version = "4.7.0", features = ["images"] }
regex = "1.7.1"
typed-builder = "0.12.0"
[dependencies.clap]

View File

@ -7,16 +7,17 @@ use std::path::Path;
pub fn search_for(
search_directory: &str,
max_depth: u8,
regx: &[&str],
) -> std::io::Result<Option<String>> {
regx: &str,
) -> Result<Option<String>, String> {
// Search in the track directory.
for entry in std::fs::read_dir(search_directory)? {
for entry in std::fs::read_dir(search_directory).map_err(|e| e.to_string())? {
let Ok(entry) = entry else { continue; };
let Ok(file_type) = entry.file_type() else { continue; };
if file_type.is_file() {
let Ok(file_name) = entry.file_name().into_string() else { continue; };
// Check if the file name matches any of the regular expressions.
if regx.iter().any(|&regx| file_name.contains(regx)) {
// Check if the file name matches the regular expression.
let matcher = regex::Regex::new(regx).map_err(|e| e.to_string())?;
if matcher.is_match(&file_name) {
let path = entry.path();
let Some(path) = path.to_str() else { continue; };
return Ok(Some(path.to_string()));
@ -64,6 +65,7 @@ pub fn process_template_placeholders(template: &String, track: &cmus::Track) ->
#[cfg(test)]
mod tests {
use super::*;
use std::assert_matches::assert_matches;
use std::str::FromStr;
use test_context::{test_context, TestContext};
@ -77,7 +79,7 @@ mod tests {
track: cmus::Track::from_str(include_str!(
"../tests/samples/cmus-remote-output-with-all-tags.txt"
))
.unwrap(),
.unwrap(),
}
}
}
@ -93,4 +95,30 @@ mod tests {
"Photograph/Alex Goot/Alex Goot & Friends, Vol. 3/8"
);
}
#[test]
fn test_search_for_cover_with_the_cover_key_world() {
let cover_path = search_for(
"tests/samples/Owl City/Cinematic",
1,
r"cover|.\.jpg|.\.png",
);
assert_matches!(cover_path, Ok(Some(_)));
assert_eq!(
cover_path.unwrap().unwrap(),
"tests/samples/Owl City/Cinematic/cover.jpg"
);
}
#[test]
fn test_search_for_cover_without_the_cover_key_world() {
let cover_path = search_for("tests/samples/Owl City/Cinematic", 1, r".\.jpg|.\.png");
assert_matches!(cover_path, Ok(Some(_)));
assert_eq!(
cover_path.unwrap().unwrap(),
"tests/samples/Owl City/Cinematic/cover.jpg"
);
}
}

View File

@ -0,0 +1,55 @@
[00:24.28]When the road is long
[00:27.65]And your strength is gone
[00:30.64]Remember I am just a prayer away
[00:37.27]When the way is hard
[00:40.39]And you are faint of heart
[00:43.79]Remember I am just a prayer away
[00:50.42]
[00:50.42]Hold fast, I'll guide you through the night
[00:56.41]And fear not for I am by your side
[01:03.25]
[01:03.25]Listen through the rain
[01:06.57]And you can hear the angels say
[01:09.53]Help is on the way
[01:13.02]The moment you begin to pray
[01:16.13]When the thunders roar
[01:19.50]You don't need to be afraid
[01:22.66]I'll lead you through the storm
[01:26.06]So please remember when I say
[01:29.68]I'm with you always
[01:56.59]
[01:56.59]When you are worn out
[02:00.06]And you are let down
[02:03.08]Remember I am just a prayer away
[02:09.87]When every door is locked
[02:13.00]And you feel so lost
[02:16.41]Remember I am just a prayer away
[02:22.73]
[02:22.73]Hold fast, I'll guide you through the night
[02:28.94]And fear not for I am by your side
[02:35.81]
[02:35.81]Listen through the rain
[02:39.06]And you can hear the angels say
[02:42.38]Help is on the way
[02:45.77]The moment you begin to pray
[02:48.61]When the thunders roar
[02:52.22]You don't need to be afraid
[02:55.22]I'll lead you through the storm
[02:58.67]So please remember when I say
[03:02.35]I'm with you always
[03:08.30]
[03:08.30]Come and walk with me, yeah
[03:14.42]Come and talk with me, yeah
[03:21.12]Soar on eagles' wings
[03:25.38]Run and not be faint
[03:32.16]
[03:32.16]Listen through the rain
[03:35.44]And you can hear the angels say
[03:38.49]Help is on the way
[03:41.84]The moment you begin to pray
[03:45.06]When the thunders roar
[03:48.50]You don't need to be afraid
[03:51.48]I'll lead you through the storm
[03:54.94]So please remember when I say
[03:58.54]I'm with you always
[04:12.24]I'm with you always

Binary file not shown.

After

Width:  |  Height:  |  Size: 966 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.3 MiB