Skip to content

Commit

Permalink
patch: interpolate patch definition file name
Browse files Browse the repository at this point in the history
  • Loading branch information
itzg committed Nov 25, 2023
1 parent 6b66956 commit ee211d8
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 73 deletions.
15 changes: 14 additions & 1 deletion src/main/java/me/itzg/helpers/patch/PatchSetProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import lombok.extern.slf4j.Slf4j;
import me.itzg.helpers.CharsetDetector;
import me.itzg.helpers.env.Interpolator;
import me.itzg.helpers.env.Interpolator.Result;
import me.itzg.helpers.env.MissingVariablesException;
import me.itzg.helpers.patch.model.PatchDefinition;
import me.itzg.helpers.patch.model.PatchOperation;
Expand Down Expand Up @@ -45,7 +47,7 @@ public void process(PatchSet patchSet) {
}

private void processPatch(PatchDefinition patch) {
final Path filePath = patch.getFile();
final Path filePath = resolveFilePath(patch.getFile());

if (Files.isRegularFile(filePath)) {
final FileFormat fileFormat = resolveFileFormat(patch, filePath);
Expand Down Expand Up @@ -79,6 +81,17 @@ private void processPatch(PatchDefinition patch) {
}
}

private Path resolveFilePath(String file) {
try {
final Result<String> fileResult = interpolator.interpolate(file);
return Paths.get(fileResult.getContent());
} catch (IOException e) {
log.warn("Failed to interpolate file name from patch", e);
return Paths.get(file);
}

}

private FileFormat resolveFileFormat(PatchDefinition patch, Path filePath) {
final String formatName = patch.getFileFormat();
final FileFormat fileFormat;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyDescription;
import lombok.Data;

import java.nio.file.Path;
import java.util.List;
import lombok.Data;

@Data
public class PatchDefinition {
@JsonProperty(required = true)
@JsonPropertyDescription("Path to the file to patch")
Path file;
String file;

@JsonPropertyDescription("If non-null, declares a specifically supported format name: json, yaml. " +
"Otherwise, the file format is detected by the file's suffix.")
Expand Down
179 changes: 111 additions & 68 deletions src/test/java/me/itzg/helpers/patch/PatchSetProcessorTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
package me.itzg.helpers.patch;

import com.fasterxml.jackson.databind.node.*;
import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.*;
import static uk.org.webcompere.modelassert.json.JsonAssertions.assertYaml;

import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.BooleanNode;
import com.fasterxml.jackson.databind.node.DoubleNode;
import com.fasterxml.jackson.databind.node.IntNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.TextNode;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import me.itzg.helpers.env.EnvironmentVariablesProvider;
import me.itzg.helpers.env.Interpolator;
import me.itzg.helpers.patch.model.PatchDefinition;
Expand All @@ -13,15 +28,6 @@
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.*;

@ExtendWith(MockitoExtension.class)
class PatchSetProcessorTest {

Expand All @@ -38,14 +44,14 @@ void setInJson(@TempDir Path tempDir) throws IOException {
);

processor.process(new PatchSet()
.setPatches(Arrays.asList(
new PatchDefinition()
.setFile(src)
.setOps(Arrays.asList(
new PatchSetOperation()
.setPath("$.outer.field1")
.setValue(new TextNode("new value"))
))
.setPatches(singletonList(
new PatchDefinition()
.setFile(src.toString())
.setOps(singletonList(
new PatchSetOperation()
.setPath("$.outer.field1")
.setValue(new TextNode("new value"))
))
))
);

Expand All @@ -64,14 +70,14 @@ void setInJson5(@TempDir Path tempDir) throws IOException {
);

processor.process(new PatchSet()
.setPatches(Arrays.asList(
new PatchDefinition()
.setFile(src)
.setOps(Arrays.asList(
new PatchSetOperation()
.setPath("$.outer.field1")
.setValue(new TextNode("new value"))
))
.setPatches(singletonList(
new PatchDefinition()
.setFile(src.toString())
.setOps(singletonList(
new PatchSetOperation()
.setPath("$.outer.field1")
.setValue(new TextNode("new value"))
))
))
);

Expand All @@ -90,14 +96,14 @@ void setInYaml(@TempDir Path tempDir) throws IOException {
);

processor.process(new PatchSet()
.setPatches(Arrays.asList(
new PatchDefinition()
.setFile(src)
.setOps(Arrays.asList(
new PatchSetOperation()
.setPath("$.outer.field1")
.setValue(new TextNode("new value"))
))
.setPatches(singletonList(
new PatchDefinition()
.setFile(src.toString())
.setOps(singletonList(
new PatchSetOperation()
.setPath("$.outer.field1")
.setValue(new TextNode("new value"))
))
))
);

Expand All @@ -116,26 +122,26 @@ void setNativeTypes(@TempDir Path tempDir) throws IOException {
);

processor.process(new PatchSet()
.setPatches(Arrays.asList(
new PatchDefinition()
.setFile(src)
.setOps(Arrays.asList(
new PatchSetOperation()
.setPath("$.outer.field1")
.setValue(new IntNode(5)),
new PatchSetOperation()
.setPath("$.outer.field2")
.setValue(new DoubleNode(5.1)),
new PatchSetOperation()
.setPath("$.outer.field3")
.setValue(BooleanNode.TRUE),
new PatchPutOperation()
.setPath("$.outer")
.setKey("field4")
.setValue(new ArrayNode(new JsonNodeFactory(true))
.add(5).add(6).add(7)
)
))
.setPatches(singletonList(
new PatchDefinition()
.setFile(src.toString())
.setOps(Arrays.asList(
new PatchSetOperation()
.setPath("$.outer.field1")
.setValue(new IntNode(5)),
new PatchSetOperation()
.setPath("$.outer.field2")
.setValue(new DoubleNode(5.1)),
new PatchSetOperation()
.setPath("$.outer.field3")
.setValue(BooleanNode.TRUE),
new PatchPutOperation()
.setPath("$.outer")
.setKey("field4")
.setValue(new ArrayNode(new JsonNodeFactory(true))
.add(5).add(6).add(7)
)
))
))
);

Expand Down Expand Up @@ -163,20 +169,20 @@ void setWithEnv(@TempDir Path tempDir) throws IOException {
);

processor.process(new PatchSet()
.setPatches(Arrays.asList(
new PatchDefinition()
.setFile(src)
.setOps(Arrays.asList(
new PatchSetOperation()
.setPath("$.outer.field1")
.setValue(new TextNode("${CFG_CUSTOM}")),
new PatchSetOperation()
.setPath("$.outer.field2")
.setValue(new TextNode("${CFG_MISSING}")),
new PatchSetOperation()
.setPath("$.outer.field3")
.setValue(new TextNode("${GETS_IGNORED}"))
))
.setPatches(singletonList(
new PatchDefinition()
.setFile(src.toString())
.setOps(Arrays.asList(
new PatchSetOperation()
.setPath("$.outer.field1")
.setValue(new TextNode("${CFG_CUSTOM}")),
new PatchSetOperation()
.setPath("$.outer.field2")
.setValue(new TextNode("${CFG_MISSING}")),
new PatchSetOperation()
.setPath("$.outer.field3")
.setValue(new TextNode("${GETS_IGNORED}"))
))
))
);

Expand All @@ -190,4 +196,41 @@ void setWithEnv(@TempDir Path tempDir) throws IOException {
verify(environmentVariablesProvider).get("CFG_MISSING");
verifyNoMoreInteractions(environmentVariablesProvider);
}

@Test
void resolvesFileNameFromEnv(@TempDir Path tempDir) throws IOException {
final Path src = tempDir.resolve("testing.yaml");
Files.write(src, Arrays.asList(
"outer:",
" field1: original"
));

when(environmentVariablesProvider.get("CFG_FILENAME_FILE"))
.thenReturn(null);
when(environmentVariablesProvider.get("CFG_FILENAME"))
.thenReturn("testing.yaml");

final PatchSetProcessor processor = new PatchSetProcessor(
new Interpolator(environmentVariablesProvider, "CFG_")
);

processor.process(new PatchSet()
.setPatches(singletonList(
new PatchDefinition()
.setFile(tempDir + "/${CFG_FILENAME}")
.setOps(singletonList(
new PatchSetOperation()
.setPath("$.outer.field1")
.setValue(new TextNode("updated"))
))
))
);

assertYaml(src)
.at("/outer/field1").hasValue("updated");

verify(environmentVariablesProvider).get("CFG_FILENAME_FILE");
verify(environmentVariablesProvider).get("CFG_FILENAME");
verifyNoMoreInteractions(environmentVariablesProvider);
}
}

0 comments on commit ee211d8

Please sign in to comment.