Skip to content

Commit

Permalink
Fix issue with querying crates.io index
Browse files Browse the repository at this point in the history
The crates.io index was queried using uppercase letters for certain crates. Lookups can only be done using lowercase letters, even though a crate can have uppercase letters in its name.

PiperOrigin-RevId: 560809149
Change-Id: I1fcea808a8b7786da9084c0752f239cb36d4ce9a
  • Loading branch information
chris-campos authored and copybara-github committed Aug 28, 2023
1 parent 68dcee2 commit f5f41ee
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
9 changes: 6 additions & 3 deletions java/com/google/copybara/rust/RustCratesIoVersionList.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import static com.google.common.collect.ImmutableSet.toImmutableSet;

import com.google.common.base.Ascii;
import com.google.common.collect.ImmutableSet;
import com.google.copybara.exception.RepoException;
import com.google.copybara.exception.ValidationException;
Expand Down Expand Up @@ -63,19 +64,21 @@ ImmutableSet<RustRegistryVersionObject> getVersionList() throws RepoException {
String url = CRATES_IO_INDEX_URL;

int nameLength = crateName.length();
String indexCrateName = Ascii.toLowerCase(crateName);

if (nameLength <= 2) {
// If the crate name's length is less than or equal to 2, then the version info is located at
// /<name length>/<crate name>
url += String.format("/%d/%s", nameLength, crateName);
url += String.format("/%d/%s", nameLength, indexCrateName);
} else if (nameLength == 3) {
// If the crate name's length is equal to 3, then the version info is at:
// /3/<first char>/<crate name>
url += String.format("/%d/%c/%s", nameLength, crateName.charAt(0), crateName);
url += String.format("/%d/%c/%s", nameLength, indexCrateName.charAt(0), indexCrateName);
} else {
url +=
String.format(
"/%s/%s/%s", crateName.substring(0, 2), crateName.substring(2, 4), crateName);
"/%s/%s/%s",
indexCrateName.substring(0, 2), indexCrateName.substring(2, 4), indexCrateName);
}

BufferedReader reader = new BufferedReader(new StringReader(executeHTTPQuery(url)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,4 +197,23 @@ public void testRustCrateIoVersionList_withFeatures() throws Exception {
assertThat(resultFeatures).containsKey("example-feature");
assertThat(resultFeatures.get("example-feature").get(0)).isEqualTo("feature2");
}

@Test
public void testRustCrateIoVersionList_cratesWithUpperCaseName() throws Exception {
JsonObject v1 = new JsonObject();
v1.add("name", new JsonPrimitive("Example"));
v1.add("vers", new JsonPrimitive("0.1.0"));
String content =
ImmutableList.of(v1).stream().map(JsonElement::toString).collect(Collectors.joining("\n"));

setUpMockTransportForSkylarkExecutor(
ImmutableMap.of(
"https://raw.githubusercontent.com/rust-lang/crates.io-index/master/ex/am/example",
content));
// The crate name starts with an uppercase letter, but when we look up in the index, we use the
// lowercase spelling.
VersionList versionList =
skylark.eval("version_list", "version_list = rust.crates_io_version_list(crate='Example')");
assertThat(versionList.list()).containsExactlyElementsIn(ImmutableList.of("0.1.0"));
}
}

0 comments on commit f5f41ee

Please sign in to comment.