Skip to content

Commit cfc1aa1

Browse files
committed
Add unit tests for subdirectory pre-commit hook path handling
Added four new test cases to verify the fix for pre-commit hook path handling when Gradle projects are in subdirectories: 1. checkHookUsesRelativeFlagInSubdirectory: Verifies that check hooks use --relative flag when Gradle is in a subdirectory 2. formatHookUsesRelativeFlagInSubdirectory: Verifies that format hooks use --relative flag and correctly prefix file paths in git add commands 3. checkHookDoesNotUseRelativeFlagAtGitRoot: Verifies that check hooks do not use --relative flag when Gradle is at git root 4. formatHookDoesNotUseRelativeFlagAtGitRoot: Verifies that format hooks do not use --relative flag or path prefixes when Gradle is at git root
1 parent ecbfb0e commit cfc1aa1

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

plugin/src/test/kotlin/org/jlleitschuh/gradle/ktlint/GitHookTasksTest.kt

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,75 @@ class GitHookTasksTest : AbstractPluginTest() {
153153
}
154154
}
155155

156+
@DisplayName("Check hook should use --relative flag when Gradle project is in subdirectory")
157+
@CommonTest
158+
fun checkHookUsesRelativeFlagInSubdirectory(gradleVersion: GradleVersion) {
159+
val gradleRoot = projectRoot.resolve("project/submodule/").also { it.mkdirs() }
160+
val gitDir = projectRoot.initGit()
161+
162+
project(gradleVersion, projectPath = gradleRoot) {
163+
build(":$INSTALL_GIT_HOOK_CHECK_TASK") {
164+
assertThat(task(":$INSTALL_GIT_HOOK_CHECK_TASK")?.outcome).isEqualTo(TaskOutcome.SUCCESS)
165+
val hookText = gitDir.preCommitGitHook().readText()
166+
assertThat(hookText).contains("--relative=project/submodule")
167+
assertThat(hookText).contains("-- project/submodule/")
168+
}
169+
}
170+
}
171+
172+
@DisplayName("Format hook should use --relative flag and correct file paths when Gradle project is in subdirectory")
173+
@CommonTest
174+
fun formatHookUsesRelativeFlagInSubdirectory(gradleVersion: GradleVersion) {
175+
val gradleRoot = projectRoot.resolve("project/submodule/").also { it.mkdirs() }
176+
val gitDir = projectRoot.initGit()
177+
178+
project(gradleVersion, projectPath = gradleRoot) {
179+
build(":$INSTALL_GIT_HOOK_FORMAT_TASK") {
180+
assertThat(task(":$INSTALL_GIT_HOOK_FORMAT_TASK")?.outcome).isEqualTo(TaskOutcome.SUCCESS)
181+
val hookText = gitDir.preCommitGitHook().readText()
182+
// Should use --relative flag for git diff
183+
assertThat(hookText).contains("--relative=project/submodule")
184+
assertThat(hookText).contains("-- project/submodule/")
185+
// Should prefix file paths in git add command
186+
assertThat(hookText).contains("if [ -f project/submodule/\$file ]; then")
187+
assertThat(hookText).contains("git add project/submodule/\$file")
188+
}
189+
}
190+
}
191+
192+
@DisplayName("Check hook should not use --relative flag when Gradle project is at git root")
193+
@CommonTest
194+
fun checkHookDoesNotUseRelativeFlagAtGitRoot(gradleVersion: GradleVersion) {
195+
project(gradleVersion) {
196+
val gitDir = projectPath.initGit()
197+
198+
build(":$INSTALL_GIT_HOOK_CHECK_TASK") {
199+
assertThat(task(":$INSTALL_GIT_HOOK_CHECK_TASK")?.outcome).isEqualTo(TaskOutcome.SUCCESS)
200+
val hookText = gitDir.preCommitGitHook().readText()
201+
// Should NOT use --relative flag when at git root
202+
assertThat(hookText).doesNotContain("--relative=")
203+
}
204+
}
205+
}
206+
207+
@DisplayName("Format hook should not use --relative flag when Gradle project is at git root")
208+
@CommonTest
209+
fun formatHookDoesNotUseRelativeFlagAtGitRoot(gradleVersion: GradleVersion) {
210+
project(gradleVersion) {
211+
val gitDir = projectPath.initGit()
212+
213+
build(":$INSTALL_GIT_HOOK_FORMAT_TASK") {
214+
assertThat(task(":$INSTALL_GIT_HOOK_FORMAT_TASK")?.outcome).isEqualTo(TaskOutcome.SUCCESS)
215+
val hookText = gitDir.preCommitGitHook().readText()
216+
// Should NOT use --relative flag when at git root
217+
assertThat(hookText).doesNotContain("--relative=")
218+
// Should NOT prefix file paths
219+
assertThat(hookText).contains("if [ -f \$file ]; then")
220+
assertThat(hookText).doesNotContain("if [ -f /\$file ]; then")
221+
}
222+
}
223+
}
224+
156225
@DisplayName("Check hook should not include files into git commit")
157226
@CommonTest
158227
fun checkHookShouldNotIncludeFilesIntoGitCommit(gradleVersion: GradleVersion) {

0 commit comments

Comments
 (0)