diff --git a/compiled-serializer/src/main/kotlin/KotlinCompiledChangeLogSerializer.kt b/compiled-serializer/src/main/kotlin/KotlinCompiledChangeLogSerializer.kt index 84707771..fc236a1a 100644 --- a/compiled-serializer/src/main/kotlin/KotlinCompiledChangeLogSerializer.kt +++ b/compiled-serializer/src/main/kotlin/KotlinCompiledChangeLogSerializer.kt @@ -49,9 +49,10 @@ class KotlinCompiledChangeLogSerializer : ChangeLogSerializer { val filePath = (changeSets[0] as? ChangeSet)?.filePath ?: return "" // \a\b\c\Clazz.kt -> a.b.c + // /user/home/b/c/Clazz.kt -> a.b.c return filePath .toUnixFileSeparator() - .removePrefix(sourceRootPath.toString().toUnixFileSeparator()) + .split(sourceRootPath.toString().toUnixFileSeparator()).last() .removePrefix("/") .substringBeforeLast("/") .replace("/", ".") diff --git a/integration-test/build.gradle.kts b/integration-test/build.gradle.kts index 03d2338a..5b2ca381 100644 --- a/integration-test/build.gradle.kts +++ b/integration-test/build.gradle.kts @@ -71,7 +71,6 @@ configurations.all { tasks.test { useJUnitPlatform() - systemProperty("kotest.framework.classpath.scanning.config.disable", "true") systemProperty("liquibaseVersion", liquibaseVersion) } diff --git a/integration-test/src/test/kotlin/momosetkn/ChangeLogSpec.kt b/integration-test/src/test/kotlin/momosetkn/ChangeLogSpec.kt index 04b3288b..b6dcc982 100644 --- a/integration-test/src/test/kotlin/momosetkn/ChangeLogSpec.kt +++ b/integration-test/src/test/kotlin/momosetkn/ChangeLogSpec.kt @@ -15,10 +15,7 @@ import org.komapper.core.dsl.query.single class ChangeLogSpec : FunSpec({ beforeEach { - DatabaseServer.start() - } - afterEach { - DatabaseServer.clear() + DatabaseServer.startAndClear() } val client = LiquibaseCommandClient { globalArgs { diff --git a/integration-test/src/test/kotlin/momosetkn/ChangeSetSpec.kt b/integration-test/src/test/kotlin/momosetkn/ChangeSetSpec.kt index cd8fafa0..9369183e 100644 --- a/integration-test/src/test/kotlin/momosetkn/ChangeSetSpec.kt +++ b/integration-test/src/test/kotlin/momosetkn/ChangeSetSpec.kt @@ -20,10 +20,7 @@ import org.komapper.core.dsl.query.single class ChangeSetSpec : FunSpec({ beforeEach { - DatabaseServer.start() - } - afterEach { - DatabaseServer.clear() + DatabaseServer.startAndClear() } val client = LiquibaseCommandClient { globalArgs { diff --git a/integration-test/src/test/kotlin/momosetkn/CustomChangeMigrationSpec.kt b/integration-test/src/test/kotlin/momosetkn/CustomChangeMigrationSpec.kt index a600cd80..9b270e05 100644 --- a/integration-test/src/test/kotlin/momosetkn/CustomChangeMigrationSpec.kt +++ b/integration-test/src/test/kotlin/momosetkn/CustomChangeMigrationSpec.kt @@ -12,11 +12,10 @@ import java.nio.file.Paths class CustomChangeMigrationSpec : FunSpec({ beforeSpec { - DatabaseServer.start() KotlinCompiledChangeLogSerializer.sourceRootPath = Paths.get(Constants.TEST_RESOURCE_DIR) } - afterSpec { - DatabaseServer.clear() + beforeEach { + DatabaseServer.startAndClear() } context("Migrate and serialize") { diff --git a/integration-test/src/test/kotlin/momosetkn/CustomJooqChangeSetSpec.kt b/integration-test/src/test/kotlin/momosetkn/CustomJooqChangeSetSpec.kt index 44fee0b8..c55090c5 100644 --- a/integration-test/src/test/kotlin/momosetkn/CustomJooqChangeSetSpec.kt +++ b/integration-test/src/test/kotlin/momosetkn/CustomJooqChangeSetSpec.kt @@ -22,10 +22,7 @@ import java.util.UUID class CustomJooqChangeSetSpec : FunSpec({ beforeEach { - DatabaseServer.start() - } - afterEach { - DatabaseServer.clear() + DatabaseServer.startAndClear() } val client = LiquibaseCommandClient { globalArgs { diff --git a/integration-test/src/test/kotlin/momosetkn/CustomKomapperJdbcChangeSetSpec.kt b/integration-test/src/test/kotlin/momosetkn/CustomKomapperJdbcChangeSetSpec.kt index 024e6627..27a7cdd0 100644 --- a/integration-test/src/test/kotlin/momosetkn/CustomKomapperJdbcChangeSetSpec.kt +++ b/integration-test/src/test/kotlin/momosetkn/CustomKomapperJdbcChangeSetSpec.kt @@ -22,10 +22,7 @@ import java.util.UUID class CustomKomapperJdbcChangeSetSpec : FunSpec({ beforeEach { - DatabaseServer.start() - } - afterEach { - DatabaseServer.clear() + DatabaseServer.startAndClear() } val client = LiquibaseCommandClient { globalArgs { diff --git a/integration-test/src/test/kotlin/momosetkn/KotestProjectConfig.kt b/integration-test/src/test/kotlin/momosetkn/KotestProjectConfig.kt new file mode 100644 index 00000000..51b047c5 --- /dev/null +++ b/integration-test/src/test/kotlin/momosetkn/KotestProjectConfig.kt @@ -0,0 +1,16 @@ +package momosetkn + +import io.kotest.core.config.AbstractProjectConfig +import momosetkn.liquibase.client.configureLiquibase + +class KotestProjectConfig : AbstractProjectConfig() { + init { + configureLiquibase { + global { + general { + showBanner = false + } + } + } + } +} diff --git a/integration-test/src/test/kotlin/momosetkn/KotlinCompiledMigrateAndSerializeSpec.kt b/integration-test/src/test/kotlin/momosetkn/KotlinCompiledMigrateAndSerializeSpec.kt index a6eda841..2c73d316 100644 --- a/integration-test/src/test/kotlin/momosetkn/KotlinCompiledMigrateAndSerializeSpec.kt +++ b/integration-test/src/test/kotlin/momosetkn/KotlinCompiledMigrateAndSerializeSpec.kt @@ -4,7 +4,6 @@ import io.kotest.core.spec.style.FunSpec import momosetkn.liquibase.changelogs.CompiledDatabaseChangelogAll import momosetkn.liquibase.client.LiquibaseClient import momosetkn.liquibase.client.LiquibaseDatabaseFactory -import momosetkn.liquibase.client.configureLiquibase import momosetkn.liquibase.kotlin.serializer.KotlinCompiledChangeLogSerializer import momosetkn.utils.Constants import momosetkn.utils.DDLUtils.sql @@ -18,22 +17,14 @@ import java.nio.file.Paths class KotlinCompiledMigrateAndSerializeSpec : FunSpec({ beforeSpec { - DatabaseServer.start() KotlinCompiledChangeLogSerializer.sourceRootPath = Paths.get(Constants.TEST_RESOURCE_DIR) - configureLiquibase { - global { - general { - showBanner = false - } - } - } } - afterSpec { - DatabaseServer.clear() + beforeEach { + DatabaseServer.startAndClear() } - context("Migrate and serialize") { - test("can migrate") { + context("Serialize output file is relative path") { + test("can migrate and serialize") { val container = DatabaseServer.startedContainer val database = LiquibaseDatabaseFactory.create( driver = container.driver, @@ -78,6 +69,53 @@ class KotlinCompiledMigrateAndSerializeSpec : FunSpec({ actual shouldMatchWithoutLineBreaks expect } } + + context("Serialize output file is absolute path") { + test("can migrate and serialize") { + val container = DatabaseServer.startedContainer + val database = LiquibaseDatabaseFactory.create( + driver = container.driver, + url = container.jdbcUrl, + username = container.username, + password = container.password, + ) + val liquibaseClient = LiquibaseClient( + changeLogFile = PARSER_INPUT_CHANGELOG, + database = database, + ) + println("${this::class.simpleName} -- before update") + liquibaseClient.update() + println("${this::class.simpleName} -- before rollback") + liquibaseClient.rollback(tagToRollBackTo = "started") + println("${this::class.simpleName} -- before update(2)") + liquibaseClient.update() + val actualSerializedChangeLogFile = + Paths.get(Constants.TEST_RESOURCE_DIR, SERIALIZER_ACTUAL_CHANGELOG) + val f = actualSerializedChangeLogFile.toFile() + if (f.exists()) f.delete() + val generateLiquibaseClient = LiquibaseClient( + changeLogFile = f.absolutePath.toString(), // absolute + database = database, + ) + println("${this::class.simpleName} -- before generateChangeLog") + val baos = ByteArrayOutputStream() + generateLiquibaseClient.generateChangeLog( + outputStream = PrintStream(baos), + ) + val generateResult = baos.toString() + println(generateResult) // empty + + // check database + val expectedDdl = getResourceAsString(PARSER_EXPECT_DDL) + DatabaseServer.generateDdl().toMainDdl() shouldMatchWithoutLineBreaks sql(expectedDdl) + + // check serializer + val actual = f.readText().maskingChangeSet() + val expect = getResourceAsString(SERIALIZER_EXPECT_CHANGELOG) + .maskingChangeSet() + actual shouldMatchWithoutLineBreaks expect + } + } }) { companion object { private val changeSetRegex = Regex("""changeSet\(author = "(.+)", id = "(\d+)-(\d)"\) \{""") diff --git a/integration-test/src/test/kotlin/momosetkn/KotlinScriptMigrateAndSerializeSpec.kt b/integration-test/src/test/kotlin/momosetkn/KotlinScriptMigrateAndSerializeSpec.kt index 13980a9f..de2e91c8 100644 --- a/integration-test/src/test/kotlin/momosetkn/KotlinScriptMigrateAndSerializeSpec.kt +++ b/integration-test/src/test/kotlin/momosetkn/KotlinScriptMigrateAndSerializeSpec.kt @@ -3,7 +3,6 @@ package momosetkn import io.kotest.core.spec.style.FunSpec import momosetkn.liquibase.client.LiquibaseClient import momosetkn.liquibase.client.LiquibaseDatabaseFactory -import momosetkn.liquibase.client.configureLiquibase import momosetkn.utils.Constants import momosetkn.utils.DDLUtils.sql import momosetkn.utils.DDLUtils.toMainDdl @@ -15,22 +14,12 @@ import java.io.PrintStream import java.nio.file.Paths class KotlinScriptMigrateAndSerializeSpec : FunSpec({ - beforeSpec { - DatabaseServer.start() - configureLiquibase { - global { - general { - showBanner = false - } - } - } - } - afterSpec { - DatabaseServer.clear() + beforeEach { + DatabaseServer.startAndClear() } - context("Migrate and serialize") { - test("can migrate") { + context("Serialize output file is relative path") { + test("can migrate and serialize") { val container = DatabaseServer.startedContainer val database = LiquibaseDatabaseFactory.create( driver = container.driver, @@ -75,6 +64,53 @@ class KotlinScriptMigrateAndSerializeSpec : FunSpec({ actual shouldMatchWithoutLineBreaks expect } } + + context("Serialize output file is absolute path") { + test("can migrate and serialize") { + val container = DatabaseServer.startedContainer + val database = LiquibaseDatabaseFactory.create( + driver = container.driver, + url = container.jdbcUrl, + username = container.username, + password = container.password, + ) + val liquibaseClient = LiquibaseClient( + changeLogFile = PARSER_INPUT_CHANGELOG, + database = database, + ) + println("${this::class.simpleName} -- before update") + liquibaseClient.update() + println("${this::class.simpleName} -- before rollback") + liquibaseClient.rollback(tagToRollBackTo = "started") + println("${this::class.simpleName} -- before update(2)") + liquibaseClient.update() + val actualSerializedChangeLogFile = + Paths.get(Constants.TEST_RESOURCE_DIR, SERIALIZER_ACTUAL_CHANGELOG) + val f = actualSerializedChangeLogFile.toFile() + if (f.exists()) f.delete() + val generateLiquibaseClient = LiquibaseClient( + changeLogFile = f.absolutePath.toString(), // absolute + database = database, + ) + println("${this::class.simpleName} -- before generateChangeLog") + val baos = ByteArrayOutputStream() + generateLiquibaseClient.generateChangeLog( + outputStream = PrintStream(baos), + ) + val generateResult = baos.toString() + println(generateResult) // empty + + // check database + val expectedDdl = getResourceAsString(PARSER_EXPECT_DDL) + DatabaseServer.generateDdl().toMainDdl() shouldMatchWithoutLineBreaks sql(expectedDdl) + + // check serializer + val actual = f.readText().maskingChangeSet() + val expect = getResourceAsString(SERIALIZER_EXPECT_CHANGELOG) + .maskingChangeSet() + actual shouldMatchWithoutLineBreaks expect + } + } }) { companion object { private val changeSetRegex = Regex("""changeSet\(author = "(.+)", id = "(\d+)-(\d)"\) \{""") diff --git a/integration-test/src/test/kotlin/momosetkn/LiquibaseClientSpec.kt b/integration-test/src/test/kotlin/momosetkn/LiquibaseClientSpec.kt index 7584c928..b152fef2 100644 --- a/integration-test/src/test/kotlin/momosetkn/LiquibaseClientSpec.kt +++ b/integration-test/src/test/kotlin/momosetkn/LiquibaseClientSpec.kt @@ -9,7 +9,6 @@ import liquibase.Scope import liquibase.changelog.ChangeLogParameters import momosetkn.liquibase.client.LiquibaseClient import momosetkn.liquibase.client.LiquibaseDatabaseFactory -import momosetkn.liquibase.client.configureLiquibase import momosetkn.liquibase.kotlin.parser.KotlinCompiledLiquibaseChangeLogParser import momosetkn.liquibase.kotlin.serializer.KotlinCompiledChangeLogSerializer import momosetkn.utils.Constants @@ -32,17 +31,7 @@ class LiquibaseClientSpec : FunSpec({ KotlinCompiledChangeLogSerializer.sourceRootPath = Paths.get(Constants.TEST_RESOURCE_DIR) } beforeEach { - DatabaseServer.start() - configureLiquibase { - global { - general { - showBanner = false - } - } - } - } - afterEach { - DatabaseServer.clear() + DatabaseServer.startAndClear() } fun liquibaseClient(changeLogFile: String? = null): LiquibaseClient { diff --git a/integration-test/src/test/resources/kotest.properties b/integration-test/src/test/resources/kotest.properties new file mode 100644 index 00000000..0645cbb5 --- /dev/null +++ b/integration-test/src/test/resources/kotest.properties @@ -0,0 +1,4 @@ +kotest.framework.classpath.scanning.config.disable=true +kotest.framework.classpath.scanning.autoscan.disable=true +kotest.framework.config.fqn=momosetkn.KotestProjectConfig +kotest.assertions.collection.print.size=100 \ No newline at end of file diff --git a/test-utils/src/main/kotlin/momosetkn/utils/DatabaseServer.kt b/test-utils/src/main/kotlin/momosetkn/utils/DatabaseServer.kt index 59659f0e..43347935 100644 --- a/test-utils/src/main/kotlin/momosetkn/utils/DatabaseServer.kt +++ b/test-utils/src/main/kotlin/momosetkn/utils/DatabaseServer.kt @@ -30,7 +30,7 @@ object DatabaseServer { } @Synchronized - fun start() { + fun startAndClear() { if (this.container == null) { this.container = createServer() }