8

Konsist: Protect Kotlin Multiplatform projects from architecture guidelines viol...

 11 months ago
source link: https://medium.com/@lahirujay/konsist-protect-kotlin-multiplatform-projects-from-architecture-guidelines-violations-d88db0614cbd
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Konsist: Protect Kotlin Multiplatform projects from architecture guidelines violations.

Step-by-step instructions on integrating Konsist with Kotlin Multiplatform Mobile.

Let’s say you have created a project architecture and you expect your team members to follow the architecture guidelines. But there might be a developer who does not follow the guidelines strictly always. Depending on the scope of the project, developers may get reassigned, or teams may be divided into multiple small teams where they must focus on specific modules. So most of the team members will not have a holistic view of the project and with time, things can get messy.

And here comes the Linter tool to save your day. Even though it won’t solve your personal debt and problems, it certainly prevents you from accumulating technical debt and also provides visibility into your code health.

So to have consistency through the project structure and to test whether the project architecture guideline hasn’t been violated we can use an architectural linter called Konsist. Konsist is a Kotlin static code analyzer that supports Kotlin-based projects including Android, Kotlin Multiplatform, and Spring. Although this library is in its early stages of development, it covers the problems and usecases we see every day today.

Konsist offers two types of checks: Declaration Checks, which test for declaration-level violations, and Architectural Checks, which test for architectural guidelines and boundaries.

Without further delay, let’s dive right in.

Prerequisites

#Kotlin Multiplatform Mobile Project
#Konsist

For this example, I’m using a Multi-Module Kotlin Multiplatform Mobile project with the following structure. Alternatively, you can use Kotlin Multiplatform Mobile’s default structure.

1*5Czgp5Pxyi0UCEtUOBx9gw.png

Multi-Module KMM

Configuration

As a first step, we’ll create a new module for the Konsist Test that separates the konsist test from the unit test to improve readability.

  1. Right click on the shared module.
  2. Select New then Module to open the Create New Module window.
  3. Select the Kotlin Multiplatform Shared Module Template
  4. Enter your desired module name. Ex: :shared:konsisttest.
  5. Name your package. The rest is the same as the following image, so click Finish.
1*qBhryHjPTsHTrQRQv_JZyg.png
1*Apft1p-1mdgJSxm9dVzZZQ.png

You will then be able to see your new module on the project as follows.

1*qkdJUQYdd0AAn2gTVB5xcg.png

Dependency

Add mavenCentral repository

repositories {
//...
mavenCentral()
//...
}

Next, we will configure the Gradle file for :shared:konsisttest.

  1. Remove the Android Gradle plugin from the :shared:konsisttest module’s Gradle, along with its corresponding ‘android()’ Kotlin Target
  2. Add the ‘jvm()’ Kotlin Target and ‘jvmTest’ source set.
  3. Add the Konsist dependency to ‘jvmTest’ in the :shared:konsisttest module’s Gradle.
val jvmTest by getting {
dependencies {
implementation("com.lemonappdev:konsist:{VERSION}")
}
}

shared/konsisttest/build.gradle.kts

Following these steps, create a file in your ‘jvmTest’ source set that will be used to write test methods.

  1. Select New then Directory from the right-click menu on the module (:shared:konsisttest)
  2. Create the source set directory (src/jvmTest/kotlin)
  3. Create your package (Ex: com.stargatex.mt.lahiru.konsisttest)
  4. Create a file (Ex: AppKonsistTest)
1*VeZKzj8iNKWUiK6y0yOfrA.png

Directory creation

1*HsVQ1bmGsebmMVaXX8goSA.png

jvmTest source set directory

1*W3pPL-vlRPAXP_Gs8JHEtQ.png

Package creation

1*8UZWG0QuTqEkO58Krbyk9g.png

Test file creation

Implementation

First let’s take a look at the steps for ‘declaration check’.

  • As a first step, we need to determine the scope, which includes files from an entire project, a module, a package, or just one Kotlin file. (Create the scope)
// Scope for Kotlin files availble in the project
Konsist.scopeFromProject()
// Scope for Kotlin files availble in the 'app' module
Konsist.scopeFromModule("app")
// Scope for Kotlin files availble in the 'test' source set
Konsist.scopeFromSourceSet("test")
// Scope for Kotlin files availble for production code
Konsist.scopeFromProduction()
  • We then query all items in the scope and filter them according to our needs. (Query and Filter)
  • Finally, we define the assertion. (Assert)
1*Pe5WXu8I_DxTN2DZSYAHVQ.png

Now let’s examine the steps for ‘architecture check’.

  • As a first step, we need to determine the scope.
  • Next, we define layers of the project and assertions using the ‘assertArchiteture’ method
Konsist
.scopeFromProduction() // Define the scope
.assertArchitecture { // Assert architecture
// Define layers
val domain = Layer("Domain", "com.stargatex.mt.kmm.lahiru.shared.domain..")

// Define the relations between each layer for architecture assertions
domain.dependsOnNothing()
}
1*Bm57jsFLq8kKAPiBajnUzQ.png

Lastly, both ‘declaration check’ and ‘architecture check’ of Konsist test code will be executed as unit tests.

Use cases

Here are some examples of use cases you might need to test in a project.

For more examples, They have provided good examples that you can use as inspiration on how this can be used (snippets).


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK