Skip to content

Commit

Permalink
Allow printing out only reformatted filenames (#148)
Browse files Browse the repository at this point in the history
* Modify matchers for written files

* Refactor results writers using common base

* Add identical copies of tests, to be modified

* Allow printing out only reformatted filenames

Co-authored-by: Albert Meltzer <[email protected]>
Co-authored-by: Ciaran Kearney <[email protected]>
  • Loading branch information
3 people authored Jul 1, 2021
1 parent 03e29b2 commit 12812d5
Show file tree
Hide file tree
Showing 9 changed files with 142 additions and 72 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ The latest release should be visible at the top of this readme.
</testSourceDirectories>
<validateOnly>false</validateOnly> <!-- check formatting without changing files -->
<onlyChangedFiles>true</onlyChangedFiles> <!-- only format (staged) files that have been changed from the specified git branch -->
<showReformattedOnly>false</showReformattedOnly> <!-- log only modified files -->
<!-- The git branch to check against
If branch.startsWith(": ") the value in <branch> tag is used as a command to run
and the output will be used as the actual branch-->
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/org/antipathy/mvn_scalafmt/FormatMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public class FormatMojo extends AbstractMojo {
private boolean validateOnly;
@Parameter(property = "format.onlyChangedFiles", defaultValue = "false")
private boolean onlyChangedFiles;
@Parameter(property = "format.showReformattedOnly", defaultValue = "false")
private boolean showReformattedOnly;
/** if branch.startsWith(": "), ex set in pom.xml:
* <pre>{@code
* <!-- the current branch-->
Expand Down Expand Up @@ -82,6 +84,7 @@ public void execute() throws MojoExecutionException {
respectVersion,
validateOnly,
onlyChangedFiles,
showReformattedOnly,
branch,
project.getBasedir(),
useSpecifiedRepositories ? getRepositoriesUrls(mavenRepositories) : new ArrayList<String>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ object ScalaFormatter {
* @param respectVersion should we respect the version in the scalafmt.conf
* @param testOnly should files be reformatted
* @param onlyChangedFiles Should only changed files be formatted
* @param showReformattedOnly print only unformatted files in loggers
* @param branch The branch to compare against for changed files
* @param workingDirectory The project working directory
* @param mavenRepositoryUrls The maven repositories to be used to dynamically load scalafmt, empty if maven central
Expand All @@ -54,6 +55,7 @@ object ScalaFormatter {
respectVersion: Boolean,
testOnly: Boolean,
onlyChangedFiles: Boolean,
showReformattedOnly: Boolean,
branch: String,
workingDirectory: File,
mavenRepositoryUrls: JList[String]
Expand All @@ -72,9 +74,9 @@ object ScalaFormatter {

val fileWriter =
if (testOnly)
new TestResultLogWriter(log)
new TestResultLogWriter(log, showReformattedOnly)
else
new FormattedFilesWriter(log)
new FormattedFilesWriter(log, showReformattedOnly)
new ScalaFormatter(sourceBuilder, changedFilesBuilder, sourceFormatter, fileWriter)
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.antipathy.mvn_scalafmt.io

import org.antipathy.mvn_scalafmt.model.{FileSummary, FormatResult, Summary}

/** Class for writing formatted source files
*/
abstract class FormatResultsWriter extends Writer[Seq[FormatResult], Summary] {

protected val showReformattedOnly: Boolean
protected val formattedDetail: String
protected val unformattedDetail: String
protected def processUnformattedFile(input: FormatResult): Unit

/** Write the passed in input
*
* @param input The input to write
*/
final override def write(input: Seq[FormatResult]): Summary = {
val unformattedFiles = input.filter(!_.isFormatted)
unformattedFiles.foreach(processUnformattedFile)
val results = if (showReformattedOnly) unformattedFiles else input
Summary(input.length, unformattedFiles.length, build(results))
}

/** Build a summary of the format run from the passed in `FormatResult`s
* @param input The input to build from
* @return The built output
*/
private def build(formatResults: Seq[FormatResult]): Seq[FileSummary] =
formatResults.map { item =>
val isFormatted = item.isFormatted
val details = if (isFormatted) formattedDetail else unformattedDetail
FileSummary(item.sourceFile.getName, details)
}

}
Original file line number Diff line number Diff line change
@@ -1,34 +1,23 @@
package org.antipathy.mvn_scalafmt.io

import java.io.File
import org.antipathy.mvn_scalafmt.builder.FilesSummaryBuilder
import org.antipathy.mvn_scalafmt.model.{FileSummaryRequest, FormatResult, Summary}
import org.antipathy.mvn_scalafmt.model.FormatResult
import org.apache.commons.io.FileUtils
import org.apache.maven.plugin.logging.Log

/** Class for writing formatted source files
*/
class FormattedFilesWriter(log: Log) extends Writer[Seq[FormatResult], Summary] with FilesSummaryBuilder {
class FormattedFilesWriter(log: Log, val showReformattedOnly: Boolean)
extends FormatResultsWriter {

/** Write the passed in input
*
* @param input The input to write
*/
override def write(input: Seq[FormatResult]): Summary = {
val unformattedFiles = input.filter(!_.isFormatted)
unformattedFiles.foreach(writeFile)
Summary(
input.length,
unformattedFiles.length,
build(FileSummaryRequest(input, "Correctly formatted", "Reformatted"))
)
}
protected val formattedDetail: String = "Correctly formatted"
protected val unformattedDetail: String = "Reformatted"

/** Write each FormatResult to disk
*
* @param input The input to write
*/
private def writeFile(input: FormatResult): Unit = {
protected def processUnformattedFile(input: FormatResult): Unit = {
import org.antipathy.mvn_scalafmt.ScalaFormatter
log.debug(s"Writing ${input.sourceFile.getName} to ${input.sourceFile.getCanonicalPath}")

Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
package org.antipathy.mvn_scalafmt.io
import org.antipathy.mvn_scalafmt.builder.FilesSummaryBuilder
import org.antipathy.mvn_scalafmt.model.{FileSummaryRequest, FormatResult, Summary}

import org.antipathy.mvn_scalafmt.model.FormatResult
import org.apache.maven.plugin.logging.Log

/** Class for writing test results to the log
*
* @param log The maven logger
*/
class TestResultLogWriter(log: Log) extends Writer[Seq[FormatResult], Summary] with FilesSummaryBuilder {
class TestResultLogWriter(log: Log, val showReformattedOnly: Boolean) extends FormatResultsWriter {

protected val formattedDetail: String = "Formatted"
protected val unformattedDetail: String = "Requires formatting"

/** Write the test results to a log
*
* @param input The input to write
*/
override def write(input: Seq[FormatResult]): Summary = {
input.filter(!_.isFormatted).foreach { item =>
log.error(s"unformatted file at: ${item.sourceFile.getCanonicalPath}")
}
Summary(
input.length,
input.count(!_.isFormatted),
build(FileSummaryRequest(input, "Formatted", "Requires formatting"))
)
}
protected def processUnformattedFile(item: FormatResult): Unit =
log.error(s"unformatted file at: ${item.sourceFile.getCanonicalPath}")
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import java.io.File
import java.nio.charset.StandardCharsets
import java.nio.file.Files

import org.antipathy.mvn_scalafmt.model.FormatResult
import org.antipathy.mvn_scalafmt.model.{FileSummary, FormatResult, Summary}
import org.apache.commons.io.FileUtils
import org.apache.maven.plugin.logging.SystemStreamLog
import org.scalatest.flatspec.AnyFlatSpec
Expand All @@ -19,14 +19,54 @@ class FormattedFilesWriterSpec extends AnyFlatSpec with GivenWhenThen with Match
val originalContent = "originalContent"
val changedContent = "changedContent"
val sourceFile = new File(s"${System.getProperty("java.io.tmpdir")}${File.separator}TempFile.scala")

FileUtils.writeStringToFile(sourceFile, originalContent, StandardCharsets.UTF_8)
new String(Files.readAllBytes(sourceFile.toPath)) should be(originalContent)

new FormattedFilesWriter(new SystemStreamLog, false)
.write(Seq(FormatResult(sourceFile, originalContent, changedContent))) shouldBe Summary(
1,
1,
Seq(FileSummary(sourceFile.getName, "Reformatted"))
)

new String(Files.readAllBytes(sourceFile.toPath)) should be(changedContent)

new FormattedFilesWriter(new SystemStreamLog, false)
.write(Seq(FormatResult(sourceFile, changedContent, changedContent))) shouldBe Summary(
1,
0,
Seq(FileSummary(sourceFile.getName, "Correctly formatted"))
)

new String(Files.readAllBytes(sourceFile.toPath)) should be(changedContent)

sourceFile.delete()
}

val formatResult = FormatResult(sourceFile, originalContent, changedContent)
val fileWriter = new FormattedFilesWriter(new SystemStreamLog)
it should "Write files and print reformatted only" in {
val originalContent = "originalContent"
val changedContent = "changedContent"
val sourceFile = new File(s"${System.getProperty("java.io.tmpdir")}${File.separator}TempFile.scala")

FileUtils.writeStringToFile(sourceFile, originalContent, StandardCharsets.UTF_8)
new String(Files.readAllBytes(sourceFile.toPath)) should be(originalContent)

fileWriter.write(Seq(formatResult))
new FormattedFilesWriter(new SystemStreamLog, true)
.write(Seq(FormatResult(sourceFile, originalContent, changedContent))) shouldBe Summary(
1,
1,
Seq(FileSummary(sourceFile.getName, "Reformatted"))
)

new String(Files.readAllBytes(sourceFile.toPath)) should be(changedContent)

new FormattedFilesWriter(new SystemStreamLog, true)
.write(Seq(FormatResult(sourceFile, changedContent, changedContent))) shouldBe Summary(
1,
0,
Seq.empty
)

new String(Files.readAllBytes(sourceFile.toPath)) should be(changedContent)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ import org.mockito.Mockito
import org.antipathy.mvn_scalafmt.model.FormatResult
import java.io.File

import org.antipathy.mvn_scalafmt.model.{FileSummary, Summary}

class TestResultLogWriterSpec extends AnyFlatSpec with GivenWhenThen with Matchers {

behavior of "TestResultLogWriter"

it should "write details of unformatted sources to a log" in {

val log = Mockito.mock(classOf[Log])
val writer = new TestResultLogWriter(log)
val writer = new TestResultLogWriter(log, false)
val unformattedFile = Mockito.mock(classOf[File])
val formattedFile = Mockito.mock(classOf[File])
val unformatted = FormatResult(
Expand All @@ -33,18 +35,42 @@ class TestResultLogWriterSpec extends AnyFlatSpec with GivenWhenThen with Matche
Mockito.when(unformattedFile.getName).thenReturn("unformatted.scala")
Mockito.when(formattedFile.getName).thenReturn("formatted.scala")

val result = writer.write(input)

result.totalFiles should be(input.length)
result.unformattedFiles should be(1)
result.fileDetails.length should be(input.length)
result.fileDetails.filter(_.name == unformattedFile.getName).foreach { fd =>
fd.name should be(unformattedFile.getName)
fd.details should be("Requires formatting")
}
result.fileDetails.filter(_.name == formattedFile.getName).foreach { fd =>
fd.name should be(formattedFile.getName)
fd.details should be("Formatted")
}
writer.write(input) shouldBe Summary(
2,
1,
Seq(
FileSummary(unformattedFile.getName, "Requires formatting"),
FileSummary(formattedFile.getName, "Formatted")
)
)
}

it should "write details of unformatted sources to a log, reformatted only" in {

val log = Mockito.mock(classOf[Log])
val writer = new TestResultLogWriter(log, true)
val unformattedFile = Mockito.mock(classOf[File])
val formattedFile = Mockito.mock(classOf[File])
val unformatted = FormatResult(
sourceFile = unformattedFile,
originalSource = "unformatted",
formattedSource = "formatted"
)
val formatted = FormatResult(
sourceFile = formattedFile,
originalSource = "formatted",
formattedSource = "formatted"
)
val input = Seq(unformatted, formatted)

Mockito.when(unformattedFile.getName).thenReturn("unformatted.scala")
Mockito.when(formattedFile.getName).thenReturn("formatted.scala")

writer.write(input) shouldBe Summary(
2,
1,
Seq(FileSummary(unformattedFile.getName, "Requires formatting"))
)
}

}

0 comments on commit 12812d5

Please sign in to comment.