Skip to content

Commit

Permalink
Fix sink
Browse files Browse the repository at this point in the history
  • Loading branch information
cstamas committed Mar 22, 2024
1 parent 18b8ebb commit 3adfc1c
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,11 @@
public final class DirectorySink implements ArtifactSink {
/**
* Creates plain "flat" directory sink, that accepts all artifacts and copies them out having filenames as
* "A[-C]-V.E" and prevents overwrite (what you usually want).
* <p>
* This means that if your set of artifacts have artifacts with different groupIDs but same artifactIDs, this sink
* will fail while accepting them, to prevent overwrite. Duplicated artifacts are filtered out.
* "G-A[-C].E" (version-less) and prevents overwrite (what you usually want).
*/
public static DirectorySink flat(Output output, Path path) throws IOException {
return new DirectorySink(
output, path, Mode.COPY, ArtifactMatcher.unique(), a -> a, ArtifactNameMapper.ACVE(), false);
output, path, Mode.COPY, ArtifactMatcher.unique(), a -> a, ArtifactNameMapper.GACE(), false);
}

/**
Expand Down Expand Up @@ -132,6 +129,7 @@ public void accept(Artifact artifact) throws IOException {
if (!writtenPaths.add(target) && !allowOverwrite) {
throw new IOException("Overwrite prevented; check mappings");
}
Files.createDirectories(target.getParent());
switch (mode) {
case COPY:
output.verbose(" copied to file {}", target);
Expand Down Expand Up @@ -171,4 +169,7 @@ public void cleanup(IOException e) {
}
}
}

@Override
public void close() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
package eu.maveniverse.maven.toolbox.shared;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.IOException;
Expand All @@ -22,35 +21,65 @@

public class DirectorySinkTest {
@Test
void smoke(@TempDir Path source, @TempDir Path target) throws IOException {
DirectorySink sink = DirectorySink.flat(new NullOutput(), target);
Path a1 = source.resolve("a1");
Path a2 = source.resolve("a2");
Files.writeString(a1, "one", StandardCharsets.UTF_8);
Files.writeString(a2, "two", StandardCharsets.UTF_8);
sink.accept(Arrays.asList(
new DefaultArtifact("g:a1:1").setFile(a1.toFile()),
new DefaultArtifact("g:a2:1").setFile(a2.toFile())));

Path a1target = target.resolve("a1-1.jar");
Path a2target = target.resolve("a2-1.jar");
assertTrue(Files.isRegularFile(a1target));
assertEquals(Files.readString(a1target, StandardCharsets.UTF_8), "one");
assertTrue(Files.isRegularFile(a2target));
assertEquals(Files.readString(a2target, StandardCharsets.UTF_8), "two");
void flat(@TempDir Path source, @TempDir Path target) throws IOException {
try (DirectorySink sink = DirectorySink.flat(new NullOutput(), target)) {
Path a1 = source.resolve("a1");
Path a2 = source.resolve("a2");
Files.writeString(a1, "one", StandardCharsets.UTF_8);
Files.writeString(a2, "two", StandardCharsets.UTF_8);
sink.accept(Arrays.asList(
new DefaultArtifact("g:a1:1").setFile(a1.toFile()),
new DefaultArtifact("g:a2:1").setFile(a2.toFile())));

Path a1target = target.resolve("g.a1.jar");
Path a2target = target.resolve("g.a2.jar");
assertTrue(Files.isRegularFile(a1target));
assertEquals(Files.readString(a1target, StandardCharsets.UTF_8), "one");
assertTrue(Files.isRegularFile(a2target));
assertEquals(Files.readString(a2target, StandardCharsets.UTF_8), "two");
}
}

@Test
void repository(@TempDir Path source, @TempDir Path target) throws IOException {
try (DirectorySink sink = DirectorySink.repository(new NullOutput(), target)) {
Path a1 = source.resolve("a1");
Path a2 = source.resolve("a2");
Files.writeString(a1, "one", StandardCharsets.UTF_8);
Files.writeString(a2, "two", StandardCharsets.UTF_8);
sink.accept(Arrays.asList(
new DefaultArtifact("g:a1:1").setFile(a1.toFile()),
new DefaultArtifact("g:a2:1").setFile(a2.toFile())));

Path a1target = target.resolve("g/a1/1/a1-1.jar");
Path a2target = target.resolve("g/a2/1/a2-1.jar");
assertTrue(Files.isRegularFile(a1target));
assertEquals(Files.readString(a1target, StandardCharsets.UTF_8), "one");
assertTrue(Files.isRegularFile(a2target));
assertEquals(Files.readString(a2target, StandardCharsets.UTF_8), "two");
}
}

@Test
void sameADifferentGRejected(@TempDir Path source, @TempDir Path target) throws IOException {
DirectorySink sink = DirectorySink.flat(new NullOutput(), target);
Path a1 = source.resolve("a1");
Path a2 = source.resolve("a2");
Files.writeString(a1, "one", StandardCharsets.UTF_8);
Files.writeString(a2, "two", StandardCharsets.UTF_8);
assertThrows(
IOException.class,
() -> sink.accept(Arrays.asList(
new DefaultArtifact("g1:a1:1").setFile(a1.toFile()),
new DefaultArtifact("g2:a1:1").setFile(a2.toFile()))));
void flatSameADifferentGAccepted(@TempDir Path source, @TempDir Path target) throws IOException {
sameADifferentGAccepted(source, target, DirectorySink.flat(new NullOutput(), target));
}

@Test
void repositorySameADifferentGAccepted(@TempDir Path source, @TempDir Path target) throws IOException {
sameADifferentGAccepted(source, target, DirectorySink.repository(new NullOutput(), target));
}

private void sameADifferentGAccepted(Path source, Path target, DirectorySink sink) throws IOException {
try (sink) {
Path a1 = source.resolve("a1");
Path a2 = source.resolve("a2");
Files.writeString(a1, "one", StandardCharsets.UTF_8);
Files.writeString(a2, "two", StandardCharsets.UTF_8);
// we do not throw
sink.accept(Arrays.asList(
new DefaultArtifact("g1:a1:1").setFile(a1.toFile()),
new DefaultArtifact("g2:a1:1").setFile(a2.toFile())));
}
}
}

0 comments on commit 3adfc1c

Please sign in to comment.