Add unit tests for BuildCodegenCLITask

Summary:
This Diff is adding some tests for BuildCodegenCLITask.
Plus I found a bug in how we pass the input/output relative to `codegenDir`
so I'm fixing the properties to use `by lazy{}`.

Moreover the `output` was not correctly annotated with `OutputDirectories`,
fixing it here.

Changelog:
[Internal] [Changed] - Add unit tests for BuildCodegenCLITask

Reviewed By: ShikaSD

Differential Revision: D31109599

fbshipit-source-id: bec75b216e8cef18072179c89c3223ee2bad74e3
This commit is contained in:
Nicola Corti 2021-09-24 02:11:05 -07:00 коммит произвёл Facebook GitHub Bot
Родитель 929cb56f22
Коммит 0d9b32769c
3 изменённых файлов: 105 добавлений и 3 удалений

Просмотреть файл

@ -27,10 +27,12 @@ abstract class BuildCodegenCLITask : Exec() {
@get:Internal abstract val bashWindowsHome: Property<String>
@get:InputFiles
val input: FileCollection =
codegenDir.files("scripts", "src", "package.json", ".babelrc", ".prettierrc")
val input: FileCollection by lazy {
codegenDir.get().files("scripts", "src", "package.json", ".babelrc", ".prettierrc")
}
@get:OutputFiles val output: FileCollection = codegenDir.files("lib", "node_modules")
@get:OutputDirectories
val output: FileCollection by lazy { codegenDir.get().files("lib", "node_modules") }
init {
// We need this condition as we want a single instance of BuildCodegenCLITask to execute

Просмотреть файл

@ -0,0 +1,75 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package com.facebook.react.tasks
import com.facebook.react.tests.createTestTask
import java.io.File
import org.gradle.api.tasks.*
import org.junit.Assert.*
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder
class BuildCodegenCLITaskTest {
@get:Rule val tempFolder = TemporaryFolder()
@Test
fun buildCodegenCli_input_isSetCorrectly() {
val task = createTestTask<BuildCodegenCLITask> { it.codegenDir.set(tempFolder.root) }
assertTrue(task.input.contains(File(tempFolder.root, "scripts")))
assertTrue(task.input.contains(File(tempFolder.root, "src")))
assertTrue(task.input.contains(File(tempFolder.root, "package.json")))
assertTrue(task.input.contains(File(tempFolder.root, ".babelrc")))
assertTrue(task.input.contains(File(tempFolder.root, ".prettierrc")))
}
@Test
fun buildCodegenCli_output_isSetCorrectly() {
val task = createTestTask<BuildCodegenCLITask> { it.codegenDir.set(tempFolder.root) }
assertTrue(task.output.contains(File(tempFolder.root, "lib")))
assertTrue(task.output.contains(File(tempFolder.root, "node_modules")))
}
@Test
fun buildCodegenCli_bashWindowsHome_isSetCorrectly() {
val bashPath = tempFolder.newFile("bash").absolutePath
val task = createTestTask<BuildCodegenCLITask> { it.bashWindowsHome.set(bashPath) }
assertEquals(bashPath, task.bashWindowsHome.get())
}
@Test
fun buildCodegenCli_onlyIf_withMissingDirectory_isSatisfied() {
File(tempFolder.root, "lib/cli/").apply { mkdirs() }
val task = createTestTask<BuildCodegenCLITask> { it.codegenDir.set(tempFolder.root) }
assertTrue(task.onlyIf.isSatisfiedBy(task))
}
@Test
fun buildCodegenCli_onlyIf_withEmptyDirectory_isSatisfied() {
File(tempFolder.root, "lib/cli/").apply { mkdirs() }
val task = createTestTask<BuildCodegenCLITask> { it.codegenDir.set(tempFolder.root) }
assertTrue(task.onlyIf.isSatisfiedBy(task))
}
@Test
fun buildCodegenCli_onlyIf_withExistingDirtyDirectory_isNotSatisfied() {
File(tempFolder.root, "lib/cli/a-file").apply {
parentFile.mkdirs()
writeText("¯\\_(ツ)_/¯")
}
val task = createTestTask<BuildCodegenCLITask> { it.codegenDir.set(tempFolder.root) }
assertFalse(task.onlyIf.isSatisfiedBy(task))
}
}

Просмотреть файл

@ -0,0 +1,25 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package com.facebook.react.tests
import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.testfixtures.ProjectBuilder
internal fun createProject(): Project {
with(ProjectBuilder.builder().build()) {
plugins.apply("com.facebook.react")
return this
}
}
internal inline fun <reified T : Task> createTestTask(
project: Project = createProject(),
taskName: String = T::class.java.simpleName,
crossinline block: (T) -> Unit = {}
): T = project.tasks.register(taskName, T::class.java) { block(it) }.get()