Skip to content

Commit

Permalink
Tests for parsing out package name
Browse files Browse the repository at this point in the history
  • Loading branch information
NicholasLYang committed Jan 3, 2025
1 parent b1d90fc commit 93929ce
Showing 1 changed file with 32 additions and 9 deletions.
41 changes: 32 additions & 9 deletions crates/turborepo-lib/src/boundaries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,18 @@ impl Run {
})
}

fn get_package_name(import: &str) -> String {
if import.starts_with("@") {
import.split('/').take(2).join("/")
} else {
import
.split_once("/")
.map(|(import, _)| import)
.unwrap_or(import)
.to_string()
}
}

fn check_package_import(
&self,
import: &str,
Expand All @@ -364,15 +376,7 @@ impl Run {
unresolved_external_dependencies: Option<&BTreeMap<String, String>>,
resolver: &Resolver,
) -> Option<BoundariesDiagnostic> {
let package_name = if import.starts_with("@") {
import.split('/').take(2).join("/")
} else {
import
.split_once("/")
.map(|(import, _)| import)
.unwrap_or(import)
.to_string()
};
let package_name = Self::get_package_name(import);

if package_name.starts_with("@types/") && matches!(import_type, ImportType::Value) {
return Some(BoundariesDiagnostic::NotTypeOnlyImport {
Expand Down Expand Up @@ -438,3 +442,22 @@ impl Run {
None
}
}

#[cfg(test)]
mod test {
use test_case::test_case;

use super::*;

#[test_case("", ""; "empty")]
#[test_case("ship", "ship"; "basic")]
#[test_case("@types/ship", "@types/ship"; "types")]
#[test_case("@scope/ship", "@scope/ship"; "scoped")]
#[test_case("@scope/foo/bar", "@scope/foo"; "scoped with path")]
#[test_case("foo/bar", "foo"; "regular with path")]
#[test_case("foo/", "foo"; "trailing slash")]
#[test_case("foo/bar/baz", "foo"; "multiple slashes")]
fn test_get_package_name(import: &str, expected: &str) {
assert_eq!(Run::get_package_name(import), expected);
}
}

0 comments on commit 93929ce

Please sign in to comment.