From 7b9799f5b58ed1269a505843a6822aec2e9c7272 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Thu, 24 Aug 2023 10:47:43 +0200 Subject: [PATCH] feat: add redirects to the lockfile (#4) --- .github/workflows/ci.yml | 6 ++-- .github/workflows/release.yml | 2 +- Cargo.lock | 23 +++++++++++++++ Cargo.toml | 1 + rust-toolchain.toml | 2 +- src/lib.rs | 54 +++++++++++++++++++++++++++++++---- 6 files changed, 78 insertions(+), 10 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c396716..722388d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -11,7 +11,7 @@ on: jobs: rust: - name: deno_semver-ubuntu-latest-release + name: ubuntu-latest-release runs-on: ubuntu-latest timeout-minutes: 30 @@ -29,7 +29,9 @@ jobs: - uses: dsherret/rust-toolchain-file@v1 - name: Cache - uses: Swatinem/rust-cache@v1 + uses: Swatinem/rust-cache@v2 + with: + save-if: ${{ github.ref == 'refs/heads/main' }} - name: Format run: cargo fmt --all -- --check diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 9fc54a1..c32b9bc 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -25,7 +25,7 @@ jobs: token: ${{ secrets.DENOBOT_PAT }} - uses: denoland/setup-deno@v1 - - uses: dtolnay/rust-toolchain@stable + - uses: dsherret/rust-toolchain-file@v1 - name: Tag and release env: diff --git a/Cargo.lock b/Cargo.lock index a573ecc..0da95c1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -24,6 +24,7 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" name = "deno_lockfile" version = "0.15.0" dependencies = [ + "pretty_assertions", "ring", "serde", "serde_json", @@ -31,6 +32,12 @@ dependencies = [ "thiserror", ] +[[package]] +name = "diff" +version = "0.1.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56254986775e3233ffa9c4d7d3faaf6d36a2c09d30b20687e9f88bc8bafc16c8" + [[package]] name = "itoa" version = "1.0.6" @@ -67,6 +74,16 @@ version = "1.17.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b7e5500299e16ebb147ae15a00a942af264cf3688f47923b8fc2cd5858f23ad3" +[[package]] +name = "pretty_assertions" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af7cee1a6c8a5b9208b3cb1061f10c0cb689087b3d8ce85fb9d2dd7a29b6ba66" +dependencies = [ + "diff", + "yansi", +] + [[package]] name = "proc-macro2" version = "1.0.56" @@ -288,3 +305,9 @@ name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "yansi" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09041cd90cf85f7f8b2df60c646f853b7f535ce68f85244eb6731cf89fa498ec" diff --git a/Cargo.toml b/Cargo.toml index 5bf9f16..654cffc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/rust-toolchain.toml b/rust-toolchain.toml index 779e905..93e249e 100644 --- a/rust-toolchain.toml +++ b/rust-toolchain.toml @@ -1,4 +1,4 @@ [toolchain] -channel = "1.68.2" +channel = "1.71.1" components = ["clippy", "rustfmt"] profile = "minimal" \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 33a4828..6ae7524 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -58,8 +58,8 @@ pub struct NpmPackageInfo { pub struct NpmContent { /// Mapping between requests for npm packages and resolved packages, eg. /// { - /// "chalk": "chalk@5.0.0" - /// "react@17": "react@17.0.1" + /// "chalk": "chalk@5.0.0", + /// "react@17": "react@17.0.1", /// "foo@latest": "foo@1.0.0" /// } pub specifiers: BTreeMap, @@ -89,6 +89,9 @@ 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, } impl LockfileContent { @@ -96,7 +99,8 @@ impl LockfileContent { 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/std@0.190.0/mod.ts" + } +}"#, + false, + ) + .unwrap(); + lockfile.content.redirects.insert( + "https://deno.land/x/other/mod.ts".to_string(), + "https://deno.land/x/other@0.1.0/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/other@0.1.0/mod.ts", + "https://deno.land/x/std/mod.ts": "https://deno.land/std@0.190.0/mod.ts" + } +} +"#, + ); + } }