From 6963eabf9e4828f377c9554acf489fed15adc6e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9D=D0=B8=D0=BA=D0=B8=D1=82=D0=B0=20=D0=92=D0=B5=D0=BB?= =?UTF-8?q?=D0=B8=D0=BA=D0=B8=D0=B9?= Date: Mon, 27 Nov 2023 00:23:55 +0500 Subject: [PATCH 1/4] Delete unused files --- src/test/testData/description.txt | 1 + src/test/testData/rename/foo.xml | 3 --- src/test/testData/rename/foo_after.xml | 3 --- 3 files changed, 1 insertion(+), 6 deletions(-) create mode 100644 src/test/testData/description.txt delete mode 100644 src/test/testData/rename/foo.xml delete mode 100644 src/test/testData/rename/foo_after.xml 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 - From 0a4e5890da12fc385718a5fa1d0de10f60e40c12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9D=D0=B8=D0=BA=D0=B8=D1=82=D0=B0=20=D0=92=D0=B5=D0=BB?= =?UTF-8?q?=D0=B8=D0=BA=D0=B8=D0=B9?= Date: Mon, 27 Nov 2023 01:12:29 +0500 Subject: [PATCH 2/4] (#8) Fix relative paths handling --- .../gitglobalhookslocator/git/HooksFolder.kt | 2 +- .../git/extensions/GitExtensions.kt | 28 +++++- .../git/utils/SystemPathUtils.kt | 32 ++++++ .../gitTests/GetGlobalHooksPathTests.kt | 98 ++++++++++++++++++- .../gitTests/testEngine/HookTestBase.kt | 2 +- 5 files changed, 155 insertions(+), 7 deletions(-) create mode 100644 src/main/kotlin/com/github/y0ung3r/gitglobalhookslocator/git/utils/SystemPathUtils.kt 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..8582149 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,36 @@ 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" +private const val ROOT_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() + ) + .replaceFirst( + Regex(ROOT_PATTERN), + SystemPathUtils.getRootPath() + ) + + 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..2018cd3 --- /dev/null +++ b/src/main/kotlin/com/github/y0ung3r/gitglobalhookslocator/git/utils/SystemPathUtils.kt @@ -0,0 +1,32 @@ +package com.github.y0ung3r.gitglobalhookslocator.git.utils + +object SystemPathUtils { + private const val ROOT_KEY = "SystemDrive" + private const val USER_HOME_KEY = "user.home" + private const val USER_DIR_KEY = "user.dir" + + @JvmStatic + private fun wrapWithSlash(path: String): String + = "$path/" + + @JvmStatic + private fun getSystemPath(key: String): String { + val systemPath = System + .getProperty(key) + .replace("\\", "/") // The replacement eliminates the need to escape the string + + return wrapWithSlash(systemPath) + } + + @JvmStatic + fun getUserHomePath(): String + = getSystemPath(USER_HOME_KEY) + + @JvmStatic + fun getCurrentDirectoryPath(): String + = getSystemPath(USER_DIR_KEY) + + @JvmStatic + fun getRootPath(): String + = wrapWithSlash(System.getenv(ROOT_KEY)) +} 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..8d87edf 100644 --- a/src/test/kotlin/com/github/y0ung3r/gitglobalhookslocator/gitTests/GetGlobalHooksPathTests.kt +++ b/src/test/kotlin/com/github/y0ung3r/gitglobalhookslocator/gitTests/GetGlobalHooksPathTests.kt @@ -4,14 +4,16 @@ 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 absolute path`() { // Arrange - val expectedPath = "~/.git/hooks" + val expectedPath = "C:/Users/user/.git/hooks" val sut = Git( RespondInterchangeably( CliResponse(Git.minRequiredVersion.toString()), @@ -22,6 +24,98 @@ class GetGlobalHooksPathTests { // Act val actualPath = sut.getGlobalHooksPath() + // Assert + assertEquals(Path.of(expectedPath), actualPath) + } + + @Test + fun `Should returns absolute global hooks path using relative path`() { + // Arrange + val expectedPath = Path.of( + SystemPathUtils.getCurrentDirectoryPath(), + ".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 $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("./.git/hooks") + ) + ) + + // Act + val actualPath = sut.getGlobalHooksPath() + + // Assert + assertEquals(expectedPath, actualPath) + } + + @Test + fun `Should resolve path using root pointer`() { + // Arrange + val expectedPath = Path.of( + SystemPathUtils.getRootPath(), + ".git", + "hooks" + ) + + val sut = Git( + RespondInterchangeably( + CliResponse(Git.minRequiredVersion.toString()), + CliResponse("/.git/hooks") + ) + ) + + // Act + val actualPath = sut.getGlobalHooksPath() + // Assert assertEquals(expectedPath, actualPath) } 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" From 7b717c663b1c25b16965ac894f872cf5f2b09bff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9D=D0=B8=D0=BA=D0=B8=D1=82=D0=B0=20=D0=92=D0=B5=D0=BB?= =?UTF-8?q?=D0=B8=D0=BA=D0=B8=D0=B9?= Date: Mon, 27 Nov 2023 01:28:01 +0500 Subject: [PATCH 3/4] Fix tests --- .../git/extensions/GitExtensions.kt | 5 ---- .../git/utils/SystemPathUtils.kt | 11 +-------- .../gitTests/GetGlobalHooksPathTests.kt | 23 ------------------- 3 files changed, 1 insertion(+), 38 deletions(-) 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 8582149..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 @@ -7,7 +7,6 @@ import java.nio.file.Path private const val SLASHES_PATTERN = "[/\\\\]" private const val HOME_PATTERN = "^~$SLASHES_PATTERN" private const val CURRENT_DIR_PATTERN = "^.$SLASHES_PATTERN" -private const val ROOT_PATTERN = "^$SLASHES_PATTERN" fun Git.getGlobalHooksPath(): Path { val rawPath = executeCommand( @@ -26,10 +25,6 @@ fun Git.getGlobalHooksPath(): Path { Regex(CURRENT_DIR_PATTERN), SystemPathUtils.getCurrentDirectoryPath() ) - .replaceFirst( - Regex(ROOT_PATTERN), - SystemPathUtils.getRootPath() - ) return Path.of(targetPath) .toAbsolutePath() 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 index 2018cd3..fee9695 100644 --- a/src/main/kotlin/com/github/y0ung3r/gitglobalhookslocator/git/utils/SystemPathUtils.kt +++ b/src/main/kotlin/com/github/y0ung3r/gitglobalhookslocator/git/utils/SystemPathUtils.kt @@ -1,21 +1,16 @@ package com.github.y0ung3r.gitglobalhookslocator.git.utils object SystemPathUtils { - private const val ROOT_KEY = "SystemDrive" private const val USER_HOME_KEY = "user.home" private const val USER_DIR_KEY = "user.dir" - @JvmStatic - private fun wrapWithSlash(path: String): String - = "$path/" - @JvmStatic private fun getSystemPath(key: String): String { val systemPath = System .getProperty(key) .replace("\\", "/") // The replacement eliminates the need to escape the string - return wrapWithSlash(systemPath) + return "$systemPath/" } @JvmStatic @@ -25,8 +20,4 @@ object SystemPathUtils { @JvmStatic fun getCurrentDirectoryPath(): String = getSystemPath(USER_DIR_KEY) - - @JvmStatic - fun getRootPath(): String - = wrapWithSlash(System.getenv(ROOT_KEY)) } 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 8d87edf..d19da67 100644 --- a/src/test/kotlin/com/github/y0ung3r/gitglobalhookslocator/gitTests/GetGlobalHooksPathTests.kt +++ b/src/test/kotlin/com/github/y0ung3r/gitglobalhookslocator/gitTests/GetGlobalHooksPathTests.kt @@ -96,27 +96,4 @@ class GetGlobalHooksPathTests { // Assert assertEquals(expectedPath, actualPath) } - - @Test - fun `Should resolve path using root pointer`() { - // Arrange - val expectedPath = Path.of( - SystemPathUtils.getRootPath(), - ".git", - "hooks" - ) - - val sut = Git( - RespondInterchangeably( - CliResponse(Git.minRequiredVersion.toString()), - CliResponse("/.git/hooks") - ) - ) - - // Act - val actualPath = sut.getGlobalHooksPath() - - // Assert - assertEquals(expectedPath, actualPath) - } } \ No newline at end of file From 89d0a318c08ea646f593e4fa8a417cc2f020666f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9D=D0=B8=D0=BA=D0=B8=D1=82=D0=B0=20=D0=92=D0=B5=D0=BB?= =?UTF-8?q?=D0=B8=D0=BA=D0=B8=D0=B9?= Date: Mon, 27 Nov 2023 01:37:34 +0500 Subject: [PATCH 4/4] Fix compiler warnings --- .../toolWindow/GitGlobalHooksLocatorWindow.kt | 3 -- .../gitTests/GetGlobalHooksPathTests.kt | 29 ++++--------------- .../gitTests/SemanticVersionTests.kt | 4 --- 3 files changed, 5 insertions(+), 31 deletions(-) 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 d19da67..77d8a0d 100644 --- a/src/test/kotlin/com/github/y0ung3r/gitglobalhookslocator/gitTests/GetGlobalHooksPathTests.kt +++ b/src/test/kotlin/com/github/y0ung3r/gitglobalhookslocator/gitTests/GetGlobalHooksPathTests.kt @@ -10,37 +10,18 @@ import org.junit.Test import java.nio.file.Path class GetGlobalHooksPathTests { - @Test - fun `Should returns absolute global hooks path using absolute path`() { - // Arrange - val expectedPath = "C:/Users/user/.git/hooks" - val sut = Git( - RespondInterchangeably( - CliResponse(Git.minRequiredVersion.toString()), - CliResponse(expectedPath) - ) - ) - - // Act - val actualPath = sut.getGlobalHooksPath() - - // Assert - assertEquals(Path.of(expectedPath), actualPath) - } - @Test fun `Should returns absolute global hooks path using relative path`() { // Arrange - val expectedPath = Path.of( - SystemPathUtils.getCurrentDirectoryPath(), - ".git", - "hooks" - ) + val relativePath = "/.git/hooks" + val expectedPath = Path + .of(relativePath) + .toAbsolutePath() val sut = Git( RespondInterchangeably( CliResponse(Git.minRequiredVersion.toString()), - CliResponse(".git/hooks") + CliResponse(relativePath) ) ) 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)