静的解析ツール detekt で任意の条件で警告させる

静的解析ツール detekt で任意の条件で警告させる

Kotlin Fest 2019 の LT 発表資料です。

F2caa6fd70717782de23d3a8e165481f?s=128

Shunsuke Maeda

August 24, 2019
Tweet

Transcript

  1. 2.

    ࣗݾ঺հ ✓ Shunsuke Maeda ✓ @duck8823 ✓ ॴଐ ✓ גࣜձࣾΤεɾΤϜɾΤε

    ✓ EarthCampusגࣜձࣾ ✓ ීஈ͸ ✓ Kotlin Ͱ։ൃʢαʔόʔαΠυʣ 2/21
  2. 5.

    ੩తղੳ ͱ CI CI (Continuous Integration) ͳͲͰৗʹಈ͔͢͜ͱ͕େࣄ ϧʔϧҧ൓͕͋ͬͨ৔߹ʹܯࠂ(= CI ͷδϣϒࣦഊ)

    ಈతʹ CIͷδϣϒ ΛϋϯυϦϯά͍ͨ͠ ྫ. - ίʔυͷن໛ʹΑͬͯܯࠂ͢Δᮢ஋Λม͍͑ͨ - PRͷ৔߹͸ࠩ෼͕͋ͬͨϑΝΠϧͷΈର৅ʹ͍ͨ͠ 5/21
  3. 7.

    CI ͱ ऴྃίʔυ CI Ͱδϣϒ͕ࣦഊ͢Δ৚݅ = ίϚϯυͷऴྃίʔυ͕ 0 Ͱͳ͍ =>

    ऴྃίʔυΛϋϯυϦϯάͰ͖Ε͹೚ҙͷ৚݅Ͱδϣϒࣦഊ 7/21
  4. 8.

    detekt ͱ ऴྃίʔυ detekt ίϚϯυͷ৔߹ fun main(args: Array<String>) { try

    { buildRunner(args).execute() } catch (e: BuildFailure) { e.printStackTrace() exitProcess(2) } catch (e: Exception) { e.printStackTrace() exitProcess(1) } exitProcess(0) } 8/21
  5. 10.

    ઃఆϑΝΠϧʹώϯτ͕͋Δ detekt --generate-config # ઃఆϑΝΠϧͷੜ੒ίϚϯυ ੜ੒͞ΕͨઃఆϑΝΠϧ console-reports: active: true exclude:

    # - 'ProjectStatisticsReport' # - 'ComplexityReport' # - 'NotificationReport' # - 'FindingsReport' # - 'BuildFailureReport' 10/21
  6. 12.

    BuildFailureReport ✓ຊମʹ૊Έࠐ·Ε͍ͯΔ Custom Reports ϓϥάΠϯͷҰछ ✓exclude ͢Δͱϧʔϧҧ൓͕͋ͬͯ΋ऴྃίʔυ͸ 0 ʹͳΔ (

    1.0.0 ) console-reports: active: true exclude: # - 'ProjectStatisticsReport' # - 'ComplexityReport' # - 'NotificationReport' # - 'FindingsReport' - 'BuildFailureReport' 12/21
  7. 13.

    detekt ͷϓϥάΠϯ ✓Custom RuleSets: ಠࣗͷϧʔϧͷ௥Ճ ✓Custom Processors: ੩తղੳ࣮ߦ࣌ʢલޙʣͷॲཧ ✓ interface

    FileProcessListener ✓Custom Reports: ੩తղੳͷ݁Ռͷग़ྗ ✓ abstract class ConsoleReport <= BuildFailureReport ✓ abstract class OutputReport see also https://arturbosch.github.io/detekt/extensions.html 13/21
  8. 14.

    BuildFailureReport class BuildFailureReport : ConsoleReport() { ... override fun render(detektion:

    Detektion): String? { ... maxIssues.reached(amount) -> { val message = "Build failed with $amount..." println(message.red()) throw BuildFailure(message) } ,,, } } ※ ver. 1.0.0 14/21
  9. 16.

    ೚ҙͷ৚݅Ͱ throw ͢Δ Custom Report import io.gitlab.arturbosch.detekt.api.ConsoleReport import io.gitlab.arturbosch.detekt.api.Detektion import

    io.gitlab.arturbosch.detekt.cli.console.BuildFailure class MyCustomReport : ConsoleReport() { override val priority: Int = Int.MIN_VALUE ... override fun render(detektion: Detektion): String? { ... # ͜͜Ͱ೚ҙͷ৚݅Ͱ throw BuildFailure("...") } } ✓priority: CustomReportΛ࣮ߦ͢Δ༏ઌ౓ 16/21
  10. 19.

    PR #1818 ޙ ( 1.0.1 ʹೖΔ༧ఆ ) ✓BuildFailureReport Ͱ͸ throw

    ͠ͳ͍ ✓Runner Ͱ throw ͢Δ => BuildFailureReport Λ exclude ͯ͠΋ऴྃεςʔλε͸มΘΒͳ͍ ※ BuildFailureReport Ͱ throw ͍ͯ͠Δͷ͸ઃܭ্ͷόά => 1.0.1Ͱमਖ਼ 19/21
  11. 21.

    ·ͱΊ ੩తղੳπʔϧ detekt Ͱ೚ҙͷ৚݅Ͱܯࠂͤ͞Δ ✓<= 1.0.0 ✓ BuildFailureReport Λແޮ ✓

    build -> maxIssues: -1 ✓ CustomReport Ͱ throw BuildFailure ✓>= 1.0.1 ✓ build -> maxIssues: -1 ✓ CustomReport Ͱ throw BuildFailure 21/21