Fix failed tests on Windows
This fixes failed tests on Windows due to different file path format.
On Linux/Mac the path to a source file could be `/foo/bar/Baz.kt`,
while on Windows it could be `C:\foo\baz\Baz.kt`.
To verify the fix we extracted room-compiler-processing and
room-compiler-processing-testing into a standalone project,
removed AndroidX plugin, and made it build on Windows.
Test: Passed existing tests with the manually created project on Windows
Bug: b/230830024
Change-Id: Ia2640f30a67bff2df3388d23b0684358de125024
diff --git a/room/room-compiler-processing-testing/src/main/java/androidx/room/compiler/processing/util/Source.kt b/room/room-compiler-processing-testing/src/main/java/androidx/room/compiler/processing/util/Source.kt
index b800570..c62cfab 100644
--- a/room/room-compiler-processing-testing/src/main/java/androidx/room/compiler/processing/util/Source.kt
+++ b/room/room-compiler-processing-testing/src/main/java/androidx/room/compiler/processing/util/Source.kt
@@ -61,7 +61,7 @@
}
override val relativePath
- get() = qName.replace(".", "/") + ".java"
+ get() = qName.replace(".", File.separator) + ".java"
}
class KotlinSource(
diff --git a/room/room-compiler-processing-testing/src/main/java/androidx/room/compiler/processing/util/compiler/SourceSet.kt b/room/room-compiler-processing-testing/src/main/java/androidx/room/compiler/processing/util/compiler/SourceSet.kt
index f83b60c..39a87cd 100644
--- a/room/room-compiler-processing-testing/src/main/java/androidx/room/compiler/processing/util/compiler/SourceSet.kt
+++ b/room/room-compiler-processing-testing/src/main/java/androidx/room/compiler/processing/util/compiler/SourceSet.kt
@@ -68,7 +68,7 @@
if (!file.path.startsWith(root.path)) {
return null
}
- val relativePath = path.substringAfter(root.canonicalPath + "/")
+ val relativePath = path.substringAfter(root.canonicalPath + File.separator)
return sources.firstOrNull {
it.relativePath == relativePath
}
@@ -98,7 +98,7 @@
"java" -> Source.loadJavaSource(
file = file,
qName = file.relativeTo(root).path
- .replace('/', '.')
+ .replace(File.separatorChar, '.')
.substringBeforeLast('.') // drop .java
)
"kt" -> Source.loadKotlinSource(
diff --git a/room/room-compiler-processing-testing/src/test/java/androidx/room/compiler/processing/util/DiagnosticMessageCollectorTest.kt b/room/room-compiler-processing-testing/src/test/java/androidx/room/compiler/processing/util/DiagnosticMessageCollectorTest.kt
index e62ba38..ed8c35c 100644
--- a/room/room-compiler-processing-testing/src/test/java/androidx/room/compiler/processing/util/DiagnosticMessageCollectorTest.kt
+++ b/room/room-compiler-processing-testing/src/test/java/androidx/room/compiler/processing/util/DiagnosticMessageCollectorTest.kt
@@ -187,6 +187,19 @@
location = null
)
),
+ // ksp kotlin on Windows
+ TestParams(
+ message = "[ksp] C:\\foo\\bar\\Subject.kt:3: the real message",
+ severity = CompilerMessageSeverity.ERROR,
+ expected = RawDiagnosticMessage(
+ kind = Diagnostic.Kind.ERROR,
+ message = "the real message",
+ location = Location(
+ path = "C:\\foo\\bar\\Subject.kt",
+ line = 3
+ )
+ )
+ ),
)
}
}
\ No newline at end of file
diff --git a/room/room-compiler-processing-testing/src/test/java/androidx/room/compiler/processing/util/GeneratedCodeMatchTest.kt b/room/room-compiler-processing-testing/src/test/java/androidx/room/compiler/processing/util/GeneratedCodeMatchTest.kt
index 08b0e61..5cebcec 100644
--- a/room/room-compiler-processing-testing/src/test/java/androidx/room/compiler/processing/util/GeneratedCodeMatchTest.kt
+++ b/room/room-compiler-processing-testing/src/test/java/androidx/room/compiler/processing/util/GeneratedCodeMatchTest.kt
@@ -24,6 +24,7 @@
import com.squareup.kotlinpoet.BOOLEAN
import com.squareup.kotlinpoet.FileSpec
import com.squareup.kotlinpoet.TypeSpec as KTypeSpec
+import java.io.File
import org.junit.Test
import org.junit.AssumptionViolatedException
import org.junit.runner.RunWith
@@ -73,7 +74,7 @@
}
assertThat(result.exceptionOrNull())
.hasMessageThat()
- .contains("Didn't generate SourceFile[foo/bar/Baz.java]")
+ .contains("Didn't generate SourceFile[${combine("foo", "bar", "Baz.java")}]")
}
@Test
@@ -132,7 +133,7 @@
}
invocation.assertCompilationResult {
generatedSource(
- Source.kotlin("foo/bar/Baz.kt", file.toString())
+ Source.kotlin(combine("foo", "bar", "Baz.kt"), file.toString())
)
}
}
@@ -165,7 +166,7 @@
}
invocation.assertCompilationResult {
generatedSource(
- Source.kotlin("foo/bar/Baz.kt", expected.toString())
+ Source.kotlin(combine("foo", "bar", "Baz.kt"), expected.toString())
)
}
}
@@ -216,3 +217,6 @@
)
}
}
+
+private fun combine(vararg elements: String): String =
+ elements.joinToString(separator = File.separator)