How do I use Kotlin Function inside TeamCity KotlinScriptBuildStep - kotlin

I have a TeamCity settings.kts code with configuration of build and I want to write a function inside settings.kts or in a file defined next to settings.kts and use the function inside kotlinScript step that is (KotlinScriptCustomBuildStep). How do I call the function (so that I don't need to write big logic inside kotlinScript.content?
//setting.kts
steps {
kotlinScript {
name = "Doing something"
//language=kotlin
content = """
import JiraReport
println(JiraReport().findLastMessage())
....
""".trimIndent()
}
//JiraReport class
import java.io.BufferedReader
import java.io.IOException
import java.io.InputStreamReader
class JiraReport {
fun findLastMessage(): String {
...
}

Related

Cannot use Duration.seconds method from kotlin.time

I'm trying to use the Duration class from kotlin.time package like this:
Duration.seconds(5)
But I see Expression 'seconds' cannot be invoked as a function. The function 'invoke()' is not found in my intelliJ.
Not sure, how should I call this function then? Or is there something I'm missing?
For time being I have to use 5.toDuration(DurationUnit.SECONDS)
Snippet:
import kotlin.time.Duration
import kotlin.time.ExperimentalTime
#ExperimentalTime
class Temp {
fun test() {
println(Duration.seconds(5))
}
}
I have kotlin ref in build.gradle.kts file
plugins {
id("org.jetbrains.kotlin.jvm") version "1.5.21"
}
Using it with kotest.
Here are my project dependencies.
dependencies {
testImplementation(kotlin("test-junit5"))
testImplementation("io.kotest:kotest-assertions-core-jvm:$kotestVersion")
testImplementation("io.kotest:kotest-runner-junit5-jvm:$kotestVersion")
testImplementation("io.rest-assured:kotlin-extensions:4.3.0")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.12.+")
implementation("com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.12.+")
implementation("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.12.+")
implementation("io.kotest.extensions:kotest-extensions-allure:1.0.1")
}
Make sure you are importing the correct Duration
import kotlin.time.Duration
as there are other Durations.
You can also use the extension function on Int,
import kotlin.time.toDuration
5.toDuration(DurationUnit.SECONDS)
For those looking to use Java Duration, you can do this using #Francesc answer and the information in the link below is (1.toDuration(DurationUnit.SECONDS)).toJavaDuration()
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.time/to-java-duration.html

Why is close() method highlighted as redundant in Intellij, when using CSVPrinter from apcache.commons?

I am using CSVPrinter class from apache.commons.csv, and I am trying to print some lines in a csv file. As I know, we need to call close() method on a FileWriter, after the writing is done. And based on that assumption, I tried to call CSVPrinter.close().However, IntelliJ IDEA warns me that this method is redundant. Besides, examples in https://www.callicoder.com/java-read-write-csv-file-apache-commons-csv/ does not include this method, either. I want to know why is that method redundant and if I just use .flush() everything will be alright?
Here is an example copied from website mentioned above.
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
public class CSVWriter {
private static final String SAMPLE_CSV_FILE = "./sample.csv";
public static void main(String[] args) throws IOException {
try (
BufferedWriter writer = Files.newBufferedWriter(Paths.get(SAMPLE_CSV_FILE));
CSVPrinter csvPrinter = new CSVPrinter(writer, CSVFormat.DEFAULT
.withHeader("ID", "Name", "Designation", "Company"));
) {
csvPrinter.printRecord("1", "Sundar Pichai ♥", "CEO", "Google");
csvPrinter.printRecord("2", "Satya Nadella", "CEO", "Microsoft");
csvPrinter.printRecord("3", "Tim cook", "CEO", "Apple");
csvPrinter.printRecord(Arrays.asList("4", "Mark Zuckerberg", "CEO", "Facebook"));
csvPrinter.flush();
// I added the following line
csvPrinter.close();
}
}
}
As #user207421 explained in the comments.
First: The try-with-resources statement provides an automatic close at the end of its scope.
Second: Flush is redundant before close.

How to add NavDestination to NavigatorProvider?

The extension function of package androidx.navigation have plusAssign, therefore it is equivalent of += but in my code it doesn't find the plusAssign.
class MyNavHostFragment: NavHostFragment() {
override fun createFragmentNavigator(): Navigator<out FragmentNavigator.Destination> {
navController.navigatorProvider += DialogFragmentNavigator(childFragmentManager)
return super.createFragmentNavigator()
}
}
©️ matpag
add below import
import androidx.navigation.plusAssign
Dynamic Feature Module Support
implementation "androidx.navigation:navigation-dynamic-features-fragment:2.3.0-alpha06"
https://developer.android.com/jetpack/androidx/releases/navigation

grails code-coverage cannot acces to method

I´m trying to do test in grails 2 with Coverage, but I cannot acces to method:
It said that:
"java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request..."
class UserController implements ResourceLoaderAware{
def index(Integer max) {
params.max = Math.min(max ?: 10, 100)
respond User.list(params), model:[userInstanceCount: User.count()]
}
}
import grails.test.mixin.Mock
import grails.test.mixin.TestMixin
import grails.test.mixin.support.GrailsUnitTestMixin
import spock.lang.Specification
import com.snt.olucarodashboard.UserController
import grails.test.mixin.TestFor
#TestMixin(GrailsUnitTestMixin)
class UserControllerSpec extends Specification {
UserController controller=new UserController()
void "Test the index action returns the correct model"() {
when: "The index action is executed"
controller.index(0)
}
}
What is the problem?
Thanks
Instead of instantiating the controller yourself, use the #TestFor annotation at the class level
import grails.test.mixin.TestFor
#TestMixin(GrailsUnitTestMixin)
#TestFor( UserController )
class UserControllerSpec extends Specification {
void "Test the index action returns the correct model"() {
when: "The index action is executed"
controller.index(0)
}
}

How to run multiple test suites in another test suite with geb

I would like to run all my test suites. I want to call the test suites in one test suit called Allrun.
Can I call test suites in another test suite. I am looking for something like this:
class Myfirst extends GebReportingSpec {
def myfunction(){
when:''
at mypage1
and:''
element1.text() == "mytext1"
}
}
class Mysecond extends GebReportingSpec {
def mysecondFunction() {
when:''
at mypage2
and:''
element2.text() == "mytext2"
}
}
class AlltestSuites extends GebReportingSpec {
Myfirst myfirst = new Myfirst ()
Mysecond mysecond = new Mysecond ()
def allrun(){
myfirst.myfunction()
mysecond.mysecondFunction()
}
}
How can i do this? Does anyone has an idea
You can create a AllRun.groovy file like below to run all class files in the specified order [Myfirst.class, Mysecond.class, etc].
package geb.groovy
import org.junit.runner.RunWith
import org.junit.runners.Suite
import geb.groovy.scripts.Myfirst
import geb.groovy.scripts.Mysecond
#RunWith(Suite.class)
#Suite.SuiteClasses([Myfirst.class, Mysecond.class])
class CustomJUnitSpecRunner {
}
And in build.gradle systemProperties System.properties, exclude the individual Myfirst.class, Mysecond.class files. So that they run only once through AllRun file.
include 'geb/groovy/AllRun/**'
exclude 'geb/groovy/scripts/**'
Groups of specs can be run using the command below, where package is a directory under src/test/groovy/specs:
mvn test -Dtest=specs.package.*
If you are relying on specs to be run in a particular order, I would reconsider your approach. If you require data in a certain state then make use of setup() and setupSpec() methods. Also consider running related tests within a single spec and run these in the order the are written by using the #Stepwise annotation.
#Stepwise
class MyStepWiseTest extends GebReportingSpec {
def myfunction(){
when:''
at mypage1
and:''
element1.text() == "mytext1"
}
def mysecondFunction() {
when:''
at mypage2
and:''
element2.text() == "mytext2"
}
}