diff --git a/src/main/kotlin/com/github/y0ung3r/gitglobalhookslocator/git/HooksFolder.kt b/src/main/kotlin/com/github/y0ung3r/gitglobalhookslocator/git/HooksFolder.kt index ac2a431..9a5c6f9 100644 --- a/src/main/kotlin/com/github/y0ung3r/gitglobalhookslocator/git/HooksFolder.kt +++ b/src/main/kotlin/com/github/y0ung3r/gitglobalhookslocator/git/HooksFolder.kt @@ -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) diff --git a/src/main/kotlin/com/github/y0ung3r/gitglobalhookslocator/git/extensions/GitExtensions.kt b/src/main/kotlin/com/github/y0ung3r/gitglobalhookslocator/git/extensions/GitExtensions.kt index aaafdf8..262da5f 100644 --- a/src/main/kotlin/com/github/y0ung3r/gitglobalhookslocator/git/extensions/GitExtensions.kt +++ b/src/main/kotlin/com/github/y0ung3r/gitglobalhookslocator/git/extensions/GitExtensions.kt @@ -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() } \ No newline at end of file diff --git a/src/main/kotlin/com/github/y0ung3r/gitglobalhookslocator/git/utils/SystemPathUtils.kt b/src/main/kotlin/com/github/y0ung3r/gitglobalhookslocator/git/utils/SystemPathUtils.kt new file mode 100644 index 0000000..fee9695 --- /dev/null +++ b/src/main/kotlin/com/github/y0ung3r/gitglobalhookslocator/git/utils/SystemPathUtils.kt @@ -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) +} diff --git a/src/main/kotlin/com/github/y0ung3r/gitglobalhookslocator/ui/toolWindow/GitGlobalHooksLocatorWindow.kt b/src/main/kotlin/com/github/y0ung3r/gitglobalhookslocator/ui/toolWindow/GitGlobalHooksLocatorWindow.kt index 731fbff..f34b0aa 100644 --- a/src/main/kotlin/com/github/y0ung3r/gitglobalhookslocator/ui/toolWindow/GitGlobalHooksLocatorWindow.kt +++ b/src/main/kotlin/com/github/y0ung3r/gitglobalhookslocator/ui/toolWindow/GitGlobalHooksLocatorWindow.kt @@ -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) { diff --git a/src/test/kotlin/com/github/y0ung3r/gitglobalhookslocator/gitTests/GetGlobalHooksPathTests.kt b/src/test/kotlin/com/github/y0ung3r/gitglobalhookslocator/gitTests/GetGlobalHooksPathTests.kt index b3419b8..77d8a0d 100644 --- a/src/test/kotlin/com/github/y0ung3r/gitglobalhookslocator/gitTests/GetGlobalHooksPathTests.kt +++ b/src/test/kotlin/com/github/y0ung3r/gitglobalhookslocator/gitTests/GetGlobalHooksPathTests.kt @@ -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") ) ) diff --git a/src/test/kotlin/com/github/y0ung3r/gitglobalhookslocator/gitTests/SemanticVersionTests.kt b/src/test/kotlin/com/github/y0ung3r/gitglobalhookslocator/gitTests/SemanticVersionTests.kt index e054a1e..7242d57 100644 --- a/src/test/kotlin/com/github/y0ung3r/gitglobalhookslocator/gitTests/SemanticVersionTests.kt +++ b/src/test/kotlin/com/github/y0ung3r/gitglobalhookslocator/gitTests/SemanticVersionTests.kt @@ -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) diff --git a/src/test/kotlin/com/github/y0ung3r/gitglobalhookslocator/gitTests/testEngine/HookTestBase.kt b/src/test/kotlin/com/github/y0ung3r/gitglobalhookslocator/gitTests/testEngine/HookTestBase.kt index 50c0838..790b1bb 100644 --- a/src/test/kotlin/com/github/y0ung3r/gitglobalhookslocator/gitTests/testEngine/HookTestBase.kt +++ b/src/test/kotlin/com/github/y0ung3r/gitglobalhookslocator/gitTests/testEngine/HookTestBase.kt @@ -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" diff --git a/src/test/testData/description.txt b/src/test/testData/description.txt new file mode 100644 index 0000000..7aadeb6 --- /dev/null +++ b/src/test/testData/description.txt @@ -0,0 +1 @@ +The current folder is used to generate test data \ No newline at end of file diff --git a/src/test/testData/rename/foo.xml b/src/test/testData/rename/foo.xml deleted file mode 100644 index b21e9f2..0000000 --- a/src/test/testData/rename/foo.xml +++ /dev/null @@ -1,3 +0,0 @@ - - 1>Foo - diff --git a/src/test/testData/rename/foo_after.xml b/src/test/testData/rename/foo_after.xml deleted file mode 100644 index 980ca96..0000000 --- a/src/test/testData/rename/foo_after.xml +++ /dev/null @@ -1,3 +0,0 @@ - - Foo -