Skip to content

Commit 8cb08ea

Browse files
authored
Verify the example .scala-steward.conf (#3571)
* Verify the example `.scala-steward.conf` This checks that the example `.scala-steward.conf` in the docs is equivalent to the JSON one gets when parsing, decoding and encoding that example. With this change, the `docs/mdoc` task fails currently with: ```diff error: repo-specific-configuration.md:211:1: Diff between parsed input (-) and encoded object (+): "groupId" : "com.example", - "version" : "2." + "version" : { + "prefix" : "2." + } }, "pullRequests" : { - "frequency" : "30 day" + "frequency" : "30 days" } "pullRequests" : { - "frequency" : "14 day" + "frequency" : "14 days" } "signoffCommits" : true, - "updatePullRequests" : "always | on-conflicts | never", + "updatePullRequests" : "on-conflicts", "updates" : { "groupId" : "org.acme", - "version" : "1.0" + "version" : { + "prefix" : "1.0" + } } "groupId" : "com.example", - "version" : "1.1." + "version" : { + "prefix" : "1.1." + } } ``` In subsequent commits I'll will fix these reported differences. Closes: #1341 * Change "day" to "days" if the value > 1 * Encode `VersionPattern` as `String` if only `prefix` is defined * Use a valid value for `updatePullRequests`
1 parent 876f4fb commit 8cb08ea

File tree

6 files changed

+71
-15
lines changed

6 files changed

+71
-15
lines changed

build.sbt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import com.typesafe.sbt.packager.docker.*
2-
import sbtcrossproject.{CrossProject, CrossType, Platform}
32
import org.typelevel.sbt.gha.JavaSpec.Distribution.Temurin
43
import org.typelevel.scalacoptions.ScalacOptions
4+
import sbtcrossproject.{CrossProject, CrossType, Platform}
55

66
/// variables
77

@@ -250,7 +250,9 @@ lazy val docs = myCrossProject("docs")
250250
.enablePlugins(MdocPlugin)
251251
.settings(noPublishSettings)
252252
.settings(
253+
libraryDependencies ++= Seq(Dependencies.munitDiff),
253254
scalacOptions += "-Ytasty-reader",
255+
tpolecatExcludeOptions := Set(ScalacOptions.fatalWarnings),
254256
mdocIn := baseDirectory.value / ".." / "mdoc",
255257
mdocOut := (LocalRootProject / baseDirectory).value / "docs",
256258
mdocVariables := Map(

docs/repo-specific-configuration.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ updates.fileExtensions = [".scala", ".sbt", ".sbt.shared", ".sc", ".yml", ".md",
139139
# you don't change it yourself.
140140
# If "never", Scala Steward will never update the PR
141141
# Default: "on-conflicts"
142-
updatePullRequests = "always" | "on-conflicts" | "never"
142+
updatePullRequests = "always"
143143

144144
# If set, Scala Steward will use this message template for the commit messages and PR titles.
145145
# Supported variables: ${artifactName}, ${currentVersion}, ${nextVersion} and ${default}
@@ -180,11 +180,11 @@ dependencyOverrides = [
180180
},
181181
{
182182
dependency = { groupId = "com.example", artifactId = "foo" },
183-
pullRequests = { frequency = "30 day" },
183+
pullRequests = { frequency = "30 days" },
184184
},
185185
{
186186
dependency = { groupId = "com.example" },
187-
pullRequests = { frequency = "14 day" },
187+
pullRequests = { frequency = "14 days" },
188188
}
189189
]
190190

modules/core/src/main/scala/org/scalasteward/core/repoconfig/VersionPattern.scala

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ final case class VersionPattern(
3232
suffix.forall(version.endsWith) &&
3333
exact.forall(_ === version) &&
3434
contains.forall(version.contains)
35+
36+
/** Returns `true` if only `prefix` is defined. */
37+
private def isPrefixPattern: Boolean =
38+
prefix.isDefined && suffix.isEmpty && exact.isEmpty && contains.isEmpty
3539
}
3640

3741
object VersionPattern {
@@ -41,6 +45,9 @@ object VersionPattern {
4145
implicit val versionPatternDecoder: Decoder[VersionPattern] =
4246
deriveDecoder[VersionPattern].or(Decoder[String].map(s => VersionPattern(prefix = Some(s))))
4347

44-
implicit val versionPatternEncoder: Encoder[VersionPattern] =
45-
deriveEncoder
48+
implicit val versionPatternEncoder: Encoder[VersionPattern] = {
49+
val prefixEncoder = Encoder.encodeOption[String]
50+
val vpEncoder = deriveEncoder[VersionPattern]
51+
Encoder.instance(vp => if (vp.isPrefixPattern) prefixEncoder(vp.prefix) else vpEncoder(vp))
52+
}
4653
}

modules/docs/mdoc/repo-specific-configuration.md

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@ The `[.]scala-steward.conf` configuration file can be located in the root of you
55
If a configuration file exists in more than one location, only the first found file is taken into account.
66

77
```scala mdoc:passthrough
8-
import io.circe.refined._
9-
import io.circe.syntax._
10-
import org.scalasteward.core.repoconfig._
8+
import io.circe.refined.*
9+
import io.circe.syntax.*
10+
import org.scalasteward.core.repoconfig.*
1111

12-
print("```properties")
13-
print(s"""
12+
val input = s"""
1413
# pullRequests.frequency allows to control how often or when Scala Steward
1514
# is allowed to create pull requests.
1615
#
@@ -145,7 +144,7 @@ updates.fileExtensions = [".scala", ".sbt", ".sbt.shared", ".sc", ".yml", ".md",
145144
# you don't change it yourself.
146145
# If "never", Scala Steward will never update the PR
147146
# Default: ${PullRequestUpdateStrategy.default.asJson.noSpaces}
148-
updatePullRequests = "always" | "on-conflicts" | "never"
147+
updatePullRequests = "always"
149148

150149
# If set, Scala Steward will use this message template for the commit messages and PR titles.
151150
# Supported variables: $${artifactName}, $${currentVersion}, $${nextVersion} and $${default}
@@ -186,11 +185,11 @@ dependencyOverrides = [
186185
},
187186
{
188187
dependency = { groupId = "com.example", artifactId = "foo" },
189-
pullRequests = { frequency = "30 day" },
188+
pullRequests = { frequency = "30 days" },
190189
},
191190
{
192191
dependency = { groupId = "com.example" },
193-
pullRequests = { frequency = "14 day" },
192+
pullRequests = { frequency = "14 days" },
194193
}
195194
]
196195

@@ -207,7 +206,12 @@ reviewers = [ "username1", "username2" ]
207206
# If true, Scala Steward will sign off all commits (e.g. `git --signoff`).
208207
# Default: false
209208
signoffCommits = true
210-
""")
209+
"""
210+
211+
DocChecker.verifyParsedEqualsEncoded[RepoConfig](input)
212+
213+
print("```properties")
214+
print(input)
211215
println("```")
212216
```
213217

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2018-2025 Scala Steward contributors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import io.circe.config.parser
18+
import io.circe.{Decoder, Encoder}
19+
import munit.diff.Diff
20+
21+
object DocChecker {
22+
def verifyParsedEqualsEncoded[A](input: String)(implicit
23+
decoder: Decoder[A],
24+
encoder: Encoder[A]
25+
): Unit = {
26+
val res = parser.parse(input).flatMap { jsonFromStr =>
27+
decoder.decodeJson(jsonFromStr).flatMap { a =>
28+
val jsonFromObj = encoder.apply(a)
29+
val diff = Diff(
30+
jsonFromObj.deepDropNullValues.spaces2SortKeys,
31+
jsonFromStr.deepDropNullValues.spaces2SortKeys
32+
)
33+
if (diff.isEmpty) Right(())
34+
else {
35+
val msg = "Diff between parsed input (-) and encoded object (+):\n" + diff.unifiedDiff
36+
Left(new Throwable(msg))
37+
}
38+
}
39+
}
40+
res.fold(throw _, identity)
41+
}
42+
}

project/Dependencies.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ object Dependencies {
3636
val monocleCore = "dev.optics" %% "monocle-core" % "3.3.0"
3737
val munit = "org.scalameta" %% "munit" % "1.1.0"
3838
val munitCatsEffect = "org.typelevel" %% "munit-cats-effect" % "2.0.0"
39+
val munitDiff = "org.scalameta" %% "munit-diff" % munit.revision
3940
val munitScalacheck = "org.scalameta" %% "munit-scalacheck" % "1.1.0"
4041
val refined = "eu.timepit" %% "refined" % "0.11.3"
4142
val refinedScalacheck = "eu.timepit" %% "refined-scalacheck" % refined.revision

0 commit comments

Comments
 (0)