From dc03ae9e0e62b9d8b13048988c87bef6d4c3f87b Mon Sep 17 00:00:00 2001 From: Burke Davison Date: Mon, 25 Sep 2023 11:54:33 -0400 Subject: [PATCH] chore: support child stub packages --- .../com/microsoft/build/YmlFilesBuilder.java | 4 +-- .../com/microsoft/lookup/PackageLookup.java | 18 ++++++++---- .../microsoft/lookup/PackageLookupTest.java | 29 ++++++++++--------- 3 files changed, 30 insertions(+), 21 deletions(-) diff --git a/third_party/docfx-doclet-143274/src/main/java/com/microsoft/build/YmlFilesBuilder.java b/third_party/docfx-doclet-143274/src/main/java/com/microsoft/build/YmlFilesBuilder.java index bb0a08f0..dd65dd7c 100644 --- a/third_party/docfx-doclet-143274/src/main/java/com/microsoft/build/YmlFilesBuilder.java +++ b/third_party/docfx-doclet-143274/src/main/java/com/microsoft/build/YmlFilesBuilder.java @@ -146,8 +146,8 @@ private TocItem buildPackage(PackageElement element) { // build stubs packageLookup - .findStubPackage(element, allPackages) - .ifPresent((PackageElement stub) -> packageTocItem.getItems().add(buildPackage(stub))); + .findStubPackages(element, allPackages) + .forEach((PackageElement stub) -> packageTocItem.getItems().add(buildPackage(stub))); return packageTocItem; } diff --git a/third_party/docfx-doclet-143274/src/main/java/com/microsoft/lookup/PackageLookup.java b/third_party/docfx-doclet-143274/src/main/java/com/microsoft/lookup/PackageLookup.java index 74b28a53..4d483b46 100644 --- a/third_party/docfx-doclet-143274/src/main/java/com/microsoft/lookup/PackageLookup.java +++ b/third_party/docfx-doclet-143274/src/main/java/com/microsoft/lookup/PackageLookup.java @@ -13,10 +13,12 @@ import com.google.docfx.doclet.ApiVersion; import com.microsoft.lookup.model.ExtendedMetadataFileItem; import com.microsoft.model.Status; +import java.util.Arrays; import java.util.Collection; import java.util.Comparator; import java.util.List; import java.util.Optional; +import java.util.stream.Collectors; import javax.lang.model.element.PackageElement; import jdk.javadoc.doclet.DocletEnvironment; @@ -79,16 +81,20 @@ public boolean isApiVersionStubPackage(PackageElement pkg) { */ @VisibleForTesting boolean isApiVersionStubPackageName(String name) { - return getLeafPackage(name).equals("stub") - && extractApiVersion(withoutLeafPackage(name)).isPresent(); + List packagePath = Arrays.asList(name.split("\\.")); + int stubIndex = packagePath.indexOf("stub"); + if (stubIndex < 1) { + return false; + } + return ApiVersion.parse(packagePath.get(stubIndex - 1)).isPresent(); } - public Optional findStubPackage( + public List findStubPackages( PackageElement pkg, Collection packages) { - String expectedStubPackage = pkg.getQualifiedName() + ".stub"; + String expectedStubPackageBase = pkg.getQualifiedName() + ".stub"; return packages.stream() - .filter(p -> String.valueOf(p.getQualifiedName()).equals(expectedStubPackage)) - .findFirst(); + .filter(p -> String.valueOf(p.getQualifiedName()).startsWith(expectedStubPackageBase)) + .collect(Collectors.toList()); } /** Compare PackageElements by their parsed ApiVersion */ diff --git a/third_party/docfx-doclet-143274/src/test/java/com/microsoft/lookup/PackageLookupTest.java b/third_party/docfx-doclet-143274/src/test/java/com/microsoft/lookup/PackageLookupTest.java index cda06e24..91d1d95b 100644 --- a/third_party/docfx-doclet-143274/src/test/java/com/microsoft/lookup/PackageLookupTest.java +++ b/third_party/docfx-doclet-143274/src/test/java/com/microsoft/lookup/PackageLookupTest.java @@ -11,7 +11,6 @@ import com.microsoft.lookup.PackageLookup.PackageGroup; import com.microsoft.model.Status; import java.util.List; -import java.util.Optional; import java.util.stream.Collectors; import javax.lang.model.element.PackageElement; import javax.lang.model.util.Elements; @@ -188,22 +187,23 @@ public void testFindStubPackage() { elements.getPackageElement("com.microsoft.samples.google.v1beta"), elements.getPackageElement("com.microsoft.samples.google")); - Optional foundStubPackage = - packageLookup.findStubPackage( + List foundStubPackages = + packageLookup.findStubPackages( elements.getPackageElement("com.microsoft.samples.google.v1"), packages); - assertThat(foundStubPackage.isPresent()).isTrue(); - assertThat(toPackageName(foundStubPackage.get())) + assertThat(foundStubPackages).isNotEmpty(); + assertThat(foundStubPackages).hasSize(1); + assertThat(toPackageName(foundStubPackages.get(0))) .isEqualTo("com.microsoft.samples.google.v1.stub"); - Optional notFoundStubPackageOfStubPackage = - packageLookup.findStubPackage( + List notFoundStubPackageOfStubPackage = + packageLookup.findStubPackages( elements.getPackageElement("com.microsoft.samples.google.v1.stub"), packages); - assertThat(notFoundStubPackageOfStubPackage.isPresent()).isFalse(); + assertThat(notFoundStubPackageOfStubPackage).isEmpty(); - Optional notFoundStubPackage = - packageLookup.findStubPackage( + List notFoundStubPackage = + packageLookup.findStubPackages( elements.getPackageElement("com.microsoft.samples.google"), packages); - assertThat(notFoundStubPackage.isPresent()).isFalse(); + assertThat(notFoundStubPackage).isEmpty(); } @Test @@ -216,10 +216,13 @@ public void testIsApiStubPackage() { packageLookup.isApiVersionStubPackage( elements.getPackageElement("com.microsoft.samples.google.v1.stub"))) .isTrue(); + assertThat( + packageLookup.isApiVersionStubPackageName("com.microsoft.samples.google.v1.stub.child")) + .isTrue(); + assertThat(packageLookup.isApiVersionStubPackageName("a")).isFalse(); // False due to not being an API version package, even though it ends in .stub - assertThat(packageLookup.isApiVersionStubPackageName("com.microsoft.samples.google.stub")) - .isFalse(); + assertThat(packageLookup.isApiVersionStubPackageName("a.stub")).isFalse(); } private List toPackageNames(List packages) {