1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-21 15:04:11 -05:00

chore(npm): add test for esm npm binary package (#15609)

This commit is contained in:
David Sherret 2022-08-26 09:17:48 -04:00 committed by GitHub
parent f35900eb5b
commit f583a7ebf4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 148 additions and 123 deletions

View file

@ -226,6 +226,13 @@ itest!(deno_run_cowthink {
http_server: true,
});
itest!(deno_run_esm_module {
args: "run --unstable -A --quiet npm:@denotest/esm-bin this is a test",
output: "npm/deno_run_esm.out",
envs: env_vars(),
http_server: true,
});
itest!(deno_run_non_existent {
args: "run --unstable npm:mkdirp@0.5.125",
output: "npm/deno_run_non_existent.out",

View file

@ -0,0 +1,4 @@
this
is
a
test

View file

@ -1,4 +1,4 @@
{
"name": "@deno/cjs-local-global-decls",
"version": "1.0.0"
}
}

View file

@ -0,0 +1,5 @@
import process from "node:process";
for (const arg of process.argv.slice(2)) {
console.log(arg);
}

View file

@ -0,0 +1,5 @@
{
"name": "@deno/esm-bin",
"version": "1.0.0",
"bin": "./cli.mjs"
}

View file

@ -14,7 +14,7 @@ use hyper::Request;
use hyper::Response;
use hyper::StatusCode;
use lazy_static::lazy_static;
use npm::custom_npm_cache;
use npm::CUSTOM_NPM_PACKAGE_CACHE;
use os_pipe::pipe;
use pretty_assertions::assert_eq;
use regex::Regex;
@ -1009,19 +1009,18 @@ fn handle_custom_npm_registry_path(
.split('/')
.filter(|p| !p.is_empty())
.collect::<Vec<_>>();
let cache = custom_npm_cache()?;
let cache = &CUSTOM_NPM_PACKAGE_CACHE;
let package_name = format!("@denotest/{}", parts[0]);
if parts.len() == 2 {
if let Some(file_bytes) =
cache.tarball_bytes(&package_name, parts[1].trim_end_matches(".tgz"))
cache.tarball_bytes(&package_name, parts[1].trim_end_matches(".tgz"))?
{
let file_resp = custom_headers("file.tgz", file_bytes.to_owned());
let file_resp = custom_headers("file.tgz", file_bytes);
return Ok(Some(file_resp));
}
} else if parts.len() == 1 {
if let Some(registry_file) = cache.registry_file(&package_name) {
let file_resp =
custom_headers("registry.json", registry_file.as_bytes().to_vec());
if let Some(registry_file) = cache.registry_file(&package_name)? {
let file_resp = custom_headers("registry.json", registry_file);
return Ok(Some(file_resp));
}
}

View file

@ -2,24 +2,17 @@ use std::collections::HashMap;
use std::fs;
use anyhow::Context;
use anyhow::Result;
use flate2::write::GzEncoder;
use flate2::Compression;
use once_cell::sync::Lazy;
use parking_lot::Mutex;
use tar::Builder;
use crate::testdata_path;
static CUSTOM_NPM_PACKAGE_CACHE: Lazy<Result<CustomNpmPackageCache, String>> =
Lazy::new(|| CustomNpmPackageCache::load().map_err(|e| e.to_string()));
/// Get a reference to the custom npm cache which is lazily created.
pub fn custom_npm_cache(
) -> Result<&'static CustomNpmPackageCache, anyhow::Error> {
match &*CUSTOM_NPM_PACKAGE_CACHE {
Ok(cache) => Ok(cache),
Err(err) => Err(anyhow::anyhow!("{}", err)),
}
}
pub static CUSTOM_NPM_PACKAGE_CACHE: Lazy<CustomNpmPackageCache> =
Lazy::new(CustomNpmPackageCache::default);
struct CustomNpmPackage {
pub registry_file: String,
@ -28,116 +21,128 @@ struct CustomNpmPackage {
/// Creates tarballs and a registry json file for npm packages
/// in the `testdata/npm/registry/@denotest` directory.
pub struct CustomNpmPackageCache(HashMap<String, CustomNpmPackage>);
#[derive(Default)]
pub struct CustomNpmPackageCache(Mutex<HashMap<String, CustomNpmPackage>>);
impl CustomNpmPackageCache {
pub fn load() -> Result<Self, anyhow::Error> {
use ring::digest::Context;
use ring::digest::SHA512;
pub fn tarball_bytes(
&self,
name: &str,
version: &str,
) -> Result<Option<Vec<u8>>> {
Ok(
self
.get_package_property(name, |p| p.tarballs.get(version).cloned())?
.flatten(),
)
}
// read all the packages in the @denotest folder
let custom_packages_path = testdata_path().join("npm/registry/@denotest");
let mut packages = HashMap::new();
for entry in fs::read_dir(&custom_packages_path)? {
let entry = entry?;
let file_type = entry.file_type()?;
if !file_type.is_dir() {
continue;
}
pub fn registry_file(&self, name: &str) -> Result<Option<Vec<u8>>> {
self.get_package_property(name, |p| p.registry_file.as_bytes().to_vec())
}
// read all the package's versions
let mut tarballs = HashMap::new();
let package_folder_name = entry.file_name().to_string_lossy().to_string();
let package_name = format!("@denotest/{}", package_folder_name);
let package_folder = custom_packages_path.join(&package_folder_name);
let mut versions = serde_json::Map::new();
for entry in fs::read_dir(&package_folder)? {
let entry = entry?;
let file_type = entry.file_type()?;
if !file_type.is_dir() {
continue;
fn get_package_property<TResult>(
&self,
package_name: &str,
func: impl FnOnce(&CustomNpmPackage) -> TResult,
) -> Result<Option<TResult>> {
// it's ok if multiple threads race here as they will do the same work twice
if !self.0.lock().contains_key(package_name) {
match get_npm_package(package_name)? {
Some(package) => {
self.0.lock().insert(package_name.to_string(), package);
}
let version = entry.file_name().to_string_lossy().to_string();
let version_folder = package_folder.join(&version);
// create the tarball
let mut tarball_bytes = Vec::new();
{
let mut encoder =
GzEncoder::new(&mut tarball_bytes, Compression::default());
{
let mut builder = Builder::new(&mut encoder);
builder
.append_dir_all("package", &version_folder)
.with_context(|| {
format!(
"Error adding tarball for directory: {}",
version_folder.display()
)
})?;
builder.finish()?;
}
encoder.finish()?;
}
// get tarball hash
let mut hash_ctx = Context::new(&SHA512);
hash_ctx.update(&tarball_bytes);
let digest = hash_ctx.finish();
let tarball_checksum = base64::encode(digest.as_ref()).to_lowercase();
// create the registry file JSON for this version
let mut dist = serde_json::Map::new();
dist.insert(
"integrity".to_string(),
format!("sha512-{}", tarball_checksum).into(),
);
dist.insert("shasum".to_string(), "dummy-value".into());
dist.insert(
"tarball".to_string(),
format!(
"http://localhost:4545/npm/registry/{}/{}.tgz",
package_name, version
)
.into(),
);
tarballs.insert(version.clone(), tarball_bytes);
let package_json_path = version_folder.join("package.json");
let package_json_text = fs::read_to_string(&package_json_path)
.with_context(|| {
format!(
"Error reading package.json at {}",
package_json_path.display()
)
})?;
let mut version_info: serde_json::Map<String, serde_json::Value> =
serde_json::from_str(&package_json_text)?;
version_info.insert("dist".to_string(), dist.into());
versions.insert(version, version_info.into());
None => return Ok(None),
}
// create the registry file for this package
let mut registry_file = serde_json::Map::new();
registry_file.insert("name".to_string(), package_name.clone().into());
registry_file.insert("versions".to_string(), versions.into());
packages.insert(
package_name,
CustomNpmPackage {
registry_file: serde_json::to_string(&registry_file).unwrap(),
tarballs,
},
);
}
Ok(Self(packages))
}
pub fn tarball_bytes(&self, name: &str, version: &str) -> Option<&Vec<u8>> {
self.0.get(name).and_then(|p| p.tarballs.get(version))
}
pub fn registry_file(&self, name: &str) -> Option<&String> {
self.0.get(name).map(|p| &p.registry_file)
Ok(self.0.lock().get(package_name).map(func))
}
}
fn get_npm_package(package_name: &str) -> Result<Option<CustomNpmPackage>> {
use ring::digest::Context;
use ring::digest::SHA512;
let package_folder = testdata_path().join("npm/registry").join(package_name);
if !package_folder.exists() {
return Ok(None);
}
// read all the package's versions
let mut tarballs = HashMap::new();
let mut versions = serde_json::Map::new();
for entry in fs::read_dir(&package_folder)? {
let entry = entry?;
let file_type = entry.file_type()?;
if !file_type.is_dir() {
continue;
}
let version = entry.file_name().to_string_lossy().to_string();
let version_folder = package_folder.join(&version);
// create the tarball
let mut tarball_bytes = Vec::new();
{
let mut encoder =
GzEncoder::new(&mut tarball_bytes, Compression::default());
{
let mut builder = Builder::new(&mut encoder);
builder
.append_dir_all("package", &version_folder)
.with_context(|| {
format!(
"Error adding tarball for directory: {}",
version_folder.display()
)
})?;
builder.finish()?;
}
encoder.finish()?;
}
// get tarball hash
let mut hash_ctx = Context::new(&SHA512);
hash_ctx.update(&tarball_bytes);
let digest = hash_ctx.finish();
let tarball_checksum = base64::encode(digest.as_ref()).to_lowercase();
// create the registry file JSON for this version
let mut dist = serde_json::Map::new();
dist.insert(
"integrity".to_string(),
format!("sha512-{}", tarball_checksum).into(),
);
dist.insert("shasum".to_string(), "dummy-value".into());
dist.insert(
"tarball".to_string(),
format!(
"http://localhost:4545/npm/registry/{}/{}.tgz",
package_name, version
)
.into(),
);
tarballs.insert(version.clone(), tarball_bytes);
let package_json_path = version_folder.join("package.json");
let package_json_text = fs::read_to_string(&package_json_path)
.with_context(|| {
format!(
"Error reading package.json at {}",
package_json_path.display()
)
})?;
let mut version_info: serde_json::Map<String, serde_json::Value> =
serde_json::from_str(&package_json_text)?;
version_info.insert("dist".to_string(), dist.into());
versions.insert(version, version_info.into());
}
// create the registry file for this package
let mut registry_file = serde_json::Map::new();
registry_file.insert("name".to_string(), package_name.to_string().into());
registry_file.insert("versions".to_string(), versions.into());
Ok(Some(CustomNpmPackage {
registry_file: serde_json::to_string(&registry_file).unwrap(),
tarballs,
}))
}