Skip to content

Commit

Permalink
fix: add insert redirect method (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret committed Aug 24, 2023
1 parent 66d45eb commit 2ccf32f
Showing 1 changed file with 55 additions and 5 deletions.
60 changes: 55 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

Expand Down Expand Up @@ -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": {}
}
"#,
);
}
Expand Down

0 comments on commit 2ccf32f

Please sign in to comment.