mirror of
https://github.com/ProvableHQ/leo.git
synced 2024-11-23 23:23:50 +03:00
Add unit test for package name checking
This commit is contained in:
parent
73b550011e
commit
c76cb0b086
@ -54,22 +54,50 @@ impl Package {
|
|||||||
/// of ASCII alphanumeric characters, and may be word-separated
|
/// of ASCII alphanumeric characters, and may be word-separated
|
||||||
/// by a single dash '-'.
|
/// by a single dash '-'.
|
||||||
pub fn is_package_name_valid(package_name: &str) -> bool {
|
pub fn is_package_name_valid(package_name: &str) -> bool {
|
||||||
// Check that the package name:
|
// Check that the package name is nonempty.
|
||||||
// 1. is lowercase,
|
if package_name.len() == 0 {
|
||||||
// 2. is ASCII alphanumeric or a dash.
|
tracing::error!("Project names must be nonempty");
|
||||||
package_name.chars().all(|x| {
|
return false;
|
||||||
if !x.is_lowercase() {
|
}
|
||||||
|
|
||||||
|
let mut previous = package_name.chars().nth(0).unwrap();
|
||||||
|
|
||||||
|
// Check that the first character is not a dash.
|
||||||
|
if previous == '-' {
|
||||||
|
tracing::error!("Project names cannot begin with a dash");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Iterate and check that the package name is valid.
|
||||||
|
for current in package_name.chars() {
|
||||||
|
// Check that the package name is lowercase.
|
||||||
|
if !current.is_ascii_lowercase() && current != '-' {
|
||||||
tracing::error!("Project names must be all lowercase");
|
tracing::error!("Project names must be all lowercase");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if x.is_ascii_alphanumeric() || x == '-' {
|
// Check that the package name is only ASCII alphanumeric or a dash.
|
||||||
|
if !current.is_ascii_alphanumeric() && current != '-' {
|
||||||
tracing::error!("Project names must be ASCII alphanumeric, and may be word-separated with a dash");
|
tracing::error!("Project names must be ASCII alphanumeric, and may be word-separated with a dash");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
true
|
// If the previous character was a dash, check that the current character is not a dash.
|
||||||
})
|
if previous == '-' && current == '-' {
|
||||||
|
tracing::error!("Project names may only be word-separated by one dash");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
previous = current;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check that the last character is not a dash.
|
||||||
|
if previous == '-' {
|
||||||
|
tracing::error!("Project names cannot end with a dash");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns `true` if a package is can be initialized at a given path.
|
/// Returns `true` if a package is can be initialized at a given path.
|
||||||
@ -229,3 +257,27 @@ impl Package {
|
|||||||
Ok(ImportsDirectory::remove_import(path, package_name)?)
|
Ok(ImportsDirectory::remove_import(path, package_name)?)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_is_package_name_valid() {
|
||||||
|
assert!(Package::is_package_name_valid("foo"));
|
||||||
|
assert!(Package::is_package_name_valid("foo-bar"));
|
||||||
|
assert!(Package::is_package_name_valid("foo-bar-baz"));
|
||||||
|
|
||||||
|
assert!(!Package::is_package_name_valid(""));
|
||||||
|
assert!(!Package::is_package_name_valid("-"));
|
||||||
|
assert!(!Package::is_package_name_valid("-foo"));
|
||||||
|
assert!(!Package::is_package_name_valid("-foo-"));
|
||||||
|
assert!(!Package::is_package_name_valid("foo--bar"));
|
||||||
|
assert!(!Package::is_package_name_valid("foo---bar"));
|
||||||
|
assert!(!Package::is_package_name_valid("foo--bar--baz"));
|
||||||
|
assert!(!Package::is_package_name_valid("foo---bar---baz"));
|
||||||
|
assert!(!Package::is_package_name_valid("foo*bar"));
|
||||||
|
assert!(!Package::is_package_name_valid("foo,bar"));
|
||||||
|
assert!(!Package::is_package_name_valid("foo_bar"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user