Skip to content
This repository has been archived by the owner on Apr 1, 2022. It is now read-only.

Commit

Permalink
Gradle: Classifies testCompileClasspath and testRuntimeClasspath as t…
Browse files Browse the repository at this point in the history
…est configs (#366)
  • Loading branch information
meghfossa authored Sep 13, 2021
1 parent 59e26ac commit 19c9852
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 4 deletions.
4 changes: 4 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Spectrometer Changelog

## v2.15.17

- Gradle: Classifies dependency from `testCompileClasspath` and `testRuntimeClasspath` configurations as test dependencies. ([#366](https://github.com/fossas/spectrometer/pull/366))

## v2.15.16

- Yarn: Analyzes yarn.lock without runtime error, when yarn.lock includes symlinked package. ([#363](https://github.com/fossas/spectrometer/pull/363))
Expand Down
18 changes: 18 additions & 0 deletions docs/strategies/gradle.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Gradle users generally specify their builds using a `build.gradle` file (written
- [Manually checking Gradle dependency results](#manually-checking-gradle-dependency-results)
- [Debugging the "Gradle build plugin" tactic](#debugging-the-gradle-build-plugin-tactic)
- [Manually specifying Gradle dependencies](#manually-specifying-gradle-dependencies)
- [Configurations For Development and Testing](#configurations-for-development-and-testing)
- [Android Gradle Configurations For Development and Testing](#android-gradle-configurations-for-development-and-testing)

## Concepts
Expand Down Expand Up @@ -192,6 +193,23 @@ Notice that the `name` field follows Maven conventions: `groupId:artifactId`.

For more details, see the [manual dependencies](../userguide.md#manually-specifying-dependencies) documentation.

## Configurations For Development and Testing

We classify following configurations are for development:

```
- compileOnly
```

and following are for testing:

```
- testImplementation
- testCompileOnly
- testRuntimeOnly
- testCompileClasspath
- testRuntimeClasspath
```
## Android Gradle Configurations For Development and Testing

We classify following configurations, and any dependencies originating from it as a test environment dependency:
Expand Down
4 changes: 3 additions & 1 deletion src/Strategy/Gradle.hs
Original file line number Diff line number Diff line change
Expand Up @@ -312,10 +312,12 @@ buildGraph projectsAndDeps = run . withLabeling toDependency $ Map.traverseWithK
edge projAsDep dep
mkRecursiveEdges dep envLabel

-- Infers environment label based on the name of configuration.
-- Ref: https://docs.gradle.org/current/userguide/java_library_plugin.html#sec:java_library_configurations_graph
configNameToLabel :: ConfigName -> GradleLabel
configNameToLabel conf = case unConfigName conf of
"compileOnly" -> Env EnvDevelopment
x | x `elem` ["testImplementation", "testCompileOnly", "testRuntimeOnly"] -> Env EnvTesting
x | x `elem` ["testImplementation", "testCompileOnly", "testRuntimeOnly", "testCompileClasspath", "testRuntimeClasspath"] -> Env EnvTesting
x | isDefaultAndroidDevConfig x -> Env EnvDevelopment
x | isDefaultAndroidTestConfig x -> Env EnvTesting
x -> Env $ EnvOther x
Expand Down
46 changes: 43 additions & 3 deletions test/Gradle/GradleSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,48 @@ packageTwo =
, dependencyTags = Map.empty
}

mkProject :: Text -> DepEnvironment -> Dependency
mkProject name env =
Dependency
{ dependencyType = SubprojectType
, dependencyName = name
, dependencyVersion = Nothing
, dependencyLocations = []
, dependencyEnvironments = [env, EnvOther "config"]
, dependencyTags = Map.empty
}

projectFour :: Dependency
projectFour = mkProject ":projectFour" EnvTesting

projectFive :: Dependency
projectFive = mkProject ":projectFive" EnvTesting

mkMavenDep :: Text -> DepEnvironment -> Dependency
mkMavenDep name env =
Dependency
{ dependencyType = MavenType
, dependencyName = name
, dependencyVersion = Just (CEq "2.0.0")
, dependencyLocations = []
, dependencyEnvironments = [env]
, dependencyTags = Map.empty
}

packageFour :: Dependency
packageFour = mkMavenDep "mygroup:packageFour" EnvTesting

packageFive :: Dependency
packageFive = mkMavenDep "mygroup:packageFive" EnvTesting

gradleOutput :: Map (Text, Text) [JsonDep]
gradleOutput =
Map.fromList
[ ((":projectOne", "config"), [ProjectDep ":projectTwo"])
[ ((":projectOne", "config"), [ProjectDep ":projectTwo", ProjectDep ":projectFour", ProjectDep ":projectFive"])
, ((":projectTwo", "compileOnly"), [ProjectDep ":projectThree", PackageDep "mygroup:packageOne" "1.0.0" []])
, ((":projectThree", "testCompileOnly"), [PackageDep "mygroup:packageTwo" "2.0.0" []])
, ((":projectFour", "testCompileClasspath"), [PackageDep "mygroup:packageFour" "2.0.0" []])
, ((":projectFive", "testRuntimeClasspath"), [PackageDep "mygroup:packageFive" "2.0.0" []])
]

wrapKeys :: (Text, Text) -> (PackageName, ConfigName)
Expand All @@ -86,12 +122,16 @@ spec = do

it "should produce expected output" $ do
let graph = buildGraph $ Map.mapKeys wrapKeys gradleOutput
expectDeps [projectOne, projectTwo, projectThree, packageOne, packageTwo] graph
expectDirect [projectOne, projectTwo, projectThree] graph
expectDeps [projectOne, projectTwo, projectThree, packageOne, packageTwo, projectFour, projectFive, packageFour, packageFive] graph
expectDirect [projectOne, projectTwo, projectThree, projectFour, projectFive] graph
expectEdges
[ (projectOne, projectTwo)
, (projectTwo, projectThree)
, (projectTwo, packageOne)
, (projectThree, packageTwo)
, (projectOne, projectFour)
, (projectOne, projectFive)
, (projectFour, packageFour)
, (projectFive, packageFive)
]
graph

0 comments on commit 19c9852

Please sign in to comment.