/
EditAlgTest.scala
128 lines (117 loc) · 4.59 KB
/
EditAlgTest.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package org.scalasteward.core.edit
import better.files.File
import org.scalasteward.core.data.Update
import org.scalasteward.core.mock.MockContext.editAlg
import org.scalasteward.core.mock.MockState
import org.scalasteward.core.util.Nel
import org.scalasteward.core.vcs.data.Repo
import org.scalatest.Matchers
import org.scalatest.funsuite.AnyFunSuite
class EditAlgTest extends AnyFunSuite with Matchers {
test("applyUpdate") {
val repo = Repo("fthomas", "scala-steward")
val update = Update.Single("org.typelevel", "cats-core", "1.2.0", Nel.of("1.3.0"))
val file1 = File.temp / "ws/fthomas/scala-steward/build.sbt"
val file2 = File.temp / "ws/fthomas/scala-steward/project/Dependencies.scala"
val state = editAlg
.applyUpdate(repo, update)
.runS(MockState.empty.add(file1, """val catsVersion = "1.2.0"""").add(file2, ""))
.unsafeRunSync()
state shouldBe MockState.empty.copy(
commands = Vector(
List("read", file1.pathAsString),
List("read", file2.pathAsString),
List("read", file1.pathAsString),
List("read", file1.pathAsString),
List("write", file1.pathAsString)
),
logs = Vector(
(None, "Trying heuristic 'strict'"),
(None, "Trying heuristic 'original'")
),
files = Map(file1 -> """val catsVersion = "1.3.0"""", file2 -> "")
)
}
test("applyUpdate with scalafmt update") {
val repo = Repo("fthomas", "scala-steward")
val update = Update.Single("org.scalameta", "scalafmt-core", "2.0.0", Nel.of("2.1.0"))
val scalafmtFile = File.temp / "ws/fthomas/scala-steward/.scalafmt.conf"
val file1 = File.temp / "ws/fthomas/scala-steward/build.sbt"
val state = editAlg
.applyUpdate(repo, update)
.runS(
MockState.empty
.add(
scalafmtFile,
"""maxColumn = 100
|version = 2.0.0
|align.openParenCallSite = false
|""".stripMargin
)
.add(file1, "")
)
.unsafeRunSync()
state shouldBe MockState.empty.copy(
commands = Vector(
List("read", scalafmtFile.pathAsString),
List("read", scalafmtFile.pathAsString),
List("read", scalafmtFile.pathAsString),
List("read", scalafmtFile.pathAsString),
List("read", scalafmtFile.pathAsString),
List("read", scalafmtFile.pathAsString),
List("read", scalafmtFile.pathAsString),
List("write", scalafmtFile.pathAsString)
),
logs = Vector(
(None, "Trying heuristic 'strict'"),
(None, "Trying heuristic 'original'"),
(None, "Trying heuristic 'relaxed'"),
(None, "Trying heuristic 'sliding'"),
(None, "Trying heuristic 'groupId'"),
(None, "Trying heuristic 'specific'")
),
files = Map(
scalafmtFile ->
"""maxColumn = 100
|version = 2.1.0
|align.openParenCallSite = false
|""".stripMargin,
file1 -> ""
)
)
}
def runApplyUpdate(update: Update, files: Map[String, String]): Map[String, String] = {
val repoDir = File.temp / "ws/owner/repo"
val filesInRepoDir = files.map { case (file, content) => repoDir / file -> content }
editAlg
.applyUpdate(Repo("owner", "repo"), update)
.runS(MockState.empty.addFiles(filesInRepoDir))
.map(_.files)
.unsafeRunSync()
.map { case (file, content) => file.toString.replace(repoDir.toString + "/", "") -> content }
}
test("reproduce https://github.com/circe/circe-config/pull/40") {
val update = Update.Single("com.typesafe", "config", "1.3.3", Nel.of("1.3.4"))
val original = Map(
"build.sbt" -> """val config = "1.3.3"""",
"project/plugins.sbt" -> """addSbtPlugin("com.typesafe.sbt" % "sbt-site" % "1.3.3")"""
)
val expected = Map(
"build.sbt" -> """val config = "1.3.3"""", // the version should have been updated here
"project/plugins.sbt" -> """addSbtPlugin("com.typesafe.sbt" % "sbt-site" % "1.3.4")"""
)
runApplyUpdate(update, original) shouldBe expected
}
test("file restriction when sbt update") {
val update = Update.Single("org.scala-sbt", "sbt", "1.1.2", Nel.of("1.2.8"))
val original = Map(
"build.properties" -> """sbt.version=1.1.2""",
"project/plugins.sbt" -> """addSbtPlugin("com.jsuereth" % "sbt-pgp" % "1.1.2")"""
)
val expected = Map(
"build.properties" -> """sbt.version=1.2.8""", // the version should have been updated here
"project/plugins.sbt" -> """addSbtPlugin("com.jsuereth" % "sbt-pgp" % "1.1.2")"""
)
runApplyUpdate(update, original) shouldBe expected
}
}