diff options
Diffstat (limited to 'tests/util/server/src/npm.rs')
-rw-r--r-- | tests/util/server/src/npm.rs | 69 |
1 files changed, 32 insertions, 37 deletions
diff --git a/tests/util/server/src/npm.rs b/tests/util/server/src/npm.rs index 809584ced..7cb6a04f2 100644 --- a/tests/util/server/src/npm.rs +++ b/tests/util/server/src/npm.rs @@ -13,7 +13,8 @@ use once_cell::sync::Lazy; use parking_lot::Mutex; use tar::Builder; -use crate::testdata_path; +use crate::tests_path; +use crate::PathRef; pub const DENOTEST_SCOPE_NAME: &str = "@denotest"; pub const DENOTEST2_SCOPE_NAME: &str = "@denotest2"; @@ -21,8 +22,11 @@ pub const DENOTEST2_SCOPE_NAME: &str = "@denotest2"; pub static PUBLIC_TEST_NPM_REGISTRY: Lazy<TestNpmRegistry> = Lazy::new(|| { TestNpmRegistry::new( NpmRegistryKind::Public, - &format!("http://localhost:{}", crate::servers::PORT), - "/npm/registry", + &format!( + "http://localhost:{}", + crate::servers::PUBLIC_NPM_REGISTRY_PORT + ), + "npm", ) }); @@ -35,8 +39,7 @@ pub static PRIVATE_TEST_NPM_REGISTRY_1: Lazy<TestNpmRegistry> = "http://localhost:{}", crate::servers::PRIVATE_NPM_REGISTRY_1_PORT ), - // TODO: change it - "/npm/registry", + "npm-private", ) }); @@ -51,37 +54,34 @@ struct CustomNpmPackage { } /// Creates tarballs and a registry json file for npm packages -/// in the `testdata/npm/registry/@denotest` directory. +/// in the `tests/registry/npm/@denotest` directory. pub struct TestNpmRegistry { #[allow(unused)] kind: NpmRegistryKind, // Eg. http://localhost:4544/ hostname: String, - // Eg. /registry/npm/ - path: String, + /// Path in the tests/registry folder (Eg. npm) + local_path: String, cache: Mutex<HashMap<String, CustomNpmPackage>>, } impl TestNpmRegistry { - pub fn new(kind: NpmRegistryKind, hostname: &str, path: &str) -> Self { + pub fn new(kind: NpmRegistryKind, hostname: &str, local_path: &str) -> Self { let hostname = hostname.strip_suffix('/').unwrap_or(hostname).to_string(); - assert!( - !path.is_empty(), - "npm test registry must have a non-empty path" - ); - let stripped = path.strip_prefix('/').unwrap_or(path); - let stripped = path.strip_suffix('/').unwrap_or(stripped); - let path = format!("/{}/", stripped); Self { hostname, - path, + local_path: local_path.to_string(), kind, cache: Default::default(), } } + pub fn root_dir(&self) -> PathRef { + tests_path().join("registry").join(&self.local_path) + } + pub fn tarball_bytes( &self, name: &str, @@ -98,6 +98,10 @@ impl TestNpmRegistry { self.get_package_property(name, |p| p.registry_file.as_bytes().to_vec()) } + pub fn package_url(&self, package_name: &str) -> String { + format!("http://{}/{}/", self.hostname, package_name) + } + fn get_package_property<TResult>( &self, package_name: &str, @@ -105,7 +109,7 @@ impl TestNpmRegistry { ) -> Result<Option<TResult>> { // it's ok if multiple threads race here as they will do the same work twice if !self.cache.lock().contains_key(package_name) { - match get_npm_package(&self.hostname, &self.path, package_name)? { + match get_npm_package(&self.hostname, &self.local_path, package_name)? { Some(package) => { self.cache.lock().insert(package_name.to_string(), package); } @@ -115,19 +119,12 @@ impl TestNpmRegistry { Ok(self.cache.lock().get(package_name).map(func)) } - pub fn strip_registry_path_prefix_from_uri_path<'s>( - &self, - uri_path: &'s str, - ) -> Option<&'s str> { - uri_path.strip_prefix(&self.path) - } - pub fn get_test_scope_and_package_name_with_path_from_uri_path<'s>( &self, uri_path: &'s str, ) -> Option<(&'s str, &'s str)> { - let prefix1 = format!("{}{}/", self.path, DENOTEST_SCOPE_NAME); - let prefix2 = format!("{}{}%2f", self.path, DENOTEST_SCOPE_NAME); + let prefix1 = format!("/{}/", DENOTEST_SCOPE_NAME); + let prefix2 = format!("/{}%2f", DENOTEST_SCOPE_NAME); let maybe_package_name_with_path = uri_path .strip_prefix(&prefix1) @@ -137,8 +134,8 @@ impl TestNpmRegistry { return Some((DENOTEST_SCOPE_NAME, package_name_with_path)); } - let prefix1 = format!("{}{}/", self.path, DENOTEST2_SCOPE_NAME); - let prefix2 = format!("{}{}%2f", self.path, DENOTEST2_SCOPE_NAME); + let prefix1 = format!("/{}/", DENOTEST2_SCOPE_NAME); + let prefix2 = format!("/{}%2f", DENOTEST2_SCOPE_NAME); let maybe_package_name_with_path = uri_path .strip_prefix(&prefix1) @@ -150,18 +147,17 @@ impl TestNpmRegistry { None } - - pub fn uri_path_starts_with_registry_path(&self, uri_path: &str) -> bool { - uri_path.starts_with(&self.path) - } } fn get_npm_package( registry_hostname: &str, - registry_path: &str, + local_path: &str, package_name: &str, ) -> Result<Option<CustomNpmPackage>> { - let package_folder = testdata_path().join("npm/registry").join(package_name); + let package_folder = tests_path() + .join("registry") + .join(local_path) + .join(package_name); if !package_folder.exists() { return Ok(None); } @@ -208,8 +204,7 @@ fn get_npm_package( dist.insert("shasum".to_string(), "dummy-value".into()); dist.insert( "tarball".to_string(), - format!("{registry_hostname}{registry_path}{package_name}/{version}.tgz") - .into(), + format!("{registry_hostname}/{package_name}/{version}.tgz").into(), ); tarballs.insert(version.clone(), tarball_bytes); |