2022-01-07 22:09:52 -05:00
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
2020-09-21 08:26:41 -04:00
|
|
|
|
2022-10-18 17:09:31 -04:00
|
|
|
use deno_core::anyhow::Context;
|
|
|
|
use deno_core::error::AnyError;
|
2022-10-19 17:30:44 -04:00
|
|
|
use deno_core::serde::Deserialize;
|
|
|
|
use deno_core::serde::Serialize;
|
2020-09-21 12:36:37 -04:00
|
|
|
use deno_core::serde_json;
|
2021-03-26 12:34:25 -04:00
|
|
|
use log::debug;
|
2020-07-02 11:54:51 -04:00
|
|
|
use std::collections::BTreeMap;
|
2022-10-18 17:09:31 -04:00
|
|
|
use std::io::Write;
|
2020-10-19 15:19:20 -04:00
|
|
|
use std::path::PathBuf;
|
2019-11-03 10:39:27 -05:00
|
|
|
|
2022-12-07 18:13:45 -05:00
|
|
|
use crate::args::config_file::LockConfig;
|
2022-11-02 11:32:30 -04:00
|
|
|
use crate::args::ConfigFile;
|
2022-11-08 14:17:24 -05:00
|
|
|
use crate::npm::NpmPackageId;
|
2022-10-25 12:20:07 -04:00
|
|
|
use crate::npm::NpmPackageReq;
|
|
|
|
use crate::npm::NpmResolutionPackage;
|
2021-12-07 19:21:04 -05:00
|
|
|
use crate::tools::fmt::format_json;
|
2022-11-28 17:28:54 -05:00
|
|
|
use crate::util;
|
2022-11-02 11:32:30 -04:00
|
|
|
use crate::Flags;
|
2021-12-07 19:21:04 -05:00
|
|
|
|
2022-12-20 12:00:57 -05:00
|
|
|
use super::DenoSubcommand;
|
|
|
|
|
2022-10-25 12:20:07 -04:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct LockfileError(String);
|
|
|
|
|
|
|
|
impl std::fmt::Display for LockfileError {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
|
|
|
f.write_str(&self.0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::error::Error for LockfileError {}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
|
|
pub struct NpmPackageInfo {
|
|
|
|
pub integrity: String,
|
|
|
|
pub dependencies: BTreeMap<String, String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
|
|
|
|
pub struct NpmContent {
|
2022-11-08 14:17:24 -05:00
|
|
|
/// Mapping between requests for npm packages and resolved packages, eg.
|
2022-10-25 12:20:07 -04:00
|
|
|
/// {
|
|
|
|
/// "chalk": "chalk@5.0.0"
|
|
|
|
/// "react@17": "react@17.0.1"
|
|
|
|
/// "foo@latest": "foo@1.0.0"
|
|
|
|
/// }
|
|
|
|
pub specifiers: BTreeMap<String, String>,
|
|
|
|
/// Mapping between resolved npm specifiers and their associated info, eg.
|
|
|
|
/// {
|
|
|
|
/// "chalk@5.0.0": {
|
|
|
|
/// "integrity": "sha512-...",
|
|
|
|
/// "dependencies": {
|
|
|
|
/// "ansi-styles": "ansi-styles@4.1.0",
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
pub packages: BTreeMap<String, NpmPackageInfo>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl NpmContent {
|
|
|
|
fn is_empty(&self) -> bool {
|
|
|
|
self.specifiers.is_empty() && self.packages.is_empty()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-19 17:30:44 -04:00
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
2022-10-21 15:24:32 -04:00
|
|
|
pub struct LockfileContent {
|
2022-10-19 17:30:44 -04:00
|
|
|
version: String,
|
2022-10-25 12:20:07 -04:00
|
|
|
// Mapping between URLs and their checksums for "http:" and "https:" deps
|
2022-10-19 17:30:44 -04:00
|
|
|
remote: BTreeMap<String, String>,
|
2022-10-25 12:20:07 -04:00
|
|
|
#[serde(skip_serializing_if = "NpmContent::is_empty")]
|
|
|
|
#[serde(default)]
|
|
|
|
pub npm: NpmContent,
|
2022-10-19 17:30:44 -04:00
|
|
|
}
|
|
|
|
|
2022-11-02 11:32:30 -04:00
|
|
|
impl LockfileContent {
|
|
|
|
fn empty() -> Self {
|
|
|
|
Self {
|
|
|
|
version: "2".to_string(),
|
|
|
|
remote: BTreeMap::new(),
|
|
|
|
npm: NpmContent::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-24 18:31:17 -04:00
|
|
|
#[derive(Debug, Clone)]
|
2019-11-03 10:39:27 -05:00
|
|
|
pub struct Lockfile {
|
2022-10-31 19:07:36 -04:00
|
|
|
pub overwrite: bool,
|
|
|
|
pub has_content_changed: bool,
|
2022-10-25 12:20:07 -04:00
|
|
|
pub content: LockfileContent,
|
2020-10-19 15:19:20 -04:00
|
|
|
pub filename: PathBuf,
|
2019-11-03 10:39:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Lockfile {
|
2022-11-02 11:32:30 -04:00
|
|
|
pub fn discover(
|
|
|
|
flags: &Flags,
|
|
|
|
maybe_config_file: Option<&ConfigFile>,
|
|
|
|
) -> Result<Option<Lockfile>, AnyError> {
|
2022-12-20 12:00:57 -05:00
|
|
|
if flags.no_lock
|
|
|
|
|| matches!(
|
|
|
|
flags.subcommand,
|
|
|
|
DenoSubcommand::Install(_) | DenoSubcommand::Uninstall(_)
|
|
|
|
)
|
|
|
|
{
|
2022-11-03 11:42:56 -04:00
|
|
|
return Ok(None);
|
|
|
|
}
|
|
|
|
|
2022-11-02 11:32:30 -04:00
|
|
|
let filename = match flags.lock {
|
|
|
|
Some(ref lock) => PathBuf::from(lock),
|
2022-11-09 18:28:01 -05:00
|
|
|
None => match maybe_config_file {
|
2022-11-02 11:32:30 -04:00
|
|
|
Some(config_file) => {
|
|
|
|
if config_file.specifier.scheme() == "file" {
|
2022-12-07 18:13:45 -05:00
|
|
|
match config_file.clone().to_lock_config()? {
|
|
|
|
Some(LockConfig::Bool(lock)) if !lock => {
|
|
|
|
return Ok(None);
|
|
|
|
}
|
|
|
|
Some(LockConfig::PathBuf(lock)) => config_file
|
|
|
|
.specifier
|
|
|
|
.to_file_path()
|
|
|
|
.unwrap()
|
|
|
|
.parent()
|
|
|
|
.unwrap()
|
|
|
|
.join(lock),
|
|
|
|
_ => {
|
|
|
|
let mut path = config_file.specifier.to_file_path().unwrap();
|
|
|
|
path.set_file_name("deno.lock");
|
|
|
|
path
|
|
|
|
}
|
|
|
|
}
|
2022-11-02 11:32:30 -04:00
|
|
|
} else {
|
|
|
|
return Ok(None);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => return Ok(None),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
let lockfile = Self::new(filename, flags.lock_write)?;
|
|
|
|
Ok(Some(lockfile))
|
|
|
|
}
|
|
|
|
|
2022-10-31 19:07:36 -04:00
|
|
|
pub fn new(filename: PathBuf, overwrite: bool) -> Result<Lockfile, AnyError> {
|
2022-10-19 17:30:44 -04:00
|
|
|
// Writing a lock file always uses the new format.
|
2022-11-02 11:32:30 -04:00
|
|
|
if overwrite {
|
|
|
|
return Ok(Lockfile {
|
|
|
|
overwrite,
|
|
|
|
has_content_changed: false,
|
|
|
|
content: LockfileContent::empty(),
|
|
|
|
filename,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
let result = match std::fs::read_to_string(&filename) {
|
|
|
|
Ok(content) => Ok(content),
|
|
|
|
Err(e) => {
|
|
|
|
if e.kind() == std::io::ErrorKind::NotFound {
|
|
|
|
return Ok(Lockfile {
|
|
|
|
overwrite,
|
|
|
|
has_content_changed: false,
|
|
|
|
content: LockfileContent::empty(),
|
|
|
|
filename,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
Err(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let s = result.with_context(|| {
|
|
|
|
format!("Unable to read lockfile: \"{}\"", filename.display())
|
|
|
|
})?;
|
|
|
|
let value: serde_json::Value =
|
|
|
|
serde_json::from_str(&s).with_context(|| {
|
|
|
|
format!(
|
|
|
|
"Unable to parse contents of the lockfile \"{}\"",
|
|
|
|
filename.display()
|
|
|
|
)
|
|
|
|
})?;
|
|
|
|
let version = value.get("version").and_then(|v| v.as_str());
|
|
|
|
let content = if version == Some("2") {
|
|
|
|
serde_json::from_value::<LockfileContent>(value).with_context(|| {
|
|
|
|
format!(
|
|
|
|
"Unable to parse contents of the lockfile \"{}\"",
|
|
|
|
filename.display()
|
|
|
|
)
|
|
|
|
})?
|
|
|
|
} else {
|
|
|
|
// If there's no version field, we assume that user is using the old
|
|
|
|
// version of the lockfile. We'll migrate it in-place into v2 and it
|
|
|
|
// will be writte in v2 if user uses `--lock-write` flag.
|
|
|
|
let remote: BTreeMap<String, String> = serde_json::from_value(value)
|
|
|
|
.with_context(|| {
|
|
|
|
format!(
|
|
|
|
"Unable to parse contents of the lockfile \"{}\"",
|
|
|
|
filename.display()
|
|
|
|
)
|
|
|
|
})?;
|
2022-10-21 15:24:32 -04:00
|
|
|
LockfileContent {
|
2022-10-19 17:30:44 -04:00
|
|
|
version: "2".to_string(),
|
2022-11-02 11:32:30 -04:00
|
|
|
remote,
|
2022-10-25 12:20:07 -04:00
|
|
|
npm: NpmContent::default(),
|
2022-10-21 15:24:32 -04:00
|
|
|
}
|
2020-07-02 11:54:51 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
Ok(Lockfile {
|
2022-10-31 19:07:36 -04:00
|
|
|
overwrite,
|
|
|
|
has_content_changed: false,
|
2022-10-19 17:30:44 -04:00
|
|
|
content,
|
2019-11-03 10:39:27 -05:00
|
|
|
filename,
|
2020-07-02 11:54:51 -04:00
|
|
|
})
|
2019-11-03 10:39:27 -05:00
|
|
|
}
|
|
|
|
|
2020-07-02 11:54:51 -04:00
|
|
|
// Synchronize lock file to disk - noop if --lock-write file is not specified.
|
2022-10-18 17:09:31 -04:00
|
|
|
pub fn write(&self) -> Result<(), AnyError> {
|
2022-10-31 19:07:36 -04:00
|
|
|
if !self.has_content_changed && !self.overwrite {
|
2020-07-02 11:54:51 -04:00
|
|
|
return Ok(());
|
|
|
|
}
|
2021-12-07 19:21:04 -05:00
|
|
|
|
2022-10-21 15:24:32 -04:00
|
|
|
let json_string = serde_json::to_string(&self.content).unwrap();
|
2022-10-19 17:30:44 -04:00
|
|
|
let format_s = format_json(&json_string, &Default::default())
|
2022-03-29 13:33:00 -04:00
|
|
|
.ok()
|
|
|
|
.flatten()
|
2022-10-19 17:30:44 -04:00
|
|
|
.unwrap_or(json_string);
|
2019-11-03 10:39:27 -05:00
|
|
|
let mut f = std::fs::OpenOptions::new()
|
|
|
|
.write(true)
|
|
|
|
.create(true)
|
|
|
|
.truncate(true)
|
|
|
|
.open(&self.filename)?;
|
2021-12-07 19:21:04 -05:00
|
|
|
f.write_all(format_s.as_bytes())?;
|
2020-10-19 15:19:20 -04:00
|
|
|
debug!("lockfile write {}", self.filename.display());
|
2019-11-03 10:39:27 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-10-25 12:20:07 -04:00
|
|
|
// TODO(bartlomieju): this function should return an error instead of a bool,
|
|
|
|
// but it requires changes to `deno_graph`'s `Locker`.
|
|
|
|
pub fn check_or_insert_remote(
|
|
|
|
&mut self,
|
|
|
|
specifier: &str,
|
|
|
|
code: &str,
|
|
|
|
) -> bool {
|
2022-11-07 22:08:24 -05:00
|
|
|
if !(specifier.starts_with("http:") || specifier.starts_with("https:")) {
|
|
|
|
return true;
|
|
|
|
}
|
2022-10-31 19:07:36 -04:00
|
|
|
if self.overwrite {
|
2020-07-02 11:54:51 -04:00
|
|
|
// In case --lock-write is specified check always passes
|
|
|
|
self.insert(specifier, code);
|
|
|
|
true
|
|
|
|
} else {
|
2022-10-31 19:07:36 -04:00
|
|
|
self.check_or_insert(specifier, code)
|
2020-07-02 11:54:51 -04:00
|
|
|
}
|
2019-11-03 10:39:27 -05:00
|
|
|
}
|
|
|
|
|
2022-10-25 12:20:07 -04:00
|
|
|
pub fn check_or_insert_npm_package(
|
|
|
|
&mut self,
|
|
|
|
package: &NpmResolutionPackage,
|
|
|
|
) -> Result<(), LockfileError> {
|
2022-10-31 19:07:36 -04:00
|
|
|
if self.overwrite {
|
2022-10-25 12:20:07 -04:00
|
|
|
// In case --lock-write is specified check always passes
|
2022-10-31 19:07:36 -04:00
|
|
|
self.insert_npm(package);
|
2022-10-25 12:20:07 -04:00
|
|
|
Ok(())
|
|
|
|
} else {
|
2022-10-31 19:07:36 -04:00
|
|
|
self.check_or_insert_npm(package)
|
2022-10-25 12:20:07 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-31 19:07:36 -04:00
|
|
|
/// Checks the given module is included, if so verify the checksum. If module
|
|
|
|
/// is not included, insert it.
|
|
|
|
fn check_or_insert(&mut self, specifier: &str, code: &str) -> bool {
|
2022-10-21 15:24:32 -04:00
|
|
|
if let Some(lockfile_checksum) = self.content.remote.get(specifier) {
|
2022-11-28 17:28:54 -05:00
|
|
|
let compiled_checksum = util::checksum::gen(&[code.as_bytes()]);
|
2022-10-21 15:24:32 -04:00
|
|
|
lockfile_checksum == &compiled_checksum
|
|
|
|
} else {
|
2022-10-31 19:07:36 -04:00
|
|
|
self.insert(specifier, code);
|
|
|
|
true
|
2020-07-02 11:54:51 -04:00
|
|
|
}
|
2019-11-03 10:39:27 -05:00
|
|
|
}
|
|
|
|
|
2020-07-02 11:54:51 -04:00
|
|
|
fn insert(&mut self, specifier: &str, code: &str) {
|
2022-11-28 17:28:54 -05:00
|
|
|
let checksum = util::checksum::gen(&[code.as_bytes()]);
|
2022-10-21 15:24:32 -04:00
|
|
|
self.content.remote.insert(specifier.to_string(), checksum);
|
2022-10-31 19:07:36 -04:00
|
|
|
self.has_content_changed = true;
|
2019-11-03 10:39:27 -05:00
|
|
|
}
|
2022-10-25 12:20:07 -04:00
|
|
|
|
2022-10-31 19:07:36 -04:00
|
|
|
fn check_or_insert_npm(
|
2022-10-25 12:20:07 -04:00
|
|
|
&mut self,
|
|
|
|
package: &NpmResolutionPackage,
|
|
|
|
) -> Result<(), LockfileError> {
|
2022-11-08 14:17:24 -05:00
|
|
|
let specifier = package.id.as_serialized();
|
2022-10-25 12:20:07 -04:00
|
|
|
if let Some(package_info) = self.content.npm.packages.get(&specifier) {
|
|
|
|
let integrity = package
|
|
|
|
.dist
|
|
|
|
.integrity
|
|
|
|
.as_ref()
|
|
|
|
.unwrap_or(&package.dist.shasum);
|
|
|
|
if &package_info.integrity != integrity {
|
|
|
|
return Err(LockfileError(format!(
|
2022-11-02 11:32:30 -04:00
|
|
|
"Integrity check failed for npm package: \"{}\". Unable to verify that the package
|
|
|
|
is the same as when the lockfile was generated.
|
|
|
|
|
|
|
|
This could be caused by:
|
|
|
|
* the lock file may be corrupt
|
|
|
|
* the source itself may be corrupt
|
|
|
|
|
|
|
|
Use \"--lock-write\" flag to regenerate the lockfile at \"{}\".",
|
2022-11-08 14:17:24 -05:00
|
|
|
package.id.display(), self.filename.display()
|
2022-10-25 12:20:07 -04:00
|
|
|
)));
|
|
|
|
}
|
2022-10-31 19:07:36 -04:00
|
|
|
} else {
|
|
|
|
self.insert_npm(package);
|
2022-10-25 12:20:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-10-31 19:07:36 -04:00
|
|
|
fn insert_npm(&mut self, package: &NpmResolutionPackage) {
|
2022-10-25 12:20:07 -04:00
|
|
|
let dependencies = package
|
|
|
|
.dependencies
|
|
|
|
.iter()
|
2022-11-08 14:17:24 -05:00
|
|
|
.map(|(name, id)| (name.to_string(), id.as_serialized()))
|
2022-10-25 12:20:07 -04:00
|
|
|
.collect::<BTreeMap<String, String>>();
|
|
|
|
|
|
|
|
let integrity = package
|
|
|
|
.dist
|
|
|
|
.integrity
|
|
|
|
.as_ref()
|
|
|
|
.unwrap_or(&package.dist.shasum);
|
|
|
|
self.content.npm.packages.insert(
|
2022-11-08 14:17:24 -05:00
|
|
|
package.id.as_serialized(),
|
2022-10-25 12:20:07 -04:00
|
|
|
NpmPackageInfo {
|
|
|
|
integrity: integrity.to_string(),
|
|
|
|
dependencies,
|
|
|
|
},
|
|
|
|
);
|
2022-10-31 19:07:36 -04:00
|
|
|
self.has_content_changed = true;
|
2022-10-25 12:20:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn insert_npm_specifier(
|
|
|
|
&mut self,
|
|
|
|
package_req: &NpmPackageReq,
|
2022-11-08 14:17:24 -05:00
|
|
|
package_id: &NpmPackageId,
|
2022-10-25 12:20:07 -04:00
|
|
|
) {
|
2022-11-08 14:17:24 -05:00
|
|
|
self
|
|
|
|
.content
|
|
|
|
.npm
|
|
|
|
.specifiers
|
|
|
|
.insert(package_req.to_string(), package_id.as_serialized());
|
2022-10-31 19:07:36 -04:00
|
|
|
self.has_content_changed = true;
|
2022-10-25 12:20:07 -04:00
|
|
|
}
|
2019-11-03 10:39:27 -05:00
|
|
|
}
|
2020-10-04 19:32:18 -04:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2022-10-31 19:07:36 -04:00
|
|
|
use crate::npm::NpmPackageId;
|
|
|
|
use crate::npm::NpmPackageVersionDistInfo;
|
|
|
|
use crate::npm::NpmVersion;
|
2020-10-04 19:32:18 -04:00
|
|
|
use deno_core::serde_json;
|
|
|
|
use deno_core::serde_json::json;
|
2022-10-31 19:07:36 -04:00
|
|
|
use std::collections::HashMap;
|
2020-10-04 19:32:18 -04:00
|
|
|
use std::fs::File;
|
|
|
|
use std::io::prelude::*;
|
|
|
|
use std::io::Write;
|
2022-04-01 11:15:37 -04:00
|
|
|
use test_util::TempDir;
|
2020-10-04 19:32:18 -04:00
|
|
|
|
2022-04-01 11:15:37 -04:00
|
|
|
fn setup(temp_dir: &TempDir) -> PathBuf {
|
2020-10-04 19:32:18 -04:00
|
|
|
let file_path = temp_dir.path().join("valid_lockfile.json");
|
|
|
|
let mut file = File::create(file_path).expect("write file fail");
|
|
|
|
|
|
|
|
let value: serde_json::Value = json!({
|
2022-10-19 17:30:44 -04:00
|
|
|
"version": "2",
|
|
|
|
"remote": {
|
|
|
|
"https://deno.land/std@0.71.0/textproto/mod.ts": "3118d7a42c03c242c5a49c2ad91c8396110e14acca1324e7aaefd31a999b71a4",
|
|
|
|
"https://deno.land/std@0.71.0/async/delay.ts": "35957d585a6e3dd87706858fb1d6b551cb278271b03f52c5a2cb70e65e00c26a"
|
2022-10-25 12:20:07 -04:00
|
|
|
},
|
|
|
|
"npm": {
|
|
|
|
"specifiers": {},
|
2022-10-31 19:07:36 -04:00
|
|
|
"packages": {
|
|
|
|
"nanoid@3.3.4": {
|
|
|
|
"integrity": "sha512-MqBkQh/OHTS2egovRtLk45wEyNXwF+cokD+1YPf9u5VfJiRdAiRwB2froX5Co9Rh20xs4siNPm8naNotSD6RBw==",
|
|
|
|
"dependencies": {}
|
|
|
|
},
|
|
|
|
"picocolors@1.0.0": {
|
|
|
|
"integrity": "sha512-foobar",
|
|
|
|
"dependencies": {}
|
|
|
|
},
|
|
|
|
}
|
2022-10-19 17:30:44 -04:00
|
|
|
}
|
2020-10-04 19:32:18 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
file.write_all(value.to_string().as_bytes()).unwrap();
|
|
|
|
|
2022-04-01 11:15:37 -04:00
|
|
|
temp_dir.path().join("valid_lockfile.json")
|
2020-10-04 19:32:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2022-11-02 11:32:30 -04:00
|
|
|
fn create_lockfile_for_nonexistent_path() {
|
2020-10-19 15:19:20 -04:00
|
|
|
let file_path = PathBuf::from("nonexistent_lock_file.json");
|
2022-11-02 11:32:30 -04:00
|
|
|
assert!(Lockfile::new(file_path, false).is_ok());
|
2020-10-04 19:32:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn new_valid_lockfile() {
|
2022-04-01 11:15:37 -04:00
|
|
|
let temp_dir = TempDir::new();
|
|
|
|
let file_path = setup(&temp_dir);
|
2020-10-04 19:32:18 -04:00
|
|
|
|
|
|
|
let result = Lockfile::new(file_path, false).unwrap();
|
|
|
|
|
2022-10-21 15:24:32 -04:00
|
|
|
let remote = result.content.remote;
|
2022-10-19 17:30:44 -04:00
|
|
|
let keys: Vec<String> = remote.keys().cloned().collect();
|
2020-10-04 19:32:18 -04:00
|
|
|
let expected_keys = vec![
|
|
|
|
String::from("https://deno.land/std@0.71.0/async/delay.ts"),
|
|
|
|
String::from("https://deno.land/std@0.71.0/textproto/mod.ts"),
|
|
|
|
];
|
|
|
|
|
|
|
|
assert_eq!(keys.len(), 2);
|
|
|
|
assert_eq!(keys, expected_keys);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn new_lockfile_from_file_and_insert() {
|
2022-04-01 11:15:37 -04:00
|
|
|
let temp_dir = TempDir::new();
|
|
|
|
let file_path = setup(&temp_dir);
|
2020-10-04 19:32:18 -04:00
|
|
|
|
|
|
|
let mut lockfile = Lockfile::new(file_path, false).unwrap();
|
|
|
|
|
|
|
|
lockfile.insert(
|
|
|
|
"https://deno.land/std@0.71.0/io/util.ts",
|
|
|
|
"Here is some source code",
|
|
|
|
);
|
|
|
|
|
2022-10-21 15:24:32 -04:00
|
|
|
let remote = lockfile.content.remote;
|
2022-10-19 17:30:44 -04:00
|
|
|
let keys: Vec<String> = remote.keys().cloned().collect();
|
2020-10-04 19:32:18 -04:00
|
|
|
let expected_keys = vec![
|
|
|
|
String::from("https://deno.land/std@0.71.0/async/delay.ts"),
|
|
|
|
String::from("https://deno.land/std@0.71.0/io/util.ts"),
|
|
|
|
String::from("https://deno.land/std@0.71.0/textproto/mod.ts"),
|
|
|
|
];
|
|
|
|
assert_eq!(keys.len(), 3);
|
|
|
|
assert_eq!(keys, expected_keys);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn new_lockfile_and_write() {
|
2022-04-01 11:15:37 -04:00
|
|
|
let temp_dir = TempDir::new();
|
|
|
|
let file_path = setup(&temp_dir);
|
2020-10-04 19:32:18 -04:00
|
|
|
|
|
|
|
let mut lockfile = Lockfile::new(file_path, true).unwrap();
|
|
|
|
|
|
|
|
lockfile.insert(
|
|
|
|
"https://deno.land/std@0.71.0/textproto/mod.ts",
|
|
|
|
"Here is some source code",
|
|
|
|
);
|
|
|
|
lockfile.insert(
|
|
|
|
"https://deno.land/std@0.71.0/io/util.ts",
|
|
|
|
"more source code here",
|
|
|
|
);
|
|
|
|
lockfile.insert(
|
|
|
|
"https://deno.land/std@0.71.0/async/delay.ts",
|
|
|
|
"this source is really exciting",
|
|
|
|
);
|
|
|
|
|
|
|
|
lockfile.write().expect("unable to write");
|
|
|
|
|
|
|
|
let file_path_buf = temp_dir.path().join("valid_lockfile.json");
|
|
|
|
let file_path = file_path_buf.to_str().expect("file path fail").to_string();
|
|
|
|
|
|
|
|
// read the file contents back into a string and check
|
|
|
|
let mut checkfile = File::open(file_path).expect("Unable to open the file");
|
|
|
|
let mut contents = String::new();
|
|
|
|
checkfile
|
|
|
|
.read_to_string(&mut contents)
|
|
|
|
.expect("Unable to read the file");
|
|
|
|
|
2020-11-26 07:16:48 -05:00
|
|
|
let contents_json =
|
|
|
|
serde_json::from_str::<serde_json::Value>(&contents).unwrap();
|
2022-10-19 17:30:44 -04:00
|
|
|
let object = contents_json["remote"].as_object().unwrap();
|
2020-11-26 07:16:48 -05:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
object
|
|
|
|
.get("https://deno.land/std@0.71.0/textproto/mod.ts")
|
|
|
|
.and_then(|v| v.as_str()),
|
|
|
|
// sha-256 hash of the source 'Here is some source code'
|
|
|
|
Some("fedebba9bb82cce293196f54b21875b649e457f0eaf55556f1e318204947a28f")
|
|
|
|
);
|
|
|
|
|
|
|
|
// confirm that keys are sorted alphabetically
|
|
|
|
let mut keys = object.keys().map(|k| k.as_str());
|
|
|
|
assert_eq!(
|
|
|
|
keys.next(),
|
|
|
|
Some("https://deno.land/std@0.71.0/async/delay.ts")
|
|
|
|
);
|
|
|
|
assert_eq!(keys.next(), Some("https://deno.land/std@0.71.0/io/util.ts"));
|
|
|
|
assert_eq!(
|
|
|
|
keys.next(),
|
|
|
|
Some("https://deno.land/std@0.71.0/textproto/mod.ts")
|
|
|
|
);
|
|
|
|
assert!(keys.next().is_none());
|
2020-10-04 19:32:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2022-10-31 19:07:36 -04:00
|
|
|
fn check_or_insert_lockfile() {
|
2022-04-01 11:15:37 -04:00
|
|
|
let temp_dir = TempDir::new();
|
|
|
|
let file_path = setup(&temp_dir);
|
2020-10-04 19:32:18 -04:00
|
|
|
|
|
|
|
let mut lockfile = Lockfile::new(file_path, false).unwrap();
|
|
|
|
|
|
|
|
lockfile.insert(
|
|
|
|
"https://deno.land/std@0.71.0/textproto/mod.ts",
|
|
|
|
"Here is some source code",
|
|
|
|
);
|
|
|
|
|
2022-10-25 12:20:07 -04:00
|
|
|
let check_true = lockfile.check_or_insert_remote(
|
2020-10-04 19:32:18 -04:00
|
|
|
"https://deno.land/std@0.71.0/textproto/mod.ts",
|
|
|
|
"Here is some source code",
|
|
|
|
);
|
|
|
|
assert!(check_true);
|
|
|
|
|
2022-10-25 12:20:07 -04:00
|
|
|
let check_false = lockfile.check_or_insert_remote(
|
2020-10-04 19:32:18 -04:00
|
|
|
"https://deno.land/std@0.71.0/textproto/mod.ts",
|
2022-10-31 19:07:36 -04:00
|
|
|
"Here is some NEW source code",
|
2020-10-04 19:32:18 -04:00
|
|
|
);
|
|
|
|
assert!(!check_false);
|
2022-10-31 19:07:36 -04:00
|
|
|
|
|
|
|
// Not present in lockfile yet, should be inserted and check passed.
|
|
|
|
let check_true = lockfile.check_or_insert_remote(
|
|
|
|
"https://deno.land/std@0.71.0/http/file_server.ts",
|
|
|
|
"This is new Source code",
|
|
|
|
);
|
|
|
|
assert!(check_true);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn check_or_insert_lockfile_npm() {
|
|
|
|
let temp_dir = TempDir::new();
|
|
|
|
let file_path = setup(&temp_dir);
|
|
|
|
|
|
|
|
let mut lockfile = Lockfile::new(file_path, false).unwrap();
|
|
|
|
|
|
|
|
let npm_package = NpmResolutionPackage {
|
|
|
|
id: NpmPackageId {
|
|
|
|
name: "nanoid".to_string(),
|
|
|
|
version: NpmVersion::parse("3.3.4").unwrap(),
|
2022-11-08 14:17:24 -05:00
|
|
|
peer_dependencies: Vec::new(),
|
2022-10-31 19:07:36 -04:00
|
|
|
},
|
2022-11-08 14:17:24 -05:00
|
|
|
copy_index: 0,
|
2022-10-31 19:07:36 -04:00
|
|
|
dist: NpmPackageVersionDistInfo {
|
2022-11-08 14:17:24 -05:00
|
|
|
tarball: "foo".to_string(),
|
|
|
|
shasum: "foo".to_string(),
|
2022-10-31 19:07:36 -04:00
|
|
|
integrity: Some("sha512-MqBkQh/OHTS2egovRtLk45wEyNXwF+cokD+1YPf9u5VfJiRdAiRwB2froX5Co9Rh20xs4siNPm8naNotSD6RBw==".to_string())
|
|
|
|
},
|
|
|
|
dependencies: HashMap::new(),
|
|
|
|
};
|
|
|
|
let check_ok = lockfile.check_or_insert_npm_package(&npm_package);
|
|
|
|
assert!(check_ok.is_ok());
|
|
|
|
|
|
|
|
let npm_package = NpmResolutionPackage {
|
|
|
|
id: NpmPackageId {
|
|
|
|
name: "picocolors".to_string(),
|
|
|
|
version: NpmVersion::parse("1.0.0").unwrap(),
|
2022-11-08 14:17:24 -05:00
|
|
|
peer_dependencies: Vec::new(),
|
2022-10-31 19:07:36 -04:00
|
|
|
},
|
2022-11-08 14:17:24 -05:00
|
|
|
copy_index: 0,
|
2022-10-31 19:07:36 -04:00
|
|
|
dist: NpmPackageVersionDistInfo {
|
2022-11-08 14:17:24 -05:00
|
|
|
tarball: "foo".to_string(),
|
|
|
|
shasum: "foo".to_string(),
|
2022-10-31 19:07:36 -04:00
|
|
|
integrity: Some("sha512-1fygroTLlHu66zi26VoTDv8yRgm0Fccecssto+MhsZ0D/DGW2sm8E8AjW7NU5VVTRt5GxbeZ5qBuJr+HyLYkjQ==".to_string())
|
|
|
|
},
|
|
|
|
dependencies: HashMap::new(),
|
|
|
|
};
|
|
|
|
// Integrity is borked in the loaded lockfile
|
|
|
|
let check_err = lockfile.check_or_insert_npm_package(&npm_package);
|
|
|
|
assert!(check_err.is_err());
|
|
|
|
|
|
|
|
let npm_package = NpmResolutionPackage {
|
|
|
|
id: NpmPackageId {
|
|
|
|
name: "source-map-js".to_string(),
|
|
|
|
version: NpmVersion::parse("1.0.2").unwrap(),
|
2022-11-08 14:17:24 -05:00
|
|
|
peer_dependencies: Vec::new(),
|
2022-10-31 19:07:36 -04:00
|
|
|
},
|
2022-11-08 14:17:24 -05:00
|
|
|
copy_index: 0,
|
2022-10-31 19:07:36 -04:00
|
|
|
dist: NpmPackageVersionDistInfo {
|
2022-11-08 14:17:24 -05:00
|
|
|
tarball: "foo".to_string(),
|
|
|
|
shasum: "foo".to_string(),
|
2022-10-31 19:07:36 -04:00
|
|
|
integrity: Some("sha512-R0XvVJ9WusLiqTCEiGCmICCMplcCkIwwR11mOSD9CR5u+IXYdiseeEuXCVAjS54zqwkLcPNnmU4OeJ6tUrWhDw==".to_string())
|
|
|
|
},
|
|
|
|
dependencies: HashMap::new(),
|
|
|
|
};
|
|
|
|
// Not present in lockfile yet, should be inserted and check passed.
|
|
|
|
let check_ok = lockfile.check_or_insert_npm_package(&npm_package);
|
|
|
|
assert!(check_ok.is_ok());
|
|
|
|
|
|
|
|
let npm_package = NpmResolutionPackage {
|
|
|
|
id: NpmPackageId {
|
|
|
|
name: "source-map-js".to_string(),
|
|
|
|
version: NpmVersion::parse("1.0.2").unwrap(),
|
2022-11-08 14:17:24 -05:00
|
|
|
peer_dependencies: Vec::new(),
|
2022-10-31 19:07:36 -04:00
|
|
|
},
|
2022-11-08 14:17:24 -05:00
|
|
|
copy_index: 0,
|
2022-10-31 19:07:36 -04:00
|
|
|
dist: NpmPackageVersionDistInfo {
|
|
|
|
tarball: "foo".to_string(),
|
|
|
|
shasum: "foo".to_string(),
|
|
|
|
integrity: Some("sha512-foobar".to_string()),
|
|
|
|
},
|
|
|
|
dependencies: HashMap::new(),
|
|
|
|
};
|
|
|
|
// Now present in lockfile, should file due to borked integrity
|
|
|
|
let check_err = lockfile.check_or_insert_npm_package(&npm_package);
|
|
|
|
assert!(check_err.is_err());
|
2020-10-04 19:32:18 -04:00
|
|
|
}
|
|
|
|
}
|