Skip to content

Commit

Permalink
Improve build target names
Browse files Browse the repository at this point in the history
  • Loading branch information
MaciejG604 committed Jun 13, 2023
1 parent 83c41ab commit 4c62231
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 11 deletions.
9 changes: 2 additions & 7 deletions modules/build/src/main/scala/scala/build/input/Inputs.scala
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ final case class Inputs(
object Inputs {
private def forValidatedElems(
validElems: Seq[Element],
baseProjectName: String,
workspace: os.Path,
needsHash: Boolean,
workspaceOrigin: WorkspaceOrigin,
Expand All @@ -152,7 +151,7 @@ object Inputs {
updatedElems,
defaultMainClassElemOpt,
workspace,
baseProjectName,
workspace.baseName,
mayAppendHash = needsHash,
workspaceOrigin = Some(workspaceOrigin),
enableMarkdown = enableMarkdown,
Expand Down Expand Up @@ -318,7 +317,6 @@ object Inputs {
private def forNonEmptyArgs(
args: Seq[String],
cwd: os.Path,
baseProjectName: String,
download: String => Either[String, Array[Byte]],
stdinOpt: => Option[Array[Byte]],
scriptSnippetList: List[String],
Expand Down Expand Up @@ -407,7 +405,6 @@ object Inputs {
else
Right(forValidatedElems(
validElems,
baseProjectName,
workspace,
needsHash,
workspaceOrigin0,
Expand All @@ -423,7 +420,6 @@ object Inputs {
def apply(
args: Seq[String],
cwd: os.Path,
baseProjectName: String = "project",
defaultInputs: () => Option[Inputs] = () => None,
download: String => Either[String, Array[Byte]] = _ => Left("URL not supported"),
stdinOpt: => Option[Array[Byte]] = None,
Expand All @@ -448,7 +444,6 @@ object Inputs {
forNonEmptyArgs(
args,
cwd,
baseProjectName,
download,
stdinOpt,
scriptSnippetList,
Expand All @@ -469,7 +464,7 @@ object Inputs {
elements = Nil,
defaultMainClassElement = None,
workspace = workspace,
baseProjectName = "project",
baseProjectName = workspace.baseName,
mayAppendHash = true,
workspaceOrigin = None,
enableMarkdown = enableMarkdown,
Expand Down
13 changes: 13 additions & 0 deletions modules/build/src/test/scala/scala/build/tests/InputsTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class InputsTests extends munit.FunSuite {
testInputs.withCustomInputs(viaDirectory = false, forcedWorkspaceOpt = Some(forcedWorkspace)) {
(root, inputs) =>
expect(inputs.workspace == root / forcedWorkspace)
expect(inputs.baseProjectName == "workspace")
}
}

Expand Down Expand Up @@ -65,6 +66,10 @@ class InputsTests extends munit.FunSuite {
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")))
}
}

Expand All @@ -85,6 +90,10 @@ class InputsTests extends munit.FunSuite {
(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")))
}
}

Expand All @@ -106,6 +115,10 @@ class InputsTests extends munit.FunSuite {
(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")))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,7 @@ object SharedOptions {
path
}
.map(ResourceDirectory.apply)

val maybeInputs = Inputs(
args,
Os.pwd,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,14 +182,15 @@ abstract class CompileTestDefinitions(val scalaVersionOpt: Option[String])

test("no arg") {
simpleInputs.fromRoot { root =>
val projectFilePrefix = root.baseName + "_"
os.proc(TestUtil.cli, "compile", extraOptions, ".").call(cwd = root)
val projDirs = os.list(root / Constants.workspaceDirName)
.filter(_.last.startsWith("project_"))
.filter(_.last.startsWith(projectFilePrefix))
.filter(os.isDir(_))
expect(projDirs.length == 1)
val projDir = projDirs.head
val projDirName = projDir.last
val elems = projDirName.stripPrefix("project_").split("[-_]").toSeq
val elems = projDirName.stripPrefix(projectFilePrefix).split("[-_]").toSeq
expect(elems.length == 1)
}
}
Expand Down Expand Up @@ -253,13 +254,15 @@ abstract class CompileTestDefinitions(val scalaVersionOpt: Option[String])
expect(isDefinedTestPathInClassPath)
checkIfCompileOutputIsCopied("Tests", tempOutput)

val projectFilePrefix = root.baseName + "_"

val projDirs = os.list(root / Constants.workspaceDirName)
.filter(_.last.startsWith("project_"))
.filter(_.last.startsWith(projectFilePrefix))
.filter(os.isDir(_))
expect(projDirs.length == 1)
val projDir = projDirs.head
val projDirName = projDir.last
val elems = projDirName.stripPrefix("project_").split("[-_]").toSeq
val elems = projDirName.stripPrefix(projectFilePrefix).split("[-_]").toSeq
expect(elems.length == 2)
expect(elems.toSet.size == 2)
}
Expand Down Expand Up @@ -696,4 +699,50 @@ abstract class CompileTestDefinitions(val scalaVersionOpt: Option[String])
expect(compileWithCompilerRes.out.trim().contains(compilerArtifactName))
}
}

test(s"reuse cached project file under .scala-build") {
TestInputs(os.rel / "main.scala" ->
"""object Main extends App {
| println("Hello")
|}""".stripMargin)
.fromRoot { root =>
val firstRes = os.proc(
TestUtil.cli,
"compile",
"main.scala"
).call(cwd = root, mergeErrIntoOut = true)

expect(os.list(root / Constants.workspaceDirName).count(
_.baseName.startsWith(root.baseName)
) == 1)
val firstOutput = TestUtil.normalizeConsoleOutput(firstRes.out.text())
expect(firstOutput.contains("Compiled project"))

val differentRes = os.proc(
TestUtil.cli,
"compile",
"main.scala",
"--jvm",
"8"
).call(cwd = root, mergeErrIntoOut = true)

expect(os.list(root / Constants.workspaceDirName).count(
_.baseName.startsWith(root.baseName)
) == 2)
val differentOutput = TestUtil.normalizeConsoleOutput(differentRes.out.text())
expect(differentOutput.contains("Compiled project"))

val secondRes = os.proc(
TestUtil.cli,
"compile",
"main.scala"
).call(cwd = root, mergeErrIntoOut = true)

expect(os.list(root / Constants.workspaceDirName).count(
_.baseName.startsWith(root.baseName)
) == 2)
val secondOutput = TestUtil.normalizeConsoleOutput(secondRes.out.text())
expect(!secondOutput.contains("Compiled project"))
}
}
}

0 comments on commit 4c62231

Please sign in to comment.