From 5c8e70a215ff4c0a667336f842c03826cb095686 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Thu, 24 Aug 2023 09:58:49 -0400 Subject: [PATCH] fix: add insert redirect method --- src/lib.rs | 60 +++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 55 insertions(+), 5 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 891375b..148c40c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -318,13 +318,21 @@ Use \"--lock-write\" flag to regenerate the lockfile at \"{}\".", if maybe_prev.is_none() || maybe_prev != Some(&serialized_package_id) { self.has_content_changed = true; + self + .content + .npm + .specifiers + .insert(serialized_package_req, serialized_package_id); } + } + + pub fn insert_redirect(&mut self, from: String, to: String) { + let maybe_prev = self.content.redirects.get(&from); - self - .content - .npm - .specifiers - .insert(serialized_package_req, serialized_package_id); + if maybe_prev.is_none() || maybe_prev != Some(&to) { + self.has_content_changed = true; + self.content.redirects.insert(from, to); + } } } @@ -597,6 +605,48 @@ mod tests { }, "remote": {} } +"#, + ); + } + + #[test] + fn test_insert_redirect() { + let mut lockfile = Lockfile::with_lockfile_content( + PathBuf::from("/foo/deno.lock"), + r#"{ + "version": "2", + "redirects": { + "https://deno.land/x/std/mod.ts": "https://deno.land/std@0.190.0/mod.ts" + }, + "remote": {} +}"#, + false, + ) + .unwrap(); + lockfile.insert_redirect( + "https://deno.land/x/std/mod.ts".to_string(), + "https://deno.land/std@0.190.0/mod.ts".to_string(), + ); + assert!(!lockfile.has_content_changed); + lockfile.insert_redirect( + "https://deno.land/x/std/mod.ts".to_string(), + "https://deno.land/std@0.190.1/mod.ts".to_string(), + ); + assert!(lockfile.has_content_changed); + lockfile.insert_redirect( + "https://deno.land/x/std/other.ts".to_string(), + "https://deno.land/std@0.190.1/other.ts".to_string(), + ); + assert_eq!( + lockfile.as_json_string(), + r#"{ + "version": "2", + "redirects": { + "https://deno.land/x/std/mod.ts": "https://deno.land/std@0.190.1/mod.ts", + "https://deno.land/x/std/other.ts": "https://deno.land/std@0.190.1/other.ts" + }, + "remote": {} +} "#, ); }