8000 Allows the ScalafmtModule to format build sources by hwielenberg · Pull Request #920 · com-lihaoyi/mill · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Allows the ScalafmtModule to format build sources #920

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

Merged
merged 1 commit into from
Jul 2, 2020
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
8 changes: 7 additions & 1 deletion scalalib/src/scalafmt/ScalafmtModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,13 @@ trait ScalafmtModule extends JavaModule {
protected def filesToFormat(sources: Seq[PathRef]) = {
for {
pathRef <- sources if os.exists(pathRef.path)
file <- os.walk(pathRef.path) if os.isFile(file) && file.ext == "scala"
file <- {
if (os.isDir(pathRef.path)) {
os.walk(pathRef.path).filter(file => os.isFile(file) && (file.ext == "scala" || file.ext == "sc"))
} else {
Seq(pathRef.path)
}
}
} yield PathRef(file)
}

Expand Down
2 changes: 2 additions & 0 deletions scalalib/test/resources/scalafmt/core/util.sc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def sayHelloWorld() =
println("Hello World")
40 changes: 34 additions & 6 deletions scalalib/test/src/scalafmt/ScalafmtTests.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package mill.scalalib.scalafmt

import mill.T
import mill.define.Sources
import mill.main.Tasks
import mill.scalalib.ScalaModule
import mill.util.{TestEvaluator, TestUtil}
Expand All @@ -13,9 +15,18 @@ object ScalafmtTests extends TestSuite {
TestUtil.getSrcPathBase() / millOuterCtx.enclosing.split('.')
}

trait BuildSrcModule {
def buildSources: Sources
}

object ScalafmtTestModule extends TestBase {
object core extends ScalaModule with ScalafmtModule {
object core extends ScalaModule with ScalafmtModule with BuildSrcModule {
def scalaVersion = "2.12.4"

def buildSources: Sources = T.sources {
millSourcePath / "util.sc"
}

}
}

Expand All @@ -35,7 +46,7 @@ object ScalafmtTests extends TestSuite {

def tests: Tests = Tests {
'scalafmt - {
def checkReformat(reformatCommand: mill.define.Command[Unit]) =
def checkReformat(reformatCommand: mill.define.Command[Unit], buildSrcIncluded:Boolean) =
workspaceTest(ScalafmtTestModule) { eval =>
val before = getProjectFiles(ScalafmtTestModule.core, eval)

Expand All @@ -54,6 +65,18 @@ object ScalafmtTests extends TestSuite {
"application.conf").modifyTime
)

if(buildSrcIncluded){
assert(
firstReformat("util.sc").modifyTime > before("util.sc").modifyTime,
firstReformat("util.sc").content != before("util.sc").content
)
} else {
assert(
firstReformat("util.sc").modifyTime == before("util.sc").modifyTime,
firstReformat("util.sc").content == before("util.sc").content,
)
}

// cached reformat
val Right(_) = eval.apply(reformatCommand)

Expand All @@ -62,6 +85,7 @@ object ScalafmtTests extends TestSuite {
assert(
cached("Main.scala").modifyTime == firstReformat("Main.scala").modifyTime,
cached("Person.scala").modifyTime == firstReformat("Person.scala").modifyTime,
cached("util.sc").modifyTime == firstReformat("util.sc").modifyTime,
cached("application.conf").modifyTime == firstReformat(
"application.conf").modifyTime
)
Expand All @@ -82,21 +106,25 @@ object ScalafmtTests extends TestSuite {
)
}

'reformat - checkReformat(ScalafmtTestModule.core.reformat())
'reformat - checkReformat(ScalafmtTestModule.core.reformat(), false)
'reformatAll - checkReformat(
ScalafmtModule.reformatAll(Tasks(Seq(ScalafmtTestModule.core.sources))))
ScalafmtModule.reformatAll(
Tasks(Seq(ScalafmtTestModule.core.sources, ScalafmtTestModule.core.buildSources)),
), true)
}
}

case class FileInfo(content: String, modifyTime: Long, path: os.Path)

def getProjectFiles(m: ScalaModule, eval: TestEvaluator) = {
def getProjectFiles(m: ScalaModule with BuildSrcModule, eval: TestEvaluator) = {
val Right((sources, _)) = eval.apply(m.sources)
val Right((resources, _)) = eval.apply(m.resources)
val Right((buildSources, _)) = eval.apply(m.buildSources)

val sourcesFiles = sources.flatMap(p => os.walk(p.path))
val resourcesFiles = resources.flatMap(p => os.walk(p.path))
(sourcesFiles ++ resourcesFiles).map { p =>
val buildFiles = buildSources.map(_.path)
(sourcesFiles ++ resourcesFiles ++ buildFiles).map { p =>
p.last -> FileInfo(os.read(p), os.mtime(p), p)
}.toMap
}
Expand Down
0