Skip to content

Commit 0036400

Browse files
authored
Merge pull request #2334 from MaciejG604/ignore-url-query-params
Ignore query parameters in input urls
2 parents 2443d24 + 04db4b9 commit 0036400

File tree

2 files changed

+72
-6
lines changed

2 files changed

+72
-6
lines changed

modules/build/src/main/scala/scala/build/input/Element.scala

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,19 @@ sealed abstract class Virtual extends SingleElement {
2828
}
2929

3030
object Virtual {
31+
val urlPathWithQueryParamsRegex = "https?://.*/([^/^?]+)(/?.*)?$".r
3132
def apply(path: String, content: Array[Byte]): Virtual = {
32-
val wrapperPath = os.sub / path.split("/").last
33-
if path.endsWith(".scala") then VirtualScalaFile(content, path)
34-
else if path.endsWith(".java") then VirtualJavaFile(content, path)
35-
else if path.endsWith(".sc") then VirtualScript(content, path, wrapperPath)
36-
else if path.endsWith(".md") then VirtualMarkdownFile(content, path, wrapperPath)
33+
val filename = path match {
34+
case urlPathWithQueryParamsRegex(name, _) => name
35+
case _ => path.split("/").last
36+
}
37+
38+
val wrapperPath = os.sub / filename
39+
40+
if filename.endsWith(".scala") then VirtualScalaFile(content, path)
41+
else if filename.endsWith(".java") then VirtualJavaFile(content, path)
42+
else if filename.endsWith(".sc") then VirtualScript(content, path, wrapperPath)
43+
else if filename.endsWith(".md") then VirtualMarkdownFile(content, path, wrapperPath)
3744
else VirtualData(content, path)
3845
}
3946
}

modules/build/src/test/scala/scala/build/tests/InputsTests.scala

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@ import bloop.rifle.BloopRifleConfig
44
import com.eed3si9n.expecty.Expecty.expect
55

66
import scala.build.Build
7-
import scala.build.input.Inputs
7+
import scala.build.input.{
8+
Inputs,
9+
ScalaCliInvokeData,
10+
VirtualJavaFile,
11+
VirtualScalaFile,
12+
VirtualScript
13+
}
814
import scala.build.input.ElementsUtils.*
915
import scala.build.options.{BuildOptions, InternalOptions, MaybeScalaVersion}
1016
import scala.build.tests.util.BloopServer
@@ -121,4 +127,57 @@ class InputsTests extends munit.FunSuite {
121127
assert(!filesUnderScalaBuild.exists(_.baseName.startsWith("project")))
122128
}
123129
}
130+
131+
test("URLs with query parameters") {
132+
val urlBase =
133+
"https://gist.githubusercontent.com/USER/hash/raw/hash"
134+
val urls = Seq(
135+
s"$urlBase/test.sc",
136+
s"$urlBase/test.sc?foo=bar",
137+
s"$urlBase/test.sc?foo=endsWith.md",
138+
s"http://gist.githubusercontent.com/USER/hash/raw/hash/test.sc?foo=bar",
139+
s"$urlBase/test.scala?foo=endsWith.java",
140+
s"$urlBase/test.java?token=123456789123456789",
141+
s"file:///Users/user/content/test.sc"
142+
)
143+
144+
TestInputs().fromRoot { root =>
145+
val elements = Inputs.validateArgs(
146+
urls,
147+
root,
148+
download = url => Right(Array.emptyByteArray),
149+
stdinOpt = None,
150+
acceptFds = true,
151+
enableMarkdown = true
152+
)(using ScalaCliInvokeData.dummy)
153+
154+
elements match {
155+
case Seq(
156+
Right(Seq(el1: VirtualScript)),
157+
Right(Seq(el2: VirtualScript)),
158+
Right(Seq(el3: VirtualScript)),
159+
Right(Seq(el4: VirtualScript)),
160+
Right(Seq(el5: VirtualScalaFile)),
161+
Right(Seq(el6: VirtualJavaFile)),
162+
Right(Seq(el7: VirtualScript))
163+
) =>
164+
Seq(el1, el2, el3, el4, el5, el6, el7)
165+
.zip(urls)
166+
.foreach {
167+
case (el: VirtualScript, url) =>
168+
expect(el.source == url)
169+
expect(el.content.isEmpty)
170+
expect(el.wrapperPath.endsWith(os.rel / "test.sc"))
171+
case (el: VirtualScalaFile, url) =>
172+
expect(el.source == url)
173+
expect(el.content.isEmpty)
174+
case (el: VirtualJavaFile, url) =>
175+
expect(el.source == url)
176+
expect(el.content.isEmpty)
177+
case _ => fail("Unexpected elements")
178+
}
179+
case _ => fail("Unexpected elements")
180+
}
181+
}
182+
}
124183
}

0 commit comments

Comments
 (0)