Skip to content

Commit

Permalink
Add fileContentsUrl option for CreateYamlFile (#4098)
Browse files Browse the repository at this point in the history
* Add fileContentsUrl option for CreateYamlFile

* Prevent eager/repeated file download & checksum

* Simplify test by getting rid of local disk access

* Set a NOOP artifact cache

* Also override compute and set context on spec

* Apply suggestion to fix test

---------

Co-authored-by: Tim te Beek <[email protected]>
  • Loading branch information
PhilKes and timtebeek authored May 8, 2024
1 parent ce7bb44 commit f83fd77
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
import org.openrewrite.*;
import org.openrewrite.internal.StringUtils;
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.remote.Remote;
import org.openrewrite.yaml.tree.Yaml;

import java.net.URI;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collection;
Expand All @@ -49,6 +51,13 @@ public class CreateYamlFile extends ScanningRecipe<AtomicBoolean> {
@Nullable
String fileContents;

@Option(displayName = "File contents URL",
description = "URL to file containing text content for the file. Use either `fileContents` or `fileContentsUrl` option.",
example = "http://foo.bar/baz.yaml",
required = false)
@Nullable
String fileContentsUrl;

@Option(displayName = "Overwrite existing file",
description = "If there is an existing file, should it be overwritten.",
required = false)
Expand Down Expand Up @@ -92,14 +101,18 @@ public TreeVisitor<?, ExecutionContext> getVisitor(AtomicBoolean created) {
@Override
public Yaml visitDocuments(Yaml.Documents documents, ExecutionContext ctx) {
if ((created.get() || Boolean.TRUE.equals(overwriteExisting)) && path.equals(documents.getSourcePath())) {
if (StringUtils.isBlank(fileContents)) {
@Language("yml") String yamlContents = fileContents;
if (yamlContents == null && fileContentsUrl != null) {
yamlContents = Remote.builder(path, URI.create(fileContentsUrl)).build().printAll(ctx);
}
if (StringUtils.isBlank(yamlContents)) {
return documents.withDocuments(emptyList());
}
if (documents.printAll().equals(fileContents)) {
if (documents.printAll().equals(yamlContents)) {
return documents;
}
Optional<SourceFile> sourceFiles = YamlParser.builder().build()
.parse(fileContents)
.parse(yamlContents)
.findFirst();
if (sourceFiles.isPresent()) {
SourceFile sourceFile = sourceFiles.get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,27 @@
*/
package org.openrewrite.yaml;

import org.intellij.lang.annotations.Language;
import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.HttpSenderExecutionContextView;
import org.openrewrite.InMemoryExecutionContext;
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.remote.RemoteArtifactCache;
import org.openrewrite.remote.RemoteExecutionContextView;
import org.openrewrite.test.MockHttpSender;
import org.openrewrite.test.RewriteTest;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.function.Consumer;

import static org.openrewrite.yaml.Assertions.yaml;

class CreateYamlFileTest implements RewriteTest {
Expand All @@ -35,6 +52,7 @@ void hasCreatedFile() {
spec -> spec.recipe(new CreateYamlFile(
"test/test.yaml",
fileContents,
null,
null
)),
yaml(
Expand All @@ -52,6 +70,7 @@ void hasOverwrittenFile() {
spec -> spec.recipe(new CreateYamlFile(
"test/test.yaml",
"after: true",
null,
true
)),
yaml(
Expand All @@ -68,6 +87,7 @@ void shouldNotChangeExistingFile() {
spec -> spec.recipe(new CreateYamlFile(
"test/test.yaml",
null,
null,
false
)),
yaml(
Expand All @@ -85,6 +105,7 @@ void shouldNotChangeExistingFileWhenOverwriteNull() {
spec -> spec.recipe(new CreateYamlFile(
"test/test.yaml",
null,
null,
null
)),
yaml(
Expand All @@ -100,6 +121,7 @@ void shouldAddAnotherFile() {
spec -> spec.recipe(new CreateYamlFile(
"test/test-file-2.yaml",
null,
null,
true
)),
yaml(
Expand All @@ -113,4 +135,77 @@ void shouldAddAnotherFile() {
)
);
}

@Test
void shouldDownloadFileContents() {
@Language("yml")
String yamlContent = """
# This is a comment
foo: x
bar:
z: y
""";
InMemoryExecutionContext ctx = new InMemoryExecutionContext(e -> e.printStackTrace());
MockHttpSender httpSender = new MockHttpSender(() ->
new ByteArrayInputStream(yamlContent.getBytes()));
HttpSenderExecutionContextView.view(ctx)
.setHttpSender(httpSender)
.setLargeFileHttpSender(httpSender);
RemoteExecutionContextView.view(ctx).setArtifactCache(new RemoteArtifactCache() {
@Override
public @Nullable Path get(URI uri) {
return null;
}

@Override
public @Nullable Path put(URI uri, InputStream is, Consumer<Throwable> onError) {
try {
var file = File.createTempFile("rewrite", "yaml");
Files.copy(is, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
return file.toPath();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
});
rewriteRun(
spec -> spec
.executionContext(ctx)
.recipeExecutionContext(ctx)
.recipe(new CreateYamlFile(
"test/test.yaml",
null,
"http://fake.url/test.yaml",
true)
),
yaml(
null,
yamlContent,
spec -> spec.path("test/test.yaml")
)
);
}

@Test
void shouldUseFileContentsWhenContentsAndContentsUrlNotNull() {
@Language("yml")
String fileContents = """
# This is a comment
x:
y: z
""";
rewriteRun(
spec -> spec.recipe(new CreateYamlFile(
"test/test.yaml",
fileContents,
"http://foo.bar/baz.yaml",
true)
),
yaml(
null,
fileContents,
spec -> spec.path("test/test.yaml")
)
);
}
}

0 comments on commit f83fd77

Please sign in to comment.