Skip to content

Commit

Permalink
Add a test task
Browse files Browse the repository at this point in the history
  • Loading branch information
alenkacz committed Jan 3, 2017
1 parent 0bcb6a2 commit 8dcef59
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ Tasks
Tasks added to your project when applying this plugin:

- *scalafmt* - formats your scala and sbt source code based on the provided configuration
- *scalafmtTest* - tests whether all files are correctly formatted, if not, the task fails
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,30 @@ import org.gradle.api.plugins.JavaBasePlugin
import org.scalafmt.Scalafmt

class ScalafmtFormatBase extends DefaultTask {
def runScalafmt() {
def runScalafmt(boolean testOnly = false) {
if (project.plugins.withType(JavaBasePlugin).empty) {
logger.info("Java or Scala gradle plugin not available in this project, nothing to format")
return
}

def misformattedFiles = new ArrayList<String>()
project.sourceSets.all { sourceSet ->
sourceSet.allSource.filter { File f -> canBeFormatted(f) }.each { File f ->
String contents = f.text
logger.debug("Formatting '$f'")
def formattedContents = Scalafmt.format(contents, ConfigFactory.load(logger, project, project.scalafmt.configFilePath), Scalafmt.format$default$3())
f.write(formattedContents.get())
if (testOnly) {
if (contents != formattedContents.get()) {
misformattedFiles.add(f.absolutePath)
}
} else {
f.write(formattedContents.get())
}
}
}

if (testOnly && !misformattedFiles.empty) {
throw new ScalafmtFormatException(misformattedFiles)
}
}

def boolean canBeFormatted(File file) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package cz.alenkacz.gradle.scalafmt

class ScalafmtFormatException extends Exception {
public ScalafmtFormatException(List<String> filePaths) {
super("Files incorrectly formatted: " + filePaths.join(","))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import org.gradle.api.Project
class ScalafmtPlugin implements Plugin<Project> {
@Override
void apply(Project project) {
ScalafmtTask scalafmtTask = project.task('scalafmt', type: ScalafmtTask)
PluginExtension extension = project.extensions.create('scalafmt', PluginExtension)
project.task('scalafmt', type: ScalafmtTask)
project.task('scalafmtTest', type: ScalafmtTestTask)
project.extensions.create('scalafmt', PluginExtension)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package cz.alenkacz.gradle.scalafmt

import org.gradle.api.tasks.TaskAction

class ScalafmtTestTask extends ScalafmtFormatBase {
@TaskAction
def format() {
runScalafmt(true)
}
}
18 changes: 18 additions & 0 deletions src/test/groovy/cz/alenkacz/gradle/scalafmt/ProjectMother.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,24 @@ import java.nio.file.Files
import java.nio.file.Paths

class ProjectMother {
static def basicProjectWithCorrectlyFormattedFile() {
TestProject testProject = null
File.createTempDir().with {
deleteOnExit()
def srcFolder = new File(absoluteFile, sourceFilePath)
srcFolder.mkdirs()
def srcFile = Files.createFile(Paths.get(srcFolder.absolutePath, "Test.scala"))
srcFile.write """import java.nio.file.{Paths, Files}
|object Test {
| foo(a, // comment
| b)
|}
|""".stripMargin()
testProject = new TestProject(absoluteFile, srcFile.toFile())
}
return testProject
}

static def basicProject() {
TestProject testProject = null
File.createTempDir().with {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package cz.alenkacz.gradle.scalafmt

import org.gradle.testfixtures.ProjectBuilder
import spock.lang.Specification

class ScalafmtTestTaskTest extends Specification {
def "fail for badly formatted code"() {
given:
def testProject = ProjectMother.basicProject()
def project = ProjectBuilder.builder().withProjectDir(testProject.projectRoot).build()
project.plugins.apply 'scala'
project.plugins.apply 'scalafmt'

when:
project.tasks.scalafmtTest.format()

then:
thrown ScalafmtFormatException
}

def "not fail for correctly formatted code"() {
given:
def testProject = ProjectMother.basicProjectWithCorrectlyFormattedFile()
def project = ProjectBuilder.builder().withProjectDir(testProject.projectRoot).build()
project.plugins.apply 'scala'
project.plugins.apply 'scalafmt'

when:
project.tasks.scalafmtTest.format()

then:
noExceptionThrown()
}
}

0 comments on commit 8dcef59

Please sign in to comment.