fix(gradle): make ci inputs same as test inputs (#31198)
<!-- Please make sure you have read the submission guidelines before posting an PR --> <!-- https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr --> <!-- Please make sure that your commit message follows our format --> <!-- Example: `fix(nx): must begin with lowercase` --> <!-- If this is a particularly complex change or feature addition, you can request a dedicated Nx release for this pull request branch. Mention someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they will confirm if the PR warrants its own release for testing purposes, and generate it for you if appropriate. --> ## Current Behavior <!-- This is the behavior we have today --> current ci test target does not contain inputs of test, it just contains the test source file ## Expected Behavior <!-- This is the behavior we should expect with the changes in this PR --> change the ci targets to contain inputs of test <img width="903" alt="Screenshot 2025-05-13 at 1 31 22 PM" src="https://github.com/user-attachments/assets/d73de232-440a-439b-adb8-e22fc3fe3d8a" /> ## Related Issue(s) <!-- Please link the issue being fixed so it gets closed when this is merged. --> Fixes #
This commit is contained in:
parent
514cdd890d
commit
e15e2ed106
@ -88,8 +88,10 @@ private fun buildTestCiTarget(
|
|||||||
testFile: File,
|
testFile: File,
|
||||||
testTask: Task,
|
testTask: Task,
|
||||||
projectRoot: String,
|
projectRoot: String,
|
||||||
workspaceRoot: String
|
workspaceRoot: String,
|
||||||
): MutableMap<String, Any?> {
|
): MutableMap<String, Any?> {
|
||||||
|
val taskInputs = getInputsForTask(testTask, projectRoot, workspaceRoot, null)
|
||||||
|
|
||||||
val target =
|
val target =
|
||||||
mutableMapOf<String, Any?>(
|
mutableMapOf<String, Any?>(
|
||||||
"executor" to "@nx/gradle:gradle",
|
"executor" to "@nx/gradle:gradle",
|
||||||
@ -100,7 +102,7 @@ private fun buildTestCiTarget(
|
|||||||
"metadata" to
|
"metadata" to
|
||||||
getMetadata("Runs Gradle test $testClassName in CI", projectBuildPath, "test"),
|
getMetadata("Runs Gradle test $testClassName in CI", projectBuildPath, "test"),
|
||||||
"cache" to true,
|
"cache" to true,
|
||||||
"inputs" to arrayOf(replaceRootInPath(testFile.path, projectRoot, workspaceRoot)))
|
"inputs" to taskInputs)
|
||||||
|
|
||||||
getDependsOnForTask(testTask, null)
|
getDependsOnForTask(testTask, null)
|
||||||
?.takeIf { it.isNotEmpty() }
|
?.takeIf { it.isNotEmpty() }
|
||||||
@ -115,7 +117,6 @@ private fun buildTestCiTarget(
|
|||||||
testTask.logger.info("${testTask.path}: found ${it.size} outputs entries")
|
testTask.logger.info("${testTask.path}: found ${it.size} outputs entries")
|
||||||
target["outputs"] = it
|
target["outputs"] = it
|
||||||
}
|
}
|
||||||
|
|
||||||
return target
|
return target
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -93,7 +93,7 @@ fun getInputsForTask(
|
|||||||
val mappedInputsIncludeExternal: MutableList<Any> = mutableListOf()
|
val mappedInputsIncludeExternal: MutableList<Any> = mutableListOf()
|
||||||
val inputs = task.inputs
|
val inputs = task.inputs
|
||||||
val externalDependencies = mutableListOf<String>()
|
val externalDependencies = mutableListOf<String>()
|
||||||
inputs.sourceFiles.forEach { file ->
|
inputs.files.forEach { file ->
|
||||||
val path: String = file.path
|
val path: String = file.path
|
||||||
// replace the absolute path to contain {projectRoot} or {workspaceRoot}
|
// replace the absolute path to contain {projectRoot} or {workspaceRoot}
|
||||||
val pathWithReplacedRoot = replaceRootInPath(path, projectRoot, workspaceRoot)
|
val pathWithReplacedRoot = replaceRootInPath(path, projectRoot, workspaceRoot)
|
||||||
@ -101,8 +101,7 @@ fun getInputsForTask(
|
|||||||
mappedInputsIncludeExternal.add((pathWithReplacedRoot))
|
mappedInputsIncludeExternal.add((pathWithReplacedRoot))
|
||||||
}
|
}
|
||||||
// if the path is outside of workspace
|
// if the path is outside of workspace
|
||||||
if (pathWithReplacedRoot == null &&
|
if (pathWithReplacedRoot == null) { // add it to external dependencies
|
||||||
externalNodes != null) { // add it to external dependencies
|
|
||||||
try {
|
try {
|
||||||
val externalDep = getExternalDepFromInputFile(path, externalNodes, task.logger)
|
val externalDep = getExternalDepFromInputFile(path, externalNodes, task.logger)
|
||||||
externalDep?.let { externalDependencies.add(it) }
|
externalDep?.let { externalDependencies.add(it) }
|
||||||
@ -251,7 +250,7 @@ fun getMetadata(
|
|||||||
*/
|
*/
|
||||||
fun getExternalDepFromInputFile(
|
fun getExternalDepFromInputFile(
|
||||||
inputFile: String,
|
inputFile: String,
|
||||||
externalNodes: MutableMap<String, ExternalNode>,
|
externalNodes: MutableMap<String, ExternalNode>?,
|
||||||
logger: org.gradle.api.logging.Logger
|
logger: org.gradle.api.logging.Logger
|
||||||
): String? {
|
): String? {
|
||||||
try {
|
try {
|
||||||
@ -279,7 +278,9 @@ fun getExternalDepFromInputFile(
|
|||||||
val externalKey = "gradle:$nameKey"
|
val externalKey = "gradle:$nameKey"
|
||||||
val node = ExternalNode("gradle", externalKey, data)
|
val node = ExternalNode("gradle", externalKey, data)
|
||||||
|
|
||||||
externalNodes[externalKey] = node
|
if (externalNodes != null) {
|
||||||
|
externalNodes[externalKey] = node
|
||||||
|
}
|
||||||
|
|
||||||
return externalKey
|
return externalKey
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
|
|||||||
@ -41,6 +41,7 @@ class AddTestCiTargetsTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
val testFiles = project.files(testFile1, testFile2)
|
val testFiles = project.files(testFile1, testFile2)
|
||||||
|
testTask.inputs.files(testFiles)
|
||||||
|
|
||||||
val targets = mutableMapOf<String, MutableMap<String, Any?>>()
|
val targets = mutableMapOf<String, MutableMap<String, Any?>>()
|
||||||
val targetGroups = mutableMapOf<String, MutableList<String>>()
|
val targetGroups = mutableMapOf<String, MutableList<String>>()
|
||||||
@ -76,7 +77,19 @@ class AddTestCiTargetsTest {
|
|||||||
val firstTarget = targets["ci--MyFirstTest"]!!
|
val firstTarget = targets["ci--MyFirstTest"]!!
|
||||||
assertEquals(firstTarget["executor"], "@nx/gradle:gradle")
|
assertEquals(firstTarget["executor"], "@nx/gradle:gradle")
|
||||||
assertEquals(true, firstTarget["cache"])
|
assertEquals(true, firstTarget["cache"])
|
||||||
assertTrue((firstTarget["inputs"] as Array<*>)[0].toString().contains("{projectRoot}"))
|
|
||||||
|
val actualInputs =
|
||||||
|
firstTarget["inputs"] as? Collection<*> ?: error("Missing or invalid 'inputs' field")
|
||||||
|
val actualInputStrings = actualInputs.map { it.toString() }
|
||||||
|
|
||||||
|
testFiles.forEach { file ->
|
||||||
|
val expectedInput =
|
||||||
|
replaceRootInPath(file.path, projectRoot.absolutePath, workspaceRoot.absolutePath)
|
||||||
|
assertTrue(
|
||||||
|
expectedInput in actualInputStrings,
|
||||||
|
"Expected input '$expectedInput' not found in actual inputs: $actualInputStrings")
|
||||||
|
}
|
||||||
|
|
||||||
assertEquals("nx:noop", parentCi["executor"])
|
assertEquals("nx:noop", parentCi["executor"])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user