Skip to content

Commit

Permalink
KotlinLintBear.py: Add bear wrapping ktlint
Browse files Browse the repository at this point in the history
Closes coala#2152
  • Loading branch information
saksham189 committed Feb 18, 2018
1 parent 5a4115e commit c2c146a
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .ci/appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ install:
- "python -c \"import struct; print(struct.calcsize('P') * 8)\""


- >
"curl -sSLO
https://github.com/shyiko/ktlint/releases/download/0.15.0/ktlint
- >
"%CMD_IN_ENV% pip install
--cache-dir=C:\\pip_cache -r requirements.txt -r test-requirements.txt
Expand Down
5 changes: 5 additions & 0 deletions .ci/deps.sh
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,8 @@ wget "https://downloads.sourceforge.net/project/astyle/astyle/astyle%203.0.1/ast
tar -xvzf ~/astyle.tar.gz -C ~/
make -C ~/astyle/build/gcc
sudo make install -C ~/astyle/build/gcc

# ktlint installation
curl -sSLO https://github.com/shyiko/ktlint/releases/download/0.15.0/ktlint
sudo chmod a+x ktlint
sudo mv ktlint /usr/local/bin/
27 changes: 27 additions & 0 deletions bears/kotlin/KotlinLintBear.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from coalib.bearlib.abstractions.Linter import linter
from dependency_management.requirements.DistributionRequirement import (
DistributionRequirement)


@linter(executable='ktlint',
global_bear=True,
output_format='regex',
output_regex=r'(?P<filename>.*):(?P<line>\d+):'
r'(?P<column>\d+): (?P<message>.+)')
class KotlinLintBear:
"""
Lints your Kotlin files.
Checks for coding standards or semantical problems that might lead
to problems in execution of the code.
"""
LANGUAGES = {'kotlin'}
REQUIREMENTS = {DistributionRequirement(brew='shyiko/ktlint/ktlint')}
AUTHORS = {'The coala developers'}
AUTHORS_EMAILS = {'[email protected]'}
LICENSE = 'AGPL-3.0'
CAN_DETECT = {'Formatting', 'Syntax'}
SEE_MORE = 'https://ktlint.github.io'

@staticmethod
def create_arguments(config_file):
return ()
Empty file added bears/kotlin/__init__.py
Empty file.
32 changes: 32 additions & 0 deletions tests/kotlin/KotlinLintBearTest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import os
import unittest
from queue import Queue

from coalib.settings.Section import Section
from bears.kotlin.KotlinLintBear import KotlinLintBear
from coalib.testing.BearTestHelper import generate_skip_decorator


@generate_skip_decorator(KotlinLintBear)
class KotlinLintBearTest(unittest.TestCase):

def setUp(self):
self.section = Section('Kotlin')
self.queue = Queue()
self.file_dict = {}
self.uut = KotlinLintBear(self.file_dict, self.section, self.queue)

def set_config_dir(self, directory):
test_path = os.path.abspath(os.path.join(
os.path.dirname(__file__), directory))
self.uut.get_config_dir = lambda *args, **kwargs: test_path

def test_bad_files(self):
self.set_config_dir('bad_files')
results = list(self.uut.run_bear_from_section([], {}))
self.assertTrue(len(results) > 5)

def test_good_files(self):
self.set_config_dir('good_files')
results = list(self.uut.run_bear_from_section([], {}))
self.assertTrue(len(results) == 0)
Empty file added tests/kotlin/__init__.py
Empty file.
8 changes: 8 additions & 0 deletions tests/kotlin/bad_files/bad_blanklines.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
fun a() {


}
fun b() {


}
19 changes: 19 additions & 0 deletions tests/kotlin/bad_files/bad_indent.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* _
*/
fun main() {
val a = 0
val b = 0
if (a == 0) {
println(a)
}
val b = builder().setX().setY()
.build()
val c = builder("long_string" +
"")
}
class A {
var x: String
get() = ""
set(v: String) { x = v }
}
6 changes: 6 additions & 0 deletions tests/kotlin/bad_files/bad_semicolon.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package a.b.c;
fun main() {
fun name() { a(); return b }
println(";")
println();
}
1 change: 1 addition & 0 deletions tests/kotlin/bad_files/bad_spacing.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fun main() { x(1,3); x(1, 3)\n \n }
6 changes: 6 additions & 0 deletions tests/kotlin/good_files/good_file.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
fun a() {
val x = 5
if (x == 0) {
println(a)
}
}

0 comments on commit c2c146a

Please sign in to comment.