Skip to content

Commit

Permalink
Create mini framework for tests & add tests for glob
Browse files Browse the repository at this point in the history
  • Loading branch information
juraj-hrivnak committed Dec 16, 2024
1 parent 31ec879 commit 7312493
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 47 deletions.
9 changes: 6 additions & 3 deletions src/commonMain/kotlin/teksturepako/pakku/io/Glob.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import java.nio.file.PathMatcher
import kotlin.io.path.*

@OptIn(ExperimentalPathApi::class)
suspend fun Path.walk(inputGlobs: List<String>): Sequence<Pair<Path, Boolean>> = withContext(Dispatchers.IO) {
val matchers: List<Pair<PathMatcher, Boolean>> = inputGlobs.mapNotNull { input ->
suspend fun Path.walk(globPatterns: List<String>): Sequence<Pair<Path, Boolean>> = withContext(Dispatchers.IO) {
val matchers: List<Pair<PathMatcher, Boolean>> = globPatterns.mapNotNull { input ->
val negating = input.startsWith("!")
val glob = (if (negating) input.removePrefix("!") else input).removePrefix("./")

Expand All @@ -30,7 +30,10 @@ suspend fun Path.walk(inputGlobs: List<String>): Sequence<Pair<Path, Boolean>> =

matcher to path
}.map { (matchers, path) ->
Path(path.absolutePathString().removePrefix(this@walk.absolutePathString()).removePrefix(File.separator)) to matchers.any { it.second }
val resultPath = Path(path.absolutePathString().removePrefix(this@walk.absolutePathString()).removePrefix(File.separator))
val negating = matchers.any { it.second }

resultPath to negating
}
}

Expand Down
60 changes: 60 additions & 0 deletions src/commonTest/kotlin/teksturepako/pakku/PakkuTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package teksturepako.pakku

import kotlinx.coroutines.runBlocking
import teksturepako.pakku.api.data.generatePakkuId
import teksturepako.pakku.api.data.workingPath
import kotlin.io.path.*
import kotlin.test.AfterTest
import kotlin.test.BeforeTest

open class PakkuTest
{
private var testName: String = ""

protected open suspend fun `on-set-up`()
{
}

protected open val teardown = true

protected fun createTestFile(vararg path: String)
{
val file = Path(workingPath, *path)
runCatching { file.createParentDirectories() }
runCatching { file.createFile() }
}

protected fun createTestDir(vararg path: String)
{
val dir = Path(workingPath, *path)
runCatching { dir.createParentDirectories() }
runCatching { dir.createDirectory() }
}

@BeforeTest
fun `set-up-test`()
{
testName = this::class.simpleName ?: generatePakkuId()

println("Setting up test: $testName")

workingPath = "./build/test/$testName"
runCatching { Path("./build/test/$testName").createParentDirectories() }
runCatching { Path("./build/test/$testName").createDirectory() }

runBlocking { this@PakkuTest.`on-set-up`() }
}

@AfterTest
@OptIn(ExperimentalPathApi::class)
fun `tear-down-test`()
{
if (!teardown) return

workingPath = "."

println("Tearing down test: $testName")

runCatching { Path("./build/test/$testName").deleteRecursively() }
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ import kotlin.test.assertEquals

class CurseForgeTest
{
@Test
fun requestProject()
{
}

@Test
fun sortByLoaders_WithValidLoaders_ShouldCompareCorrectly() {
val files = listOf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@ class ProjectTest
@Test
fun hasAliasOf_whenProjectHasAlias_returnsTrue()
{
val project1 = mockk<Project>() {
val project1 = mockk<Project> {
every { id } returns mutableMapOf("id1" to "id1")
every { name } returns mutableMapOf("name1" to "name1")
every { slug } returns mutableMapOf("slug1" to "slug1")
every { aliases } returns mutableSetOf("id3")
every { hasAliasOf(any()) } answers { callOriginal() }
}
val project2 = mockk<Project>() {
val project2 = mockk<Project> {
every { id } returns mutableMapOf("id2" to "id2")
every { name } returns mutableMapOf("name2" to "name2")
every { slug } returns mutableMapOf("slug2" to "slug2")
every { aliases } returns mutableSetOf("name1")
every { hasAliasOf(any()) } answers { callOriginal() }
}
val project3 = mockk<Project>() {
val project3 = mockk<Project> {
every { id } returns mutableMapOf("id3" to "id3")
every { name } returns mutableMapOf("name3" to "name3")
every { slug } returns mutableMapOf("slug3" to "slug3")
Expand Down
16 changes: 6 additions & 10 deletions src/commonTest/kotlin/teksturepako/pakku/cli/cmd/CfgPrjTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package teksturepako.pakku.cli.cmd
import com.github.ajalt.clikt.testing.test
import com.github.michaelbull.result.runCatching
import kotlinx.coroutines.runBlocking
import teksturepako.pakku.PakkuTest
import teksturepako.pakku.api.data.ConfigFile
import teksturepako.pakku.api.data.LockFile
import teksturepako.pakku.api.data.workingPath
Expand All @@ -16,21 +17,13 @@ import kotlin.test.assertContains
import kotlin.test.assertEquals
import kotlin.test.assertNotNull

@OptIn(ExperimentalPathApi::class)
class CfgPrjTest
class CfgPrjTest : PakkuTest()
{
init
{
workingPath = "./build/test"
runCatching { Path("./build/test").deleteRecursively() }
runCatching { Path("./build/test").createDirectory() }
}

@Test
fun `should fail without lock file`()
{
val cmd = CfgPrj()
val output = cmd.test("test -p test").output
val output = cmd.test("test --subpath test-subpath").output

assertContains(output, "Could not read '$workingPath/${LockFile.FILE_NAME}'")
}
Expand All @@ -54,9 +47,12 @@ class CfgPrjTest

val cmd = CfgPrj()
val output = cmd.test("test -p test -s both -u latest -r true")

assertEquals("", output.stderr, "Command failed to execute")
assertNotNull(ConfigFile.readOrNull(), "Config file should be created")

val config = ConfigFile.readOrNull()!!.projects["test"]

assertNotNull(config, "Project config should be created")
assertEquals(UpdateStrategy.LATEST, config.updateStrategy)
assertEquals(true, config.redistributable)
Expand Down
14 changes: 3 additions & 11 deletions src/commonTest/kotlin/teksturepako/pakku/cli/cmd/CfgTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package teksturepako.pakku.cli.cmd

import com.github.ajalt.clikt.testing.test
import com.github.michaelbull.result.runCatching
import teksturepako.pakku.PakkuTest
import teksturepako.pakku.api.data.ConfigFile
import teksturepako.pakku.api.data.workingPath
import teksturepako.pakku.api.projects.ProjectType
Expand All @@ -10,22 +11,13 @@ import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotNull

@OptIn(ExperimentalPathApi::class)
class CfgTest
class CfgTest : PakkuTest()
{
init
{
workingPath = "./build/test"
runCatching { Path("./build/test").deleteRecursively() }
runCatching { Path("./build/test").createDirectory() }
}

@Test
fun `should success with options`()
{
val cmd = Cfg()
val output =
cmd.test("-n foo -v 1.20.1 -d bar -a test --mods-path ./dummy-mods --resource-packs-path ./dummy-resourcepacks --data-packs-path ./datapacks --worlds-path ./worlds --shaders-path ./shaders")
val output = cmd.test("-n foo -v 1.20.1 -d bar -a test --mods-path ./dummy-mods --resource-packs-path ./dummy-resourcepacks --data-packs-path ./datapacks --worlds-path ./worlds --shaders-path ./shaders")
assertEquals("", output.stderr, "Command failed to execute")
val config = ConfigFile.readOrNull()
assertNotNull(config, "Config file should be created")
Expand Down
11 changes: 2 additions & 9 deletions src/commonTest/kotlin/teksturepako/pakku/cli/cmd/ExportTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,20 @@ package teksturepako.pakku.cli.cmd

import com.github.ajalt.clikt.testing.test
import com.github.michaelbull.result.runCatching
import teksturepako.pakku.PakkuTest
import teksturepako.pakku.api.data.LockFile
import teksturepako.pakku.api.data.workingPath
import kotlin.io.path.*
import kotlin.test.Test
import kotlin.test.assertContains

@OptIn(ExperimentalPathApi::class)
class ExportTest
class ExportTest : PakkuTest()
{
private val testName = "TestModpack"
private val testVersion = "1.0.0"
private val testDescription = "This is a test modpack."
private val testAuthor = "TestAuthor"

init
{
workingPath = "./build/test"
runCatching { Path("./build/test").deleteRecursively() }
runCatching { Path("./build/test").createDirectory() }
}

@Test
fun `try without lock file & config file`()
{
Expand Down
40 changes: 40 additions & 0 deletions src/commonTest/kotlin/teksturepako/pakku/io/GlobTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package teksturepako.pakku.io

import kotlinx.coroutines.runBlocking
import teksturepako.pakku.PakkuTest
import teksturepako.pakku.api.data.workingPath
import kotlin.io.path.Path
import kotlin.test.Test
import kotlin.test.assertContains

class GlobTest : PakkuTest()
{
private val testFileName = "test_file.txt"
private val testDirName = "test_dir"

override suspend fun `on-set-up`()
{
createTestFile(testFileName)
createTestDir(testDirName)
createTestFile(testDirName, testFileName)
}

@Test
fun `test with basic file name`() = runBlocking {
val expandedGlob = listOf(testFileName).expandWithGlob(Path(workingPath))
assertContains(expandedGlob, testFileName)
}

@Test
fun `test with negating pattern`() = runBlocking {
val expandedGlob = listOf("!$testFileName").expandWithGlob(Path(workingPath))
assert(testFileName !in expandedGlob)
}

@Test
fun `test with dir pattern`() = runBlocking {
val expandedGlob = listOf("$testDirName/**").expandWithGlob(Path(workingPath))
assert(testDirName !in expandedGlob) { "$expandedGlob should not contain $testDirName" }
assertContains(expandedGlob, "$testDirName/$testFileName")
}
}

0 comments on commit 7312493

Please sign in to comment.