Skip to content

Commit

Permalink
Allow file scheme in RemoteArchive to simplify testing
Browse files Browse the repository at this point in the history
While it might look a bit controversial, the file scheme can also point to a remote (for instance a mounted network share) file. By allowing the `file://` scheme we can use `RemoteArchive` for those files.

As a useful side effect, this makes testing RemoteArchive handling a lot easier.
  • Loading branch information
pstreef committed Dec 16, 2024
1 parent d449490 commit 107f249
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.UUID;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -92,6 +93,9 @@ public InputStream getInputStream(ExecutionContext ctx) {
try {
Path localArchive = cache.compute(uri, () -> {
//noinspection resource
if ("file".equals(uri.getScheme())) {
return Files.newInputStream(Paths.get(uri));
}
HttpSender.Response response = httpSender.send(httpSender.get(uri.toString()).build());
if (response.isSuccessful()) {
return response.getBody();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@
import org.openrewrite.ExecutionContext;
import org.openrewrite.HttpSenderExecutionContextView;
import org.openrewrite.InMemoryExecutionContext;
import org.openrewrite.PrintOutputCapture;
import org.openrewrite.test.MockHttpSender;

import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Paths;
import java.util.concurrent.*;
Expand All @@ -40,10 +42,6 @@ class RemoteArchiveTest {
void gradleWrapper(String version) throws Exception {
URL distributionUrl = requireNonNull(RemoteArchiveTest.class.getClassLoader().getResource("gradle-" + version + "-bin.zip"));
ExecutionContext ctx = new InMemoryExecutionContext();
RemoteExecutionContextView.view(ctx).setArtifactCache(new LocalRemoteArtifactCache(
Paths.get(System.getProperty("user.home") + "/.rewrite/remote/gradleWrapper")));
HttpSenderExecutionContextView.view(ctx)
.setLargeFileHttpSender(new MockHttpSender(distributionUrl::openStream));

RemoteArchive remoteArchive = Remote
.builder(
Expand All @@ -58,10 +56,9 @@ void gradleWrapper(String version) throws Exception {

@Test
void gradleWrapperDownloadFails() throws Exception {
URL distributionUrl = requireNonNull(RemoteArchiveTest.class.getClassLoader().getResource("gradle-7.4.2-bin.zip"));
URL distributionUrl = new URL("http://example.com");
ExecutionContext ctx = new InMemoryExecutionContext();
RemoteExecutionContextView.view(ctx).setArtifactCache(new LocalRemoteArtifactCache(
Paths.get(System.getProperty("user.home") + "/.rewrite/remote/gradleWrapperDownloadFails")));

HttpSenderExecutionContextView.view(ctx)
.setLargeFileHttpSender(new MockHttpSender(408));

Expand Down Expand Up @@ -116,6 +113,21 @@ void gradleWrapperConcurrent(String version) throws Exception {
executorService.shutdown();
}

@Test
void printingRemoteArchive() throws URISyntaxException {
URL zipUrl = requireNonNull(RemoteArchiveTest.class.getClassLoader().getResource("zipfile.zip"));

RemoteArchive remoteArchive = Remote
.builder(
Paths.get("content.txt"),
zipUrl.toURI()
)
.build("content.txt");

String printed = remoteArchive.printAll(new PrintOutputCapture<>(0, PrintOutputCapture.MarkerPrinter.DEFAULT));
assertThat(printed).isEqualTo("this is a zipped file");
}

private Long getInputStreamSize(InputStream is) {
BlackHoleOutputStream out = new BlackHoleOutputStream();
try {
Expand Down
Binary file added rewrite-core/src/test/resources/zipfile.zip
Binary file not shown.

0 comments on commit 107f249

Please sign in to comment.