Create The Scope
Access the Kotlin files using Konsist API
Scope represents a set of Kotlin files to be further queried, filtered (Declaration Query And Filter), and verified (Declaration Assert).
Scopes are an alternative for baseline
file. Subsets of the codebase can be refactored to be aligned with Konsit tests e.g. code in the single module.
Every scope contains a set of KoFile
instances. Every KoFile
instance contains the declarations (see Declaration) representing code entities present in the file e.g.:
Konsist is built on top of Kotlin Compiler Psi. It wraps the Kotlin compiler parser and provides a simple API to access Kotlin code base declarations. Konsist Declaration tree mimics the Kotlin code structure:
The scope can be created for an entire project, module, package, and Kotlin file.
The scope is dynamically built based on the Kotlin files present in the project, enabling it to adapt seamlessly as the project evolves. For instance, when the scope is set to encapsulate a specific module, any additional file introduced to that module will be automatically incorporated into the scope. This ensures that the scope consistently offers thorough and current coverage.
To execute Konsist tests, the Konsist dependency must be integrated into a module. Yet, by integrating Konsist into a single module (e.g. app
module), Konsist can still access the entire project. The specific files evaluated are determined by the evolving scope that's been defined.
Scope Creation
Various methods can be used to obtain instances of the scope. This allows the definition of more granular Konsist tests e.g. tests covering only certain modules, source sets, packages, or folders.
Project Scope
The widest scope is the scope containing all Kotlin files present inside the project:
To print a list of files within koScope
use the koScope.print()
method:
To review the scope content in more detail see Debug Konsist Test.
Module Scope
The scopeFromModule
method allows the creation of more granular scopes based on the module name e.g. creating a scope containing all Kotlin files present in the app
module:
Selection:
This approach may be helpful when refactoring existing project modules by module.
Nested Module Scope
A nested module is a module that exists within another module.
The nested modules
the feature is not complete. The community is reporting that this feature works, however, we still have to take a closer look, review expectations, and add tests. Consider this feature as experimental for now.
Consider this feature
module existing inside app
module:
To narrow the scope to feature
module use:
Source Set Scope
The scopeFromSourceSet
method argument allows the creation of more granular scopes based on the source set name e.g. create a scope containing all Kotlin files present in the test
source set:
Selection:
Module and Source Set Scope
To retrieve scope by using both module and source set use the scopeFromProject
method with moduleName
and sourceSetName
arguments:
Selection:
Production Codebase
The scopeFromProduction
method allows the creation of a scope containing only a production code:
Selection:
Test Codebase
The scopeFromTest
method allows the creation of a scope containing only a test code:
Selection:
Package Scope
The sourceFromPackage
method allows the creation of a scope containing code present in a given package e.g. com.usecase
package:
Selection:
The double dots (..
) syntax means zero or more packages. Check the Package Wildcard page.
Directory Scope
The scopeFromDirectory
method allows the creation of a scope containing code present in a given project folder e.g. domain
directory:
Selection:
File Scope
It is also possible to create scope from one or more file paths:
We have added a new way of creating the scope from a list of files. This can help with certain development workflows e.g. runing Konsist Tests only on files modified in a given PR:
Scope Slice
For even more granular control you can use the KoScope.slice
method to retrieve a scope containing a subset of files from the given scope:
The KoScope
can be printed to display a list of all files present in the scope. Here is an example:
Scope Reuse
Reuse Scope In Test Class
To reuse scope across the test class define the scope in the companion object and access it from multiple tests:
Reuse Scope In Test Source Set
To reuse scope across the multiple test classes define the scope in the file and access it from multiple test classes:
Here is the file structure representing the above snippet:
Scope Composition
Konsist scope supports Kotlin Operator overloading, so copes can be further combined together to create the desired scope, tailored to project needs. In this example scopes from myFeature1
module and myFeature2
module are combined together:
Scope Subtraction
Scope subtraction is also supported, so it is possible for example to exclude a part of a given module. Here scope is created from myFeature
module and then the ..data..
package is excluded:
Print Scope
To print all files within the scope use the print()
method:
See Debug Konsist Test.
Access Specific Declarations
To access specific declaration types such as interfaces, classes, constructors, functions, etc. utilize the Declaration Query And Filter.
Last updated