From 1cfa97506c74bc3b3ea75cf41d2e53fc68b7cd40 Mon Sep 17 00:00:00 2001 From: Thomas Sievert Date: Tue, 25 Jun 2024 08:54:28 +0200 Subject: [PATCH] - increase delay to fix flaky tests - add new config options to README - some refactoring --- README.md | 40 ++++++++++--------- detekt.yml | 4 ++ .../squit/config/ConfigExtensions.kt | 12 +++--- .../squit/task/SquitRequestTask.kt | 5 +-- .../squit/config/ConfigExtensionsTest.kt | 23 ++++++++--- .../src/squit/project/post_run.groovy | 4 +- .../src/squit/project/pre_run.groovy | 4 +- 7 files changed, 55 insertions(+), 37 deletions(-) diff --git a/README.md b/README.md index 9d23fd14..fae51451 100644 --- a/README.md +++ b/README.md @@ -123,25 +123,27 @@ or `local.conf`) files. `test.conf` files are in the [HOCON](https://github.com/lightbend/config/blob/master/HOCON.md) format and support all of its features. As of the current version, these are the supported properties: -| Name | Description | Example | -| ---------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------- | -| endpoint | The endpoint of your backend to call. | `endpoint = "http://localhost:1234/api"` | -| mediaType | The media type of your content to send. | `mediaType = "application/soap+xml"` | -| method | The method for the request to use. The default is POST and requires a request.xml. Methods like GET do not require one. | `method = "GET"` | -| exclude | Excludes or un-excludes the test or test group. | `exclude = true` | -| ignore | Ignores or un-ignores the test or test group. This means that the test is run, but does not show up in anything generated at the end of the build. | `ignore = true` | -| databaseConfigurations | An array of database configurations to use for pre- and post scripts. See below for details. | `databaseConfigurations = [ { / *content */ } ]` | -| preProcessors | An array of pre processor classes to use. | `preProcessors = ["com.example.ExamplePreProcessor"]` | -| postProcessors | An array of post processor classes to use. | `postProcessors = ["com.example.ExamplePostProcessor"]` | -| preProcessorScripts | An array of paths to groovy pre processor scripts to use. | `preProcessorScripts = [./scripts/pre_processor.groovy]` | -| postProcessorScripts | An array of paths to groovy post processor scripts to use. | `postProcessorScripts = [./scripts/post_processor.groovy]` | -| preRunners | An array of pre runner classes to use. | `preRunners= ["com.example.ExamplePreRunner"]` | -| postRunners | An array of post runner classes to use. | `postRunners = ["com.example.ExamplePostRunner"]` | -| preRunnerScripts | An array of paths to groovy pre runner scripts to use. | `preRunnerScripts = [./scripts/pre_runner.groovy]` | -| postRunnerScripts | An array of paths to groovy post runner scripts to use. | `postRunnerScripts = [./scripts/post_runner.groovy]` | -| headers | A map of headers to use for requests. | `headers = { "some-header": "value" }` | -| title | An optional alternative title for the test. | `title = "Something"` | -| expectedResponseCode | An optional expected HTTP response code. Default is the 200-range. | `expectedResponseCode = 400` | +| Name | Description | Example | +|------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------| +| endpoint | The endpoint of your backend to call. | `endpoint = "http://localhost:1234/api"` | +| mediaType | The media type of your content to send. | `mediaType = "application/soap+xml"` | +| method | The method for the request to use. The default is POST and requires a request.xml. Methods like GET do not require one. | `method = "GET"` | +| exclude | Excludes or un-excludes the test or test group. | `exclude = true` | +| ignore | Ignores or un-ignores the test or test group. This means that the test is run, but does not show up in anything generated at the end of the build. | `ignore = true` | +| databaseConfigurations | An array of database configurations to use for pre- and post scripts. See below for details. | `databaseConfigurations = [ { / *content */ } ]` | +| preProcessors | An array of pre processor classes to use. | `preProcessors = ["com.example.ExamplePreProcessor"]` | +| postProcessors | An array of post processor classes to use. | `postProcessors = ["com.example.ExamplePostProcessor"]` | +| preProcessorScripts | An array of paths to groovy pre processor scripts to use. | `preProcessorScripts = [./scripts/pre_processor.groovy]` | +| postProcessorScripts | An array of paths to groovy post processor scripts to use. | `postProcessorScripts = [./scripts/post_processor.groovy]` | +| preRunners | An array of pre runner classes to use. | `preRunners= ["com.example.ExamplePreRunner"]` | +| preTestTasks | An array of tasks to execute before the test. The order of the entries determines the order of execution. | `preTestTasks = ["DATABASE_SCRIPTS", "PRE_RUNNERS", "PRE_RUNNER_SCRIPTS"]` | +| postRunners | An array of post runner classes to use. | `postRunners = ["com.example.ExamplePostRunner"]` | +| preRunnerScripts | An array of paths to groovy pre runner scripts to use. | `preRunnerScripts = [./scripts/pre_runner.groovy]` | +| postRunnerScripts | An array of paths to groovy post runner scripts to use. | `postRunnerScripts = [./scripts/post_runner.groovy]` | +| preTestTasks | An array of tasks to execute after the test. The order of the entries determines the order of execution. | `postTestTasks = ["DATABASE_SCRIPTS", "POST_RUNNERS", "POST_RUNNER_SCRIPTS"]` | +| headers | A map of headers to use for requests. | `headers = { "some-header": "value" }` | +| title | An optional alternative title for the test. | `title = "Something"` | +| expectedResponseCode | An optional expected HTTP response code. Default is the 200-range. | `expectedResponseCode = 400` | > The parameter `endpoint` is required and the build will fail if it is missing for a test. diff --git a/detekt.yml b/detekt.yml index 3f94d92c..5224c65b 100644 --- a/detekt.yml +++ b/detekt.yml @@ -5,3 +5,7 @@ exceptions: style: MagicNumber: active: false + +complexity: + TooManyFunctions: + ignorePrivate: true diff --git a/src/main/kotlin/de/smartsquare/squit/config/ConfigExtensions.kt b/src/main/kotlin/de/smartsquare/squit/config/ConfigExtensions.kt index 87a576d6..58ab4358 100644 --- a/src/main/kotlin/de/smartsquare/squit/config/ConfigExtensions.kt +++ b/src/main/kotlin/de/smartsquare/squit/config/ConfigExtensions.kt @@ -112,9 +112,9 @@ val Config.preRunnerScripts get() = getSafePathList(PRE_RUN_SCRIPTS) * preTestTasks to execute. * default: PRE_RUNNERS, PRE_RUNNER_SCRIPTS, DATABASE_SCRIPTS */ -val Config.preTestTasks get() = - when (hasPath(PRE_TEST_TASKS)) { - true -> getEnumList(SquitPreTestTask::class.java, PRE_TEST_TASKS)!! +val Config.preTestTasks: List + get() = when (hasPath(PRE_TEST_TASKS)) { + true -> getEnumList(SquitPreTestTask::class.java, PRE_TEST_TASKS) else -> listOf( SquitPreTestTask.PRE_RUNNERS, SquitPreTestTask.PRE_RUNNER_SCRIPTS, @@ -146,9 +146,9 @@ val Config.postRunnerScripts get() = getSafePathList(POST_RUN_SCRIPTS) * postTestTasks to execute. * default: DATABASE_SCRIPTS, POST_RUNNERS, POST_RUNNER_SCRIPTS */ -val Config.postTestTasks get() = - when (hasPath(POST_TEST_TASKS)) { - true -> getEnumList(SquitPostTestTask::class.java, POST_TEST_TASKS)!! +val Config.postTestTasks: List + get() = when (hasPath(POST_TEST_TASKS)) { + true -> getEnumList(SquitPostTestTask::class.java, POST_TEST_TASKS) else -> listOf( SquitPostTestTask.DATABASE_SCRIPTS, SquitPostTestTask.POST_RUNNERS, diff --git a/src/main/kotlin/de/smartsquare/squit/task/SquitRequestTask.kt b/src/main/kotlin/de/smartsquare/squit/task/SquitRequestTask.kt index 5b08253d..417fdd9e 100644 --- a/src/main/kotlin/de/smartsquare/squit/task/SquitRequestTask.kt +++ b/src/main/kotlin/de/smartsquare/squit/task/SquitRequestTask.kt @@ -60,7 +60,6 @@ import java.util.concurrent.TimeUnit * Task for running requests against the given api. Also capable of running existing sql scripts before and after the * request. */ -@Suppress("TooManyFunctions") open class SquitRequestTask : DefaultTask() { /** @@ -250,7 +249,7 @@ open class SquitRequestTask : DefaultTask() { private fun doPreScriptExecutions(config: Config, testDirectoryPath: Path) { config.preTestTasks.forEach { task -> - when (task!!) { + when (task) { SquitPreTestTask.PRE_RUNNERS -> executePreRunners(config) SquitPreTestTask.PRE_RUNNER_SCRIPTS -> executePreRunnerScripts(config) SquitPreTestTask.DATABASE_SCRIPTS -> executePreDatabaseScripts(config, testDirectoryPath) @@ -289,7 +288,7 @@ open class SquitRequestTask : DefaultTask() { private fun doPostScriptExecutions(config: Config, testDirectoryPath: Path) { config.postTestTasks.forEach { task -> - when (task!!) { + when (task) { SquitPostTestTask.POST_RUNNER_SCRIPTS -> executePostRunnerScripts(config) SquitPostTestTask.POST_RUNNERS -> executePostRunners(config) SquitPostTestTask.DATABASE_SCRIPTS -> executePostDatabaseScripts(config, testDirectoryPath) diff --git a/src/test/kotlin/de/smartsquare/squit/config/ConfigExtensionsTest.kt b/src/test/kotlin/de/smartsquare/squit/config/ConfigExtensionsTest.kt index b41446d3..7f87a861 100644 --- a/src/test/kotlin/de/smartsquare/squit/config/ConfigExtensionsTest.kt +++ b/src/test/kotlin/de/smartsquare/squit/config/ConfigExtensionsTest.kt @@ -493,19 +493,32 @@ class ConfigExtensionsTest { call shouldNotThrow AnyException } + @Test + fun `config object with an valid preTestTask and postTestTask`() { + val config = ConfigFactory.parseMap( + mapOf( + "endpoint" to "https://example.com", + "preTestTasks" to listOf("DATABASE_SCRIPTS", "PRE_RUNNERS", "PRE_RUNNER_SCRIPTS"), + "postTestTasks" to emptyList(), + ) + ) + + val call = { config.validate() } + call shouldNotThrow AnyException + } + @Test fun `config object with an invalid preTestTask`() { val config = ConfigFactory.parseMap( mapOf( "endpoint" to "https://example.com", - "preTestTasks" to listOf("[NotExistingTask]"), + "preTestTasks" to listOf("NotExistingTask"), ) ) val call = { config.validate() } - @Suppress("MaxLineLength") call shouldThrow BadValue::class withMessage "hardcoded value: Invalid value at 'preTestTasks': " + - "The enum class SquitPreTestTask has no constant of the name '[NotExistingTask]' " + + "The enum class SquitPreTestTask has no constant of the name 'NotExistingTask' " + "(should be one of [DATABASE_SCRIPTS, PRE_RUNNERS, PRE_RUNNER_SCRIPTS].)" } @@ -514,13 +527,13 @@ class ConfigExtensionsTest { val config = ConfigFactory.parseMap( mapOf( "endpoint" to "https://example.com", - "postTestTasks" to listOf("[NotExistingTask]"), + "postTestTasks" to listOf("NotExistingTask"), ) ) val call = { config.validate() } call shouldThrow BadValue::class withMessage "hardcoded value: Invalid value at 'postTestTasks': " + - "The enum class SquitPostTestTask has no constant of the name '[NotExistingTask]' " + + "The enum class SquitPostTestTask has no constant of the name 'NotExistingTask' " + "(should be one of [DATABASE_SCRIPTS, POST_RUNNERS, POST_RUNNER_SCRIPTS].)" } } diff --git a/src/test/resources/test-project-task-config/src/squit/project/post_run.groovy b/src/test/resources/test-project-task-config/src/squit/project/post_run.groovy index 10174a95..21c5b03a 100644 --- a/src/test/resources/test-project-task-config/src/squit/project/post_run.groovy +++ b/src/test/resources/test-project-task-config/src/squit/project/post_run.groovy @@ -1,5 +1,5 @@ import java.time.Instant -Thread.sleep(1) +Thread.sleep(10) def file = new File(config.getString("rootDir") + "/build/post_run.txt") file.text = Instant.now().toEpochMilli().toString() -Thread.sleep(1) +Thread.sleep(10) diff --git a/src/test/resources/test-project-task-config/src/squit/project/pre_run.groovy b/src/test/resources/test-project-task-config/src/squit/project/pre_run.groovy index 6b653239..1ecdc762 100644 --- a/src/test/resources/test-project-task-config/src/squit/project/pre_run.groovy +++ b/src/test/resources/test-project-task-config/src/squit/project/pre_run.groovy @@ -1,5 +1,5 @@ import java.time.Instant -Thread.sleep(1) +Thread.sleep(10) def file = new File(config.getString("rootDir") + "/build/pre_run.txt") file.text = Instant.now().toEpochMilli().toString() -Thread.sleep(1) +Thread.sleep(10)