Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

(#8) Fix relative paths handling #13

Merged
merged 4 commits into from
Nov 26, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class HooksFolder(git: Git) {
= hooks.isEmpty()

init {
path = Path.of(git.getGlobalHooksPath())
path = git.getGlobalHooksPath()

val files = try {
Files.list(path)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,31 @@
package com.github.y0ung3r.gitglobalhookslocator.git.extensions

import com.github.y0ung3r.gitglobalhookslocator.git.Git
import com.github.y0ung3r.gitglobalhookslocator.git.utils.SystemPathUtils
import java.nio.file.Path

fun Git.getGlobalHooksPath(): String {
val path = executeCommand(
private const val SLASHES_PATTERN = "[/\\\\]"
private const val HOME_PATTERN = "^~$SLASHES_PATTERN"
private const val CURRENT_DIR_PATTERN = "^.$SLASHES_PATTERN"

fun Git.getGlobalHooksPath(): Path {
val rawPath = executeCommand(
Git.GIT_CONFIG_COMMAND,
Git.GIT_GLOBAL_COMMAND,
Git.GIT_CONFIG_GET_COMMAND,
Git.GIT_HOOKS_PATH_SECTION
)

return path.value
val targetPath = rawPath.value
.replaceFirst(
Regex(HOME_PATTERN),
SystemPathUtils.getUserHomePath()
)
.replaceFirst(
Regex(CURRENT_DIR_PATTERN),
SystemPathUtils.getCurrentDirectoryPath()
)

return Path.of(targetPath)
.toAbsolutePath()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.github.y0ung3r.gitglobalhookslocator.git.utils

object SystemPathUtils {
private const val USER_HOME_KEY = "user.home"
private const val USER_DIR_KEY = "user.dir"

@JvmStatic
private fun getSystemPath(key: String): String {
val systemPath = System
.getProperty(key)
.replace("\\", "/") // The replacement eliminates the need to escape the string

return "$systemPath/"
}

@JvmStatic
fun getUserHomePath(): String
= getSystemPath(USER_HOME_KEY)

@JvmStatic
fun getCurrentDirectoryPath(): String
= getSystemPath(USER_DIR_KEY)
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ import com.intellij.ide.wizard.withVisualPadding
import com.intellij.openapi.wm.ToolWindow
import com.intellij.ui.components.JBCheckBox
import com.intellij.ui.dsl.builder.Align
import com.intellij.ui.dsl.builder.LabelPosition
import com.intellij.ui.dsl.builder.panel
import com.intellij.ui.dsl.gridLayout.HorizontalAlign
import com.intellij.ui.dsl.gridLayout.VerticalAlign
import javax.swing.JComponent

class GitGlobalHooksLocatorWindow(toolWindow: ToolWindow) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,70 @@ import com.github.y0ung3r.gitglobalhookslocator.gitTests.testEngine.RespondInter
import com.github.y0ung3r.gitglobalhookslocator.git.Git
import com.github.y0ung3r.gitglobalhookslocator.git.cli.CliResponse
import com.github.y0ung3r.gitglobalhookslocator.git.extensions.getGlobalHooksPath
import com.github.y0ung3r.gitglobalhookslocator.git.utils.SystemPathUtils
import org.junit.Assert.assertEquals
import org.junit.Test
import java.nio.file.Path

class GetGlobalHooksPathTests {
@Test
fun `Should returns global hooks path`() {
fun `Should returns absolute global hooks path using relative path`() {
// Arrange
val expectedPath = "~/.git/hooks"
val relativePath = "/.git/hooks"
val expectedPath = Path
.of(relativePath)
.toAbsolutePath()

val sut = Git(
RespondInterchangeably(
CliResponse(Git.minRequiredVersion.toString()),
CliResponse(relativePath)
)
)

// Act
val actualPath = sut.getGlobalHooksPath()

// Assert
assertEquals(expectedPath, actualPath)
}

@Test
fun `Should resolve path using $HOME variable`() {
// Arrange
val expectedPath = Path.of(
SystemPathUtils.getUserHomePath(),
".git",
"hooks"
)

val sut = Git(
RespondInterchangeably(
CliResponse(Git.minRequiredVersion.toString()),
CliResponse("~/.git/hooks")
)
)

// Act
val actualPath = sut.getGlobalHooksPath()

// Assert
assertEquals(expectedPath, actualPath)
}

@Test
fun `Should resolve path using $CURRENTDIR variable`() {
// Arrange
val expectedPath = Path.of(
SystemPathUtils.getCurrentDirectoryPath(),
".git",
"hooks"
)

val sut = Git(
RespondInterchangeably(
CliResponse(Git.minRequiredVersion.toString()),
CliResponse(expectedPath)
CliResponse("./.git/hooks")
)
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package com.github.y0ung3r.gitglobalhookslocator.gitTests

import com.github.y0ung3r.gitglobalhookslocator.git.SemanticVersion
import com.github.y0ung3r.gitglobalhookslocator.git.exceptions.ProvidedSemanticVersionIsInvalidException
import org.junit.Assert.assertEquals
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.Parameterized
import org.junit.runners.Parameterized.Parameters

//@RunWith(Parameterized::class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import java.nio.file.Path

abstract class HookTestBase {
companion object {
const val BASE_PATH = "src/test/testData/hooks"
private const val BASE_PATH = "src/test/testData/hooks"
private const val DISABLED_HOOK = "disabled"
private const val ENABLED_HOOK = "enabled"

Expand Down
1 change: 1 addition & 0 deletions src/test/testData/description.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The current folder is used to generate test data
3 changes: 0 additions & 3 deletions src/test/testData/rename/foo.xml

This file was deleted.

3 changes: 0 additions & 3 deletions src/test/testData/rename/foo_after.xml

This file was deleted.

Loading