Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

modulePath returns incorrect path for nested-module structures #14

Open
arecmetafora opened this issue Mar 26, 2024 · 1 comment
Open

Comments

@arecmetafora
Copy link

arecmetafora commented Mar 26, 2024

If you define your project in such a way you have more than one layer of modules, whenever you run tests for a specific class it will start a gradle task on the topmost parent module in the hierarchy.

For instance, imagine we have the following project structure:

  • myProject root
    • model
    • domain
    • presentation
      • feature1
      • feature2
      • featureN

Running any contextual testing action for a feature class will trigger a gradle task for the whole presentation module.
That can be problematic for projects with dozen of other sibling modules.

Executing tasks: [verifyPaparazziDebug, --tests, com.company.presentation.feature1.myClass] in project /Users/alex/git/myProject/presentation

The problem seems to be caused by the Path.modulePath extension, which probably assumes there will be only one layer of modules.

internal fun Project.modulePath(file: VirtualFile): String? {
return modules.asSequence().map { it.getModuleDir() }.firstOrNull { file.path.startsWith(it) }
?: basePath?.let { projectPath ->
val relativePath = FileUtil.getRelativePath(projectPath, file.path, File.separatorChar)
val moduleName = relativePath?.split(File.separator)?.firstOrNull()
if (moduleName != null) projectPath + File.separator + moduleName else null
}
}

@arecmetafora
Copy link
Author

I checked the getModuleDir result for my project and it returned strange module paths under .idea folder, like:

/Users/USER/MyProject/.idea/modules/presentation/feature1

An easy workaround would be removing the ".idea/modules" from the module's path.
In additional to that, because we have nested modules, we need to get the path that is closer to the file. This can be solved by ordering the paths by size and choosing the first match with the biggest size.

The following implementation works and address both issues mentioned above:

internal fun Project.modulePath(file: VirtualFile): String? {
    return modules.asSequence().map {  it.getModuleDir().replace(".idea/modules/", "") }
            .sortedByDescending { it.length }
            .firstOrNull { file.path.startsWith(it) }
        ?: basePath?.let { projectPath ->
            val relativePath = FileUtil.getRelativePath(projectPath, file.path, File.separatorChar)
            val moduleName = relativePath?.split(File.separator)?.firstOrNull()
            if (moduleName != null) projectPath + File.separator + moduleName else null
        }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant