11package kscript.app.resolver
22
3+ import org.junit.jupiter.api.Assertions
34import org.junit.jupiter.api.Test
5+ import java.io.ByteArrayOutputStream
6+ import java.io.File
7+ import java.io.PrintStream
48
59class IntegrationTests {
610
711 @Test
812 fun `it should run kscript and resolve dependencies` (){
9- kscript.app.main(arrayOf(" log4j_dep.kts" ))
13+ // clear .m2 cache
14+ val log4jCached = File (System .getProperty(" user.home" ), " .m2/repository/log4j/log4j/1.2.14/" )
15+ if (log4jCached.isDirectory) {
16+ System .err.println (" Cleaning up cached .m2 copy of log4j" )
17+ log4jCached.deleteRecursively()
18+ }
19+
20+ // clear kscript cache
21+ kscript.app.main(arrayOf(" --clear-cache" ))
22+
23+ // run as when being on commandline
24+ // val observed = captureOutput {
25+ kscript.app.main(arrayOf(" test/resources/depends_on_annot.kts" ))
26+ // }.stdout.replace(System.getProperty("user.home")!!, "")
27+
28+ // Assertions.assertEquals("kotlin -classpath /.m2/repository/log4j/log4j/1.2.14/log4j-1.2.14.jar:/.m2/repository/com/offbytwo/docopt/0.6.0.20150202/docopt-0.6.0.20150202.jar:/.kscript/cache/jar_7cb43f5abd032b31680d6649e4ef6a87/scriplet.jar:/.sdkman/candidates/kotlin/1.5.31/lib/kotlin-script-runtime.jar Main_Depends_on_annot", observed)
29+ // todo complete the assertion to fix the test
1030 }
31+ }
32+
33+
34+ internal data class CapturedOutput (val stdout : String , val stderr : String )
35+
36+ internal fun captureOutput (expr : () -> Any ): CapturedOutput {
37+ val origOut = System .out
38+ val origErr = System .err
39+ // https://stackoverflow.com/questions/216894/get-an-outputstream-into-a-string
40+
41+ val baosOut = ByteArrayOutputStream ()
42+ val baosErr = ByteArrayOutputStream ()
43+
44+ System .setOut(PrintStream (baosOut));
45+ System .setErr(PrintStream (baosErr));
46+
47+
48+ // run the expression
49+ expr()
50+
51+ val stdout = String (baosOut.toByteArray()).trim()
52+ val stderr = String (baosErr.toByteArray()).trim()
53+
54+ System .setOut(origOut)
55+ System .setErr(origErr)
56+
57+ return CapturedOutput (stdout, stderr)
1158}
0 commit comments