Skip to content

Commit

Permalink
Configure linting (#13)
Browse files Browse the repository at this point in the history
* Configure ktlint in the project
* Fix existant code linting
* Include linting in CI configuration
  • Loading branch information
Serchinastico committed Jun 21, 2018
1 parent ccf8211 commit 141fc9c
Show file tree
Hide file tree
Showing 12 changed files with 65 additions and 96 deletions.
5 changes: 5 additions & 0 deletions .editorconfig
@@ -0,0 +1,5 @@
[*.{kt,kts}]
indent_size=4
continuation_indent_size=4
insert_final_newline=true
max_line_length=100
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -2,3 +2,4 @@ build/
.idea/
.gradle/
local.properties
*.iml
2 changes: 1 addition & 1 deletion .travis.yml
@@ -1,4 +1,4 @@
language: java

script:
- ./gradlew clean test --info
- ./gradlew clean ktlint test
59 changes: 0 additions & 59 deletions KotlinSnapshot.iml

This file was deleted.

32 changes: 26 additions & 6 deletions build.gradle
@@ -1,5 +1,5 @@
buildscript {
ext.kotlin_version = '1.1.51'
ext.kotlin_version = '1.2.10'
repositories {
jcenter()
}
Expand All @@ -21,16 +21,36 @@ sourceSets {
test.kotlin.srcDirs += 'src/test/kotlin'
}

configurations {
ktlint
}

dependencies {
ktlint "com.github.shyiko:ktlint:0.23.1"
implementation fileTree(dir: 'libs', include: ['*.jar'])

implementation"org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
testImplementation 'junit:junit:4.12'
}

test {
// update snapshots via command line
systemProperty "updateSnapshots", System.getProperty("u")
// purge snapshots via command line
systemProperty "purgeSnapshots", System.getProperty("p")
// update snapshots via command line
systemProperty "updateSnapshots", System.getProperty("u")
// purge snapshots via command line
systemProperty "purgeSnapshots", System.getProperty("p")
}

task ktlint(type: JavaExec, group: "verification") {
description = "Check Kotlin code style."
classpath = configurations.ktlint
main = "com.github.shyiko.ktlint.Main"
args "--color", "src/**/*.kt"
}
check.dependsOn ktlint

task ktlintFormat(type: JavaExec, group: "formatting") {
description = "Fix Kotlin code style deviations."
classpath = configurations.ktlint
main = "com.github.shyiko.ktlint.Main"
args "-F", "src/**/*.kt"
}
16 changes: 7 additions & 9 deletions src/main/kotlin/com/gaumala/kotlinsnapshot/Camera.kt
Expand Up @@ -17,14 +17,14 @@ class Camera(relativePath: String) {
purgeSnapshotsIfNeeded(snapshotDir)
}

constructor(): this("")
constructor() : this("")

private val shouldUpdateSnapshots: Boolean by lazy {
System.getProperty("updateSnapshots") == "1"
}

private fun differsFromSnapshot(diffs: List<diff_match_patch.Diff>): Boolean =
diffs.find { diff -> diff.operation != diff_match_patch.Operation.EQUAL } != null
diffs.find { diff -> diff.operation != diff_match_patch.Operation.EQUAL } != null

private fun matchValueWithExistingSnapshot(snapshotFile: File, value: Any) {
val snapshotContents = snapshotFile.readText()
Expand All @@ -37,15 +37,13 @@ class Camera(relativePath: String) {
val msg = DiffPrinter.toReadableConsoleMessage(snapshotFile.name, diffs)
throw SnapshotException(diffs, msg)
}


}

private fun writeSnapshot(update: Boolean, snapshotFile: File, value: Any) {
snapshotFile.writeText(value.toString())
val fileName = "\"${snapshotFile.name}\""
val msg = if (update) "${Term.green("1 snapshot, $fileName,")} updated."
else "${Term.green("1 snapshot, $fileName,")} written."
else "${Term.green("1 snapshot, $fileName,")} written."
println(msg)
}

Expand All @@ -60,7 +58,7 @@ class Camera(relativePath: String) {
private companion object {
private val purgedDirectories = HashSet<String>()
fun createSnapshotDir(relativePath: String): File {
val dir = System.getProperty("user.dir");
val dir = System.getProperty("user.dir")
val snapshotDirPath = Paths.get(dir, relativePath, "__snapshot__").toString()
val snapshotDir = File(snapshotDirPath)
snapshotDir.mkdirs()
Expand All @@ -69,13 +67,13 @@ class Camera(relativePath: String) {

fun purgeSnapshotsIfNeeded(snapshotDir: File) {
val pathToPurge = snapshotDir.absolutePath
val shouldPurge = System.getProperty("purgeSnapshots") == "1"
&& ! purgedDirectories.contains(pathToPurge)
val shouldPurge = System.getProperty("purgeSnapshots") == "1" &&
!purgedDirectories.contains(pathToPurge)

if (shouldPurge) {
snapshotDir.deleteAllContainedFiles()
purgedDirectories.add(pathToPurge)
}
}
}
}
}
19 changes: 11 additions & 8 deletions src/main/kotlin/com/gaumala/kotlinsnapshot/DiffPrinter.kt
@@ -1,7 +1,7 @@
package com.gaumala.kotlinsnapshot

import name.fraser.neil.plaintext.diff_match_patch
import java.util.*
import java.util.LinkedList

/**
* Created by gabriel on 3/25/18.
Expand All @@ -11,13 +11,16 @@ internal object DiffPrinter {
private val dmp = diff_match_patch()

private fun printDiff(diff: diff_match_patch.Diff): String =
when (diff.operation) {
diff_match_patch.Operation.EQUAL, null -> " ${diff.text}\n"
diff_match_patch.Operation.DELETE -> Term.green("- ${diff.text}\n")
diff_match_patch.Operation.INSERT -> Term.red("+ ${diff.text}\n")
}
when (diff.operation) {
diff_match_patch.Operation.EQUAL, null -> " ${diff.text}\n"
diff_match_patch.Operation.DELETE -> Term.green("- ${diff.text}\n")
diff_match_patch.Operation.INSERT -> Term.red("+ ${diff.text}\n")
}

fun toReadableConsoleMessage(snapshotName: String, diffs: LinkedList<diff_match_patch.Diff>): String {
fun toReadableConsoleMessage(
snapshotName: String,
diffs: LinkedList<diff_match_patch.Diff>
): String {
val sb = StringBuilder("${Term.red("Received value")} does not match ${
Term.green("stored snapshot: \"$snapshotName\"")}\n\n${
Term.green("-Snapshot")}\n${Term.red("+Received")}\n\n")
Expand All @@ -26,4 +29,4 @@ internal object DiffPrinter {
diffs.forEach { diff -> sb.append(printDiff(diff)) }
return sb.toString()
}
}
}
Expand Up @@ -6,4 +6,4 @@ import name.fraser.neil.plaintext.diff_match_patch
* Created by gabriel on 3/24/18.
*/

class SnapshotException(val diffs: List<diff_match_patch.Diff>, msg: String): Exception(msg)
class SnapshotException(val diffs: List<diff_match_patch.Diff>, msg: String) : Exception(msg)
6 changes: 3 additions & 3 deletions src/main/kotlin/com/gaumala/kotlinsnapshot/Term.kt
Expand Up @@ -4,10 +4,10 @@ package com.gaumala.kotlinsnapshot
* Created by gabriel on 3/24/18.
*/
internal object Term {
private val RESET = "\u001B[0m";
private val RESET = "\u001B[0m"
private val GREEN = "\u001B[32m"
private val RED = "\u001B[31m";
private val RED = "\u001B[31m"

fun green(s: String): String = "$GREEN$s$RESET"
fun red(s: String): String = "$RED$s$RESET"
}
}
15 changes: 8 additions & 7 deletions src/test/kotlin/com/gaumala/kotlinsnapshot/CameraTest.kt
Expand Up @@ -5,6 +5,7 @@ import org.junit.Test
class CameraTest {

private val camera = Camera()

data class User(val id: Int, val name: String)

@Test
Expand All @@ -16,18 +17,18 @@ class CameraTest {
@Test
fun should_take_snapshots_of_collections() {
val list = listOf(
User(1, "gabriel"),
User(2, "andres"),
User(3, "miguel"))
User(1, "gabriel"),
User(2, "andres"),
User(3, "miguel"))
camera.matchWithSnapshot("should take snapshot of a list", list)
}

@Test
fun should_take_snapshots_of_maps() {
val map = mapOf(
Pair(1, User(1, "gabriel")),
Pair(2, User(2, "andres")),
Pair(3, User(3, "miguel")))
Pair(1, User(1, "gabriel")),
Pair(2, User(2, "andres")),
Pair(3, User(3, "miguel")))
camera.matchWithSnapshot("should take snapshot of a map", map)
}

Expand All @@ -36,4 +37,4 @@ class CameraTest {
val json = """{"name":"gabriel","id":5}"""
camera.matchWithSnapshot("should take snapshot of a json string", json)
}
}
}
Expand Up @@ -22,4 +22,4 @@ class DiffPrinterTest {

camera.matchWithSnapshot(snapshotName = snapshotName, value = msg)
}
}
}
Expand Up @@ -13,4 +13,4 @@ class RelativePathCameraTest {
val json = """{"name":"gabriel","id":5}"""
camera.matchWithSnapshot("should take snapshot of a json string", json)
}
}
}

0 comments on commit 141fc9c

Please sign in to comment.