Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add insert redirect method #6

Merged
merged 1 commit into from
Aug 24, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/[email protected]/mod.ts"
},
"remote": {}
}"#,
false,
)
.unwrap();
lockfile.insert_redirect(
"https://deno.land/x/std/mod.ts".to_string(),
"https://deno.land/[email protected]/mod.ts".to_string(),
);
assert!(!lockfile.has_content_changed);
lockfile.insert_redirect(
"https://deno.land/x/std/mod.ts".to_string(),
"https://deno.land/[email protected]/mod.ts".to_string(),
);
assert!(lockfile.has_content_changed);
lockfile.insert_redirect(
"https://deno.land/x/std/other.ts".to_string(),
"https://deno.land/[email protected]/other.ts".to_string(),
);
assert_eq!(
lockfile.as_json_string(),
r#"{
"version": "2",
"redirects": {
"https://deno.land/x/std/mod.ts": "https://deno.land/[email protected]/mod.ts",
"https://deno.land/x/std/other.ts": "https://deno.land/[email protected]/other.ts"
},
"remote": {}
}
"#,
);
}
Expand Down