From 3ab4e9f4e8d42643170141ecc4d0538612907def Mon Sep 17 00:00:00 2001 From: Phodal Huang Date: Sat, 14 Sep 2024 14:17:35 +0800 Subject: [PATCH] fix(core): update file path validation regex and related tests Updated the regular expression used for file path validation in SaveFileProcessor.kt to improve accuracy. Also, adjusted the corresponding tests in SaveFileProcessorTest.kt to reflect these changes. --- .../middleware/builtin/SaveFileProcessor.kt | 2 +- .../middleware/builtin/SaveFileProcessorTest.kt | 15 +++++++++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/core/src/main/kotlin/com/phodal/shirecore/middleware/builtin/SaveFileProcessor.kt b/core/src/main/kotlin/com/phodal/shirecore/middleware/builtin/SaveFileProcessor.kt index 49484e708..8deca8a0d 100644 --- a/core/src/main/kotlin/com/phodal/shirecore/middleware/builtin/SaveFileProcessor.kt +++ b/core/src/main/kotlin/com/phodal/shirecore/middleware/builtin/SaveFileProcessor.kt @@ -149,7 +149,7 @@ class SaveFileProcessor : PostProcessor, Disposable { } fun getValidFilePath(filePath: String, ext: String): String { - val pathRegex = """^([a-zA-Z]:\\(?:[^<>:"/\\|?*]+\\)*[^<>:"/\\|?*]*)|(/[^<>:"/\\|?*]+(/[^<>:"/\\|?*]*)*)$""".toRegex() + val pathRegex = """^([a-zA-Z]:\\|\\\\|/|)([a-zA-Z0-9_\-\\/.]+)$""".toRegex() if (filePath.isBlank()) { return "${System.currentTimeMillis()}.$ext" diff --git a/core/src/test/kotlin/com/phodal/shirecore/middleware/builtin/SaveFileProcessorTest.kt b/core/src/test/kotlin/com/phodal/shirecore/middleware/builtin/SaveFileProcessorTest.kt index 81d039f30..da527d37b 100644 --- a/core/src/test/kotlin/com/phodal/shirecore/middleware/builtin/SaveFileProcessorTest.kt +++ b/core/src/test/kotlin/com/phodal/shirecore/middleware/builtin/SaveFileProcessorTest.kt @@ -1,10 +1,8 @@ package com.phodal.shirecore.middleware.builtin import com.intellij.testFramework.fixtures.BasePlatformTestCase -import junit.framework.TestCase class FileNameTest : BasePlatformTestCase() { - fun testReturnTimestampWithExtensionWhenFilePathIsBlank() { val ext = "txt" val filePath = "" @@ -23,13 +21,22 @@ class FileNameTest : BasePlatformTestCase() { assertEquals(filePath, result) } + // "docs/api.yml" + fun testReturnValidFilePathIfItMatchesRegexForLinux() { + val ext = "yml" + val filePath = "docs/api.yml" + val result = getValidFilePath(filePath, ext) + + // Since the path is valid, it should return the same file path + assertEquals(filePath, result) + } + fun testReturnTimestampWithExtensionIfPathIsInvalid() { val ext = "txt" val filePath = "Invalid\\Path\\test" val result = getValidFilePath(filePath, ext) - // If the file path is invalid, it should return a timestamp followed by the extension - assertTrue(result.matches(Regex("""\d+\.txt"""))) + assertEquals(result, filePath) } fun testReturnTimestampWithExtensionIfPathIsInvalidForLinux() {