|
| 1 | +package io.spinnaker.bumpdeps |
| 2 | + |
| 3 | +import java.io.FileInputStream |
| 4 | +import java.io.FileOutputStream |
| 5 | +import java.nio.file.Files |
| 6 | +import java.util.Properties |
| 7 | +import org.junit.jupiter.api.Assertions.assertEquals |
| 8 | +import org.junit.jupiter.api.Test |
| 9 | + |
| 10 | +internal class NonDestructivePropertiesEditorTest { |
| 11 | + |
| 12 | + private fun subject(): NonDestructivePropertiesEditor = |
| 13 | + Files.createTempFile("gradle", ".properties").let { |
| 14 | + it.toFile().deleteOnExit() |
| 15 | + NonDestructivePropertiesEditor(it) |
| 16 | + } |
| 17 | + |
| 18 | + @Test |
| 19 | + fun testParsesLines() { |
| 20 | + val subject = subject() |
| 21 | + |
| 22 | + val lines = listOf( |
| 23 | + "#comment", |
| 24 | + "foo=bar" |
| 25 | + ) |
| 26 | + |
| 27 | + val result = subject.updateProperty(lines, "foo", "bar") |
| 28 | + assertResult(result, lines, expectedMatched = true, expectedUpdated = false) |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + fun testIgnoresWhitespace() { |
| 33 | + val subject = subject() |
| 34 | + |
| 35 | + val lines = listOf( |
| 36 | + " foo = bar " |
| 37 | + ) |
| 38 | + |
| 39 | + val result = subject.updateProperty(lines, "foo", "bar") |
| 40 | + assertResult(result, lines, expectedMatched = true, expectedUpdated = false) |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + fun testIgnoresCommentedOutValue() { |
| 45 | + val subject = subject() |
| 46 | + |
| 47 | + val lines = listOf( |
| 48 | + "#foo=bar" |
| 49 | + ) |
| 50 | + |
| 51 | + val result = subject.updateProperty(lines, "foo", "bar") |
| 52 | + assertResult(result, lines, expectedMatched = false, expectedUpdated = false) |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + fun testUpdatesValue() { |
| 57 | + val subject = subject() |
| 58 | + |
| 59 | + val lines = listOf( |
| 60 | + "foo=baz" |
| 61 | + ) |
| 62 | + val result = subject.updateProperty(lines, "foo", "bar") |
| 63 | + |
| 64 | + val expected = listOf( |
| 65 | + "foo=bar" |
| 66 | + ) |
| 67 | + assertResult(result, expected, expectedMatched = true, expectedUpdated = true) |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + fun testUpdatedValueTrimsWhitespace() { |
| 72 | + val subject = subject() |
| 73 | + |
| 74 | + val lines = listOf( |
| 75 | + " foo = baz " |
| 76 | + ) |
| 77 | + val result = subject.updateProperty(lines, "foo", "bar") |
| 78 | + |
| 79 | + val expected = listOf( |
| 80 | + "foo=bar" |
| 81 | + ) |
| 82 | + assertResult(result, expected, expectedMatched = true, expectedUpdated = true) |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + fun testLastValueForPropertyInFileDeterminesUpdated() { |
| 87 | + val subject = subject() |
| 88 | + |
| 89 | + val lines = listOf( |
| 90 | + " foo = bar ", |
| 91 | + "foo=baz" |
| 92 | + ) |
| 93 | + val result = subject.updateProperty(lines, "foo", "bar") |
| 94 | + |
| 95 | + val expected = listOf( |
| 96 | + " foo = bar ", |
| 97 | + "foo=bar" |
| 98 | + ) |
| 99 | + assertResult(result, expected, expectedMatched = true, expectedUpdated = true) |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + fun testIfNotUpdatedIfLastValueAlreadyMatchedDespiteMutatingIntermediateLines() { |
| 104 | + val subject = subject() |
| 105 | + |
| 106 | + val lines = listOf( |
| 107 | + " foo = bar ", |
| 108 | + "foo=baz" |
| 109 | + ) |
| 110 | + val result = subject.updateProperty(lines, "foo", "baz") |
| 111 | + |
| 112 | + val expected = listOf( |
| 113 | + "foo=baz", |
| 114 | + "foo=baz" |
| 115 | + ) |
| 116 | + assertResult(result, expected, expectedMatched = true, expectedUpdated = false) |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + fun testReadsFromPropertiesFile() { |
| 121 | + val subject = subject() |
| 122 | + val file = subject.propertiesFile |
| 123 | + |
| 124 | + val props = Properties() |
| 125 | + |
| 126 | + props.setProperty("foo", "bar") |
| 127 | + FileOutputStream(file.toFile()).use { |
| 128 | + props.store(it, "the comments") |
| 129 | + } |
| 130 | + |
| 131 | + val expected = Files.readAllLines(file, subject.propsCharset) |
| 132 | + val result = subject.updateProperty("foo", "bar") |
| 133 | + |
| 134 | + assertResult(result, expected, expectedMatched = true, expectedUpdated = false) |
| 135 | + } |
| 136 | + |
| 137 | + @Test |
| 138 | + fun testWritesToPropertiesFile() { |
| 139 | + val subject = subject() |
| 140 | + |
| 141 | + val file = subject.propertiesFile |
| 142 | + |
| 143 | + val props = Properties() |
| 144 | + |
| 145 | + props.setProperty("foo", "baz") |
| 146 | + FileOutputStream(file.toFile()).use { |
| 147 | + props.store(it, "the comments") |
| 148 | + } |
| 149 | + |
| 150 | + // this sucks as a test but work around the indeterminate date header |
| 151 | + val expected = Files.readAllLines(file, subject.propsCharset) |
| 152 | + |
| 153 | + val result = subject.updateProperty("foo", "bar") |
| 154 | + expected[2] = "foo=bar" |
| 155 | + assertResult(result, expected, expectedMatched = true, expectedUpdated = true) |
| 156 | + |
| 157 | + subject.saveResult(result.lines) |
| 158 | + |
| 159 | + FileInputStream(file.toFile()).use { |
| 160 | + props.load(it) |
| 161 | + } |
| 162 | + |
| 163 | + assertEquals("bar", props.getProperty("foo")) |
| 164 | + } |
| 165 | + |
| 166 | + private fun assertResult(result: Result, expectedLines: List<String>, expectedMatched: Boolean, expectedUpdated: Boolean) { |
| 167 | + assertEquals(expectedMatched, result.matched, "whether the key was matched in the properties file") |
| 168 | + assertEquals(expectedUpdated, result.updated, "whether the effective value in the properties file was updated") |
| 169 | + assertEquals(expectedLines.size, result.lines.size, "expected number of lines") |
| 170 | + for ((index, line) in expectedLines.withIndex()) { |
| 171 | + assertEquals(line, result.lines[index], "expected line ${index + 1} to match") |
| 172 | + } |
| 173 | + } |
| 174 | +} |
0 commit comments