From 898048513951b471a492afa23e46112d14bcb236 Mon Sep 17 00:00:00 2001 From: Norbert Preining Date: Thu, 5 Mar 2020 03:00:59 +0000 Subject: CTAN sync 202003050300 --- .../arara/filehandling/FileHandlingUtilsTest.kt | 62 ++++++++++++++++++++++ .../arara/filehandling/FileSearchingUtilsTest.kt | 58 ++++++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 support/arara/source/src/test/kotlin/org/islandoftex/arara/filehandling/FileHandlingUtilsTest.kt create mode 100644 support/arara/source/src/test/kotlin/org/islandoftex/arara/filehandling/FileSearchingUtilsTest.kt (limited to 'support/arara/source/src/test/kotlin/org/islandoftex/arara/filehandling') diff --git a/support/arara/source/src/test/kotlin/org/islandoftex/arara/filehandling/FileHandlingUtilsTest.kt b/support/arara/source/src/test/kotlin/org/islandoftex/arara/filehandling/FileHandlingUtilsTest.kt new file mode 100644 index 0000000000..55b13a8fe5 --- /dev/null +++ b/support/arara/source/src/test/kotlin/org/islandoftex/arara/filehandling/FileHandlingUtilsTest.kt @@ -0,0 +1,62 @@ +// SPDX-License-Identifier: BSD-3-Clause +package org.islandoftex.arara.filehandling + +import io.kotlintest.shouldBe +import io.kotlintest.shouldThrow +import io.kotlintest.specs.ShouldSpec +import java.io.File +import java.nio.file.Files +import org.islandoftex.arara.Arara +import org.islandoftex.arara.configuration.AraraSpec +import org.islandoftex.arara.model.AraraException + +class FileHandlingUtilsTest : ShouldSpec({ + should("fail generating CRC sums on inexistent files") { + shouldThrow { + FileHandlingUtils.calculateHash(File("QUACK")) + } + } + should("generate correct CRC sums") { + FileHandlingUtils.calculateHash(File("../LICENSE")) shouldBe "2396b4e2" + FileHandlingUtils.calculateHash(File("../CODE_OF_CONDUCT.md")) shouldBe "536c426f" + } + + should("find correct extension") { + FileHandlingUtils.getFileExtension(File("QUACK")) shouldBe "" + FileHandlingUtils.getFileExtension(File("a.tex")) shouldBe "tex" + FileHandlingUtils.getFileExtension(File(".tex")) shouldBe "tex" + } + should("find correct basename") { + FileHandlingUtils.getBasename(File("QUACK")) shouldBe "QUACK" + FileHandlingUtils.getBasename(File("a.tex")) shouldBe "a" + FileHandlingUtils.getBasename(File(".tex")) shouldBe "" + } + + should("get subdirecotry relationship right") { + FileHandlingUtils.isSubDirectory(File("../docs"), File("..")) shouldBe true + FileHandlingUtils.isSubDirectory(File(".."), File("../docs")) shouldBe false + shouldThrow { + FileHandlingUtils.isSubDirectory(File("../LICENSE"), File("..")) + } + shouldThrow { + FileHandlingUtils.isSubDirectory(File(".."), File("../LICENSE")) + } + } + + should("detect changes on file") { + val file = Files.createTempFile(null, null).toFile() + val referenceBackup = Arara.config[AraraSpec.Execution.reference] + Arara.config[AraraSpec.Execution.reference] = file.parentFile.resolve("reference") + FileHandlingUtils.hasChanged(file) shouldBe true + FileHandlingUtils.hasChanged(file) shouldBe false + file.writeText("QUACK") + FileHandlingUtils.hasChanged(file) shouldBe true + FileHandlingUtils.hasChanged(file) shouldBe false + file.writeText("QUACK2") + FileHandlingUtils.hasChanged(file) shouldBe true + file.delete() + FileHandlingUtils.hasChanged(file) shouldBe true + FileHandlingUtils.hasChanged(file) shouldBe false + Arara.config[AraraSpec.Execution.reference] = referenceBackup + } +}) diff --git a/support/arara/source/src/test/kotlin/org/islandoftex/arara/filehandling/FileSearchingUtilsTest.kt b/support/arara/source/src/test/kotlin/org/islandoftex/arara/filehandling/FileSearchingUtilsTest.kt new file mode 100644 index 0000000000..47f9916c43 --- /dev/null +++ b/support/arara/source/src/test/kotlin/org/islandoftex/arara/filehandling/FileSearchingUtilsTest.kt @@ -0,0 +1,58 @@ +// SPDX-License-Identifier: BSD-3-Clause +package org.islandoftex.arara.filehandling + +import io.kotlintest.shouldBe +import io.kotlintest.specs.ShouldSpec +import java.nio.file.Files +import java.nio.file.Path +import kotlin.reflect.full.declaredMemberFunctions +import kotlin.reflect.jvm.isAccessible + +class FileSearchingUtilsTest : ShouldSpec({ + // TODO: test implicit extensions + + fun prepareFileSystem(): Path { + val tempDir = Files.createTempDirectory(System.nanoTime().toString()) + tempDir.resolve("quack/quack").toFile().mkdirs() + listOf("quack", "quack/quack", "quack/quack/quack").forEach { + tempDir.resolve("$it.tex").toFile().writeText(" ") + tempDir.resolve("$it.txt").toFile().writeText(" ") + } + return tempDir + } + + should("fail looking up inexistent file") { + val lookupFile = FileSearchingUtils::class.declaredMemberFunctions + .first { it.name == "lookupFile" } + lookupFile.isAccessible = true + lookupFile.call(FileSearchingUtils, "QUACK") shouldBe null + } + + should("fail on existing directory") { + val lookupFile = FileSearchingUtils::class.declaredMemberFunctions + .first { it.name == "lookupFile" } + lookupFile.isAccessible = true + lookupFile.call(FileSearchingUtils, "../buildSrc") shouldBe null + } + + should("find file by extension") { + val tempDir = prepareFileSystem() + FileSearchingUtils.listFilesByExtensions(tempDir.toFile(), + listOf("tex"), false).toSet() shouldBe + setOf(tempDir.resolve("quack.tex").toFile()) + FileSearchingUtils.listFilesByExtensions(tempDir.toFile(), + listOf("tex"), true).toSet() shouldBe + listOf("quack", "quack/quack", "quack/quack/quack") + .map { tempDir.resolve("$it.tex").toFile() }.toSet() + } + should("find file by pattern") { + val tempDir = prepareFileSystem() + FileSearchingUtils.listFilesByPatterns(tempDir.toFile(), + listOf("*q*.txt"), false).toSet() shouldBe + setOf(tempDir.resolve("quack.txt").toFile()) + FileSearchingUtils.listFilesByPatterns(tempDir.toFile(), + listOf("q*.txt"), true).toSet() shouldBe + listOf("quack", "quack/quack", "quack/quack/quack") + .map { tempDir.resolve("$it.txt").toFile() }.toSet() + } +}) -- cgit v1.2.3