Skip to content

Commit

Permalink
Fix Azure path comparison when filePath is root ("/") (#385)
Browse files Browse the repository at this point in the history
* Fix Azure path comparison

* Address comments

* Formatting
  • Loading branch information
tzuan16 authored Oct 18, 2024
1 parent b33d84a commit 3191ea0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ protected final String ensureTrailingSlash(String location) {
}
}

/** If a path doesn't start with `/`, this will add one */
protected final @NotNull String ensureLeadingSlash(@NotNull String location) {
if (location.startsWith("/")) {
return location;
} else {
return "/" + location;
}
}

@Override
public int hashCode() {
return location.hashCode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,10 @@ public boolean isChildOf(@NotNull StorageLocation potentialParent) {
AzureLocation potentialAzureParent = (AzureLocation) potentialParent;
if (this.container.equals(potentialAzureParent.container)) {
if (this.storageAccount.equals(potentialAzureParent.storageAccount)) {
String slashTerminatedFilePath = ensureTrailingSlash(this.filePath);
String slashTerminatedParentFilePath = ensureTrailingSlash(potentialAzureParent.filePath);
return slashTerminatedFilePath.startsWith(slashTerminatedParentFilePath);
String formattedFilePath = ensureLeadingSlash(ensureTrailingSlash(this.filePath));
String formattedParentFilePath =
ensureLeadingSlash(ensureTrailingSlash(potentialAzureParent.filePath));
return formattedFilePath.startsWith(formattedParentFilePath);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,22 @@ public void testCrossSchemeComparisons() {
Assertions.assertThat(abfsLocation.isChildOf(wasbLocation)).isTrue();
}

@Test
public void testLocationComparisons() {
StorageLocation location =
AzureLocation.of("abfss://[email protected]/some_file/metadata");
StorageLocation parentLocation =
AzureLocation.of("abfss://[email protected]");
StorageLocation parentLocationTrailingSlash =
AzureLocation.of("abfss://[email protected]/");

Assertions.assertThat(location).isNotEqualTo(parentLocation);
Assertions.assertThat(location).isNotEqualTo(parentLocationTrailingSlash);

Assertions.assertThat(location.isChildOf(parentLocation)).isTrue();
Assertions.assertThat(location.isChildOf(parentLocationTrailingSlash)).isTrue();
}

@Test
public void testLocation_negative_cases() {
Assertions.assertThatThrownBy(
Expand Down

0 comments on commit 3191ea0

Please sign in to comment.