Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Use Gradle command exit code as hook exit code #551

Merged
merged 2 commits into from
Dec 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/).

### Fixed
- Deleted file causes file not found exception ([issue: #539](https://github.com/JLLeitschuh/ktlint-gradle/issues/539), [#548](https://github.com/JLLeitschuh/ktlint-gradle/pull/548))

- Use Gradle command exit code as hook exit code to ensure un-staged changes are always re-applied to the working directory [#551](https://github.com/JLLeitschuh/ktlint-gradle/pull/551)
### Removed
- ?

Expand Down
12 changes: 7 additions & 5 deletions plugin/src/main/kotlin/org/jlleitschuh/gradle/ktlint/GitHook.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ internal const val FILTER_INCLUDE_PROPERTY_NAME = "internalKtlintGitFilter"
internal val shShebang =
"""
#!/bin/sh
set -e

""".trimIndent()

Expand Down Expand Up @@ -61,6 +60,7 @@ private fun postCheck(
}

internal const val NF = "\$NF"
internal const val gradleCommandExitCode = "\$gradleCommandExitCode"

@Language("Sh")
internal fun generateGitHook(
Expand All @@ -79,25 +79,27 @@ internal fun generateGitHook(

echo "Running ktlint over these files:"
echo "${'$'}CHANGED_FILES"

diff=.git/unstaged-ktlint-git-hook.diff
git diff --color=never > ${'$'}diff
if [ -s ${'$'}diff ]; then
git apply -R ${'$'}diff
fi

${generateGradleCommand(taskName, gradleRootDirPrefix)}
$gradleCommandExitCode=$?

echo "Completed ktlint run."
${postCheck(shouldUpdateCommit)}

if [ -s ${'$'}diff ]; then
git apply --ignore-whitespace ${'$'}diff
fi
rm ${'$'}diff
unset diff

echo "Completed ktlint hook."
exit $gradleCommandExitCode

""".trimIndent()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class GitHookTasksTest : AbstractPluginTest() {
projectPath.initGit()
settingsGradle.appendText(
"""

include ":some-module"
""".trimIndent()
)
Expand Down Expand Up @@ -97,11 +97,11 @@ class GitHookTasksTest : AbstractPluginTest() {
gitDir.preCommitGitHook().writeText(
"""
$shShebang

echo "test1"
$startHookSection


$endHookSection
echo "test2"
""".trimIndent()
Expand All @@ -114,7 +114,7 @@ class GitHookTasksTest : AbstractPluginTest() {
assertThat(hookContent).startsWith(
"""
$shShebang

echo "test1"
""".trimIndent()
)
Expand Down Expand Up @@ -151,6 +151,44 @@ class GitHookTasksTest : AbstractPluginTest() {
}
}

@DisplayName("Collects check run exit code and uses it to indicate check success")
@CommonTest
fun checkUsesGradleExitCode(gradleVersion: GradleVersion) {
// This test ensures that we use the exit code of the check gradle command as the exit code
// of the hook script to indicate success/failure instead of using set -e, because
// that will prevent the saved un-staged changes from being re-applied to the working dir.
// See [#551](https://github.com/JLLeitschuh/ktlint-gradle/pull/551)
project(gradleVersion) {
val gitDir = projectPath.initGit()

build(":$INSTALL_GIT_HOOK_CHECK_TASK") {
assertThat(task(":$INSTALL_GIT_HOOK_CHECK_TASK")?.outcome).isEqualTo(TaskOutcome.SUCCESS)
assertThat(gitDir.preCommitGitHook().readText()).doesNotContain("set -e")
assertThat(gitDir.preCommitGitHook().readText()).contains("gradleCommandExitCode=\$?")
assertThat(gitDir.preCommitGitHook().readText()).contains("exit \$gradleCommandExitCode")
}
}
}

@DisplayName("Collects format run exit code and uses it to indicate format success")
@CommonTest
fun formatUsesGradleExitCode(gradleVersion: GradleVersion) {
// This test ensures that we use the exit code of the format gradle command as the exit code
// of the hook script to indicate success/failure instead of using set -e, because
// that will prevent the saved un-staged changes from being re-applied to the working dir.
// See [#551](https://github.com/JLLeitschuh/ktlint-gradle/pull/551)
project(gradleVersion) {
val gitDir = projectPath.initGit()

build(":$INSTALL_GIT_HOOK_FORMAT_TASK") {
assertThat(task(":$INSTALL_GIT_HOOK_FORMAT_TASK")?.outcome).isEqualTo(TaskOutcome.SUCCESS)
assertThat(gitDir.preCommitGitHook().readText()).doesNotContain("set -e")
assertThat(gitDir.preCommitGitHook().readText()).contains("gradleCommandExitCode=\$?")
assertThat(gitDir.preCommitGitHook().readText()).contains("exit \$gradleCommandExitCode")
}
}
}

@DisplayName("Format hook should include updated files into git commit")
@CommonTest
fun formatIncludeUpdatedFiles(gradleVersion: GradleVersion) {
Expand Down