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: use latest tag version directly if version req is * #30

Merged
merged 1 commit into from
Sep 6, 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
43 changes: 43 additions & 0 deletions src/resolution/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ impl NpmVersionResolver {
) -> Result<&'a NpmPackageVersionInfo, NpmPackageVersionResolutionError> {
if let Some(tag) = version_req.tag() {
self.tag_to_version_info(info, tag)
// When the version is *, if there is a latest tag, use it directly.
// No need to care about @types/node here, because it'll be handled specially below.
} else if *version_req == *WILDCARD_VERSION_REQ
&& info.dist_tags.contains_key("latest")
&& info.name != "@types/node"
{
self.tag_to_version_info(info, "latest")
} else {
let mut maybe_best_version: Option<&'a NpmPackageVersionInfo> = None;
for version_info in info.versions.values() {
Expand Down Expand Up @@ -290,4 +297,40 @@ mod test {
);
assert_eq!(result.unwrap().version.to_string(), "1.0.0");
}

#[test]
fn test_wildcard_version_req() {
let package_req = PackageReq::from_str("some-pkg").unwrap();
let package_info = NpmPackageInfo {
name: "some-pkg".to_string(),
versions: HashMap::from([
(
Version::parse_from_npm("1.0.0-rc.1").unwrap(),
NpmPackageVersionInfo {
version: Version::parse_from_npm("1.0.0-rc.1").unwrap(),
..Default::default()
},
),
(
Version::parse_from_npm("2.0.0").unwrap(),
NpmPackageVersionInfo {
version: Version::parse_from_npm("2.0.0").unwrap(),
..Default::default()
},
),
]),
dist_tags: HashMap::from([(
"latest".to_string(),
Version::parse_from_npm("1.0.0-rc.1").unwrap(),
)]),
};
let resolver = NpmVersionResolver {
types_node_version_req: None,
};
let result = resolver.get_resolved_package_version_and_info(
&package_req.version_req,
&package_info,
);
assert_eq!(result.unwrap().version.to_string(), "1.0.0-rc.1");
}
}