From 75c89a362dde3ef06ebe0a61787139a31d6fc8e5 Mon Sep 17 00:00:00 2001 From: Jaehwa Noh Date: Mon, 1 Apr 2024 09:32:28 +0900 Subject: [PATCH 1/6] Set and reset Locale to pass test for non-english contributor. --- .../ktlint/cli/reporter/baseline/BaselineTest.kt | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/ktlint-cli-reporter-baseline/src/test/kotlin/com/pinterest/ktlint/cli/reporter/baseline/BaselineTest.kt b/ktlint-cli-reporter-baseline/src/test/kotlin/com/pinterest/ktlint/cli/reporter/baseline/BaselineTest.kt index 50f83d7246..98be90e1e7 100644 --- a/ktlint-cli-reporter-baseline/src/test/kotlin/com/pinterest/ktlint/cli/reporter/baseline/BaselineTest.kt +++ b/ktlint-cli-reporter-baseline/src/test/kotlin/com/pinterest/ktlint/cli/reporter/baseline/BaselineTest.kt @@ -20,6 +20,7 @@ import org.junit.jupiter.params.provider.EnumSource import org.xml.sax.SAXParseException import java.nio.file.Path import java.nio.file.Paths +import java.util.Locale import kotlin.io.path.Path import kotlin.io.path.copyTo import kotlin.io.path.pathString @@ -106,11 +107,14 @@ class BaselineTest { @Nested inner class `Baseline has invalid xml structure` { + private val currentLocale = Locale.getDefault() + @Test fun `Given that the baseline is loaded with classic loader then the log contains an error message`( @TempDir tempDir: Path, ) { + setLocaleToEnglish() val path = "baseline-invalid.xml".copyResourceToFileIn(tempDir) val logCaptor = LogCaptor.forClass(Baseline::class.java) @@ -118,6 +122,7 @@ class BaselineTest { loadBaseline(path) assertThat(logCaptor.errorLogs).contains("Unable to parse baseline file: $path") + resetLocale(default = currentLocale) } @Test @@ -125,6 +130,7 @@ class BaselineTest { @TempDir tempDir: Path, ) { + setLocaleToEnglish() val path = "baseline-invalid.xml".copyResourceToFileIn(tempDir) assertThatExceptionOfType(BaselineLoaderException::class.java) @@ -133,6 +139,7 @@ class BaselineTest { .withCauseInstanceOf(SAXParseException::class.java) .havingCause() .withMessage("The element type \"file\" must be terminated by the matching end-tag \"\".") + resetLocale(default = currentLocale) } @Test @@ -148,6 +155,9 @@ class BaselineTest { assertThat(logCaptor.errorLogs).contains("Unable to parse baseline file: $path") } + + private fun setLocaleToEnglish() = Locale.setDefault(Locale.ENGLISH) + private fun resetLocale(default: Locale) = Locale.setDefault(default) } /** From a7f0f05f00fe9f240f5b252a931da92dbaefa24e Mon Sep 17 00:00:00 2001 From: Jaehwa Noh Date: Mon, 1 Apr 2024 09:32:28 +0900 Subject: [PATCH 2/6] Set and reset Locale to pass test for non-english contributor. Fixes #2621 --- .../ktlint/cli/reporter/baseline/BaselineTest.kt | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/ktlint-cli-reporter-baseline/src/test/kotlin/com/pinterest/ktlint/cli/reporter/baseline/BaselineTest.kt b/ktlint-cli-reporter-baseline/src/test/kotlin/com/pinterest/ktlint/cli/reporter/baseline/BaselineTest.kt index 50f83d7246..98be90e1e7 100644 --- a/ktlint-cli-reporter-baseline/src/test/kotlin/com/pinterest/ktlint/cli/reporter/baseline/BaselineTest.kt +++ b/ktlint-cli-reporter-baseline/src/test/kotlin/com/pinterest/ktlint/cli/reporter/baseline/BaselineTest.kt @@ -20,6 +20,7 @@ import org.junit.jupiter.params.provider.EnumSource import org.xml.sax.SAXParseException import java.nio.file.Path import java.nio.file.Paths +import java.util.Locale import kotlin.io.path.Path import kotlin.io.path.copyTo import kotlin.io.path.pathString @@ -106,11 +107,14 @@ class BaselineTest { @Nested inner class `Baseline has invalid xml structure` { + private val currentLocale = Locale.getDefault() + @Test fun `Given that the baseline is loaded with classic loader then the log contains an error message`( @TempDir tempDir: Path, ) { + setLocaleToEnglish() val path = "baseline-invalid.xml".copyResourceToFileIn(tempDir) val logCaptor = LogCaptor.forClass(Baseline::class.java) @@ -118,6 +122,7 @@ class BaselineTest { loadBaseline(path) assertThat(logCaptor.errorLogs).contains("Unable to parse baseline file: $path") + resetLocale(default = currentLocale) } @Test @@ -125,6 +130,7 @@ class BaselineTest { @TempDir tempDir: Path, ) { + setLocaleToEnglish() val path = "baseline-invalid.xml".copyResourceToFileIn(tempDir) assertThatExceptionOfType(BaselineLoaderException::class.java) @@ -133,6 +139,7 @@ class BaselineTest { .withCauseInstanceOf(SAXParseException::class.java) .havingCause() .withMessage("The element type \"file\" must be terminated by the matching end-tag \"\".") + resetLocale(default = currentLocale) } @Test @@ -148,6 +155,9 @@ class BaselineTest { assertThat(logCaptor.errorLogs).contains("Unable to parse baseline file: $path") } + + private fun setLocaleToEnglish() = Locale.setDefault(Locale.ENGLISH) + private fun resetLocale(default: Locale) = Locale.setDefault(default) } /** From 2a2557560474b679c932872620e9b225e93aca36 Mon Sep 17 00:00:00 2001 From: Jaehwa Noh Date: Mon, 1 Apr 2024 10:02:53 +0900 Subject: [PATCH 3/6] Apply ktlint. --- .../com/pinterest/ktlint/cli/reporter/baseline/BaselineTest.kt | 1 + 1 file changed, 1 insertion(+) diff --git a/ktlint-cli-reporter-baseline/src/test/kotlin/com/pinterest/ktlint/cli/reporter/baseline/BaselineTest.kt b/ktlint-cli-reporter-baseline/src/test/kotlin/com/pinterest/ktlint/cli/reporter/baseline/BaselineTest.kt index 98be90e1e7..2ddddd37a5 100644 --- a/ktlint-cli-reporter-baseline/src/test/kotlin/com/pinterest/ktlint/cli/reporter/baseline/BaselineTest.kt +++ b/ktlint-cli-reporter-baseline/src/test/kotlin/com/pinterest/ktlint/cli/reporter/baseline/BaselineTest.kt @@ -157,6 +157,7 @@ class BaselineTest { } private fun setLocaleToEnglish() = Locale.setDefault(Locale.ENGLISH) + private fun resetLocale(default: Locale) = Locale.setDefault(default) } From c38d2f4eaa035388909005b401e11d311ce8daf8 Mon Sep 17 00:00:00 2001 From: Jaehwa Noh Date: Mon, 1 Apr 2024 20:20:28 +0900 Subject: [PATCH 4/6] Remove variable currentLocale and Locale Helper function. --- .../ktlint/cli/reporter/baseline/BaselineTest.kt | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/ktlint-cli-reporter-baseline/src/test/kotlin/com/pinterest/ktlint/cli/reporter/baseline/BaselineTest.kt b/ktlint-cli-reporter-baseline/src/test/kotlin/com/pinterest/ktlint/cli/reporter/baseline/BaselineTest.kt index 2ddddd37a5..12d86f1c96 100644 --- a/ktlint-cli-reporter-baseline/src/test/kotlin/com/pinterest/ktlint/cli/reporter/baseline/BaselineTest.kt +++ b/ktlint-cli-reporter-baseline/src/test/kotlin/com/pinterest/ktlint/cli/reporter/baseline/BaselineTest.kt @@ -107,14 +107,11 @@ class BaselineTest { @Nested inner class `Baseline has invalid xml structure` { - private val currentLocale = Locale.getDefault() - @Test fun `Given that the baseline is loaded with classic loader then the log contains an error message`( @TempDir tempDir: Path, ) { - setLocaleToEnglish() val path = "baseline-invalid.xml".copyResourceToFileIn(tempDir) val logCaptor = LogCaptor.forClass(Baseline::class.java) @@ -122,7 +119,6 @@ class BaselineTest { loadBaseline(path) assertThat(logCaptor.errorLogs).contains("Unable to parse baseline file: $path") - resetLocale(default = currentLocale) } @Test @@ -130,7 +126,6 @@ class BaselineTest { @TempDir tempDir: Path, ) { - setLocaleToEnglish() val path = "baseline-invalid.xml".copyResourceToFileIn(tempDir) assertThatExceptionOfType(BaselineLoaderException::class.java) @@ -139,7 +134,6 @@ class BaselineTest { .withCauseInstanceOf(SAXParseException::class.java) .havingCause() .withMessage("The element type \"file\" must be terminated by the matching end-tag \"\".") - resetLocale(default = currentLocale) } @Test @@ -155,10 +149,6 @@ class BaselineTest { assertThat(logCaptor.errorLogs).contains("Unable to parse baseline file: $path") } - - private fun setLocaleToEnglish() = Locale.setDefault(Locale.ENGLISH) - - private fun resetLocale(default: Locale) = Locale.setDefault(default) } /** From 3c6498f36425c15c13f890fa0fbf8b62f8713b95 Mon Sep 17 00:00:00 2001 From: Jaehwa Noh Date: Mon, 1 Apr 2024 20:21:53 +0900 Subject: [PATCH 5/6] Create BeforeEach and AfterEach to set locale and reset. --- .../ktlint/cli/reporter/baseline/BaselineTest.kt | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/ktlint-cli-reporter-baseline/src/test/kotlin/com/pinterest/ktlint/cli/reporter/baseline/BaselineTest.kt b/ktlint-cli-reporter-baseline/src/test/kotlin/com/pinterest/ktlint/cli/reporter/baseline/BaselineTest.kt index 12d86f1c96..2b4b776e8c 100644 --- a/ktlint-cli-reporter-baseline/src/test/kotlin/com/pinterest/ktlint/cli/reporter/baseline/BaselineTest.kt +++ b/ktlint-cli-reporter-baseline/src/test/kotlin/com/pinterest/ktlint/cli/reporter/baseline/BaselineTest.kt @@ -12,6 +12,8 @@ import io.github.oshai.kotlinlogging.KotlinLogging import nl.altindag.log.LogCaptor import org.assertj.core.api.Assertions.assertThat import org.assertj.core.api.Assertions.assertThatExceptionOfType +import org.junit.jupiter.api.AfterEach +import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.Nested import org.junit.jupiter.api.Test import org.junit.jupiter.api.io.TempDir @@ -44,6 +46,14 @@ private fun KLogger.underlyingLogger(): Logger? = ?.underlyingLogger class BaselineTest { + private val currentLocale = Locale.getDefault() + + @BeforeEach + fun setLocaleEnglish() = Locale.setDefault(Locale.ENGLISH) + + @AfterEach + fun resetLocaleDefault() = Locale.setDefault(currentLocale) + @Nested inner class `Given a valid baseline file` { @Test From 491b3151feebb9fd1de80dcba1ec84c4acbcdf5f Mon Sep 17 00:00:00 2001 From: Paul Dingemans Date: Tue, 2 Apr 2024 15:59:49 +0200 Subject: [PATCH 6/6] Add clarifying comment --- .../pinterest/ktlint/cli/reporter/baseline/BaselineTest.kt | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/ktlint-cli-reporter-baseline/src/test/kotlin/com/pinterest/ktlint/cli/reporter/baseline/BaselineTest.kt b/ktlint-cli-reporter-baseline/src/test/kotlin/com/pinterest/ktlint/cli/reporter/baseline/BaselineTest.kt index 2b4b776e8c..d95348f06e 100644 --- a/ktlint-cli-reporter-baseline/src/test/kotlin/com/pinterest/ktlint/cli/reporter/baseline/BaselineTest.kt +++ b/ktlint-cli-reporter-baseline/src/test/kotlin/com/pinterest/ktlint/cli/reporter/baseline/BaselineTest.kt @@ -49,7 +49,10 @@ class BaselineTest { private val currentLocale = Locale.getDefault() @BeforeEach - fun setLocaleEnglish() = Locale.setDefault(Locale.ENGLISH) + fun setLocaleEnglish() { + // SAXParseException messages are localized. Prevent tests from failing when run on a system for which local is not set to English. + Locale.setDefault(Locale.ENGLISH) + } @AfterEach fun resetLocaleDefault() = Locale.setDefault(currentLocale)