<@U0CHHN4F4> does that mean I can't unit test a sc...
# scripting
@ilya.chernikov does that mean I can't unit test a script which uses args?
Maybe I’m not completely understand your setup. I thought that you have a kts file in your sources, and it contains tests, right? Or you’re trying to run a script from your test manually?
I have: 1 KTS file which contains my script logic (basically, a script which performs some logic based off data returned by shell commands) 1 KTS file which contains a JUnit test class. I’d like to be able to run the JUnit test class against the script with the logic in it. Running the script manually from the tests would also be acceptable, as I’d still be able to verify the behaviour of the script.
Also, for what it’s worth, using the option you mentioned gives the same error:
Copy code
compileKotlin {
    kotlinOptions.freeCompilerArgs += ["-script-templates", "kotlin.script.templates.standard.SimpleScriptTemplate"]
Copy code
e: java.lang.RuntimeException: Primary constructor not found for script template class SimpleScriptTemplate (not found)
        at org.jetbrains.kotlin.codegen.ScriptCodegen.genConstructor(ScriptCodegen.java:157)
        at org.jetbrains.kotlin.codegen.ScriptCodegen.generateBody(ScriptCodegen.java:103)
        at org.jetbrains.kotlin.codegen.MemberCodegen.generate(MemberCodegen.java:130)
        at org.jetbrains.kotlin.codegen.PackageCodegenImpl.generateFile(PackageCodegenImpl.java:120)
Ok, I see. The simplest solution would be to put tests into a regular kotlin file, and run the script with logic from it. You may find samples of this approach in our repo, e.g.: https://github.com/JetBrains/kotlin/blob/master/compiler/tests/org/jetbrains/kotlin/scripts/ScriptTest.kt#L40 (You probably don’t need all the logic of
there, and simply invoke command-line compiler instead.)
Running tests placed into the script is perhaps possible too, but this is not something we have tried, so there could be many problems on the way.
Ok thanks very much :)
Your suggestion worked 🙂 Unfortunately, it takes a very long time (about 5 secondsish per test) for script startup, but it’s a start! Plus, I’m sure support will get a lot better over the next year or so. I’m going to document this into a medium post, thanks for your help!
I’m glad it works. Command line compiler startup is slow now, yes. You can try to speed it up by calling the compiler directly as it is done in our tests. But API used there is not public and we do not guarantee anything about it. We are working on the stable compiler API that could be used in such cases, but it will take some time.
It’s all experimental at this point anyhow, I’m just glad I got something working. I’ll avoid internal APIs and wait out something more stable.