mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(codeartifact): list a package version's direct dependencies (#8474)
Adds ListPackageVersionDependencies, extracted from each format's own metadata
file: the POM for Maven, package.json for npm, the wheel/sdist metadata for
pypi, and the .nuspec for NuGet. Scoped to exactly npm/pypi/maven/nuget,
matching AWS's own documented Valid Values for this action's format
parameter; generic, cargo, ruby, and swift are rejected with a
ValidationException even though cargo's own sparse index already carries the
data, since AWS does not document cargo as a valid format for this specific
action.
A new RepositorySidecarManager#listPackageVersionDependencies default method
(throwing UnsupportedOperationException, the same convention
listPackageVersionAssetNames already uses) is overridden by all four valid
formats' sidecar clients:
- ReposiliteSidecarClient tries the version's standard release POM name
first, falling back to the real directory listing for a -SNAPSHOT
version's own timestamped filename (and surfacing a genuine fetch error
through that same fallback, rather than reporting it as an empty
dependency list), and parses it with a new PomDependencies helper. That
helper only ever walks direct children of a known element rather than
Document#getElementsByTagName, specifically so a managed version pin under
dependencyManagement or a conditionally-activated dependency under profiles
is never mistaken for a real, always-active dependency. One synthetic
"parent"-typed entry is emitted when <parent> is present. No ${property}
placeholder resolution on a dependency's raw version text, a documented
limitation.
- VerdaccioSidecarClient reads dependencies/devDependencies/
peerDependencies/optionalDependencies straight off the already-fetched
packument's version document, splitting a scoped dependency name the same
way CodeArtifactNpmDataPlane already does for the package's own name.
- BagetterSidecarClient reuses the same already-proven .nuspec fetch
fetchPackageVersionReadme falls back to, with NuspecXml extended with a new
dependencies() method that flattens every <group targetFramework> block
(and the older schema's bare <dependency> with no group wrapper) into one
list.
- PypiserverSidecarClient is new ground: a new PypiVersionMetadata helper
opens a real wheel via plain ZipInputStream, or an sdist via GZIPInputStream
plus a new minimal hand-rolled TAR reader (classic 512-byte headers only,
no long-name or PAX support) for .tar.gz/.tgz, or via ZipInputStream for a
.zip sdist, bounding the total bytes it will ever decompress while
scanning one archive so a small, highly compressible entry ahead of the
real metadata entry cannot force unbounded decompression. Only the exact
top-level dist-info/METADATA or PKG-INFO entry is matched, never a nested
one, so a wheel or sdist that vendors another package's own dist-info
alongside its own cannot have that vendored entry mistaken for the real
one. Requires-Dist headers are parsed with RFC822 continuation-line
folding, stopping at the blank line that ends the real header block so a
Requires-Dist-looking line inside the package's own long description is
never read as a real dependency. Any "; extra == ..." environment marker
is kept verbatim in versionRequirement rather than stripped, since
PackageDependency has no marker field. A release available only as a real
.tar.bz2/.tar.xz sdist is reported as having no dependencies rather than
attempted, since bzip2/xz decompression would need a new runtime
dependency this codebase deliberately avoids.
XmlParser gained a new parseDocument(byte[]) overload so a POM or .nuspec's
own raw bytes reach the parser directly: the existing String-based overload
pre-decodes as UTF-8 before the parser ever sees the original bytes, which
would silently corrupt or fail to parse a document that declares a different
encoding. PomDependencies and NuspecXml both now go through the byte-based
overload.
CodeArtifactService#listPackageVersionDependencies validates the format
against this narrower set before the shared validatePackageCoordinates call,
which already enforces the maven-namespace-required and
pypi/nuget-namespace-forbidden rules AWS documents for this action.
Pagination's cursor now folds in each entry's own original-list index as a
final tiebreaker, so two byte-for-byte identical dependencies (e.g. the same
NuGet package id declared under two different target-framework groups with
the same version requirement) never share a cursor and silently drop across
a page boundary.
Verified with standalone parser unit tests, per-client wire-contract tests
against a JDK HttpServer fake, CodeArtifactServiceTest dispatch and
validation cases, and a live Docker-gated round trip added to all four
existing format Docker integration tests, each publishing a real artifact
with real dependency metadata through the real sidecar container.
This commit is contained in:
1 parent
5059a3cd40
commit
6be7641359
25 files changed
+1689
-5
No files matched your search
@@ -337,6 +337,41 @@ class BagetterSidecarClientTest {
|
||||
assertTrue(result.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void listPackageVersionDependenciesReadsTheRealNuspecDependenciesGroups() throws Exception {
|
||||
BagetterSidecarManager manager = manager();
|
||||
seedPooledContainer(manager, "nuget-repo-1", "tracked-bagetter-1", backendUrl);
|
||||
respondWithVersions("demo-pkg", "1.0.0");
|
||||
String nuspec = "<?xml version=\"1.0\"?><package><metadata><id>demo-pkg</id><version>1.0.0</version>"
|
||||
+ "<dependencies><group targetFramework=\"net6.0\">"
|
||||
+ "<dependency id=\"Newtonsoft.Json\" version=\"13.0.1\"/></group></dependencies>"
|
||||
+ "</metadata></package>";
|
||||
backend.createContext("/v3/package/demo-pkg/1.0.0/demo-pkg.nuspec", exchange -> {
|
||||
byte[] body = nuspec.getBytes(StandardCharsets.UTF_8);
|
||||
exchange.sendResponseHeaders(200, body.length);
|
||||
exchange.getResponseBody().write(body);
|
||||
exchange.close();
|
||||
});
|
||||
|
||||
List<CodeArtifactService.PackageDependency> dependencies = client(manager).listPackageVersionDependencies(
|
||||
"nuget-repo-1", "dom", "repo", null, "demo-pkg", "1.0.0");
|
||||
|
||||
assertEquals(List.of(new CodeArtifactService.PackageDependency(null, "Newtonsoft.Json", null, "13.0.1")),
|
||||
dependencies);
|
||||
}
|
||||
|
||||
@Test
|
||||
void listPackageVersionDependenciesIsEmptyWhenTheVersionWasNeverPublished() throws Exception {
|
||||
BagetterSidecarManager manager = manager();
|
||||
seedPooledContainer(manager, "nuget-repo-1", "tracked-bagetter-1", backendUrl);
|
||||
respondWithVersions("demo-pkg", "1.0.0");
|
||||
|
||||
List<CodeArtifactService.PackageDependency> dependencies = client(manager).listPackageVersionDependencies(
|
||||
"nuget-repo-1", "dom", "repo", null, "demo-pkg", "9.9.9");
|
||||
|
||||
assertTrue(dependencies.isEmpty());
|
||||
}
|
||||
|
||||
/**
|
||||
* BaGetter's flat container (what {@link #respondWithVersions} fakes) always returns
|
||||
* normalized, lowercased version strings, but its registration resource's {@code
|
||||
|
||||
+36
@@ -834,6 +834,42 @@ class CodeArtifactMavenDockerIntegrationTest {
|
||||
assertEquals(before + metadata.length, after);
|
||||
}
|
||||
|
||||
/**
|
||||
* A throwaway groupId/artifactId, not {@link #GAV}: {@code ListPackageVersionDependencies}
|
||||
* parses this version's own real POM, fetched back off the real Reposilite sidecar, rather
|
||||
* than anything hand-built in process.
|
||||
*/
|
||||
@Test
|
||||
@Order(25)
|
||||
void listPackageVersionDependenciesParsesTheRealPomFetchedBackFromReposilite() {
|
||||
byte[] jar = "depcheck-jar-bytes".getBytes(StandardCharsets.UTF_8);
|
||||
String pomXml = "<project><parent><groupId>com.example</groupId><artifactId>depcheck-parent</artifactId>"
|
||||
+ "<version>9.0.0</version></parent><dependencies>"
|
||||
+ "<dependency><groupId>com.example</groupId><artifactId>depcheck-dep</artifactId>"
|
||||
+ "<version>2.0.0</version><scope>test</scope></dependency>"
|
||||
+ "</dependencies></project>";
|
||||
given().header("Authorization", "Bearer " + bearerToken).body(jar)
|
||||
.put("/codeartifact/maven/" + DOMAIN + "/" + REPO + "/com/example/depcheck/1.0.0/depcheck-1.0.0.jar")
|
||||
.then().statusCode(200);
|
||||
given().header("Authorization", "Bearer " + bearerToken).body(pomXml.getBytes(StandardCharsets.UTF_8))
|
||||
.put("/codeartifact/maven/" + DOMAIN + "/" + REPO + "/com/example/depcheck/1.0.0/depcheck-1.0.0.pom")
|
||||
.then().statusCode(200);
|
||||
|
||||
JsonPath body = given().header("Authorization", AUTH)
|
||||
.post("/v1/package/version/dependencies?domain=" + DOMAIN + "&repository=" + REPO + "&format=maven"
|
||||
+ "&namespace=com.example&package=depcheck&version=1.0.0")
|
||||
.then().statusCode(200)
|
||||
.body("package", equalTo("depcheck"))
|
||||
.body("dependencies.size()", equalTo(2))
|
||||
.extract().jsonPath();
|
||||
List<String> dependencyTypes = body.getList("dependencies.dependencyType", String.class);
|
||||
List<String> packageNames = body.getList("dependencies.package", String.class);
|
||||
assertTrue(dependencyTypes.contains("parent"), "expected a parent entry among " + dependencyTypes);
|
||||
assertTrue(packageNames.contains("depcheck-parent"));
|
||||
assertTrue(dependencyTypes.contains("test"), "expected a test-scoped entry among " + dependencyTypes);
|
||||
assertTrue(packageNames.contains("depcheck-dep"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads the real Reposilite instance's own {@code maven} settings domain directly (the same
|
||||
* shared container {@link ReposiliteSidecarManager} started for every test in this class).
|
||||
|
||||
+34
@@ -753,6 +753,40 @@ class CodeArtifactNpmDockerIntegrationTest {
|
||||
+ copiedVersion.path("dist").path("tarball").asText());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(25)
|
||||
void listPackageVersionDependenciesReadsAllFourDependencyMapsFromTheRealSidecar() {
|
||||
String packageName = "listdeps-npm-pkg";
|
||||
byte[] tarball = "list-deps-npm-tarball-bytes".getBytes(StandardCharsets.UTF_8);
|
||||
String envelope = "{\"name\":\"" + packageName + "\",\"versions\":{\"1.0.0\":{\"name\":\"" + packageName
|
||||
+ "\",\"version\":\"1.0.0\","
|
||||
+ "\"dependencies\":{\"@types/node\":\"^18.0.0\"},"
|
||||
+ "\"devDependencies\":{\"jest\":\"^29.0.0\"},"
|
||||
+ "\"peerDependencies\":{\"react\":\">=16\"},"
|
||||
+ "\"optionalDependencies\":{\"fsevents\":\"^2.0.0\"},"
|
||||
+ "\"dist\":{\"tarball\":\"http://ignored/" + packageName + "-1.0.0.tgz\"}}},\"_attachments\":{\""
|
||||
+ packageName + "-1.0.0.tgz\":{\"content_type\":\"application/octet-stream\",\"data\":\""
|
||||
+ Base64.getEncoder().encodeToString(tarball) + "\",\"length\":" + tarball.length + "}}}";
|
||||
given().header("Authorization", "Bearer " + bearerToken).contentType("application/json").body(envelope)
|
||||
.put("/codeartifact/npm/" + DOMAIN + "/" + REPO + "/" + packageName)
|
||||
.then().statusCode(anyOf201Or200());
|
||||
|
||||
JsonPath body = given().header("Authorization", AUTH)
|
||||
.post("/v1/package/version/dependencies?domain=" + DOMAIN + "&repository=" + REPO + "&format=npm"
|
||||
+ "&package=" + packageName + "&version=1.0.0")
|
||||
.then().statusCode(200)
|
||||
.body("package", equalTo(packageName))
|
||||
.body("dependencies.size()", equalTo(4))
|
||||
.extract().jsonPath();
|
||||
List<String> packageNames = body.getList("dependencies.package", String.class);
|
||||
List<String> namespaces = body.getList("dependencies.namespace", String.class);
|
||||
assertTrue(packageNames.contains("node"));
|
||||
assertEquals("types", namespaces.get(packageNames.indexOf("node")));
|
||||
assertTrue(packageNames.contains("jest"));
|
||||
assertTrue(packageNames.contains("react"));
|
||||
assertTrue(packageNames.contains("fsevents"));
|
||||
}
|
||||
|
||||
/**
|
||||
* The real-world gap this closes: {@code ListPackageVersionAssets} used to not exist at all.
|
||||
* Confirms the bridge enumerates npm's one-asset-per-version packument entry against the real
|
||||
|
||||
+48
@@ -641,6 +641,54 @@ class CodeArtifactNuGetDockerIntegrationTest {
|
||||
* entry and reference it with a nuspec {@code <readme>} element, the real NuGet readme-file
|
||||
* feature.
|
||||
*/
|
||||
/**
|
||||
* A real {@code .nupkg} whose {@code .nuspec} declares one {@code <dependencies>} group, for
|
||||
* {@code ListPackageVersionDependencies} to parse back off the real BaGetter sidecar.
|
||||
*/
|
||||
private static byte[] buildNupkgWithDependency(String id, String version, String dependencyId,
|
||||
String dependencyVersion) {
|
||||
String nuspec = "<?xml version=\"1.0\"?>"
|
||||
+ "<package xmlns=\"http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd\">"
|
||||
+ "<metadata><id>" + id + "</id><version>" + version + "</version><authors>floci</authors>"
|
||||
+ "<description>a package with a real dependency</description>"
|
||||
+ "<dependencies><group targetFramework=\"net6.0\">"
|
||||
+ "<dependency id=\"" + dependencyId + "\" version=\"" + dependencyVersion + "\"/>"
|
||||
+ "</group></dependencies></metadata></package>";
|
||||
try {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
try (ZipOutputStream zip = new ZipOutputStream(out)) {
|
||||
zip.putNextEntry(new ZipEntry(id + ".nuspec"));
|
||||
zip.write(nuspec.getBytes(StandardCharsets.UTF_8));
|
||||
zip.closeEntry();
|
||||
}
|
||||
return out.toByteArray();
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(19)
|
||||
void listPackageVersionDependenciesReadsTheRealNuspecFromTheRealBagetterSidecar() {
|
||||
String packageId = "ListDeps.Package";
|
||||
byte[] nupkg = buildNupkgWithDependency(packageId, VERSION, "Newtonsoft.Json", "13.0.1");
|
||||
given().header("Authorization", "Bearer " + bearerToken)
|
||||
.contentType("multipart/form-data; boundary=boundary123")
|
||||
.body(pushBody("boundary123", nupkg))
|
||||
.put("/codeartifact/nuget/" + DOMAIN + "/" + REPO + "/api/v2/package")
|
||||
.then().statusCode(201);
|
||||
|
||||
given().header("Authorization", AUTH)
|
||||
.post("/v1/package/version/dependencies?domain=" + DOMAIN + "&repository=" + REPO + "&format=nuget"
|
||||
+ "&package=" + packageId + "&version=" + VERSION)
|
||||
.then().statusCode(200)
|
||||
.body("package", equalTo(packageId))
|
||||
.body("dependencies.size()", equalTo(1))
|
||||
.body("dependencies[0].package", equalTo("Newtonsoft.Json"))
|
||||
.body("dependencies[0].versionRequirement", equalTo("13.0.1"))
|
||||
.body("dependencies[0].containsKey('dependencyType')", equalTo(false));
|
||||
}
|
||||
|
||||
private static byte[] buildNupkg(String id, String version, String description, String readmePathOrNull,
|
||||
String readmeTextOrNull) {
|
||||
String readmeElement = readmePathOrNull == null ? "" : "<readme>" + readmePathOrNull + "</readme>";
|
||||
|
||||
+57
@@ -15,6 +15,7 @@ import org.junit.jupiter.api.Order;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestMethodOrder;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
@@ -27,6 +28,7 @@ import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.zip.GZIPOutputStream;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
@@ -922,6 +924,61 @@ class CodeArtifactPypiDockerIntegrationTest {
|
||||
return output.isBlank() ? 0 : (int) output.lines().count();
|
||||
}
|
||||
|
||||
/**
|
||||
* A real gzipped tarball holding one {@code <name>-<version>/PKG-INFO} entry with real
|
||||
* {@code Requires-Dist} headers, the exact shape {@code PypiVersionMetadata.extractSdistMetadata}
|
||||
* expects: classic 512-byte TAR header blocks, no long-name or PAX extensions.
|
||||
*/
|
||||
private static byte[] buildSdistWithRequiresDist(String packageDirectoryName, String metadataText)
|
||||
throws IOException {
|
||||
byte[] metadataBytes = metadataText.getBytes(StandardCharsets.UTF_8);
|
||||
byte[] header = new byte[512];
|
||||
byte[] nameBytes = (packageDirectoryName + "/PKG-INFO").getBytes(StandardCharsets.UTF_8);
|
||||
System.arraycopy(nameBytes, 0, header, 0, nameBytes.length);
|
||||
byte[] sizeOctal = String.format("%011o", metadataBytes.length).getBytes(StandardCharsets.US_ASCII);
|
||||
System.arraycopy(sizeOctal, 0, header, 124, sizeOctal.length);
|
||||
header[156] = '0';
|
||||
int padding = (512 - metadataBytes.length % 512) % 512;
|
||||
|
||||
ByteArrayOutputStream tar = new ByteArrayOutputStream();
|
||||
tar.write(header);
|
||||
tar.write(metadataBytes);
|
||||
tar.write(new byte[padding]);
|
||||
tar.write(new byte[1024]);
|
||||
|
||||
ByteArrayOutputStream gzipped = new ByteArrayOutputStream();
|
||||
try (GZIPOutputStream gzip = new GZIPOutputStream(gzipped)) {
|
||||
gzip.write(tar.toByteArray());
|
||||
}
|
||||
return gzipped.toByteArray();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(23)
|
||||
void listPackageVersionDependenciesParsesRequiresDistFromARealSdistAtTheRealPypiserverSidecar()
|
||||
throws IOException {
|
||||
String packageName = "listdeps-pypi-pkg";
|
||||
String filename = "listdeps_pypi_pkg-1.0.0.tar.gz";
|
||||
byte[] sdist = buildSdistWithRequiresDist("listdeps_pypi_pkg-1.0.0",
|
||||
"Metadata-Version: 2.1\nName: listdeps-pypi-pkg\nVersion: 1.0.0\n"
|
||||
+ "Requires-Dist: requests (>=2.0.0)\n");
|
||||
given().header("Authorization", "Bearer " + bearerToken)
|
||||
.contentType("multipart/form-data; boundary=boundary123")
|
||||
.body(twineUploadBody("boundary123", packageName, filename, sdist))
|
||||
.post("/codeartifact/pypi/" + DOMAIN + "/" + REPO + "/")
|
||||
.then().statusCode(200);
|
||||
|
||||
given().header("Authorization", AUTH)
|
||||
.post("/v1/package/version/dependencies?domain=" + DOMAIN + "&repository=" + REPO + "&format=pypi"
|
||||
+ "&package=" + packageName + "&version=1.0.0")
|
||||
.then().statusCode(200)
|
||||
.body("package", equalTo(packageName))
|
||||
.body("dependencies.size()", equalTo(1))
|
||||
.body("dependencies[0].package", equalTo("requests"))
|
||||
.body("dependencies[0].dependencyType", equalTo("Requires-Dist"))
|
||||
.body("dependencies[0].versionRequirement", equalTo("(>=2.0.0)"));
|
||||
}
|
||||
|
||||
/**
|
||||
* The fields a real {@code twine upload} sends (verified by hand against a live pypiserver
|
||||
* instance during design); pypiserver, and this controller's own parsing, only look at
|
||||
|
||||
@@ -5328,6 +5328,141 @@ class CodeArtifactServiceTest {
|
||||
assertEquals("ValidationException", missingVersion.getErrorCode());
|
||||
}
|
||||
|
||||
// ----------------------------------------------------- ListPackageVersionDependencies
|
||||
|
||||
@Test
|
||||
void listPackageVersionDependenciesBridgesToReposiliteForMaven() {
|
||||
service.createDomain(REGION, "dom", null, Map.of());
|
||||
CodeArtifactRepository created = service.createRepository(REGION, "dom", null, "repo", null, null, Map.of());
|
||||
String repoId = created.getSidecarContainerIds().get("maven");
|
||||
when(reposiliteClient.packageVersionExists(eq(repoId), eq("dom"), eq("repo"), eq("com.example"),
|
||||
eq("a"), eq("1.0"))).thenReturn(true);
|
||||
when(reposiliteClient.listPackageVersionDependencies(eq(repoId), eq("dom"), eq("repo"), eq("com.example"),
|
||||
eq("a"), eq("1.0"))).thenReturn(
|
||||
List.of(new CodeArtifactService.PackageDependency("com.example", "b", "compile", "2.0.0")));
|
||||
|
||||
CodeArtifactService.PackageVersionDependenciesListResult result = service.listPackageVersionDependencies(
|
||||
REGION, "dom", null, "repo", "maven", "com.example", "a", "1.0", null);
|
||||
|
||||
assertEquals(1, result.dependencies().size());
|
||||
assertEquals("b", result.dependencies().get(0).packageName());
|
||||
assertEquals("compile", result.dependencies().get(0).dependencyType());
|
||||
}
|
||||
|
||||
@Test
|
||||
void listPackageVersionDependenciesFailsNotFoundForAMavenVersionThatWasNeverPublished() {
|
||||
service.createDomain(REGION, "dom", null, Map.of());
|
||||
service.createRepository(REGION, "dom", null, "repo", null, null, Map.of());
|
||||
|
||||
AwsException e = assertThrows(AwsException.class, () -> service.listPackageVersionDependencies(REGION, "dom",
|
||||
null, "repo", "maven", "com.example", "a", "1.0", null));
|
||||
assertEquals("ResourceNotFoundException", e.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void listPackageVersionDependenciesRoutesEveryValidFormatToTheSidecarBridge() {
|
||||
service.createDomain(REGION, "dom", null, Map.of());
|
||||
service.createRepository(REGION, "dom", null, "repo", null, null, Map.of());
|
||||
|
||||
for (String format : List.of("npm", "pypi", "nuget")) {
|
||||
AwsException e = assertThrows(AwsException.class, () -> service.listPackageVersionDependencies(REGION,
|
||||
"dom", null, "repo", format, null, "my-pkg", "1.0.0", null));
|
||||
assertEquals("ResourceNotFoundException", e.getErrorCode());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void listPackageVersionDependenciesRejectsAFormatAwsDoesNotDocumentForThisAction() {
|
||||
service.createDomain(REGION, "dom", null, Map.of());
|
||||
service.createRepository(REGION, "dom", null, "repo", null, null, Map.of());
|
||||
|
||||
for (String format : List.of("generic", "cargo", "ruby", "swift", "not-a-real-format")) {
|
||||
AwsException e = assertThrows(AwsException.class, () -> service.listPackageVersionDependencies(REGION,
|
||||
"dom", null, "repo", format, null, "my-pkg", "1.0.0", null));
|
||||
assertEquals("ValidationException", e.getErrorCode());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void listPackageVersionDependenciesRequiresANamespaceForMaven() {
|
||||
service.createDomain(REGION, "dom", null, Map.of());
|
||||
service.createRepository(REGION, "dom", null, "repo", null, null, Map.of());
|
||||
|
||||
AwsException e = assertThrows(AwsException.class, () -> service.listPackageVersionDependencies(REGION,
|
||||
"dom", null, "repo", "maven", null, "a", "1.0", null));
|
||||
assertEquals("ValidationException", e.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void listPackageVersionDependenciesRejectsANamespaceForPypiAndNuGet() {
|
||||
service.createDomain(REGION, "dom", null, Map.of());
|
||||
service.createRepository(REGION, "dom", null, "repo", null, null, Map.of());
|
||||
|
||||
for (String format : List.of("pypi", "nuget")) {
|
||||
AwsException e = assertThrows(AwsException.class, () -> service.listPackageVersionDependencies(REGION,
|
||||
"dom", null, "repo", format, "a-namespace", "my-pkg", "1.0.0", null));
|
||||
assertEquals("ValidationException", e.getErrorCode());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void listPackageVersionDependenciesPaginatesWithANextToken() {
|
||||
service.createDomain(REGION, "dom", null, Map.of());
|
||||
CodeArtifactRepository created = service.createRepository(REGION, "dom", null, "repo", null, null, Map.of());
|
||||
String repoId = created.getSidecarContainerIds().get("npm");
|
||||
when(verdaccioClient.packageVersionExists(eq(repoId), eq("dom"), eq("repo"), isNull(), eq("my-pkg"),
|
||||
eq("1.0.0"))).thenReturn(true);
|
||||
List<CodeArtifactService.PackageDependency> many = new ArrayList<>();
|
||||
for (int i = 0; i < 150; i++) {
|
||||
many.add(new CodeArtifactService.PackageDependency(null, "dep-" + String.format("%03d", i), "regular",
|
||||
"1.0.0"));
|
||||
}
|
||||
when(verdaccioClient.listPackageVersionDependencies(eq(repoId), eq("dom"), eq("repo"), isNull(),
|
||||
eq("my-pkg"), eq("1.0.0"))).thenReturn(many);
|
||||
|
||||
CodeArtifactService.PackageVersionDependenciesListResult firstPage = service.listPackageVersionDependencies(
|
||||
REGION, "dom", null, "repo", "npm", null, "my-pkg", "1.0.0", null);
|
||||
|
||||
assertEquals(100, firstPage.dependencies().size());
|
||||
assertTrue(firstPage.nextToken() != null);
|
||||
|
||||
CodeArtifactService.PackageVersionDependenciesListResult secondPage = service.listPackageVersionDependencies(
|
||||
REGION, "dom", null, "repo", "npm", null, "my-pkg", "1.0.0", firstPage.nextToken());
|
||||
|
||||
assertEquals(50, secondPage.dependencies().size());
|
||||
}
|
||||
|
||||
/**
|
||||
* Qodo review finding: byte-for-byte identical dependency entries (a real shape for NuGet,
|
||||
* where the same package id can be declared under more than one target-framework group with
|
||||
* the same version requirement) must not share a pagination cursor - if a 100-item page ends
|
||||
* between two of them, resuming "strictly after" a shared cursor would silently drop whichever
|
||||
* one(s) land past the boundary.
|
||||
*/
|
||||
@Test
|
||||
void listPackageVersionDependenciesPaginationNeverDropsIdenticalEntriesAcrossAPageBoundary() {
|
||||
service.createDomain(REGION, "dom", null, Map.of());
|
||||
CodeArtifactRepository created = service.createRepository(REGION, "dom", null, "repo", null, null, Map.of());
|
||||
String repoId = created.getSidecarContainerIds().get("nuget");
|
||||
when(bagetterClient.packageVersionExists(eq(repoId), eq("dom"), eq("repo"), isNull(), eq("my-pkg"),
|
||||
eq("1.0.0"))).thenReturn(true);
|
||||
List<CodeArtifactService.PackageDependency> many = new ArrayList<>();
|
||||
for (int i = 0; i < 150; i++) {
|
||||
// Every entry is byte-for-byte identical: the real worst case for a cursor collision.
|
||||
many.add(new CodeArtifactService.PackageDependency(null, "Shared.Package", null, "1.0.0"));
|
||||
}
|
||||
when(bagetterClient.listPackageVersionDependencies(eq(repoId), eq("dom"), eq("repo"), isNull(),
|
||||
eq("my-pkg"), eq("1.0.0"))).thenReturn(many);
|
||||
|
||||
CodeArtifactService.PackageVersionDependenciesListResult firstPage = service.listPackageVersionDependencies(
|
||||
REGION, "dom", null, "repo", "nuget", null, "my-pkg", "1.0.0", null);
|
||||
CodeArtifactService.PackageVersionDependenciesListResult secondPage = service.listPackageVersionDependencies(
|
||||
REGION, "dom", null, "repo", "nuget", null, "my-pkg", "1.0.0", firstPage.nextToken());
|
||||
|
||||
assertEquals(100, firstPage.dependencies().size());
|
||||
assertEquals(50, secondPage.dependencies().size());
|
||||
}
|
||||
|
||||
// ----------------------------------------------------- ListPackageVersions
|
||||
|
||||
@Test
|
||||
|
||||
@@ -7,6 +7,7 @@ import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
@@ -108,6 +109,41 @@ class NuspecXmlTest {
|
||||
assertThrows(IllegalStateException.class, () -> NuspecXml.extractFromNupkg(nupkg));
|
||||
}
|
||||
|
||||
@Test
|
||||
void dependenciesFlattensEveryTargetFrameworkGroupAndAllowsTheSamePackageTwice() {
|
||||
String nuspec = "<?xml version=\"1.0\"?><package><metadata><id>Demo</id><version>1.0.0</version>"
|
||||
+ "<dependencies>"
|
||||
+ "<group targetFramework=\"net6.0\"><dependency id=\"Newtonsoft.Json\" version=\"13.0.1\"/></group>"
|
||||
+ "<group targetFramework=\"netstandard2.0\"><dependency id=\"Newtonsoft.Json\" version=\"12.0.3\"/>"
|
||||
+ "<dependency id=\"NoVersion\"/></group>"
|
||||
+ "</dependencies></metadata></package>";
|
||||
|
||||
List<NuspecXml.RawDependency> dependencies = NuspecXml.dependencies(nuspec.getBytes(StandardCharsets.UTF_8));
|
||||
|
||||
assertEquals(3, dependencies.size());
|
||||
assertEquals(new NuspecXml.RawDependency("Newtonsoft.Json", "13.0.1"), dependencies.get(0));
|
||||
assertEquals(new NuspecXml.RawDependency("Newtonsoft.Json", "12.0.3"), dependencies.get(1));
|
||||
assertEquals(new NuspecXml.RawDependency("NoVersion", null), dependencies.get(2));
|
||||
}
|
||||
|
||||
@Test
|
||||
void dependenciesHandlesTheLegacySchemaWithNoGroupWrapper() {
|
||||
String nuspec = "<?xml version=\"1.0\"?><package><metadata><id>Demo</id><version>1.0.0</version>"
|
||||
+ "<dependencies><dependency id=\"Old.Style\" version=\"1.2.3\"/></dependencies>"
|
||||
+ "</metadata></package>";
|
||||
|
||||
List<NuspecXml.RawDependency> dependencies = NuspecXml.dependencies(nuspec.getBytes(StandardCharsets.UTF_8));
|
||||
|
||||
assertEquals(List.of(new NuspecXml.RawDependency("Old.Style", "1.2.3")), dependencies);
|
||||
}
|
||||
|
||||
@Test
|
||||
void dependenciesIsEmptyWhenTheNuspecDeclaresNone() {
|
||||
List<NuspecXml.RawDependency> dependencies = NuspecXml.dependencies(NUSPEC.getBytes(StandardCharsets.UTF_8));
|
||||
|
||||
assertTrue(dependencies.isEmpty());
|
||||
}
|
||||
|
||||
private static byte[] buildNupkg(String rootNuspecOrNull) {
|
||||
try {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
package io.floci.aws.services.codeartifact;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class PomDependenciesTest {
|
||||
|
||||
@Test
|
||||
void parseReadsTheParentAndEveryDirectDependencyWithTheirOwnDependencyType() {
|
||||
String pom = "<project>"
|
||||
+ "<parent><groupId>com.example</groupId><artifactId>parent-pom</artifactId><version>1.0.0</version>"
|
||||
+ "</parent>"
|
||||
+ "<dependencies>"
|
||||
+ "<dependency><groupId>com.example</groupId><artifactId>no-scope</artifactId>"
|
||||
+ "<version>1.2.3</version></dependency>"
|
||||
+ "<dependency><groupId>com.example</groupId><artifactId>test-scoped</artifactId>"
|
||||
+ "<version>2.0.0</version><scope>test</scope></dependency>"
|
||||
+ "<dependency><groupId>com.example</groupId><artifactId>optional-and-scoped</artifactId>"
|
||||
+ "<version>3.0.0</version><scope>provided</scope><optional>true</optional></dependency>"
|
||||
+ "</dependencies>"
|
||||
+ "</project>";
|
||||
|
||||
List<CodeArtifactService.PackageDependency> dependencies =
|
||||
PomDependencies.parse(pom.getBytes(StandardCharsets.UTF_8));
|
||||
|
||||
assertEquals(4, dependencies.size());
|
||||
assertEquals(new CodeArtifactService.PackageDependency("com.example", "parent-pom", "parent", "1.0.0"),
|
||||
dependencies.get(0));
|
||||
assertEquals(new CodeArtifactService.PackageDependency("com.example", "no-scope", "compile", "1.2.3"),
|
||||
dependencies.get(1));
|
||||
assertEquals(new CodeArtifactService.PackageDependency("com.example", "test-scoped", "test", "2.0.0"),
|
||||
dependencies.get(2));
|
||||
assertEquals(new CodeArtifactService.PackageDependency("com.example", "optional-and-scoped", "optional",
|
||||
"3.0.0"), dependencies.get(3));
|
||||
}
|
||||
|
||||
@Test
|
||||
void parseIgnoresDependencyManagementAndProfileDependencies() {
|
||||
String pom = "<project>"
|
||||
+ "<dependencyManagement><dependencies>"
|
||||
+ "<dependency><groupId>com.example</groupId><artifactId>managed-only</artifactId>"
|
||||
+ "<version>9.9.9</version></dependency>"
|
||||
+ "</dependencies></dependencyManagement>"
|
||||
+ "<profiles><profile><id>extra</id><dependencies>"
|
||||
+ "<dependency><groupId>com.example</groupId><artifactId>profile-only</artifactId>"
|
||||
+ "<version>8.8.8</version></dependency>"
|
||||
+ "</dependencies></profile></profiles>"
|
||||
+ "<dependencies>"
|
||||
+ "<dependency><groupId>com.example</groupId><artifactId>real-dependency</artifactId>"
|
||||
+ "<version>1.0.0</version></dependency>"
|
||||
+ "</dependencies>"
|
||||
+ "</project>";
|
||||
|
||||
List<CodeArtifactService.PackageDependency> dependencies =
|
||||
PomDependencies.parse(pom.getBytes(StandardCharsets.UTF_8));
|
||||
|
||||
assertEquals(List.of(new CodeArtifactService.PackageDependency("com.example", "real-dependency", "compile",
|
||||
"1.0.0")), dependencies);
|
||||
}
|
||||
|
||||
@Test
|
||||
void parseReturnsNoRawPropertyResolution() {
|
||||
String pom = "<project><dependencies>"
|
||||
+ "<dependency><groupId>com.example</groupId><artifactId>versioned-by-property</artifactId>"
|
||||
+ "<version>${some.property}</version></dependency>"
|
||||
+ "</dependencies></project>";
|
||||
|
||||
List<CodeArtifactService.PackageDependency> dependencies =
|
||||
PomDependencies.parse(pom.getBytes(StandardCharsets.UTF_8));
|
||||
|
||||
assertEquals("${some.property}", dependencies.get(0).versionRequirement());
|
||||
}
|
||||
|
||||
@Test
|
||||
void parseReturnsNoDependenciesForAPomWithNeitherAParentNorADependenciesBlock() {
|
||||
String pom = "<project><groupId>com.example</groupId><artifactId>leaf</artifactId><version>1.0.0</version>"
|
||||
+ "</project>";
|
||||
|
||||
List<CodeArtifactService.PackageDependency> dependencies =
|
||||
PomDependencies.parse(pom.getBytes(StandardCharsets.UTF_8));
|
||||
|
||||
assertTrue(dependencies.isEmpty());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,198 @@
|
||||
package io.floci.aws.services.codeartifact;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.zip.GZIPOutputStream;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class PypiVersionMetadataTest {
|
||||
|
||||
private static final String METADATA_TEXT = "Metadata-Version: 2.1\n"
|
||||
+ "Name: demo-package\n"
|
||||
+ "Version: 1.0.0\n"
|
||||
+ "Requires-Dist: requests (>=2.0.0)\n"
|
||||
+ "Requires-Dist: a-very-long-dependency-name-that-wraps\n"
|
||||
+ " (>=1.0,<2.0)\n"
|
||||
+ "Requires-Dist: colorama; sys_platform == \"win32\"\n"
|
||||
+ "Requires-Dist: bare-name\n";
|
||||
|
||||
@Test
|
||||
void extractWheelMetadataFindsTheDistInfoMetadataEntry() throws IOException {
|
||||
byte[] wheel = buildZip("demo_package-1.0.0.dist-info/METADATA", METADATA_TEXT);
|
||||
|
||||
Optional<byte[]> metadata = PypiVersionMetadata.extractWheelMetadata(wheel);
|
||||
|
||||
assertTrue(metadata.isPresent());
|
||||
assertEquals(METADATA_TEXT, new String(metadata.get(), StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
@Test
|
||||
void extractWheelMetadataIsEmptyWhenNoDistInfoMetadataEntryExists() throws IOException {
|
||||
byte[] wheel = buildZip("demo_package/__init__.py", "# not metadata");
|
||||
|
||||
assertTrue(PypiVersionMetadata.extractWheelMetadata(wheel).isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void extractSdistMetadataFindsThePkgInfoEntry() throws IOException {
|
||||
byte[] sdist = buildTarGz("demo_package-1.0.0/PKG-INFO", METADATA_TEXT);
|
||||
|
||||
Optional<byte[]> metadata = PypiVersionMetadata.extractSdistMetadata(sdist);
|
||||
|
||||
assertTrue(metadata.isPresent());
|
||||
assertEquals(METADATA_TEXT, new String(metadata.get(), StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
@Test
|
||||
void extractSdistMetadataIsEmptyWhenNoPkgInfoEntryExists() throws IOException {
|
||||
byte[] sdist = buildTarGz("demo_package-1.0.0/setup.py", "print('hi')");
|
||||
|
||||
assertTrue(PypiVersionMetadata.extractSdistMetadata(sdist).isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void parseRequiresDistFoldsContinuationsAndKeepsEnvironmentMarkersVerbatim() {
|
||||
List<CodeArtifactService.PackageDependency> dependencies =
|
||||
PypiVersionMetadata.parseRequiresDist(METADATA_TEXT.getBytes(StandardCharsets.UTF_8));
|
||||
|
||||
assertEquals(4, dependencies.size());
|
||||
assertEquals(new CodeArtifactService.PackageDependency(null, "requests", "Requires-Dist", "(>=2.0.0)"),
|
||||
dependencies.get(0));
|
||||
assertEquals(new CodeArtifactService.PackageDependency(null, "a-very-long-dependency-name-that-wraps",
|
||||
"Requires-Dist", "(>=1.0,<2.0)"), dependencies.get(1));
|
||||
assertEquals(new CodeArtifactService.PackageDependency(null, "colorama", "Requires-Dist",
|
||||
"; sys_platform == \"win32\""), dependencies.get(2));
|
||||
assertEquals(new CodeArtifactService.PackageDependency(null, "bare-name", "Requires-Dist", null),
|
||||
dependencies.get(3));
|
||||
}
|
||||
|
||||
/**
|
||||
* Qodo review finding: a wheel that vendors another package's own dist-info alongside its own
|
||||
* (a real setuptools pattern) must not have the vendored, nested entry mistaken for the
|
||||
* wheel's own top-level one, even when it appears first in the archive.
|
||||
*/
|
||||
@Test
|
||||
void extractWheelMetadataIgnoresAVendoredNestedDistInfoEntry() throws IOException {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
try (ZipOutputStream zip = new ZipOutputStream(out)) {
|
||||
zip.putNextEntry(new ZipEntry("demo_package/_vendor/other-9.0.dist-info/METADATA"));
|
||||
zip.write("Name: other\nRequires-Dist: not-the-real-one\n".getBytes(StandardCharsets.UTF_8));
|
||||
zip.closeEntry();
|
||||
zip.putNextEntry(new ZipEntry("demo_package-1.0.0.dist-info/METADATA"));
|
||||
zip.write(METADATA_TEXT.getBytes(StandardCharsets.UTF_8));
|
||||
zip.closeEntry();
|
||||
}
|
||||
|
||||
Optional<byte[]> metadata = PypiVersionMetadata.extractWheelMetadata(out.toByteArray());
|
||||
|
||||
assertTrue(metadata.isPresent());
|
||||
assertEquals(METADATA_TEXT, new String(metadata.get(), StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
@Test
|
||||
void extractZipSdistMetadataFindsTheTopLevelPkgInfoEntryAndIgnoresANestedOne() throws IOException {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
try (ZipOutputStream zip = new ZipOutputStream(out)) {
|
||||
zip.putNextEntry(new ZipEntry("demo_package-1.0.0/vendored/other/PKG-INFO"));
|
||||
zip.write("Requires-Dist: not-the-real-one\n".getBytes(StandardCharsets.UTF_8));
|
||||
zip.closeEntry();
|
||||
zip.putNextEntry(new ZipEntry("demo_package-1.0.0/PKG-INFO"));
|
||||
zip.write(METADATA_TEXT.getBytes(StandardCharsets.UTF_8));
|
||||
zip.closeEntry();
|
||||
}
|
||||
|
||||
Optional<byte[]> metadata = PypiVersionMetadata.extractZipSdistMetadata(out.toByteArray());
|
||||
|
||||
assertTrue(metadata.isPresent());
|
||||
assertEquals(METADATA_TEXT, new String(metadata.get(), StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
/**
|
||||
* Qodo review finding: a small, highly compressible entry ahead of the real metadata entry
|
||||
* must not let this reader decompress an effectively unbounded amount of data looking for it.
|
||||
* 80 MiB of zeros compresses to only a few hundred bytes, so this stays a fast unit test while
|
||||
* still exceeding the reader's own 64 MiB total-scan budget.
|
||||
*/
|
||||
@Test
|
||||
void extractWheelMetadataAbortsRatherThanDecompressingAnUnboundedPrecedingEntry() throws IOException {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
try (ZipOutputStream zip = new ZipOutputStream(out)) {
|
||||
zip.putNextEntry(new ZipEntry("huge-padding.bin"));
|
||||
zip.write(new byte[80 * 1024 * 1024]);
|
||||
zip.closeEntry();
|
||||
zip.putNextEntry(new ZipEntry("demo_package-1.0.0.dist-info/METADATA"));
|
||||
zip.write(METADATA_TEXT.getBytes(StandardCharsets.UTF_8));
|
||||
zip.closeEntry();
|
||||
}
|
||||
byte[] wheel = out.toByteArray();
|
||||
|
||||
assertThrows(IllegalStateException.class, () -> PypiVersionMetadata.extractWheelMetadata(wheel));
|
||||
}
|
||||
|
||||
/**
|
||||
* Qodo review finding: a `Requires-Dist:`-looking line inside the long description (the
|
||||
* message body, after the RFC822 header/body blank-line boundary) must not be reported as a
|
||||
* real dependency. {@code Requires-Dist} is literally used as an example string in PyPA's own
|
||||
* packaging guide, so this is a real, not contrived, risk.
|
||||
*/
|
||||
@Test
|
||||
void parseRequiresDistIgnoresAHeaderLookingLineInsideTheDescriptionBody() {
|
||||
String metadata = "Metadata-Version: 2.1\n"
|
||||
+ "Name: demo-package\n"
|
||||
+ "Requires-Dist: requests\n"
|
||||
+ "\n"
|
||||
+ "A long description that happens to mention Requires-Dist: fake-package as an example.\n";
|
||||
|
||||
List<CodeArtifactService.PackageDependency> dependencies =
|
||||
PypiVersionMetadata.parseRequiresDist(metadata.getBytes(StandardCharsets.UTF_8));
|
||||
|
||||
assertEquals(List.of(new CodeArtifactService.PackageDependency(null, "requests", "Requires-Dist", null)),
|
||||
dependencies);
|
||||
}
|
||||
|
||||
private static byte[] buildZip(String entryName, String content) throws IOException {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
try (ZipOutputStream zip = new ZipOutputStream(out)) {
|
||||
zip.putNextEntry(new ZipEntry(entryName));
|
||||
zip.write(content.getBytes(StandardCharsets.UTF_8));
|
||||
zip.closeEntry();
|
||||
}
|
||||
return out.toByteArray();
|
||||
}
|
||||
|
||||
private static byte[] buildTarGz(String entryName, String content) throws IOException {
|
||||
byte[] contentBytes = content.getBytes(StandardCharsets.UTF_8);
|
||||
ByteArrayOutputStream tar = new ByteArrayOutputStream();
|
||||
tar.write(tarHeader(entryName, contentBytes.length));
|
||||
tar.write(contentBytes);
|
||||
int padding = (512 - contentBytes.length % 512) % 512;
|
||||
tar.write(new byte[padding]);
|
||||
tar.write(new byte[1024]);
|
||||
|
||||
ByteArrayOutputStream gzipped = new ByteArrayOutputStream();
|
||||
try (GZIPOutputStream gzip = new GZIPOutputStream(gzipped)) {
|
||||
gzip.write(tar.toByteArray());
|
||||
}
|
||||
return gzipped.toByteArray();
|
||||
}
|
||||
|
||||
private static byte[] tarHeader(String name, int size) {
|
||||
byte[] header = new byte[512];
|
||||
byte[] nameBytes = name.getBytes(StandardCharsets.UTF_8);
|
||||
System.arraycopy(nameBytes, 0, header, 0, nameBytes.length);
|
||||
byte[] sizeOctal = String.format("%011o", size).getBytes(StandardCharsets.US_ASCII);
|
||||
System.arraycopy(sizeOctal, 0, header, 124, sizeOctal.length);
|
||||
header[156] = '0';
|
||||
return header;
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,7 @@ import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.InputStream;
|
||||
import java.lang.reflect.Field;
|
||||
import java.net.InetSocketAddress;
|
||||
@@ -23,6 +24,9 @@ import java.util.Optional;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.zip.GZIPOutputStream;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
@@ -531,6 +535,121 @@ class PypiserverSidecarClientTest {
|
||||
() -> client(manager).deletePackage("pypi-repo-1", "dom", "repo", null, "demo-pkg"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void listPackageVersionDependenciesParsesRequiresDistFromARealWheel() throws Exception {
|
||||
PypiserverSidecarManager manager = manager();
|
||||
seedPooledContainer(manager, "pypi-repo-1", "tracked-pypiserver-1", backendUrl);
|
||||
String filename = "demo_pkg-1.0.0-py3-none-any.whl";
|
||||
respondWithJsonReleases("demo-pkg", Map.of("1.0.0", List.of(filename)));
|
||||
respondWithSimpleIndex("demo-pkg", filename);
|
||||
byte[] wheel = buildWheel("demo_pkg-1.0.0.dist-info/METADATA",
|
||||
"Metadata-Version: 2.1\nName: demo-pkg\nRequires-Dist: requests (>=2.0.0)\n");
|
||||
backend.createContext("/packages/" + filename, exchange -> {
|
||||
exchange.sendResponseHeaders(200, wheel.length);
|
||||
exchange.getResponseBody().write(wheel);
|
||||
exchange.close();
|
||||
});
|
||||
|
||||
List<CodeArtifactService.PackageDependency> dependencies = client(manager).listPackageVersionDependencies(
|
||||
"pypi-repo-1", "dom", "repo", null, "demo-pkg", "1.0.0");
|
||||
|
||||
assertEquals(List.of(new CodeArtifactService.PackageDependency(null, "requests", "Requires-Dist",
|
||||
"(>=2.0.0)")), dependencies);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@code .tar.bz2}/{@code .tar.xz} are real, {@code ARCHIVE_SUFFIX}-recognized sdist formats
|
||||
* this reader deliberately never attempts to decompress (bzip2/xz would need a new runtime
|
||||
* dependency) — a version available only in one of those must come back empty, not throw.
|
||||
*/
|
||||
@Test
|
||||
void listPackageVersionDependenciesIsEmptyWhenTheVersionHasNoWheelOrSupportedSdist() throws Exception {
|
||||
PypiserverSidecarManager manager = manager();
|
||||
seedPooledContainer(manager, "pypi-repo-1", "tracked-pypiserver-1", backendUrl);
|
||||
respondWithJsonReleases("demo-pkg", Map.of("1.0.0", List.of("demo_pkg-1.0.0.tar.bz2")));
|
||||
|
||||
List<CodeArtifactService.PackageDependency> dependencies = client(manager).listPackageVersionDependencies(
|
||||
"pypi-repo-1", "dom", "repo", null, "demo-pkg", "1.0.0");
|
||||
|
||||
assertTrue(dependencies.isEmpty());
|
||||
}
|
||||
|
||||
/** {@code .tgz} is the same real gzip+tar format as {@code .tar.gz}, just a shorter suffix. */
|
||||
@Test
|
||||
void listPackageVersionDependenciesParsesRequiresDistFromARealTgzSdist() throws Exception {
|
||||
PypiserverSidecarManager manager = manager();
|
||||
seedPooledContainer(manager, "pypi-repo-1", "tracked-pypiserver-1", backendUrl);
|
||||
String filename = "demo_pkg-1.0.0.tgz";
|
||||
respondWithJsonReleases("demo-pkg", Map.of("1.0.0", List.of(filename)));
|
||||
respondWithSimpleIndex("demo-pkg", filename);
|
||||
byte[] sdist = buildTarGzSdist("demo_pkg-1.0.0/PKG-INFO",
|
||||
"Metadata-Version: 2.1\nName: demo-pkg\nRequires-Dist: requests (>=2.0.0)\n");
|
||||
backend.createContext("/packages/" + filename, exchange -> {
|
||||
exchange.sendResponseHeaders(200, sdist.length);
|
||||
exchange.getResponseBody().write(sdist);
|
||||
exchange.close();
|
||||
});
|
||||
|
||||
List<CodeArtifactService.PackageDependency> dependencies = client(manager).listPackageVersionDependencies(
|
||||
"pypi-repo-1", "dom", "repo", null, "demo-pkg", "1.0.0");
|
||||
|
||||
assertEquals(List.of(new CodeArtifactService.PackageDependency(null, "requests", "Requires-Dist",
|
||||
"(>=2.0.0)")), dependencies);
|
||||
}
|
||||
|
||||
/** {@code .zip} is a real, pypiserver-recognized sdist format alongside {@code .tar.gz}. */
|
||||
@Test
|
||||
void listPackageVersionDependenciesParsesRequiresDistFromARealZipSdist() throws Exception {
|
||||
PypiserverSidecarManager manager = manager();
|
||||
seedPooledContainer(manager, "pypi-repo-1", "tracked-pypiserver-1", backendUrl);
|
||||
String filename = "demo_pkg-1.0.0.zip";
|
||||
respondWithJsonReleases("demo-pkg", Map.of("1.0.0", List.of(filename)));
|
||||
respondWithSimpleIndex("demo-pkg", filename);
|
||||
byte[] sdist = buildWheel("demo_pkg-1.0.0/PKG-INFO",
|
||||
"Metadata-Version: 2.1\nName: demo-pkg\nRequires-Dist: requests (>=2.0.0)\n");
|
||||
backend.createContext("/packages/" + filename, exchange -> {
|
||||
exchange.sendResponseHeaders(200, sdist.length);
|
||||
exchange.getResponseBody().write(sdist);
|
||||
exchange.close();
|
||||
});
|
||||
|
||||
List<CodeArtifactService.PackageDependency> dependencies = client(manager).listPackageVersionDependencies(
|
||||
"pypi-repo-1", "dom", "repo", null, "demo-pkg", "1.0.0");
|
||||
|
||||
assertEquals(List.of(new CodeArtifactService.PackageDependency(null, "requests", "Requires-Dist",
|
||||
"(>=2.0.0)")), dependencies);
|
||||
}
|
||||
|
||||
private static byte[] buildTarGzSdist(String entryName, String content) throws Exception {
|
||||
byte[] contentBytes = content.getBytes(StandardCharsets.UTF_8);
|
||||
byte[] header = new byte[512];
|
||||
byte[] nameBytes = entryName.getBytes(StandardCharsets.UTF_8);
|
||||
System.arraycopy(nameBytes, 0, header, 0, nameBytes.length);
|
||||
byte[] sizeOctal = String.format("%011o", contentBytes.length).getBytes(StandardCharsets.US_ASCII);
|
||||
System.arraycopy(sizeOctal, 0, header, 124, sizeOctal.length);
|
||||
header[156] = '0';
|
||||
ByteArrayOutputStream tar = new ByteArrayOutputStream();
|
||||
tar.write(header);
|
||||
tar.write(contentBytes);
|
||||
tar.write(new byte[(512 - contentBytes.length % 512) % 512]);
|
||||
tar.write(new byte[1024]);
|
||||
ByteArrayOutputStream gzipped = new ByteArrayOutputStream();
|
||||
try (GZIPOutputStream gzip = new GZIPOutputStream(gzipped)) {
|
||||
gzip.write(tar.toByteArray());
|
||||
}
|
||||
return gzipped.toByteArray();
|
||||
}
|
||||
|
||||
private static byte[] buildWheel(String entryName, String metadataText) throws Exception {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
try (ZipOutputStream zip = new ZipOutputStream(out)) {
|
||||
zip.putNextEntry(new ZipEntry(entryName));
|
||||
zip.write(metadataText.getBytes(StandardCharsets.UTF_8));
|
||||
zip.closeEntry();
|
||||
}
|
||||
return out.toByteArray();
|
||||
}
|
||||
|
||||
private void respondWithJsonReleases(String packageName, Map<String, List<String>> releases) {
|
||||
StringBuilder json = new StringBuilder("{\"info\":{\"version\":\"\"},\"releases\":{");
|
||||
boolean firstVersion = true;
|
||||
|
||||
@@ -739,6 +739,85 @@ class ReposiliteSidecarClientTest {
|
||||
"com.example", "weird"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void listPackageVersionDependenciesParsesTheFetchedPomsParentAndDependencies() {
|
||||
settingsBody.set("{\"repositories\":[{\"id\":\"dom--repo\",\"visibility\":\"PUBLIC\",\"redeployment\":false}]}");
|
||||
server.createContext("/api/settings/domain/maven", exchange -> respond(exchange, 200, settingsBody.get()));
|
||||
String pom = "<project><parent><groupId>com.example</groupId><artifactId>parent-pom</artifactId>"
|
||||
+ "<version>1.0.0</version></parent><dependencies>"
|
||||
+ "<dependency><groupId>com.example</groupId><artifactId>other</artifactId>"
|
||||
+ "<version>2.0.0</version><scope>test</scope></dependency>"
|
||||
+ "</dependencies></project>";
|
||||
server.createContext("/dom--repo/com/example/a/1.0/a-1.0.pom", exchange -> respond(exchange, 200, pom));
|
||||
|
||||
List<CodeArtifactService.PackageDependency> dependencies = client.listPackageVersionDependencies("dom--repo",
|
||||
"dom", "repo", "com.example", "a", "1.0");
|
||||
|
||||
assertThat(dependencies, containsInAnyOrder(
|
||||
new CodeArtifactService.PackageDependency("com.example", "parent-pom", "parent", "1.0.0"),
|
||||
new CodeArtifactService.PackageDependency("com.example", "other", "test", "2.0.0")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void listPackageVersionDependenciesIsEmptyWhenTheVersionHasNoPom() {
|
||||
settingsBody.set("{\"repositories\":[{\"id\":\"dom--repo\",\"visibility\":\"PUBLIC\",\"redeployment\":false}]}");
|
||||
server.createContext("/api/settings/domain/maven", exchange -> respond(exchange, 200, settingsBody.get()));
|
||||
server.createContext("/dom--repo/com/example/a/1.0/a-1.0.pom", exchange -> respond(exchange, 404, ""));
|
||||
server.createContext("/api/maven/details/dom--repo/com/example/a/1.0",
|
||||
exchange -> respond(exchange, 200, "{\"name\":\"1.0\",\"type\":\"DIRECTORY\",\"files\":["
|
||||
+ "{\"name\":\"a-1.0.jar\",\"type\":\"FILE\"}]}"));
|
||||
|
||||
assertThat(client.listPackageVersionDependencies("dom--repo", "dom", "repo", "com.example", "a", "1.0"),
|
||||
equalTo(List.of()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Qodo review finding: a {@code -SNAPSHOT} version's real POM is not named
|
||||
* {@code <artifactId>-<version>.pom} at all - a real {@code mvn deploy} writes it under a
|
||||
* timestamped unique-snapshot name instead - so the literal-name fetch misses and the fallback
|
||||
* to the real directory listing must find and fetch the real file.
|
||||
*/
|
||||
@Test
|
||||
void listPackageVersionDependenciesResolvesASnapshotsRealTimestampedPom() {
|
||||
settingsBody.set("{\"repositories\":[{\"id\":\"dom--repo\",\"visibility\":\"PUBLIC\",\"redeployment\":false}]}");
|
||||
server.createContext("/api/settings/domain/maven", exchange -> respond(exchange, 200, settingsBody.get()));
|
||||
server.createContext("/dom--repo/com/example/a/1.0-SNAPSHOT/a-1.0-SNAPSHOT.pom",
|
||||
exchange -> respond(exchange, 404, ""));
|
||||
server.createContext("/api/maven/details/dom--repo/com/example/a/1.0-SNAPSHOT",
|
||||
exchange -> respond(exchange, 200, "{\"name\":\"1.0-SNAPSHOT\",\"type\":\"DIRECTORY\",\"files\":["
|
||||
+ "{\"name\":\"a-1.0-20261115.120000-1.jar\",\"type\":\"FILE\"},"
|
||||
+ "{\"name\":\"a-1.0-20261115.120000-1.pom\",\"type\":\"FILE\"}]}"));
|
||||
String pom = "<project><dependencies><dependency><groupId>com.example</groupId>"
|
||||
+ "<artifactId>snapshot-dep</artifactId><version>1.0.0</version></dependency>"
|
||||
+ "</dependencies></project>";
|
||||
server.createContext("/dom--repo/com/example/a/1.0-SNAPSHOT/a-1.0-20261115.120000-1.pom",
|
||||
exchange -> respond(exchange, 200, pom));
|
||||
|
||||
List<CodeArtifactService.PackageDependency> dependencies = client.listPackageVersionDependencies("dom--repo",
|
||||
"dom", "repo", "com.example", "a", "1.0-SNAPSHOT");
|
||||
|
||||
assertThat(dependencies, equalTo(List.of(
|
||||
new CodeArtifactService.PackageDependency("com.example", "snapshot-dep", "compile", "1.0.0"))));
|
||||
}
|
||||
|
||||
/**
|
||||
* Qodo review finding: a genuine Reposilite error on the POM fetch (not a real 404) must
|
||||
* surface as an error, not be silently reported as "no dependencies" - this reuses {@link
|
||||
* #listPackageVersionAssetNames}'s own already-established unexpected-status handling as the
|
||||
* fallback path, which is exactly what makes this case throw rather than return quietly.
|
||||
*/
|
||||
@Test
|
||||
void listPackageVersionDependenciesThrowsWhenTheFallbackListingFailsWithARealError() {
|
||||
settingsBody.set("{\"repositories\":[{\"id\":\"dom--repo\",\"visibility\":\"PUBLIC\",\"redeployment\":false}]}");
|
||||
server.createContext("/api/settings/domain/maven", exchange -> respond(exchange, 200, settingsBody.get()));
|
||||
server.createContext("/dom--repo/com/example/a/1.0/a-1.0.pom", exchange -> respond(exchange, 404, ""));
|
||||
server.createContext("/api/maven/details/dom--repo/com/example/a/1.0",
|
||||
exchange -> respond(exchange, 500, "{\"status\":500,\"message\":\"internal error\"}"));
|
||||
|
||||
assertThrows(IllegalStateException.class,
|
||||
() -> client.listPackageVersionDependencies("dom--repo", "dom", "repo", "com.example", "a", "1.0"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void listPackageVersionAssetNamesReturnsOnlyTheFileEntriesFromTheVersionDirectory() {
|
||||
server.createContext("/api/maven/details/dom--repo/com/example/a/1.0",
|
||||
|
||||
@@ -374,6 +374,51 @@ class VerdaccioSidecarClientTest {
|
||||
null, "demo-pkg"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void listPackageVersionDependenciesReadsAllFourDependencyMapsFromTheStoredVersionDoc() throws Exception {
|
||||
VerdaccioSidecarManager manager = manager();
|
||||
seedPooledContainer(manager, "npm-repo-1", "tracked-verdaccio-1", backendUrl);
|
||||
String body = "{\"versions\":{\"1.0.0\":{"
|
||||
+ "\"dependencies\":{\"@types/node\":\"^18.0.0\"},"
|
||||
+ "\"devDependencies\":{\"jest\":\"^29.0.0\"},"
|
||||
+ "\"peerDependencies\":{\"react\":\">=16\"},"
|
||||
+ "\"optionalDependencies\":{\"fsevents\":\"^2.0.0\"}"
|
||||
+ "}}}";
|
||||
backend.createContext("/demo-pkg", exchange -> {
|
||||
byte[] bytes = body.getBytes(StandardCharsets.UTF_8);
|
||||
exchange.getResponseHeaders().set("Content-Type", "application/json");
|
||||
exchange.sendResponseHeaders(200, bytes.length);
|
||||
exchange.getResponseBody().write(bytes);
|
||||
exchange.close();
|
||||
});
|
||||
|
||||
List<CodeArtifactService.PackageDependency> dependencies = client(manager).listPackageVersionDependencies(
|
||||
"npm-repo-1", "dom", "repo", null, "demo-pkg", "1.0.0");
|
||||
|
||||
assertEquals(4, dependencies.size());
|
||||
assertTrue(dependencies.contains(
|
||||
new CodeArtifactService.PackageDependency("types", "node", "regular", "^18.0.0")));
|
||||
assertTrue(dependencies.contains(new CodeArtifactService.PackageDependency(null, "jest", "dev", "^29.0.0")));
|
||||
assertTrue(dependencies.contains(new CodeArtifactService.PackageDependency(null, "react", "peer", ">=16")));
|
||||
assertTrue(dependencies.contains(
|
||||
new CodeArtifactService.PackageDependency(null, "fsevents", "optional", "^2.0.0")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void listPackageVersionDependenciesIsEmptyWhenThePackageDoesNotExist() throws Exception {
|
||||
VerdaccioSidecarManager manager = manager();
|
||||
seedPooledContainer(manager, "npm-repo-1", "tracked-verdaccio-1", backendUrl);
|
||||
backend.createContext("/demo-pkg", exchange -> {
|
||||
exchange.sendResponseHeaders(404, -1);
|
||||
exchange.close();
|
||||
});
|
||||
|
||||
List<CodeArtifactService.PackageDependency> dependencies = client(manager).listPackageVersionDependencies(
|
||||
"npm-repo-1", "dom", "repo", null, "demo-pkg", "1.0.0");
|
||||
|
||||
assertTrue(dependencies.isEmpty());
|
||||
}
|
||||
|
||||
private void respondWithMetadata(String packagePath, String version, String tarballPath) {
|
||||
String body = "{\"versions\":{\"" + version + "\":{\"dist\":{\"tarball\":\"http://ignored" + tarballPath
|
||||
+ "\"}}}}";
|
||||
|
||||
Reference in new issue
Block a user