Skip to content
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

Eliminate trailing double newline in dump #192

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/functionalTest/kotlin/kotlinx/validation/api/TestDsl.kt
Expand Up @@ -27,8 +27,10 @@ internal fun BaseKotlinGradleTest.test(
createNewFile()
}

scope.files.forEach {
scope.files.forEachIndexed { index, it ->
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fact that you can add multiple dump files and have them magically joined is weird. It's only used in one test. That test should specify a combined file and this functionality should be eliminated.

Can do in a follow-up if you agree.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not only about dumps, and I personally find it quite convenient when it comes to constructing Gradle build scripts, like here:

resolve("/examples/gradle/configuration/jarAsInput/inputJar.gradle.kts")

val fileContent = readFileList(it)

if (index > 0) fileWriteTo.appendText("\n")
fileWriteTo.appendText(fileContent)
}
}
Expand Down
Expand Up @@ -132,7 +132,7 @@ internal class DefaultConfigTests : BaseKotlinGradleTest() {

runner.buildAndFail().apply {
val dumpOutput =
" @@ -1,1 +1,7 @@\n" +
" @@ -1,1 +1,6 @@\n" +
" +public final class com/company/BuildConfig {\n" +
" +\tpublic fun <init> ()V\n" +
" +\tpublic final fun function ()I\n" +
Expand Down
Expand Up @@ -3,4 +3,3 @@ public final class org/different/pack/BuildConfig {
public final fun f1 ()I
public final fun getP1 ()I
}

Expand Up @@ -3,4 +3,3 @@ public final class com/company/BuildCon {
public final fun f1 ()I
public final fun getP1 ()I
}

Expand Up @@ -20,4 +20,3 @@ public final class foo/api/ClassInPublicPackage {
public final class foo/api/ClassInPublicPackage$Inner {
public fun <init> ()V
}

1 change: 0 additions & 1 deletion src/functionalTest/resources/examples/classes/JavaLib.dump
Expand Up @@ -9,4 +9,3 @@ public final class org/jetbrains/kotlinx/android/java/library/BuildConfig {
public static final field LIBRARY_PACKAGE_NAME Ljava/lang/String;
public fun <init> ()V
}

Expand Up @@ -9,4 +9,3 @@ public final class org/jetbrains/kotlinx/android/kotlin/library/BuildConfig {
public static final field LIBRARY_PACKAGE_NAME Ljava/lang/String;
public fun <init> ()V
}

Expand Up @@ -2,4 +2,3 @@ public final class mixed/MarkedPublicWithPrivateMembers {
public fun <init> ()V
public final fun otherFun ()V
}

Expand Up @@ -7,4 +7,3 @@ public abstract interface annotation class foo/HiddenField : java/lang/annotatio

public abstract interface annotation class foo/HiddenProperty : java/lang/annotation/Annotation {
}

Expand Up @@ -5,4 +5,3 @@ public final class foo/ClassWithProperties {
public final fun setBar1 (I)V
public final fun setBar2 (I)V
}

Expand Up @@ -2,4 +2,3 @@ public final class com/company/subsub1/Subsub1Class {
public fun <init> ()V
public final fun getProp ()I
}

Expand Up @@ -2,4 +2,3 @@ public final class com/company/subsub2/Subsub2Class {
public fun <init> ()V
public final fun getProp ()I
}

8 changes: 1 addition & 7 deletions src/main/kotlin/KotlinApiBuildTask.kt
Expand Up @@ -107,13 +107,7 @@ public open class KotlinApiBuildTask @Inject constructor(
outputApiDir.resolve("$projectName.api").bufferedWriter().use { writer ->
filteredSignatures
.sortedBy { it.name }
.forEach { api ->
writer.append(api.signature).appendLine(" {")
api.memberSignatures
.sortedWith(MEMBER_SORT_ORDER)
.forEach { writer.append("\t").appendLine(it.signature) }
writer.appendLine("}\n")
}
.dump(writer)
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/main/kotlin/api/KotlinSignaturesLoading.kt
Expand Up @@ -313,13 +313,15 @@ public fun List<ClassBinarySignature>.dump(): PrintStream = dump(to = System.out

@ExternalApi
public fun <T : Appendable> List<ClassBinarySignature>.dump(to: T): T {
forEach { classApi ->
forEachIndexed { index, classApi ->
with(to) {
if (index > 0) appendLine()

append(classApi.signature).appendLine(" {")
classApi.memberSignatures
.sortedWith(MEMBER_SORT_ORDER)
.forEach { append("\t").appendLine(it.signature) }
appendLine("}\n")
appendLine("}")
}
}
return to
Expand Down
1 change: 0 additions & 1 deletion src/test/kotlin/cases/annotations/annotations.txt
@@ -1,4 +1,3 @@
public abstract interface annotation class cases/annotations/Foo : java/lang/annotation/Annotation {
public abstract fun i ()I
}

1 change: 0 additions & 1 deletion src/test/kotlin/cases/companions/companions.txt
Expand Up @@ -113,4 +113,3 @@ public final class cases/companions/PublishedApiClasses$PublishedApiCompanion {

public final class cases/companions/PublishedApiClasses$PublishedApiCompanion$Companion {
}

1 change: 0 additions & 1 deletion src/test/kotlin/cases/default/default.txt
Expand Up @@ -22,4 +22,3 @@ public final class cases/default/PublishedApiWithDefaultsKt {
public static final fun twoDefaults (ILjava/lang/Object;Ljava/lang/String;)V
public static synthetic fun twoDefaults$default (ILjava/lang/Object;Ljava/lang/String;ILjava/lang/Object;)V
}

1 change: 0 additions & 1 deletion src/test/kotlin/cases/enums/enums.txt
Expand Up @@ -18,4 +18,3 @@ public final class cases/enums/JavaEnum : java/lang/Enum {
public static fun valueOf (Ljava/lang/String;)Lcases/enums/JavaEnum;
public static fun values ()[Lcases/enums/JavaEnum;
}

1 change: 0 additions & 1 deletion src/test/kotlin/cases/inline/inline.txt
Expand Up @@ -9,4 +9,3 @@ public final class cases/inline/InternalClassExposed {
public final fun getPropertyExposed ()Ljava/lang/String;
public final fun setPropertyExposed (Ljava/lang/String;)V
}

1 change: 0 additions & 1 deletion src/test/kotlin/cases/interfaces/interfaces.txt
Expand Up @@ -24,4 +24,3 @@ public final class cases/interfaces/DerivedWithoutImpl$DefaultImpls {
public abstract interface class cases/interfaces/EmptyImpls {
public abstract fun getProperty ()Ljava/lang/String;
}

1 change: 0 additions & 1 deletion src/test/kotlin/cases/internal/internal.txt
Expand Up @@ -13,4 +13,3 @@ public final class cases/internal/PublicClassInternalCompanionWithNameShadowing
public final class cases/internal/PublicClassNamedInternalCompanion {
public fun <init> ()V
}

1 change: 0 additions & 1 deletion src/test/kotlin/cases/java/java.txt
Expand Up @@ -3,4 +3,3 @@ public class cases/java/Facade {
public static fun publicMethod (I)V
public static fun publicMethod (Ljava/lang/String;)V
}

1 change: 0 additions & 1 deletion src/test/kotlin/cases/localClasses/localClasses.txt
Expand Up @@ -15,4 +15,3 @@ public final class cases/localClasses/L {
public final class cases/localClasses/LambdasKt {
public static final fun box ()V
}

1 change: 0 additions & 1 deletion src/test/kotlin/cases/marker/marker.txt
Expand Up @@ -10,4 +10,3 @@ public abstract interface annotation class cases/marker/HiddenMethod : java/lang

public abstract interface annotation class cases/marker/HiddenProperty : java/lang/annotation/Annotation {
}

1 change: 0 additions & 1 deletion src/test/kotlin/cases/nestedClasses/nestedClasses.txt
Expand Up @@ -83,4 +83,3 @@ protected abstract interface class cases/nestedClasses/PublicOpenClass$NestedPro
protected final class cases/nestedClasses/PublicOpenClass$ObjProtected {
public static final field INSTANCE Lcases/nestedClasses/PublicOpenClass$ObjProtected;
}

Expand Up @@ -5,4 +5,3 @@ public final class cases/packageAnnotations/b/b/ShouldRemainPublic {
public final class cases/packageAnnotations/b/c/ShouldNotBeRemoved {
public fun <init> ()V
}

1 change: 0 additions & 1 deletion src/test/kotlin/cases/protected/protected.txt
Expand Up @@ -16,4 +16,3 @@ public class cases/protected/PublicOpenClass {
protected final fun protectedFun ()I
protected final fun setProtectedVar (I)V
}

1 change: 0 additions & 1 deletion src/test/kotlin/cases/public/public.txt
Expand Up @@ -6,4 +6,3 @@ public final class cases/public/MultifileKt {
public final class cases/public/PublicPartKt {
public static final fun publicFun ()V
}

1 change: 0 additions & 1 deletion src/test/kotlin/cases/special/special.txt
Expand Up @@ -46,4 +46,3 @@ public final class cases/special/JvmNamesKt {
public static final fun publicVarGetter ()I
public static final fun publicVarSetter (I)V
}

1 change: 0 additions & 1 deletion src/test/kotlin/cases/whenMappings/whenMappings.txt
Expand Up @@ -31,4 +31,3 @@ public final class cases/whenMappings/SealedClassWhenKt {
public static final fun deacronimize (Lcases/whenMappings/SampleSealed;)Ljava/lang/String;
public static final fun switch (Lcases/whenMappings/SampleSealed;Lkotlin/jvm/functions/Function0;Lkotlin/jvm/functions/Function0;Lkotlin/jvm/functions/Function0;)V
}

12 changes: 3 additions & 9 deletions src/test/kotlin/tests/utils.kt
Expand Up @@ -18,14 +18,13 @@ fun List<ClassBinarySignature>.dumpAndCompareWith(to: File) {
to.bufferedWriter().use { dump(to = it) }
fail("Expected data file did not exist. Generating: $to")
} else {
val actual = dump(to = StringBuilder())
val actual = dump(to = StringBuilder()).toString()
assertEqualsToFile(to, actual)
}
}

private fun assertEqualsToFile(expectedFile: File, actual: CharSequence) {
val actualText = actual.trimTrailingWhitespacesAndAddNewlineAtEOF()
val expectedText = expectedFile.readText().trimTrailingWhitespacesAndAddNewlineAtEOF()
private fun assertEqualsToFile(expectedFile: File, actualText: String) {
val expectedText = expectedFile.readText()

if (OVERWRITE_EXPECTED_OUTPUT && expectedText != actualText) {
expectedFile.writeText(actualText)
Expand All @@ -34,8 +33,3 @@ private fun assertEqualsToFile(expectedFile: File, actual: CharSequence) {

assertEquals(expectedText, actualText, "Actual data differs from file content: ${expectedFile.name}\nTo overwrite the expected API rerun with -Doverwrite.output=true parameter\n")
}

private fun CharSequence.trimTrailingWhitespacesAndAddNewlineAtEOF(): String =
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another testing oddity. This fundamentally undermines the ability to validate the textual representation of the dump and prevented actually asserting against the behavior change in this PR.

The presence or absence of trailing whitespace on a line and the presence or absence of trailing newlines are part of the output and should be validated as such.

this.lineSequence().map { it.trimEnd() }.joinToString(separator = "\n").let {
if (it.endsWith("\n")) it else it + "\n"
}