-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add redirects to the lockfile (#4)
- Loading branch information
Showing
6 changed files
with
78 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,4 +14,5 @@ serde_json = "1.0.85" | |
thiserror = "1.0.40" | ||
|
||
[dev-dependencies] | ||
pretty_assertions = "1.4.0" | ||
temp-dir = "0.1.11" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
[toolchain] | ||
channel = "1.68.2" | ||
channel = "1.71.1" | ||
components = ["clippy", "rustfmt"] | ||
profile = "minimal" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,8 +58,8 @@ pub struct NpmPackageInfo { | |
pub struct NpmContent { | ||
/// Mapping between requests for npm packages and resolved packages, eg. | ||
/// { | ||
/// "chalk": "[email protected]" | ||
/// "react@17": "[email protected]" | ||
/// "chalk": "[email protected]", | ||
/// "react@17": "[email protected]", | ||
/// "foo@latest": "[email protected]" | ||
/// } | ||
pub specifiers: BTreeMap<String, String>, | ||
|
@@ -89,14 +89,18 @@ pub struct LockfileContent { | |
#[serde(skip_serializing_if = "NpmContent::is_empty")] | ||
#[serde(default)] | ||
pub npm: NpmContent, | ||
#[serde(skip_serializing_if = "BTreeMap::is_empty")] | ||
#[serde(default)] | ||
pub redirects: BTreeMap<String, String>, | ||
} | ||
|
||
impl LockfileContent { | ||
fn empty() -> Self { | ||
Self { | ||
version: "2".to_string(), | ||
remote: BTreeMap::new(), | ||
npm: NpmContent::default(), | ||
npm: Default::default(), | ||
redirects: Default::default(), | ||
} | ||
} | ||
} | ||
|
@@ -177,6 +181,7 @@ impl Lockfile { | |
version: "2".to_string(), | ||
remote, | ||
npm: NpmContent::default(), | ||
redirects: Default::default(), | ||
} | ||
}; | ||
|
||
|
@@ -188,20 +193,24 @@ impl Lockfile { | |
}) | ||
} | ||
|
||
pub fn as_json_string(&self) -> String { | ||
let mut json_string = serde_json::to_string_pretty(&self.content).unwrap(); | ||
json_string.push('\n'); // trailing newline in file | ||
json_string | ||
} | ||
|
||
// Synchronize lock file to disk - noop if --lock-write file is not specified. | ||
pub fn write(&self) -> Result<(), Error> { | ||
if !self.has_content_changed && !self.overwrite { | ||
return Ok(()); | ||
} | ||
|
||
let mut json_string = serde_json::to_string_pretty(&self.content).unwrap(); | ||
json_string.push('\n'); // trailing newline in file | ||
let mut f = std::fs::OpenOptions::new() | ||
.write(true) | ||
.create(true) | ||
.truncate(true) | ||
.open(&self.filename)?; | ||
f.write_all(json_string.as_bytes())?; | ||
f.write_all(self.as_json_string().as_bytes())?; | ||
Ok(()) | ||
} | ||
|
||
|
@@ -321,6 +330,7 @@ Use \"--lock-write\" flag to regenerate the lockfile at \"{}\".", | |
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use pretty_assertions::assert_eq; | ||
use std::fs::File; | ||
use std::io::prelude::*; | ||
use std::io::Write; | ||
|
@@ -557,4 +567,36 @@ mod tests { | |
let check_err = lockfile.check_or_insert_npm_package(npm_package); | ||
assert!(check_err.is_err()); | ||
} | ||
|
||
#[test] | ||
fn lockfile_with_redirects() { | ||
let mut lockfile = Lockfile::with_lockfile_content( | ||
PathBuf::from("/foo/deno.lock"), | ||
r#"{ | ||
"version": "2", | ||
"remote": {}, | ||
"redirects": { | ||
"https://deno.land/x/std/mod.ts": "https://deno.land/[email protected]/mod.ts" | ||
} | ||
}"#, | ||
false, | ||
) | ||
.unwrap(); | ||
lockfile.content.redirects.insert( | ||
"https://deno.land/x/other/mod.ts".to_string(), | ||
"https://deno.land/x/[email protected]/mod.ts".to_string(), | ||
); | ||
assert_eq!( | ||
lockfile.as_json_string(), | ||
r#"{ | ||
"version": "2", | ||
"remote": {}, | ||
"redirects": { | ||
"https://deno.land/x/other/mod.ts": "https://deno.land/x/[email protected]/mod.ts", | ||
"https://deno.land/x/std/mod.ts": "https://deno.land/[email protected]/mod.ts" | ||
} | ||
} | ||
"#, | ||
); | ||
} | ||
} |