-
Notifications
You must be signed in to change notification settings - Fork 97
Stop fabricating PackageId and look it up instead #341
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
---|---|---|
|
@@ -8,6 +8,8 @@ use std::fmt; | |
use std::path::Path; | ||
use std::str::FromStr; | ||
|
||
use crate::metadata::MergedMetadata; | ||
|
||
impl EncodableResolve { | ||
pub fn load_lock_file(path: &Path) -> Result<EncodableResolve, Error> { | ||
let config = &std::fs::read_to_string(path) | ||
|
@@ -27,8 +29,19 @@ impl EncodableResolve { | |
|
||
pub fn get_hashes_by_package_id( | ||
&self, | ||
metadata: &MergedMetadata, | ||
hashes: &mut HashMap<PackageId, String>, | ||
) -> Result<(), Error> { | ||
let mut package_id_by_source = HashMap::new(); | ||
for p in &metadata.packages { | ||
let Some(ref source) = p.source else { | ||
// local crate | ||
continue; | ||
}; | ||
let key = (p.name.as_str(), source.repr.as_str(), p.version.to_string()); | ||
package_id_by_source.insert(key, &p.id); | ||
} | ||
|
||
for EncodableDependency { | ||
name, | ||
version, | ||
|
@@ -37,13 +50,17 @@ impl EncodableResolve { | |
.. | ||
} in self.package.iter() | ||
{ | ||
if let (Some(source), Some(checksum)) = (source, checksum) { | ||
let package_id = PackageId { | ||
repr: format!("{} {} ({})", name, version, source), | ||
}; | ||
if checksum != "<none>" { | ||
hashes.insert(package_id, checksum.clone()); | ||
} | ||
let Some((source, checksum)) = Option::zip(source.as_ref(), checksum.as_ref()) else { | ||
continue; | ||
}; | ||
if checksum == "<none>" { | ||
continue; | ||
} | ||
|
||
if let Some(package_id) = | ||
package_id_by_source.get(&(name.as_str(), source.as_str(), version.clone())) | ||
{ | ||
hashes.insert((*package_id).clone(), checksum.clone()); | ||
} | ||
} | ||
|
||
|
@@ -66,88 +83,6 @@ impl EncodableResolve { | |
} | ||
} | ||
|
||
#[test] | ||
fn test_no_legacy_checksums() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please don't delete them, rather add some for the new cases :) |
||
let config = r#" | ||
[[package]] | ||
name = "aho-corasick" | ||
version = "0.7.6" | ||
source = "registry+https://github.com/rust-lang/crates.io-index" | ||
dependencies = [ | ||
"memchr 2.3.0 (registry+https://github.com/rust-lang/crates.io-index)", | ||
] | ||
"#; | ||
let resolve = EncodableResolve::load_lock_string(Path::new("dummy"), config).unwrap(); | ||
let mut hashes = HashMap::new(); | ||
resolve.get_hashes_by_package_id(&mut hashes).unwrap(); | ||
assert_eq!(hashes, HashMap::new()); | ||
} | ||
|
||
#[test] | ||
fn test_some_legacy_checksums() { | ||
let config = r#" | ||
[[package]] | ||
name = "aho-corasick" | ||
version = "0.7.6" | ||
source = "registry+https://github.com/rust-lang/crates.io-index" | ||
dependencies = [ | ||
"memchr 2.3.0 (registry+https://github.com/rust-lang/crates.io-index)", | ||
] | ||
|
||
[metadata] | ||
"checksum structopt 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)" = "16c2cdbf9cc375f15d1b4141bc48aeef444806655cd0e904207edc8d68d86ed7" | ||
"checksum structopt-derive 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)" = "53010261a84b37689f9ed7d395165029f9cc7abb9f56bbfe86bee2597ed25107" | ||
|
||
"#; | ||
let resolve = EncodableResolve::load_lock_string(Path::new("dummy"), config).unwrap(); | ||
let mut hashes = HashMap::new(); | ||
resolve.get_hashes_by_package_id(&mut hashes).unwrap(); | ||
assert_eq!( | ||
hashes, | ||
[( | ||
PackageId { repr: "structopt 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)".to_string() }, | ||
"16c2cdbf9cc375f15d1b4141bc48aeef444806655cd0e904207edc8d68d86ed7" | ||
), | ||
( | ||
PackageId { repr: "structopt-derive 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)".to_string()}, | ||
"53010261a84b37689f9ed7d395165029f9cc7abb9f56bbfe86bee2597ed25107" | ||
)] | ||
.iter() | ||
.map(|(package_id, hash)| (package_id.clone(), hash.to_string())) | ||
.collect::<HashMap<_, _>>() | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_some_inline_checksums() { | ||
let config = r#" | ||
[[package]] | ||
name = "aho-corasick" | ||
version = "0.7.6" | ||
source = "registry+https://github.com/rust-lang/crates.io-index" | ||
dependencies = [ | ||
"memchr 2.3.0 (registry+https://github.com/rust-lang/crates.io-index)", | ||
] | ||
checksum = "16c2cdbf9cc375f15d1b4141bc48aeef444806655cd0e904207edc8d68d86ed7" | ||
"#; | ||
let resolve = EncodableResolve::load_lock_string(Path::new("dummy"), config).unwrap(); | ||
let mut hashes = HashMap::new(); | ||
resolve.get_hashes_by_package_id(&mut hashes).unwrap(); | ||
assert_eq!( | ||
hashes, | ||
[( | ||
PackageId { | ||
repr: "aho-corasick 0.7.6 (registry+https://github.com/rust-lang/crates.io-index)" | ||
.to_string() | ||
}, | ||
"16c2cdbf9cc375f15d1b4141bc48aeef444806655cd0e904207edc8d68d86ed7" | ||
)] | ||
.iter() | ||
.map(|(package_id, hash)| (package_id.clone(), hash.to_string())) | ||
.collect::<HashMap<_, _>>() | ||
); | ||
} | ||
|
||
// | ||
// The code below was copied/adjusted from Cargo. | ||
// | ||
|
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.