From ee2bcca0e5ba146d91c579a024a15abb2b51b427 Mon Sep 17 00:00:00 2001 From: Jens Peters Date: Mon, 2 Dec 2024 15:07:42 +0100 Subject: [PATCH 1/2] Use assertk --- .../generatr/site/StructurizrUtilitiesTest.kt | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/test/kotlin/nl/avisi/structurizr/site/generatr/site/StructurizrUtilitiesTest.kt b/src/test/kotlin/nl/avisi/structurizr/site/generatr/site/StructurizrUtilitiesTest.kt index 7ebb8900..8657d901 100644 --- a/src/test/kotlin/nl/avisi/structurizr/site/generatr/site/StructurizrUtilitiesTest.kt +++ b/src/test/kotlin/nl/avisi/structurizr/site/generatr/site/StructurizrUtilitiesTest.kt @@ -1,15 +1,16 @@ package nl.avisi.structurizr.site.generatr.site +import assertk.assertThat +import assertk.assertions.isEqualTo import com.structurizr.Workspace import nl.avisi.structurizr.site.generatr.includedProperties import kotlin.test.Test -import kotlin.test.assertEquals class StructurizrUtilitiesTest { - protected val svgFactory = { _: String, _: String -> "" } + private val svgFactory = { _: String, _: String -> "" } - protected fun generatorContext( + private fun generatorContext( workspaceName: String = "Workspace name", branches: List = listOf("main"), currentBranch: String = "main", @@ -27,8 +28,8 @@ class StructurizrUtilitiesTest { val includedProperties = element.includedProperties - assertEquals(3, includedProperties.size) - assertEquals(includedProperties.keys, setOf("structurizrnotquite.key", "generatrbut.not.key", "other.key")) + assertThat(includedProperties.size).isEqualTo(3) + assertThat(includedProperties.keys).isEqualTo(setOf("structurizrnotquite.key", "generatrbut.not.key", "other.key")) } @Test @@ -39,15 +40,15 @@ class StructurizrUtilitiesTest { val includedProperties = element.includedProperties - assertEquals(2, includedProperties.size) - assertEquals("key1", includedProperties.keys.first()) - assertEquals("key2", includedProperties.keys.last()) + assertThat(includedProperties.size).isEqualTo(2) + assertThat(includedProperties.keys.first()).isEqualTo("key1") + assertThat(includedProperties.keys.last()).isEqualTo("key2") } @Test fun `includedProperties returns empty map when no properties are set`() { val element = generatorContext().workspace.model.addSoftwareSystem("System 1") val includedProperties = element.includedProperties - assertEquals(0, includedProperties.size) + assertThat(includedProperties.size).isEqualTo(0) } } From 3681e445618b5b1562a1d00162d6a69e04f541a8 Mon Sep 17 00:00:00 2001 From: Jens Peters Date: Mon, 2 Dec 2024 15:25:06 +0100 Subject: [PATCH 2/2] Exclude more reserved file name characters --- .../site/generatr/StringUtilities.kt | 20 +++++++++++++++- .../site/generatr/site/StringUtilitiesTest.kt | 23 +++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 src/test/kotlin/nl/avisi/structurizr/site/generatr/site/StringUtilitiesTest.kt diff --git a/src/main/kotlin/nl/avisi/structurizr/site/generatr/StringUtilities.kt b/src/main/kotlin/nl/avisi/structurizr/site/generatr/StringUtilities.kt index 1afba68e..6599720d 100644 --- a/src/main/kotlin/nl/avisi/structurizr/site/generatr/StringUtilities.kt +++ b/src/main/kotlin/nl/avisi/structurizr/site/generatr/StringUtilities.kt @@ -1,3 +1,21 @@ package nl.avisi.structurizr.site.generatr -fun String.normalize(): String = lowercase().replace("\\s+".toRegex(), "-") +// based on https://stackoverflow.com/questions/1976007/what-characters-are-forbidden-in-windows-and-linux-directory-names +private const val reservedChars = "|\\?*<\":>+[]/'" +private val reservedNames = setOf( + "CON", "PRN", "AUX", "NUL", + "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9", + "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9" +) + +fun String.normalize(): String = + lowercase() + .replace("\\s+".toRegex(), "-") + .filterNot { reservedChars.contains(it) } + .trim() + .let { + if (reservedNames.contains(it.uppercase())) + "${it}-" + else + it + } diff --git a/src/test/kotlin/nl/avisi/structurizr/site/generatr/site/StringUtilitiesTest.kt b/src/test/kotlin/nl/avisi/structurizr/site/generatr/site/StringUtilitiesTest.kt new file mode 100644 index 00000000..0b959d21 --- /dev/null +++ b/src/test/kotlin/nl/avisi/structurizr/site/generatr/site/StringUtilitiesTest.kt @@ -0,0 +1,23 @@ +package nl.avisi.structurizr.site.generatr.site + +import assertk.assertThat +import assertk.assertions.isEqualTo +import nl.avisi.structurizr.site.generatr.normalize +import org.junit.jupiter.api.DynamicTest +import org.junit.jupiter.api.TestFactory + +class StringUtilitiesTest { + + @TestFactory + fun `normalize strips invalid chars`() = listOf( + listOf("doc", "doc"), + listOf("d c", "d-c"), + listOf("doc:", "doc"), + listOf(" doc ", "-doc-"), + listOf("aux", "aux-"), + ).map { (actual, expected) -> + DynamicTest.dynamicTest("normalize replaces $actual with $expected") { + assertThat(actual.normalize()).isEqualTo(expected) + } + } +}