Skip to content

Commit 6a8bd10

Browse files
committed
fixed mocked/non-mocked time in cache cleanup tests #SCL-24107
1 parent b6f7ccc commit 6a8bd10

File tree

10 files changed

+38
-25
lines changed

10 files changed

+38
-25
lines changed

ideaSupport/src/main/scala/org/jetbrains/sbtidea/download/cachesCleanup/CleanupUtils.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import java.time.temporal.ChronoUnit
99
object CleanupUtils {
1010

1111
val DateFormatter: DateTimeFormatter = DateTimeFormatter.ofPattern("dd MMM yy")
12+
@volatile
1213
private var MockTodayDate: Option[LocalDate] = None
1314

1415
@TestOnly
@@ -21,6 +22,8 @@ object CleanupUtils {
2122
items.map(presenter).map(ListSeparator + _).mkString("\n").indented(2)
2223
}
2324

25+
def cleanupRelevantTime(): LocalDate = MockTodayDate.getOrElse(LocalDate.now())
26+
2427
/**
2528
* Formats a given date into a human-readable string indicating how long ago it was
2629
* (e.g., "2 days ago", "1 week ago", "more than 1 month ago").
@@ -29,7 +32,7 @@ object CleanupUtils {
2932
* @return a string indicating how long ago the given date occurred (e.g., "today", "3 days ago", "more than 2 months ago")
3033
*/
3134
def formatAgo(date: LocalDate): String = {
32-
val now = MockTodayDate.getOrElse(LocalDate.now())
35+
val now = cleanupRelevantTime()
3336

3437
val daysAgo = ChronoUnit.DAYS.between(date, now)
3538
val weeksAgo = daysAgo / 7

ideaSupport/src/main/scala/org/jetbrains/sbtidea/download/cachesCleanup/OldDownloadsDetector.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ object OldDownloadsDetector {
1515
*/
1616
def detectOldDownloads(report: DownloadsReport): Seq[FileMetaInfo] = {
1717
report.fileInfos.filter { fileInfo =>
18-
isLaterThen(fileInfo.metaData.creationDate, RetentionDays)
18+
isOlderThan(fileInfo.metaData.creationDate, RetentionDays)
1919
}
2020
}
2121
}

ideaSupport/src/main/scala/org/jetbrains/sbtidea/download/cachesCleanup/OldSdkDetector.scala

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,18 +77,12 @@ private object OldSdkDetector {
7777
else
7878
KeepPreviousReleasesThreshold
7979

80-
val oldVersionsFromLatest = latestMinorVersions.filter(laterThen(_, KeepLongerLatestMinorVersionsThreshold))
81-
val oldVersionsFromOlder = olderMinorVersions.filter(laterThen(_, threshold))
80+
val oldVersionsFromLatest = latestMinorVersions.filter(olderThan(_, KeepLongerLatestMinorVersionsThreshold))
81+
val oldVersionsFromOlder = olderMinorVersions.filter(olderThan(_, threshold))
8282

8383
oldVersionsFromLatest ++ oldVersionsFromOlder
8484
}
8585

86-
private def laterThen(sdk: IntellijSdkDirInfo, duration: FiniteDuration): Boolean =
87-
laterThen(sdk.dirInfo.creationDate, duration)
88-
89-
private def laterThen(date: LocalDate, duration: FiniteDuration): Boolean = {
90-
val now = LocalDate.now()
91-
val diff = ChronoUnit.DAYS.between(date, now)
92-
diff > duration.toDays
93-
}
86+
private def olderThan(sdk: IntellijSdkDirInfo, duration: FiniteDuration): Boolean =
87+
isOlderThan(sdk.dirInfo.creationDate, duration)
9488
}

ideaSupport/src/main/scala/org/jetbrains/sbtidea/download/cachesCleanup/package.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ package object cachesCleanup {
6666

6767

6868
private[download]
69-
def isLaterThen(date: LocalDate, duration: FiniteDuration): Boolean = {
70-
val now = LocalDate.now()
69+
def isOlderThan(date: LocalDate, duration: FiniteDuration): Boolean = {
70+
val now = CleanupUtils.cleanupRelevantTime()
7171
val diff = ChronoUnit.DAYS.between(date, now)
7272
diff > duration.toDays
7373
}

ideaSupport/src/test/scala/org/jetbrains/sbtidea/download/cachesCleanup/OldDownloadsCleanupTest.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,21 @@ import org.scalatest.matchers.should.Matchers
77

88
import java.nio.file.{Files, Path}
99

10-
class OldDownloadsCleanupTest extends AnyFunSuite with Matchers with BeforeAndAfterEach {
10+
class OldDownloadsCleanupTest extends AnyFunSuite with Matchers with BeforeAndAfterEach with WithMockedTime {
1111

1212
// Create a temporary directory for tests
1313
private var tempDir: Path = _
1414

1515
override def beforeEach(): Unit = {
16-
CleanupUtils.setMockTodayDate(MockedTodayDate)
16+
super.beforeEach()
1717
tempDir = Files.createTempDirectory("old-downloads-cleanup-test")
1818
}
1919

2020
override def afterEach(): Unit = {
2121
if (tempDir != null && Files.exists(tempDir)) {
2222
FileUtils.deleteDirectory(tempDir)
2323
}
24+
super.afterEach()
2425
}
2526

2627
private def createMockedDownloadsReport(fileInfos: Seq[FileMetaInfo]): DownloadsReport = {

ideaSupport/src/test/scala/org/jetbrains/sbtidea/download/cachesCleanup/OldDownloadsDetectorTest.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import org.scalatest.funsuite.AnyFunSuite
55
import java.nio.file.Paths
66
import java.time.LocalDate
77

8-
class OldDownloadsDetectorTest extends AnyFunSuite {
8+
class OldDownloadsDetectorTest extends AnyFunSuite with WithMockedTime {
99

1010
test("detectOldDownloads should identify files older than 1 month") {
11-
val oldDate = LocalDate.now().minusDays(40)
12-
val recentDate = LocalDate.now().minusDays(15)
11+
val oldDate = mockedNow().minusDays(40)
12+
val recentDate = mockedNow().minusDays(15)
1313

1414
val oldFile = FileMetaInfo(
1515
Paths.get("/tmp/old-file.zip"),
@@ -33,7 +33,7 @@ class OldDownloadsDetectorTest extends AnyFunSuite {
3333
}
3434

3535
test("detectOldDownloads should return empty list when no files are old") {
36-
val recentDate = LocalDate.now().minusDays(15)
36+
val recentDate = mockedNow().minusDays(15)
3737

3838
val recentFile = FileMetaInfo(
3939
Paths.get("/tmp/recent-file.zip"),

ideaSupport/src/test/scala/org/jetbrains/sbtidea/download/cachesCleanup/OldSdkCleanupTest.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,21 @@ import org.scalatest.matchers.should.Matchers
77

88
import java.nio.file.{Files, Path}
99

10-
class OldSdkCleanupTest extends AnyFunSuite with Matchers with BeforeAndAfterEach {
10+
class OldSdkCleanupTest extends AnyFunSuite with Matchers with BeforeAndAfterEach with WithMockedTime {
1111

1212
// Create a temporary directory for tests
1313
private var tempDir: Path = _
1414

1515
override def beforeEach(): Unit = {
16-
CleanupUtils.setMockTodayDate(MockedTodayDate)
16+
super.beforeEach()
1717
tempDir = Files.createTempDirectory("old-sdk-cleanup-test")
1818
}
1919

2020
override def afterEach(): Unit = {
2121
if (tempDir != null && Files.exists(tempDir)) {
2222
FileUtils.deleteDirectory(tempDir)
2323
}
24+
super.afterEach()
2425
}
2526

2627
private def createMockSdkDirectories(sdkInfos: Seq[IntellijSdkDirInfo]): Seq[Path] = {

ideaSupport/src/test/scala/org/jetbrains/sbtidea/download/cachesCleanup/OldSdkDetectorTest.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ import org.jetbrains.sbtidea.download.cachesCleanup.TestUtils.createSdkInfoMock
44
import org.scalatest.funsuite.AnyFunSuite
55
import org.scalatest.matchers.should.Matchers
66

7-
class OldSdkDetectorTest extends AnyFunSuite with Matchers {
8-
7+
class OldSdkDetectorTest extends AnyFunSuite with Matchers with WithMockedTime {
98
test("detectOldSdks") {
109
val sdkInfos = Seq(
1110
// 242 major version (older than 2 releases from the latest release)

ideaSupport/src/test/scala/org/jetbrains/sbtidea/download/cachesCleanup/TestUtils.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ object TestUtils {
5555
message.replaceAll("Total size: [0-9,.]+\\s*[KMGT]?B", "Total size: SIZE_PLACEHOLDER")
5656

5757
def createFileMetaInfo(file: Path, daysAgo: Int): FileMetaInfo = {
58-
val creationDate = LocalDate.now().minusDays(daysAgo)
58+
val creationDate = MockedTodayDate.minusDays(daysAgo)
5959
FileMetaInfo(file, DirectoryMetaData(creationDate))
6060
}
6161

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.jetbrains.sbtidea.download.cachesCleanup
2+
3+
import org.jetbrains.sbtidea.download.cachesCleanup.TestUtils.MockedTodayDate
4+
import org.scalatest.{BeforeAndAfterEach, Suite}
5+
6+
import java.time.LocalDate
7+
8+
trait WithMockedTime extends BeforeAndAfterEach { this: Suite =>
9+
def mockedNow(): LocalDate = MockedTodayDate
10+
11+
override def beforeEach(): Unit = {
12+
CleanupUtils.setMockTodayDate(MockedTodayDate)
13+
super.beforeEach()
14+
}
15+
}

0 commit comments

Comments
 (0)