From c49bf7c6b828e529ee499c0cf18e8ab10ea5903e Mon Sep 17 00:00:00 2001 From: Michael Droettboom Date: Fri, 26 Oct 2018 15:31:36 -0400 Subject: [PATCH] Make sure Kotlin output is ktlint-compliant --- .travis.yml | 6 +++++- glean_parser/templates/kotlin.jinja2.kt | 9 ++++----- tests/test_kotlin.py | 14 +++++++++++--- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/.travis.yml b/.travis.yml index 24067b51..67461085 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,10 +7,14 @@ matrix: dist: xenial sudo: true -install: pip install -U tox-travis flake8 +install: + - pip install -U tox-travis flake8 + - curl -sSLO https://github.com/shyiko/ktlint/releases/download/0.29.0/ktlint && + chmod a+x ktlint # Command to run tests, e.g. python setup.py test script: + - export PATH=.:$PATH - flake8 . - tox diff --git a/glean_parser/templates/kotlin.jinja2.kt b/glean_parser/templates/kotlin.jinja2.kt index e9657b3e..0fcc53f6 100644 --- a/glean_parser/templates/kotlin.jinja2.kt +++ b/glean_parser/templates/kotlin.jinja2.kt @@ -11,18 +11,17 @@ package GleanMetrics {% for metric_type in metric_types -%} import mozilla.components.service.glean.{{ metric_type|Camelize }}MetricType {% endfor %} - object {{ category_name|Camelize }} { - {% for metric in metrics.values() %} +{%- for metric in metrics.values() %} /** * {{ metric.description|wordwrap(wrapstring='\n * ') }} */ val {{ metric.name|camelize }}: {{ metric.type|Camelize }}MetricType by lazy { {{ metric.type|Camelize }}MetricType( {% for arg_name in extra_args if metric[arg_name] is defined -%} - {{ arg_name|camelize }}={{ metric[arg_name]|kotlin }}{{ "," if not loop.last }} + {{ arg_name|camelize }} = {{ metric[arg_name]|kotlin }}{{ "," if not loop.last }} {% endfor -%} ) - }; - {% endfor %} + } +{% endfor -%} } diff --git a/tests/test_kotlin.py b/tests/test_kotlin.py index 1ff27052..bdb5a255 100644 --- a/tests/test_kotlin.py +++ b/tests/test_kotlin.py @@ -3,8 +3,9 @@ # Any copyright is dedicated to the Public Domain. # http://creativecommons.org/publicdomain/zero/1.0/ -import os from pathlib import Path +import shutil +import subprocess from glean_parser import kotlin from glean_parser import translate @@ -15,10 +16,12 @@ ROOT = Path(__file__).parent def test_parser(tmpdir): """Test translating metrics to Kotlin files.""" - translate.translate(ROOT / "data" / "core.yaml", 'kotlin', Path(tmpdir)) + tmpdir = Path(tmpdir) + + translate.translate(ROOT / "data" / "core.yaml", 'kotlin', tmpdir) assert ( - set(os.listdir(tmpdir)) == + set(x.name for x in tmpdir.iterdir()) == set(['CorePing.kt', 'Telemetry.kt', 'Environment.kt']) ) @@ -32,6 +35,11 @@ def test_parser(tmpdir): content = fd.read() assert 'جمع 搜集' in content + # Only run this test if ktlint is on the path + if shutil.which('ktlint'): + for filepath in tmpdir.glob('*.kt'): + subprocess.check_call(['ktlint', filepath]) + def test_kotlin_generator(): kdf = kotlin.kotlin_datatypes_filter