Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not return on exact match when searching extensions #39370

Merged
merged 1 commit into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading