-
-
Notifications
You must be signed in to change notification settings - Fork 141
A new task to build type usage report (ComputeTypeUsageTask). #1639
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| // Copyright (c) 2026. Tony Robalik. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| package com.autonomousapps.jvm | ||
|
|
||
| import com.autonomousapps.jvm.projects.TypeUsageProject | ||
| import com.autonomousapps.jvm.projects.TypeUsageWithFiltersProject | ||
|
|
||
| import static com.autonomousapps.utils.Runner.build | ||
| import static com.google.common.truth.Truth.assertThat | ||
|
|
||
| final class TypeUsageSpec extends AbstractJvmSpec { | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Following project convention to test |
||
|
|
||
| def "generates type usage report (#gradleVersion)"() { | ||
| given: | ||
| def project = new TypeUsageProject() | ||
| gradleProject = project.gradleProject | ||
|
|
||
| when: | ||
| build(gradleVersion, gradleProject.rootDir, 'computeTypeUsageMain') | ||
|
|
||
| then: 'has correct summary' | ||
| def usage = project.actualTypeUsage() | ||
| assertThat(usage.projectPath).isEqualTo(':proj') | ||
| assertThat(usage.summary.totalTypes).isGreaterThan(0) | ||
| assertThat(usage.summary.totalFiles).isEqualTo(2) | ||
| assertThat(usage.summary.internalTypes).isEqualTo(1) | ||
|
|
||
| and: 'tracks internal usage' | ||
| assertThat(usage.internal).containsKey('com.example.Example') | ||
| // Note: Internal class is not tracked because it's defined but never used | ||
|
|
||
| and: 'tracks library dependencies' | ||
| assertThat(usage.libraryDependencies).isNotEmpty() | ||
|
|
||
| and: 'tracks commons-collections usage' | ||
| assertThat(usage.libraryDependencies) | ||
| .containsKey('org.apache.commons:commons-collections4') | ||
| def commonsUsage = usage.libraryDependencies.get('org.apache.commons:commons-collections4') | ||
| assertThat(commonsUsage).containsKey('org.apache.commons.collections4.bag.HashBag') | ||
|
|
||
| and: 'tracks kotlin stdlib usage' | ||
| assert usage.libraryDependencies.keySet().any { it.startsWith('org.jetbrains.kotlin:kotlin-stdlib') } | ||
|
|
||
| where: | ||
| gradleVersion << gradleVersions() | ||
| } | ||
|
|
||
| def "excludes filtered types (#gradleVersion)"() { | ||
| given: | ||
| def project = new TypeUsageWithFiltersProject() | ||
| gradleProject = project.gradleProject | ||
|
|
||
| when: | ||
| build(gradleVersion, gradleProject.rootDir, 'computeTypeUsageMain') | ||
|
|
||
| then: 'excluded packages are not present' | ||
| def usage = project.actualTypeUsage() | ||
| def allTypes = usage.libraryDependencies.values() | ||
| .collectMany { it.keySet() } | ||
|
|
||
| assertThat(allTypes).doesNotContain('org.apache.commons.collections4.bag.HashBag') | ||
|
|
||
| and: 'excluded types are not present' | ||
| assertThat(allTypes).doesNotContain('kotlin.Unit') | ||
|
|
||
| and: 'non-excluded types are still present' | ||
| assertThat(usage.internal).containsKey('com.example.Example') | ||
|
|
||
| where: | ||
| gradleVersion << gradleVersions() | ||
| } | ||
|
|
||
| def "categorizes dependencies correctly (#gradleVersion)"() { | ||
| given: | ||
| def project = new TypeUsageProject() | ||
| gradleProject = project.gradleProject | ||
|
|
||
| when: | ||
| build(gradleVersion, gradleProject.rootDir, 'computeTypeUsageMain') | ||
|
|
||
| then: 'internal types are in internal map' | ||
| def usage = project.actualTypeUsage() | ||
| assertThat(usage.internal).isNotEmpty() | ||
| assertThat(usage.internal).containsKey('com.example.Example') | ||
|
|
||
| and: 'library types are in libraryDependencies map' | ||
| assertThat(usage.libraryDependencies).isNotEmpty() | ||
|
|
||
| and: 'no project dependencies (single-project)' | ||
| assertThat(usage.projectDependencies).isEmpty() | ||
|
|
||
| and: 'summary counts match' | ||
| assertThat(usage.summary.internalTypes).isEqualTo(usage.internal.size()) | ||
| assertThat(usage.summary.libraryDependencies).isEqualTo(usage.libraryDependencies.size()) | ||
| assertThat(usage.summary.projectDependencies).isEqualTo(0) | ||
|
|
||
| where: | ||
| gradleVersion << gradleVersions() | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| // Copyright (c) 2026. Tony Robalik. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| package com.autonomousapps.jvm.projects | ||
|
|
||
| import com.autonomousapps.AbstractProject | ||
| import com.autonomousapps.kit.GradleProject | ||
| import com.autonomousapps.kit.Source | ||
| import com.autonomousapps.kit.SourceType | ||
| import com.autonomousapps.model.ProjectTypeUsage | ||
|
|
||
| import static com.autonomousapps.kit.gradle.dependencies.Dependencies.* | ||
|
|
||
| final class TypeUsageProject extends AbstractProject { | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added a functional test for this task (tried to follow project conventions) |
||
|
|
||
| final GradleProject gradleProject | ||
|
|
||
| TypeUsageProject() { | ||
| this.gradleProject = build() | ||
| } | ||
|
|
||
| private GradleProject build() { | ||
| return newGradleProjectBuilder() | ||
| .withSubproject('proj') { s -> | ||
| s.sources = sources | ||
| s.withBuildScript { bs -> | ||
| bs.plugins = kotlin | ||
| bs.dependencies = [ | ||
| commonsCollections('implementation'), | ||
| kotlinStdLib('implementation') | ||
| ] | ||
| } | ||
| } | ||
| .write() | ||
| } | ||
|
|
||
| private sources = [ | ||
| new Source( | ||
| SourceType.KOTLIN, "Example", "com/example", | ||
| """\ | ||
| package com.example | ||
|
|
||
| import org.apache.commons.collections4.bag.HashBag | ||
|
|
||
| class Example { | ||
| private val bag = HashBag<String>() | ||
|
|
||
| fun doSomething() { | ||
| bag.add("test") | ||
| } | ||
| } | ||
| """.stripIndent() | ||
| ), | ||
| new Source( | ||
| SourceType.KOTLIN, "Internal", "com/example", | ||
| """\ | ||
| package com.example | ||
|
|
||
| internal class Internal { | ||
| fun helper() = Example() | ||
| } | ||
| """.stripIndent() | ||
| ) | ||
| ] | ||
|
|
||
| ProjectTypeUsage actualTypeUsage() { | ||
| def typeUsage = gradleProject.singleArtifact(':proj', | ||
| com.autonomousapps.internal.OutputPathsKt.getTypeUsagePath('main')) | ||
| def adapter = com.autonomousapps.internal.utils.MoshiUtils.MOSHI | ||
| .adapter(com.autonomousapps.model.ProjectTypeUsage) | ||
| return adapter.fromJson(typeUsage.asPath.text) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| // Copyright (c) 2026. Tony Robalik. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| package com.autonomousapps.jvm.projects | ||
|
|
||
| import com.autonomousapps.AbstractProject | ||
| import com.autonomousapps.kit.GradleProject | ||
| import com.autonomousapps.kit.Source | ||
| import com.autonomousapps.kit.SourceType | ||
| import com.autonomousapps.model.ProjectTypeUsage | ||
|
|
||
| import static com.autonomousapps.kit.gradle.dependencies.Dependencies.* | ||
|
|
||
| final class TypeUsageWithFiltersProject extends AbstractProject { | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another functional test for the new task (tried to follow project conventions) |
||
|
|
||
| final GradleProject gradleProject | ||
|
|
||
| TypeUsageWithFiltersProject() { | ||
| this.gradleProject = build() | ||
| } | ||
|
|
||
| private GradleProject build() { | ||
| return newGradleProjectBuilder() | ||
| .withSubproject('proj') { s -> | ||
| s.sources = sources | ||
| s.withBuildScript { bs -> | ||
| bs.plugins = kotlin | ||
| bs.dependencies = [ | ||
| commonsCollections('implementation'), | ||
| kotlinStdLib('implementation') | ||
| ] | ||
| bs.additions = """\ | ||
| dependencyAnalysis { | ||
| typeUsage { | ||
| excludePackages('org.apache.commons') | ||
| excludeTypes('kotlin.Unit') | ||
| } | ||
| } | ||
| """.stripIndent() | ||
| } | ||
| } | ||
| .write() | ||
| } | ||
|
|
||
| private sources = [ | ||
| new Source( | ||
| SourceType.KOTLIN, "Example", "com/example", | ||
| """\ | ||
| package com.example | ||
|
|
||
| import org.apache.commons.collections4.bag.HashBag | ||
|
|
||
| class Example { | ||
| private val bag = HashBag<String>() | ||
|
|
||
| fun doSomething(): Unit { | ||
| bag.add("test") | ||
| } | ||
| } | ||
| """.stripIndent() | ||
| ), | ||
| new Source( | ||
| SourceType.KOTLIN, "Internal", "com/example", | ||
| """\ | ||
| package com.example | ||
|
|
||
| internal class Internal { | ||
| fun helper() = Example() | ||
| } | ||
| """.stripIndent() | ||
| ) | ||
| ] | ||
|
|
||
| ProjectTypeUsage actualTypeUsage() { | ||
| def typeUsage = gradleProject.singleArtifact(':proj', | ||
| com.autonomousapps.internal.OutputPathsKt.getTypeUsagePath('main')) | ||
| def adapter = com.autonomousapps.internal.utils.MoshiUtils.MOSHI | ||
| .adapter(com.autonomousapps.model.ProjectTypeUsage) | ||
| return adapter.fromJson(typeUsage.asPath.text) | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please provide guidance here - I've added this, but I'm not sure what should actually be included here.