-
-
Notifications
You must be signed in to change notification settings - Fork 424
Make the output of PropertiesFileTransformer reproducible
#1861
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+172
−75
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 0 additions & 7 deletions
7
src/functionalTest/kotlin/com/github/jengelman/gradle/plugins/shadow/util/Paths.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,7 @@ | ||
| package com.github.jengelman.gradle.plugins.shadow.util | ||
|
|
||
| import java.nio.file.FileSystems | ||
| import java.nio.file.Path | ||
| import kotlin.io.path.readText | ||
| import kotlin.io.path.writeText | ||
|
|
||
| fun Path.prependText(text: String) = writeText(text + readText()) | ||
|
|
||
| val String.invariantEolString: String get() = replace(System.lineSeparator(), "\n") | ||
|
|
||
| val String.variantSeparatorsPathString: String get() = replace("/", fileSystem.separator) | ||
|
|
||
| private val fileSystem = FileSystems.getDefault() |
41 changes: 25 additions & 16 deletions
41
src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/internal/CleanProperties.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,31 +1,40 @@ | ||
| package com.github.jengelman.gradle.plugins.shadow.internal | ||
|
|
||
| import java.io.BufferedWriter | ||
| import java.io.IOException | ||
| import java.io.OutputStream | ||
| import java.io.StringWriter | ||
| import java.io.Writer | ||
| import java.util.Date | ||
| import java.nio.charset.Charset | ||
| import java.util.Properties | ||
|
|
||
| /** | ||
| * Introduced in order to remove prepended timestamp when creating output stream. | ||
| * Provides functionality for reproducible serialization. | ||
| */ | ||
| internal class CleanProperties : Properties() { | ||
| @Throws(IOException::class) | ||
| override fun store(writer: Writer, comments: String) { | ||
| super.store(StripCommentsWithTimestampBufferedWriter(writer), comments) | ||
| throw UnsupportedOperationException("use writeWithoutComments()") | ||
| } | ||
|
|
||
| private class StripCommentsWithTimestampBufferedWriter(out: Writer) : BufferedWriter(out) { | ||
| private val lengthOfExpectedTimestamp = ("#" + Date().toString()).length | ||
| override fun store(out: OutputStream, comments: String?) { | ||
| throw UnsupportedOperationException("use writeWithoutComments()") | ||
| } | ||
|
|
||
| @Throws(IOException::class) | ||
| override fun write(str: String) { | ||
| if (str.couldBeCommentWithTimestamp) return | ||
| super.write(str) | ||
| } | ||
| fun writeWithoutComments(charset: Charset, os: OutputStream) { | ||
| val bufferedReader = StringWriter().apply { | ||
| super.store(this, null) | ||
| }.toString().reader().buffered() | ||
|
|
||
| private val String?.couldBeCommentWithTimestamp: Boolean get() { | ||
| return this != null && startsWith("#") && length == lengthOfExpectedTimestamp | ||
| } | ||
| os.bufferedWriter(charset).apply { | ||
| var line: String? = null | ||
| while (bufferedReader.readLine().also { line = it } != null && line != null) { | ||
Goooler marked this conversation as resolved.
Show resolved
Hide resolved
Goooler marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (!line.startsWith("#")) { | ||
| write(line) | ||
| newLine() | ||
| } | ||
| } | ||
| }.flush() | ||
| } | ||
|
|
||
| override val entries: MutableSet<MutableMap.MutableEntry<Any, Any>> | ||
| // Yields the entries for Properties.store0() in sorted order. | ||
| get() = super.entries.toSortedSet(compareBy { it.key.toString() }) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
src/test/kotlin/com/github/jengelman/gradle/plugins/shadow/internal/CleanPropertiesTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| package com.github.jengelman.gradle.plugins.shadow.internal | ||
|
|
||
| import assertk.assertThat | ||
| import assertk.assertions.isEqualTo | ||
| import com.github.jengelman.gradle.plugins.shadow.testkit.invariantEolString | ||
| import java.io.ByteArrayOutputStream | ||
| import java.nio.charset.Charset | ||
| import java.nio.charset.StandardCharsets | ||
| import org.junit.jupiter.params.ParameterizedTest | ||
| import org.junit.jupiter.params.provider.MethodSource | ||
|
|
||
| class CleanPropertiesTest { | ||
| @ParameterizedTest | ||
| @MethodSource("generalCharsetsProvider") | ||
| fun emptyProperties(charset: Charset) { | ||
| val output = CleanProperties().writeToString(charset) | ||
|
|
||
| assertThat(output).isEqualTo("") | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @MethodSource("generalCharsetsProvider") | ||
| fun asciiProps(charset: Charset) { | ||
| val output = CleanProperties().also { props -> | ||
| props["key"] = "value" | ||
| props["key2"] = "value2" | ||
| props["a"] = "b" | ||
| props["d"] = "e" | ||
| props["0"] = "1" | ||
| props["b"] = "c" | ||
| props["c"] = "d" | ||
| props["e"] = "f" | ||
| }.writeToString(charset) | ||
|
|
||
| assertThat(output).isEqualTo( | ||
| """ | ||
| |0=1 | ||
| |a=b | ||
| |b=c | ||
| |c=d | ||
| |d=e | ||
| |e=f | ||
| |key=value | ||
| |key2=value2 | ||
| | | ||
| """.trimMargin(), | ||
| ) | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @MethodSource("utfCharsetsProvider") | ||
| fun utfProps(charset: Charset) { | ||
Goooler marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| val output = CleanProperties().also { props -> | ||
| props["äöüß"] = "aouss" | ||
| props["áèô"] = "aeo" | ||
| props["€²³"] = "x" | ||
| props["传傳磨宿说説"] = "b" | ||
| }.writeToString(charset) | ||
|
|
||
| assertThat(output).isEqualTo( | ||
| """ | ||
| |áèô=aeo | ||
| |äöüß=aouss | ||
| |€²³=x | ||
| |传傳磨宿说説=b | ||
| | | ||
| """.trimMargin(), | ||
| ) | ||
| } | ||
|
|
||
| private companion object Companion { | ||
| @JvmStatic | ||
| fun generalCharsetsProvider() = listOf( | ||
| StandardCharsets.ISO_8859_1, | ||
| StandardCharsets.US_ASCII, | ||
| ) + utfCharsetsProvider() | ||
|
|
||
| @JvmStatic | ||
| fun utfCharsetsProvider() = listOf( | ||
| StandardCharsets.UTF_8, | ||
| StandardCharsets.UTF_16, | ||
| ) | ||
|
|
||
| fun CleanProperties.writeToString(charset: Charset): String { | ||
| return ByteArrayOutputStream().also { writeWithoutComments(charset, it) } | ||
| .toString(charset.name()).invariantEolString | ||
| } | ||
| } | ||
| } | ||
9 changes: 9 additions & 0 deletions
9
src/testKit/kotlin/com/github/jengelman/gradle/plugins/shadow/testkit/Strings.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package com.github.jengelman.gradle.plugins.shadow.testkit | ||
|
|
||
| import java.nio.file.FileSystems | ||
|
|
||
| val String.invariantEolString: String get() = replace(System.lineSeparator(), "\n") | ||
|
|
||
| val String.variantSeparatorsPathString: String get() = replace("/", fileSystem.separator) | ||
|
|
||
| private val fileSystem = FileSystems.getDefault() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The old logic is useful for removing date comments only. All comments are removed in this PR. See Goooler#102 (comment).
I'm considering whether we should introduce a new property to opt for the feature in. The new flag will control:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure a flag's worth it.
The comment was always empty (via this).
Sorting vs not-sorting doesn't change the contents.
But we could (or should?) add a comment saying something like
WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right!
Let's keep the current behavior; otherwise, we need to add comments for the property-related transformers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated a test for comments, see #1868.