Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ScalafmtSession: add check for git constraint #2928

Merged
merged 1 commit into from
Nov 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ final case class ScalafmtDynamicSession(
override def matchesProjectFilters(file: Path): Boolean =
cfg.isIncludedInProject(file)

override def isGitOnly: Boolean = cfg.projectIsGit

private def tryFormat(
file: Path,
code: String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ class ScalafmtReflectConfig private[dynamic] (val fmtReflect: ScalafmtReflect)(
target.invoke("indent")
}

lazy val projectIsGit =
Try(projectField.invokeAs[Boolean]("git")).getOrElse(false)

@inline def getVersion = fmtReflect.version

@inline def isIncludedInProject(path: Path): Boolean =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,14 @@ class DynamicSuite extends FunSuite {
assertEquals(error, "Missing version")
}

private def assertDynamicConfig(
fmt: Format
)(f: ScalafmtReflectConfig => Unit): Unit =
fmt.dynamic.resolveConfig(fmt.config) match {
case Left(e) => fail("failed to load config", e)
case Right(cfg) => f(cfg)
}

private def checkDynamicConfig(
name: String,
version: String,
Expand All @@ -485,13 +493,22 @@ class DynamicSuite extends FunSuite {
)(f: ScalafmtReflectConfig => Unit): Unit = {
check(s"$name [v=$version d=$dialect]") { fmt =>
fmt.setVersion(version, dialect, rest: _*)
fmt.dynamic.resolveConfig(fmt.config) match {
case Left(e) => fail("failed to load config", e)
case Right(cfg) => f(cfg)
}
assertDynamicConfig(fmt)(f)
}
}

checkExhaustive("check project.git=true") { _ => "project.git = true" } {
(f, _) => assertDynamicConfig(f)(x => assertEquals(x.projectIsGit, true))
}

checkExhaustive("check project.git=false") { _ => "project.git = false" } {
(f, _) => assertDynamicConfig(f)(x => assertEquals(x.projectIsGit, false))
}

checkExhaustive("check project.git missing") { _ => "" } { (f, _) =>
assertDynamicConfig(f)(x => assertEquals(x.projectIsGit, false))
}

checkDynamicConfig(
s"check indent.main",
"3.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,9 @@ public interface ScalafmtSession {
*/
boolean matchesProjectFilters(Path file);

/**
* Whether this configuration intends to limit files to those managed by git.
*/
boolean isGitOnly();

}