Skip to content

Commit cafa83c

Browse files
committed
Add tests for topWrapper
1 parent 643bf56 commit cafa83c

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package scala.build.tests
2+
3+
import com.eed3si9n.expecty.Expecty.expect
4+
import coursier.cache.CacheLogger
5+
import org.scalajs.logging.{NullLogger, Logger as ScalaJsLogger}
6+
7+
import java.util.concurrent.TimeUnit
8+
import scala.build.Ops.*
9+
import scala.build.{Build, BuildThreads, Directories, GeneratedSource, LocalRepo}
10+
import scala.build.options.{BuildOptions, InternalOptions, Scope}
11+
import scala.build.bsp.{
12+
BspServer,
13+
ScalaScriptBuildServer,
14+
WrappedSourceItem,
15+
WrappedSourcesItem,
16+
WrappedSourcesParams,
17+
WrappedSourcesResult
18+
}
19+
import scala.collection.mutable.ArrayBuffer
20+
import scala.jdk.CollectionConverters.*
21+
import scala.build.bsp.{WrappedSourcesItem, WrappedSourcesResult}
22+
import scala.build.internal.ClassCodeWrapper
23+
24+
class BspServerTests extends munit.FunSuite {
25+
val extraRepoTmpDir = os.temp.dir(prefix = "scala-cli-tests-actionable-diagnostic-")
26+
val directories = Directories.under(extraRepoTmpDir)
27+
val baseOptions = BuildOptions(
28+
internal = InternalOptions(
29+
localRepository = LocalRepo.localRepo(directories.localRepoDir)
30+
)
31+
)
32+
val buildThreads = BuildThreads.create()
33+
34+
def getScriptBuildServer(
35+
generatedSources: Seq[GeneratedSource],
36+
workspace: os.Path,
37+
scope: Scope = Scope.Main
38+
): ScalaScriptBuildServer = {
39+
val bspServer = new BspServer(null, null, null)
40+
bspServer.setGeneratedSources(Scope.Main, generatedSources)
41+
bspServer.setProjectName(workspace, "test", scope)
42+
43+
bspServer
44+
}
45+
46+
test("correct topWrapper and bottomWrapper for wrapped scripts") {
47+
val testInputs = TestInputs(
48+
os.rel / "script.sc" ->
49+
s"""def msg: String = "Hello"
50+
|
51+
|println(msg)
52+
|""".stripMargin
53+
)
54+
55+
testInputs.withBuild(baseOptions, buildThreads, None) {
56+
(root, _, maybeBuild) =>
57+
val build: Build = maybeBuild.orThrow
58+
59+
build match {
60+
case success: Build.Successful =>
61+
val generatedSources = success.generatedSources
62+
expect(generatedSources.size == 1)
63+
val wrappedScript = generatedSources.head
64+
val wrappedScriptCode = os.read(wrappedScript.generated)
65+
val topWrapper = wrappedScriptCode
66+
.linesIterator
67+
.take(wrappedScript.topWrapperLineCount)
68+
.mkString("", System.lineSeparator(), System.lineSeparator())
69+
70+
val bspServer = getScriptBuildServer(generatedSources, root)
71+
72+
val wrappedSourcesResult: WrappedSourcesResult = bspServer
73+
.buildTargetWrappedSources(new WrappedSourcesParams(ArrayBuffer.empty.asJava))
74+
.get(10, TimeUnit.SECONDS)
75+
val wrappedItems = wrappedSourcesResult.getItems.asScala
76+
77+
expect(wrappedItems.size == 1)
78+
expect(wrappedItems.head.getSources().asScala.size == 1)
79+
80+
expect(wrappedItems.head.getSources().asScala.head.getTopWrapper == topWrapper)
81+
82+
case _ => munit.Assertions.fail("Build Failed")
83+
}
84+
85+
}
86+
}
87+
}

0 commit comments

Comments
 (0)