Skip to content

Commit

Permalink
SLCORE-302 Add utility to convert a server path to an IDE path
Browse files Browse the repository at this point in the history
  • Loading branch information
jblievremont authored and henryju committed Mar 1, 2021
1 parent 3b9ce50 commit 2995e3b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.sonarsource.sonarlint.core.client.api.connected;

import java.util.Objects;
import java.util.Optional;

/**
* Describes the link between a project in the IDE and a project in SonarQube.
Expand Down Expand Up @@ -49,6 +50,21 @@ public String idePathPrefix() {
return idePathPrefix;
}

public Optional<String> serverPathToIdePath(String serverPath) {
if (!serverPath.startsWith(sqPathPrefix())) {
return Optional.empty();
}
int localPrefixLen = sqPathPrefix().length();
if (localPrefixLen > 0) {
localPrefixLen++;
}
String actualLocalPrefix = idePathPrefix();
if (!actualLocalPrefix.isEmpty()) {
actualLocalPrefix = actualLocalPrefix + "/";
}
return Optional.of(actualLocalPrefix + serverPath.substring(localPrefixLen));
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,28 @@ void equals_and_hashCode_should_use_all_fields() {
assertThat(projectBinding1.hashCode()).isNotEqualTo(projectBinding4.hashCode());
assertThat(projectBinding1.hashCode()).isEqualTo(projectBinding5.hashCode());
}

@Test
void serverPathToIdePath_no_match_from_server_path() {
ProjectBinding projectBinding = new ProjectBinding("key", "sqPrefix", "localPrefix");
assertThat(projectBinding.serverPathToIdePath("notSqPrefix/some/path")).isEmpty();
}

@Test
void serverPathToIdePath_general_case() {
ProjectBinding projectBinding = new ProjectBinding("key", "sq/path/prefix", "local/prefix");
assertThat(projectBinding.serverPathToIdePath("sq/path/prefix/some/path")).hasValue("local/prefix/some/path");
}

@Test
void serverPathToIdePath_empty_local_path() {
ProjectBinding projectBinding = new ProjectBinding("key", "sq/path/prefix", "");
assertThat(projectBinding.serverPathToIdePath("sq/path/prefix/some/path")).hasValue("some/path");
}

@Test
void serverPathToIdePath_empty_sq_path() {
ProjectBinding projectBinding = new ProjectBinding("key", "", "local/prefix");
assertThat(projectBinding.serverPathToIdePath("some/path")).hasValue("local/prefix/some/path");
}
}

0 comments on commit 2995e3b

Please sign in to comment.