Skip to content

Commit aedcb1e

Browse files
committed
Add some unit test to cover find_is_type_dev_for_path
1 parent d3d023d commit aedcb1e

File tree

1 file changed

+110
-9
lines changed

1 file changed

+110
-9
lines changed

rewatch/src/config.rs

Lines changed: 110 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -112,26 +112,26 @@ impl Source {
112112
fn find_is_type_dev_for_sub_folder(
113113
&self,
114114
relative_parent_path: &Path,
115-
relative_source_file: &Path,
115+
target_source_folder: &Path,
116116
) -> bool {
117117
match &self {
118118
Source::Shorthand(sub_folder) => {
119-
relative_parent_path.join(Path::new(sub_folder)) == *relative_source_file
119+
relative_parent_path.join(Path::new(sub_folder)) == *target_source_folder
120120
}
121121
Source::Qualified(package_source) => {
122122
// Note that we no longer check if type_ is dev of the nested subfolder.
123123
// A parent was type:dev, so we assume all subfolders are as well.
124124
let next_parent_path = relative_parent_path.join(Path::new(&package_source.dir));
125-
if next_parent_path == *relative_source_file {
125+
if next_parent_path == *target_source_folder {
126126
return true;
127127
};
128128

129129
match &package_source.subdirs {
130130
None => false,
131131
Some(Subdirs::Recurse(false)) => false,
132-
Some(Subdirs::Recurse(true)) => relative_source_file.starts_with(&next_parent_path),
132+
Some(Subdirs::Recurse(true)) => target_source_folder.starts_with(&next_parent_path),
133133
Some(Subdirs::Qualified(nested_sources)) => nested_sources.iter().any(|nested_source| {
134-
nested_source.find_is_type_dev_for_sub_folder(&next_parent_path, relative_source_file)
134+
nested_source.find_is_type_dev_for_sub_folder(&next_parent_path, target_source_folder)
135135
}),
136136
}
137137
}
@@ -220,7 +220,7 @@ pub type GenTypeConfig = serde_json::Value;
220220

221221
/// # bsconfig.json representation
222222
/// This is tricky, there is a lot of ambiguity. This is probably incomplete.
223-
#[derive(Deserialize, Debug, Clone)]
223+
#[derive(Deserialize, Debug, Clone, Default)]
224224
pub struct Config {
225225
pub name: String,
226226
// In the case of monorepos, the root source won't necessarily have to have sources. It can
@@ -543,9 +543,9 @@ impl Config {
543543
None => false,
544544
Some(Subdirs::Recurse(false)) => false,
545545
Some(Subdirs::Recurse(true)) => relative_path.starts_with(dir_path),
546-
Some(Subdirs::Qualified(sub_dirs)) => sub_dirs.iter().any(|sub_dir| {
547-
sub_dir.find_is_type_dev_for_sub_folder(Path::new(relative_path), relative_parent)
548-
}),
546+
Some(Subdirs::Qualified(sub_dirs)) => sub_dirs
547+
.iter()
548+
.any(|sub_dir| sub_dir.find_is_type_dev_for_sub_folder(dir_path, relative_parent)),
549549
}
550550
}
551551
})
@@ -851,4 +851,105 @@ mod tests {
851851
Some(vec!["@testrepo/main".to_string()])
852852
);
853853
}
854+
855+
fn test_find_is_type_dev(source: OneOrMore<Source>, path: &Path, expected: bool) {
856+
let config = Config {
857+
name: String::from("testrepo"),
858+
sources: Some(source),
859+
..Default::default()
860+
};
861+
let result = config.find_is_type_dev_for_path(path);
862+
assert_eq!(result, expected);
863+
}
864+
865+
#[test]
866+
fn test_find_is_type_dev_for_exact_match() {
867+
test_find_is_type_dev(
868+
OneOrMore::Single(Source::Qualified(PackageSource {
869+
dir: String::from("src"),
870+
subdirs: None,
871+
type_: Some(String::from("dev")),
872+
})),
873+
Path::new("src/Foo.res"),
874+
true,
875+
)
876+
}
877+
878+
#[test]
879+
fn test_find_is_type_dev_for_none_dev() {
880+
test_find_is_type_dev(
881+
OneOrMore::Single(Source::Qualified(PackageSource {
882+
dir: String::from("src"),
883+
subdirs: None,
884+
type_: None,
885+
})),
886+
Path::new("src/Foo.res"),
887+
false,
888+
)
889+
}
890+
891+
#[test]
892+
fn test_find_is_type_dev_for_multiple_sources() {
893+
test_find_is_type_dev(
894+
OneOrMore::Multiple(vec![Source::Qualified(PackageSource {
895+
dir: String::from("src"),
896+
subdirs: None,
897+
type_: Some(String::from("dev")),
898+
})]),
899+
Path::new("src/Foo.res"),
900+
true,
901+
)
902+
}
903+
904+
#[test]
905+
fn test_find_is_type_dev_for_shorthand() {
906+
test_find_is_type_dev(
907+
OneOrMore::Multiple(vec![Source::Shorthand(String::from("src"))]),
908+
Path::new("src/Foo.res"),
909+
false,
910+
)
911+
}
912+
913+
#[test]
914+
fn test_find_is_type_dev_for_recursive_folder() {
915+
test_find_is_type_dev(
916+
OneOrMore::Multiple(vec![Source::Qualified(PackageSource {
917+
dir: String::from("src"),
918+
subdirs: Some(Subdirs::Recurse(true)),
919+
type_: Some(String::from("dev")),
920+
})]),
921+
Path::new("src/bar/Foo.res"),
922+
true,
923+
)
924+
}
925+
926+
#[test]
927+
fn test_find_is_type_dev_for_sub_folder() {
928+
test_find_is_type_dev(
929+
OneOrMore::Multiple(vec![Source::Qualified(PackageSource {
930+
dir: String::from("src"),
931+
subdirs: Some(Subdirs::Qualified(vec![Source::Qualified(PackageSource {
932+
dir: String::from("bar"),
933+
subdirs: None,
934+
type_: None,
935+
})])),
936+
type_: Some(String::from("dev")),
937+
})]),
938+
Path::new("src/bar/Foo.res"),
939+
true,
940+
)
941+
}
942+
943+
#[test]
944+
fn test_find_is_type_dev_for_sub_folder_shorthand() {
945+
test_find_is_type_dev(
946+
OneOrMore::Multiple(vec![Source::Qualified(PackageSource {
947+
dir: String::from("src"),
948+
subdirs: Some(Subdirs::Qualified(vec![Source::Shorthand(String::from("bar"))])),
949+
type_: Some(String::from("dev")),
950+
})]),
951+
Path::new("src/bar/Foo.res"),
952+
true,
953+
)
954+
}
854955
}

0 commit comments

Comments
 (0)