From 45308747b1d56fdb848fad92832d8cfdf334b66c Mon Sep 17 00:00:00 2001 From: Alexandre Archambault Date: Wed, 21 Dec 2022 12:39:16 +0100 Subject: [PATCH] Erase things in working dir in publish Working dir is a directory of our own, this shouldn't be a problem. This was making 'scala-cli publish local -w .' fail with file-already-exists exceptions. --- .../scala/cli/commands/publish/Publish.scala | 6 +- .../cli/integration/PublishTestsDefault.scala | 83 ++++++++++++++----- 2 files changed, 64 insertions(+), 25 deletions(-) diff --git a/modules/cli/src/main/scala/scala/cli/commands/publish/Publish.scala b/modules/cli/src/main/scala/scala/cli/commands/publish/Publish.scala index 8820da9eaf..b8c55e1fd5 100644 --- a/modules/cli/src/main/scala/scala/cli/commands/publish/Publish.scala +++ b/modules/cli/src/main/scala/scala/cli/commands/publish/Publish.scala @@ -517,7 +517,7 @@ object Publish extends ScalaCommand[PublishOptions] with BuildCommandHelpers { } val content = Library.libraryJar(build, mainClassOpt) val dest = workingDir / org / s"$moduleName-$ver.jar" - os.write(dest, content, createFolders = true) + os.write.over(dest, content, createFolders = true) dest } @@ -525,7 +525,7 @@ object Publish extends ScalaCommand[PublishOptions] with BuildCommandHelpers { if (publishOptions.contextual(isCi).sourceJar.getOrElse(true)) { val content = PackageCmd.sourceJar(build, now.toEpochMilli) val sourceJar = workingDir / org / s"$moduleName-$ver-sources.jar" - os.write(sourceJar, content, createFolders = true) + os.write.over(sourceJar, content, createFolders = true) Some(sourceJar) } else @@ -538,7 +538,7 @@ object Publish extends ScalaCommand[PublishOptions] with BuildCommandHelpers { case Some(docBuild) => val docJarPath = value(PackageCmd.docJar(docBuild, logger, Nil)) val docJar = workingDir / org / s"$moduleName-$ver-javadoc.jar" - os.copy(docJarPath, docJar, createFolders = true) + os.copy.over(docJarPath, docJar, createFolders = true) Some(docJar) } else diff --git a/modules/integration/src/test/scala/scala/cli/integration/PublishTestsDefault.scala b/modules/integration/src/test/scala/scala/cli/integration/PublishTestsDefault.scala index 2d5b66913c..94fb32a57e 100644 --- a/modules/integration/src/test/scala/scala/cli/integration/PublishTestsDefault.scala +++ b/modules/integration/src/test/scala/scala/cli/integration/PublishTestsDefault.scala @@ -4,31 +4,35 @@ import com.eed3si9n.expecty.Expecty.expect class PublishTestsDefault extends PublishTestDefinitions(scalaVersionOpt = None) { - test("publish local") { - val testOrg = "test-local-org.sth" - val testName = "my-proj" - val testVersion = "1.5.6" + private object PublishTestInputs { + def testOrg = "test-local-org.sth" + def testName = "my-proj" + def testVersion = "1.5.6" + def sbv = "2.13" + def projFile(message: String): String = + s"""//> using publish.organization "$testOrg" + |//> using publish.name "$testName" + |//> using publish.version "$testVersion" + | + |//> using scala "$sbv" + |//> using lib "com.lihaoyi::os-lib:0.8.1" + | + |object Project { + | def message = "$message" + | + | def main(args: Array[String]): Unit = + | println(message) + |} + |""".stripMargin val inputs = TestInputs( - os.rel / "Project.scala" -> - s"""//> using publish.organization "$testOrg" - |//> using publish.name "$testName" - |//> using publish.version "$testVersion" - | - |//> using scala "2.13" - |//> using lib "com.lihaoyi::os-lib:0.8.1" - | - |object Project { - | def message = "Hello" - | - | def main(args: Array[String]): Unit = - | println(message) - |} - |""".stripMargin + os.rel / "Project.scala" -> projFile("Hello") ) + } + test("publish local") { val expectedFiles = { - val modName = s"${testName}_2.13" - val base = os.rel / testOrg / modName / testVersion + val modName = s"${PublishTestInputs.testName}_2.13" + val base = os.rel / PublishTestInputs.testOrg / modName / PublishTestInputs.testVersion val baseFiles = Seq( base / "jars" / s"$modName.jar", base / "docs" / s"$modName-javadoc.jar", @@ -45,7 +49,7 @@ class PublishTestsDefault extends PublishTestDefinitions(scalaVersionOpt = None) .toSet } - inputs.fromRoot { root => + PublishTestInputs.inputs.fromRoot { root => os.proc(TestUtil.cli, "publish", "local", "Project.scala", "--ivy2-home", os.rel / "ivy2") .call(cwd = root) val ivy2Local = root / "ivy2" / "local" @@ -64,6 +68,41 @@ class PublishTestsDefault extends PublishTestDefinitions(scalaVersionOpt = None) } } + test("publish local twice") { + PublishTestInputs.inputs.fromRoot { root => + def publishLocal(): os.CommandResult = + os.proc( + TestUtil.cli, + "publish", + "local", + "Project.scala", + "--ivy2-home", + os.rel / "ivy2", + "--working-dir", + os.rel / "work-dir" + ) + .call(cwd = root) + def output(): String = + os.proc( + TestUtil.cs, + s"-J-Divy.home=${root / "ivy2"}", + "launch", + s"${PublishTestInputs.testOrg}:${PublishTestInputs.testName}_${PublishTestInputs.sbv}:${PublishTestInputs.testVersion}" + ) + .call(cwd = root) + .out.trim() + + publishLocal() + val output1 = output() + expect(output1 == "Hello") + + os.write.over(root / "Project.scala", PublishTestInputs.projFile("olleH")) + publishLocal() + val output2 = output() + expect(output2 == "olleH") + } + } + test("Pure Java") { val testOrg = "test-org.foo" val testName = "foo"