[2.x] refactor: Use `IO.withTemporaryDirectory` instead of `File.createTempFile` (#9084)

* Use IO.withTemporaryDirectory instead of File.createTempFile

* fix JUnitXmlTestsListenerSpec
This commit is contained in:
kenji yoshida 2026-04-14 04:07:32 +09:00 committed by GitHub
parent 840887820c
commit 8bcc6ae420
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 3 additions and 17 deletions

View File

@ -13,16 +13,14 @@ import java.util.concurrent.atomic.AtomicReference
import testing.{ Event as TEvent, OptionalThrowable, Status as TStatus, TestSelector }
import util.{ AbstractLogger, Level, ControlEvent, LogEvent }
import sbt.io.IO
import sbt.protocol.testing.TestResult
import verify.BasicTestSuite
object JUnitXmlTestsListenerSpec extends BasicTestSuite:
test("JUnitXmlTestsListener should log debug message when writing test report"):
val tempDir = File.createTempFile("junit-test", "")
tempDir.delete()
tempDir.mkdirs()
try
IO.withTemporaryDirectory: tempDir =>
val loggedMessages = new AtomicReference[List[String]](Nil)
val mockLogger = new AbstractLogger:
def getLevel: Level.Value = Level.Debug
@ -66,17 +64,9 @@ object JUnitXmlTestsListenerSpec extends BasicTestSuite:
messages.exists(_.contains("TEST-TestSuite.xml")),
s"Expected log message containing 'TEST-TestSuite.xml', but got: $messages"
)
finally
// Cleanup
if tempDir.exists() then
tempDir.listFiles().foreach(_.delete())
tempDir.delete()
test("JUnitXmlTestsListener should handle null logger gracefully"):
val tempDir = File.createTempFile("junit-test", "")
tempDir.delete()
tempDir.mkdirs()
try
IO.withTemporaryDirectory: tempDir =>
val listener = new JUnitXmlTestsListener(tempDir, false, null)
listener.doInit()
listener.startGroup("TestSuite")
@ -97,9 +87,5 @@ object JUnitXmlTestsListenerSpec extends BasicTestSuite:
// Verify XML file was still created
val xmlFile = new File(tempDir, "TEST-TestSuite.xml")
assert(xmlFile.exists(), "XML file should be created even when logger is null")
finally
if tempDir.exists() then
tempDir.listFiles().foreach(_.delete())
tempDir.delete()
end JUnitXmlTestsListenerSpec