forked from VirtusLab/scala-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
InputsTests.scala
183 lines (170 loc) · 6.44 KB
/
InputsTests.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package scala.build.tests
import bloop.rifle.BloopRifleConfig
import com.eed3si9n.expecty.Expecty.expect
import scala.build.Build
import scala.build.input.{
Inputs,
ScalaCliInvokeData,
VirtualJavaFile,
VirtualScalaFile,
VirtualScript
}
import scala.build.input.ElementsUtils.*
import scala.build.options.{BuildOptions, InternalOptions, MaybeScalaVersion}
import scala.build.tests.util.BloopServer
import scala.build.{BuildThreads, Directories, LocalRepo}
import scala.build.internal.Constants
class InputsTests extends munit.FunSuite {
val buildThreads: BuildThreads = BuildThreads.create()
val extraRepoTmpDir: os.Path = os.temp.dir(prefix = "scala-cli-tests-extra-repo-")
val directories: Directories = Directories.under(extraRepoTmpDir)
def bloopConfigOpt: Option[BloopRifleConfig] = Some(BloopServer.bloopConfig)
val buildOptions: BuildOptions = BuildOptions(
internal = InternalOptions(
localRepository = LocalRepo.localRepo(directories.localRepoDir),
keepDiagnostics = true
)
)
test("forced workspace") {
val testInputs = TestInputs(
os.rel / "Foo.scala" ->
"""object Foo {
| def main(): Unit = {
| println("Hello")
| }
|}
|""".stripMargin
)
val forcedWorkspace = os.rel / "workspace"
testInputs.withCustomInputs(viaDirectory = false, forcedWorkspaceOpt = Some(forcedWorkspace)) {
(root, inputs) =>
expect(inputs.workspace == root / forcedWorkspace)
expect(inputs.baseProjectName == "workspace")
}
}
test("project file") {
val projectFileName = Constants.projectFileName
val testInputs = TestInputs(
files = Seq(
os.rel / "custom-dir" / projectFileName -> "",
os.rel / projectFileName -> s"//> using javaProp \"foo=bar\"".stripMargin,
os.rel / "foo.scala" ->
s"""object Foo {
| def main(args: Array[String]): Unit =
| println("Foo")
|}
|""".stripMargin
),
inputArgs = Seq("foo.scala", "custom-dir", projectFileName)
)
testInputs.withBuild(buildOptions, buildThreads, bloopConfigOpt) {
(root, _, buildMaybe) =>
val javaOptsCheck = buildMaybe match {
case Right(build: Build.Successful) =>
build.options.javaOptions.javaOpts.toSeq.head.value.value == "-Dfoo=bar"
case _ => false
}
assert(javaOptsCheck)
assert(os.exists(root / "custom-dir" / Constants.workspaceDirName))
assert(!os.exists(root / Constants.workspaceDirName))
val filesUnderScalaBuild = os.list(root / "custom-dir" / Constants.workspaceDirName)
assert(filesUnderScalaBuild.exists(_.baseName.startsWith("custom-dir")))
assert(!filesUnderScalaBuild.exists(_.baseName.startsWith("project")))
}
}
test("setting root dir without project settings file") {
val testInputs = TestInputs(
files = Seq(
os.rel / "custom-dir" / "foo.scala" ->
s"""object Foo {
| def main(args: Array[String]): Unit =
| println("Foo")
|}
|""".stripMargin,
os.rel / "bar.scala" -> ""
),
inputArgs = Seq("custom-dir", "bar.scala")
)
testInputs.withBuild(buildOptions, buildThreads, bloopConfigOpt) {
(root, _, _) =>
assert(os.exists(root / "custom-dir" / Constants.workspaceDirName))
assert(!os.exists(root / Constants.workspaceDirName))
val filesUnderScalaBuild = os.list(root / "custom-dir" / Constants.workspaceDirName)
assert(filesUnderScalaBuild.exists(_.baseName.startsWith("custom-dir")))
assert(!filesUnderScalaBuild.exists(_.baseName.startsWith("project")))
}
}
test("passing project file and its parent directory") {
val projectFileName = Constants.projectFileName
val testInputs = TestInputs(
files = Seq(
os.rel / "foo.scala" ->
s"""object Foo {
| def main(args: Array[String]): Unit =
| println("Foo")
|}
|""".stripMargin,
os.rel / projectFileName -> ""
),
inputArgs = Seq(".", projectFileName)
)
testInputs.withBuild(buildOptions, buildThreads, bloopConfigOpt) {
(root, inputs, _) =>
assert(os.exists(root / Constants.workspaceDirName))
assert(inputs.elements.projectSettingsFiles.length == 1)
val filesUnderScalaBuild = os.list(root / Constants.workspaceDirName)
assert(filesUnderScalaBuild.exists(_.baseName.startsWith(root.baseName)))
assert(!filesUnderScalaBuild.exists(_.baseName.startsWith("project")))
}
}
test("URLs with query parameters") {
val urlBase =
"https://gist.githubusercontent.com/USER/hash/raw/hash"
val urls = Seq(
s"$urlBase/test.sc",
s"$urlBase/test.sc?foo=bar",
s"$urlBase/test.sc?foo=endsWith.md",
s"http://gist.githubusercontent.com/USER/hash/raw/hash/test.sc?foo=bar",
s"$urlBase/test.scala?foo=endsWith.java",
s"$urlBase/test.java?token=123456789123456789",
s"file:///Users/user/content/test.sc"
)
TestInputs().fromRoot { root =>
val elements = Inputs.validateArgs(
urls,
root,
download = url => Right(Array.emptyByteArray),
stdinOpt = None,
acceptFds = true,
enableMarkdown = true
)(using ScalaCliInvokeData.dummy)
elements match {
case Seq(
Right(Seq(el1: VirtualScript)),
Right(Seq(el2: VirtualScript)),
Right(Seq(el3: VirtualScript)),
Right(Seq(el4: VirtualScript)),
Right(Seq(el5: VirtualScalaFile)),
Right(Seq(el6: VirtualJavaFile)),
Right(Seq(el7: VirtualScript))
) =>
Seq(el1, el2, el3, el4, el5, el6, el7)
.zip(urls)
.foreach {
case (el: VirtualScript, url) =>
expect(el.source == url)
expect(el.content.isEmpty)
expect(el.wrapperPath.endsWith(os.rel / "test.sc"))
case (el: VirtualScalaFile, url) =>
expect(el.source == url)
expect(el.content.isEmpty)
case (el: VirtualJavaFile, url) =>
expect(el.source == url)
expect(el.content.isEmpty)
case _ => fail("Unexpected elements")
}
case _ => fail("Unexpected elements")
}
}
}
}