Skip to content

Commit

Permalink
Do not return on exact match when searching extensions
Browse files Browse the repository at this point in the history
When looking for "rest", we want all the extensions matching "rest"
returned and not quarkus-rest only.

Fixes quarkusio#39312
  • Loading branch information
gsmet committed Mar 12, 2024
1 parent 7215b28 commit 2f9db47
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public QuarkusCommandOutcome execute(QuarkusCommandInvocation invocation) throws
invocation.getQuarkusProject().getExtensionManager());

final Collection<Extension> extensions = search == null ? invocation.getExtensionsCatalog().getExtensions()
: QuarkusCommandHandlers.select(search, invocation.getExtensionsCatalog().getExtensions(), true)
: QuarkusCommandHandlers.listExtensions(search, invocation.getExtensionsCatalog().getExtensions(), true)
.getExtensions();

if (extensions.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ static List<Extension> computeExtensionsFromQuery(ExtensionCatalog catalog,
result = new SelectionResult(List.of(), false);
}
} else {
result = select(query, extensionCatalog, false);
result = selectExtensions(query, extensionCatalog, false);
}
if (result.matches()) {
builder.addAll(result.getExtensions());
Expand Down Expand Up @@ -96,6 +96,22 @@ static List<ArtifactCoords> computeCoordsFromQuery(final QuarkusCommandInvocatio
: extensions.stream().map(e -> Extensions.stripVersion(e.getArtifact())).collect(Collectors.toList());
}

/**
* Select extensions and return only one if exact match on the name or short name.
*/
static SelectionResult selectExtensions(final String query, final Collection<Extension> allExtensions,
boolean labelLookup) {
return listExtensions(query, allExtensions, true, labelLookup);
}

/**
* List extensions. Returns all matching extensions.
*/
static SelectionResult listExtensions(final String query, final Collection<Extension> allExtensions,
boolean labelLookup) {
return listExtensions(query, allExtensions, false, labelLookup);
}

/**
* Selection algorithm.
*
Expand All @@ -105,8 +121,8 @@ static List<ArtifactCoords> computeCoordsFromQuery(final QuarkusCommandInvocatio
* be {@code false} by default.
* @return the list of matching candidates and whether or not a match has been found.
*/
static SelectionResult select(final String query, final Collection<Extension> allExtensions,
final boolean labelLookup) {
private static SelectionResult listExtensions(final String query, final Collection<Extension> allExtensions,
boolean returnOnExactMatch, boolean labelLookup) {
String q = query.trim().toLowerCase();

final Map<ArtifactKey, Extension> matches = new LinkedHashMap<>();
Expand All @@ -117,7 +133,7 @@ static SelectionResult select(final String query, final Collection<Extension> al
.filter(extension -> extension.getName().equalsIgnoreCase(q)
|| matchesArtifactId(extension.getArtifact().getArtifactId(), q))
.forEach(e -> matches.putIfAbsent(e.getArtifact().getKey(), e));
if (matches.size() == 1) {
if (matches.size() == 1 && returnOnExactMatch) {
return new SelectionResult(matches.values(), true);
}

Expand All @@ -126,7 +142,7 @@ static SelectionResult select(final String query, final Collection<Extension> al
// Try short names
listedExtensions.stream().filter(extension -> matchesShortName(extension, q))
.forEach(e -> matches.putIfAbsent(e.getArtifact().getKey(), e));
if (matches.size() == 1) {
if (matches.size() == 1 && returnOnExactMatch) {
return new SelectionResult(matches.values(), true);
}

Expand All @@ -138,7 +154,7 @@ static SelectionResult select(final String query, final Collection<Extension> al
.forEach(e -> matches.putIfAbsent(e.getArtifact().getKey(), e));
// Even if we have a single partial match, if the name, artifactId and short names are ambiguous, so not
// consider it as a match.
if (matches.size() == 1) {
if (matches.size() == 1 && returnOnExactMatch) {
return new SelectionResult(matches.values(), true);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.quarkus.devtools.commands.handlers;

import static io.quarkus.devtools.commands.handlers.QuarkusCommandHandlers.select;
import static io.quarkus.devtools.commands.handlers.QuarkusCommandHandlers.listExtensions;
import static io.quarkus.devtools.commands.handlers.QuarkusCommandHandlers.selectExtensions;
import static java.util.Arrays.asList;

import java.util.Arrays;
Expand Down Expand Up @@ -35,11 +36,11 @@ void testMultiMatchByLabels() {

List<Extension> extensions = asList(e1, e2, e3);
Collections.shuffle(extensions);
SelectionResult matches = select("foo", extensions, true);
SelectionResult matches = selectExtensions("foo", extensions, true);
Assertions.assertFalse(matches.matches());
Assertions.assertEquals(2, matches.getExtensions().size());

matches = select("foo", extensions, false);
matches = selectExtensions("foo", extensions, false);
Assertions.assertFalse(matches.matches());
Assertions.assertEquals(0, matches.getExtensions().size());
}
Expand All @@ -58,7 +59,7 @@ void testThatSingleLabelMatchIsNotAMatch() {

List<Extension> extensions = asList(e1, e2);
Collections.shuffle(extensions);
SelectionResult matches = select("foo", extensions, true);
SelectionResult matches = selectExtensions("foo", extensions, true);
Assertions.assertFalse(matches.matches());
Assertions.assertEquals(1, matches.getExtensions().size());
}
Expand All @@ -80,11 +81,11 @@ void testMultiMatchByArtifactIdsAndNames() {

List<Extension> extensions = asList(e1, e2, e3);
Collections.shuffle(extensions);
SelectionResult matches = select("foo", extensions, false);
SelectionResult matches = selectExtensions("foo", extensions, false);
Assertions.assertFalse(matches.matches(), " " + matches.getExtensions().size());
Assertions.assertEquals(2, matches.getExtensions().size());

matches = select("foo", extensions, true);
matches = selectExtensions("foo", extensions, true);
Assertions.assertFalse(matches.matches());
Assertions.assertEquals(3, matches.getExtensions().size());

Expand All @@ -108,7 +109,7 @@ void testShortNameSelection() {

List<Extension> extensions = asList(e1, e2, e3);
Collections.shuffle(extensions);
SelectionResult matches = select("foo", extensions, false);
SelectionResult matches = selectExtensions("foo", extensions, false);
Assertions.assertTrue(matches.matches());
Assertions.assertEquals(1, matches.getExtensions().size());
Assertions.assertTrue(matches.iterator().hasNext());
Expand All @@ -135,12 +136,35 @@ void testArtifactIdSelectionWithQuarkusPrefix() {

List<Extension> extensions = asList(e1, e2, e3);
Collections.shuffle(extensions);
SelectionResult matches = select("foo", extensions, false);
SelectionResult matches = selectExtensions("foo", extensions, false);
Assertions.assertEquals(1, matches.getExtensions().size());
Assertions.assertTrue(matches.iterator().hasNext());
Assertions.assertTrue(matches.iterator().next().getArtifact().getArtifactId().equalsIgnoreCase("quarkus-foo"));
}

@Test
void testList() {
Extension e1 = Extension.builder()
.setArtifact(ArtifactCoords.jar("org.acme", "quarkus-rest", "1.0"))
.setName("Quarkus REST");

Extension e2 = Extension.builder()
.setArtifact(ArtifactCoords.jar("org.acme", "quarkus-rest-jackson", "1.0"))
.setName("Quarkus REST Jackson");

Extension e3 = Extension.builder()
.setArtifact(ArtifactCoords.jar("org.acme", "quarkus-kafka", "1.0"))
.setName("unrelated");

List<Extension> extensions = asList(e1, e2, e3);
Collections.shuffle(extensions);
SelectionResult matches = selectExtensions("rest", extensions, true);
Assertions.assertEquals(1, matches.getExtensions().size());

matches = listExtensions("rest", extensions, true);
Assertions.assertEquals(2, matches.getExtensions().size());
}

@Test
void testListedVsUnlisted() {
Extension e1 = Extension.builder()
Expand All @@ -163,10 +187,10 @@ void testListedVsUnlisted() {

List<Extension> extensions = asList(e1, e2, e3);
Collections.shuffle(extensions);
SelectionResult matches = select("quarkus-foo", extensions, true);
SelectionResult matches = selectExtensions("quarkus-foo", extensions, true);
Assertions.assertEquals(2, matches.getExtensions().size());

matches = select("quarkus-foo-unlisted", extensions, true);
matches = selectExtensions("quarkus-foo-unlisted", extensions, true);
Assertions.assertEquals(1, matches.getExtensions().size());

}
Expand Down

0 comments on commit 2f9db47

Please sign in to comment.