From f5f41ee172d3189216a0ec6a9ed7416c9ca84c0a Mon Sep 17 00:00:00 2001 From: Chris Campos Date: Mon, 28 Aug 2023 13:51:32 -0700 Subject: [PATCH] Fix issue with querying crates.io index 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 --- .../rust/RustCratesIoVersionList.java | 9 ++++++--- .../rust/RustCratesIoVersionListTest.java | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/java/com/google/copybara/rust/RustCratesIoVersionList.java b/java/com/google/copybara/rust/RustCratesIoVersionList.java index 46efeeb32..bc5d19e84 100644 --- a/java/com/google/copybara/rust/RustCratesIoVersionList.java +++ b/java/com/google/copybara/rust/RustCratesIoVersionList.java @@ -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; @@ -63,19 +64,21 @@ ImmutableSet 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 // // - 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// - 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))); diff --git a/javatests/com/google/copybara/rust/RustCratesIoVersionListTest.java b/javatests/com/google/copybara/rust/RustCratesIoVersionListTest.java index f2737b712..61baa1c14 100644 --- a/javatests/com/google/copybara/rust/RustCratesIoVersionListTest.java +++ b/javatests/com/google/copybara/rust/RustCratesIoVersionListTest.java @@ -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")); + } }