Skip to content

Commit

Permalink
use try catch clause for more accurate relative path check.
Browse files Browse the repository at this point in the history
  • Loading branch information
neetopia committed Oct 12, 2022
1 parent e011a24 commit 063a0fb
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
Expand Up @@ -156,11 +156,13 @@ class CodeGeneratorImpl(
}

private val File.relativeFile: File
get() =
if (this.startsWith(buildDir))
get() {
val buildDirPrefix = if (buildDir.path.startsWith("/")) buildDir.path else buildDir.path.replace("\\", "/")
return if (this.startsWith(buildDirPrefix))
relativeTo(buildDir)
else
relativeTo(projectBase)
}

private fun associate(sources: List<KSFile>, outputPath: File) {
if (!isIncremental)
Expand Down
Expand Up @@ -230,11 +230,14 @@ class IncrementalContext(

// Ugly, but better than copying the private logics out of stdlib.
// TODO: get rid of `relativeTo` if possible.
private fun String.toRelativeFile(): File =
if (this.startsWith(buildDir.path))
private fun String.toRelativeFile(): File {
val buildDirPrefix = if (buildDir.path.startsWith("/")) buildDir.path else buildDir.path.replace("\\", "/")
return if (this.startsWith(buildDirPrefix)) {
File(this).relativeTo(buildDir)
else
} else {
File(this).relativeTo(baseDir)
}
}

private val KSFile.relativeFile
get() = filePath.toRelativeFile()
Expand Down
Expand Up @@ -65,6 +65,28 @@ class PlaygroundIT {
project.restore("workload/build.gradle.kts")
}

@Test
fun testArbitraryBuildDir() {
Assume.assumeTrue(System.getProperty("os.name").startsWith("Windows", ignoreCase = true))
val gradleRunner = GradleRunner.create().withProjectDir(project.root)

File(project.root, "workload/build.gradle.kts")
.appendText("project.buildDir = File(\"D:/build/\")")
val result = gradleRunner.withArguments("build").build()

Assert.assertEquals(TaskOutcome.SUCCESS, result.task(":workload:build")?.outcome)

val artifact = File("D:/build/libs/workload-1.0-SNAPSHOT.jar")
Assert.assertTrue(artifact.exists())

JarFile(artifact).use { jarFile ->
Assert.assertTrue(jarFile.getEntry("TestProcessor.log").size > 0)
Assert.assertTrue(jarFile.getEntry("hello/HELLO.class").size > 0)
Assert.assertTrue(jarFile.getEntry("g/G.class").size > 0)
Assert.assertTrue(jarFile.getEntry("com/example/AClassBuilder.class").size > 0)
}
}

@Test
fun testAllowSourcesFromOtherPlugins() {
fun checkGBuilder() {
Expand Down

0 comments on commit 063a0fb

Please sign in to comment.