diff options
author | Valerij Fredriksen <valerijf@oath.com> | 2018-04-10 14:51:40 +0200 |
---|---|---|
committer | Valerij Fredriksen <valerijf@oath.com> | 2018-04-10 14:54:55 +0200 |
commit | 5beba082f89d2caf078a5a869c773bf4adabad4c (patch) | |
tree | 7dfbab0f6e292e0aaeb25ae983d37dbcdd9ec6de | |
parent | e5b2095628ed2aa7c24245f403d086b04ef0727c (diff) |
Add insertBefore insertAfter and multiline replace
3 files changed, 79 insertions, 22 deletions
diff --git a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/Editor.java b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/Editor.java index cfd7756ad0e..83954551905 100644 --- a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/Editor.java +++ b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/Editor.java @@ -44,30 +44,35 @@ public class Editor { for (String line : lines) { LineEdit edit = editor.edit(line); + if (!edit.prependLines().isEmpty()) { + modified = true; + maybeAdd(diff, edit.prependLines()); + newLines.addAll(edit.prependLines()); + } + switch (edit.getType()) { - case REMOVE: - modified = true; - maybeRemove(diff, line); - break; case REPLACE: modified = true; - String replacementLine = edit.replacementLine(); - newLines.add(replacementLine); maybeRemove(diff, line); - maybeAdd(diff, replacementLine); break; case NONE: newLines.add(line); break; default: throw new IllegalArgumentException("Unknown EditType " + edit.getType()); } + + if (!edit.appendLines().isEmpty()) { + modified = true; + maybeAdd(diff, edit.appendLines()); + newLines.addAll(edit.appendLines()); + } } List<String> linesToAppend = editor.onComplete(); if (!linesToAppend.isEmpty()) { modified = true; newLines.addAll(linesToAppend); - linesToAppend.forEach(line -> maybeAdd(diff, line)); + maybeAdd(diff, linesToAppend); } if (!modified) { @@ -80,9 +85,11 @@ public class Editor { return true; } - private static void maybeAdd(StringBuilder diff, String line) { - if (!diffTooLarge(diff)) { - diff.append('+').append(line).append('\n'); + private static void maybeAdd(StringBuilder diff, List<String> lines) { + for (String line : lines) { + if (!diffTooLarge(diff)) { + diff.append('+').append(line).append('\n'); + } } } diff --git a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/LineEdit.java b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/LineEdit.java index d822b6fae80..78e7a3e71b6 100644 --- a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/LineEdit.java +++ b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/LineEdit.java @@ -1,10 +1,13 @@ // Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.vespa.hosted.node.admin.task.util.file; +import com.google.common.collect.ImmutableList; + import javax.annotation.concurrent.Immutable; -import java.util.Optional; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; -import static com.yahoo.vespa.hosted.node.admin.task.util.file.LineEdit.Type.REMOVE; import static com.yahoo.vespa.hosted.node.admin.task.util.file.LineEdit.Type.REPLACE; import static com.yahoo.vespa.hosted.node.admin.task.util.file.LineEdit.Type.NONE; @@ -13,19 +16,31 @@ import static com.yahoo.vespa.hosted.node.admin.task.util.file.LineEdit.Type.NON */ @Immutable public class LineEdit { - enum Type { NONE, REPLACE, REMOVE} - private final Type type; - private final Optional<String> line; + enum Type { NONE, REPLACE } + + public static LineEdit none() { return insert(Collections.emptyList(), Collections.emptyList()); } + public static LineEdit remove() { return replaceWith(Collections.emptyList()); } - public static LineEdit none() { return new LineEdit(NONE, Optional.empty()); } - public static LineEdit remove() { return new LineEdit(REMOVE, Optional.empty()); } - public static LineEdit replaceWith(String line) { return new LineEdit(REPLACE, Optional.of(line)); } + public static LineEdit insertBefore(String... prepend) { return insertBefore(Arrays.asList(prepend)); } + public static LineEdit insertBefore(List<String> prepend) { return insert(prepend, Collections.emptyList()); } + public static LineEdit insertAfter(String... append) { return insertAfter(Arrays.asList(append)); } + public static LineEdit insertAfter(List<String> append) { return insert(Collections.emptyList(), append); } + public static LineEdit insert(List<String> prepend, List<String> append) { return new LineEdit(NONE, prepend, append); } + + public static LineEdit replaceWith(String... lines) { return replaceWith(Arrays.asList(lines)); } + public static LineEdit replaceWith(List<String> insertLines) { return new LineEdit(REPLACE, Collections.emptyList(), insertLines); } + + private final Type type; + private final List<String> prependLines; + private final List<String> appendLines; - private LineEdit(Type type, Optional<String> newLine) { + private LineEdit(Type type, List<String> prependLines, List<String> appendLines) { this.type = type; - this.line = newLine; + this.prependLines = ImmutableList.copyOf(prependLines); + this.appendLines = ImmutableList.copyOf(appendLines); } public Type getType() { return type; } - public String replacementLine() { return line.get(); } + public List<String> prependLines() { return prependLines; } + public List<String> appendLines() { return appendLines; } } diff --git a/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/file/EditorTest.java b/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/file/EditorTest.java index c89164e6cfb..c08f7dad694 100644 --- a/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/file/EditorTest.java +++ b/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/file/EditorTest.java @@ -8,6 +8,7 @@ import org.junit.Test; import org.mockito.ArgumentCaptor; import java.nio.file.FileSystem; +import java.util.Collections; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; @@ -54,6 +55,40 @@ public class EditorTest { } @Test + public void testInsert() { + path.writeUtf8File("second\neight\nfifth\nseventh\n"); + + LineEditor lineEditor = mock(LineEditor.class); + when(lineEditor.edit(any())).thenReturn( + LineEdit.insertBefore("first"), // insert first, and keep the second line + LineEdit.replaceWith("third", "fourth"), // remove eight, and replace with third and fourth instead + LineEdit.none(), // Keep fifth + LineEdit.insert(Collections.singletonList("sixth"), // insert sixth before seventh + Collections.singletonList("eight"))); // add eight after seventh + + Editor editor = new Editor(path.toPath(), lineEditor); + TaskContext context = mock(TaskContext.class); + + assertTrue(editor.converge(context)); + + // Verify the system modification message + ArgumentCaptor<String> modificationMessage = ArgumentCaptor.forClass(String.class); + verify(context).recordSystemModification(any(), modificationMessage.capture()); + assertEquals( + "Patching file /file:\n+" + + "first\n" + + "-eight\n" + + "+third\n" + + "+fourth\n" + + "+sixth\n" + + "+eight\n", + modificationMessage.getValue()); + + // Verify the new contents of the file: + assertEquals("first\nsecond\nthird\nfourth\nfifth\nsixth\nseventh\neight\n", path.readUtf8File()); + } + + @Test public void noop() { path.writeUtf8File("line\n"); |