Skip to content

Commit

Permalink
Merge pull request #28 from square/develop
Browse files Browse the repository at this point in the history
Prepare for v0.1.3 release
  • Loading branch information
pablobaxter authored Apr 18, 2024
2 parents f943e5c + 20de5e0 commit 253c22f
Show file tree
Hide file tree
Showing 11 changed files with 168 additions and 25 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## Unreleased

## v0.1.3
- `affected-paths-core`: Fix custom Gradle flags not being properly set
- `tooling-support-android`: Evaluate projects with `com.android.test` plugin

## v0.1.2
- `affected-paths-core`, `tooling-support-*`: Add in support for composite builds being analyzed
- `affected-paths-core`: Remove filter of root project
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ internal class BaseConfigurationOptions {

@Option(
names = ["--changed-files"],
description = ["List of changed files to use instead of the Git diff"]
description = ["List of changed files to use instead of the Git diff"],
split = " "
)
var changedFiles: List<String> = emptyList()
internal set
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ public data class CoreOptions @JvmOverloads constructor(
}

internal val gradleArgs: List<String> = buildList {
addAll(customGradleFlags)
if (autoInjectPlugin) {
add("-I")
add(
Expand All @@ -107,7 +108,7 @@ public data class CoreOptions @JvmOverloads constructor(
mavenCentral()
}
dependencies {
classpath "com.squareup.affected.paths:tooling-support:0.1.2"
classpath "com.squareup.affected.paths:tooling-support:0.1.3"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,12 @@ internal suspend fun findAffectedPaths(
changedFiles: List<String>
): List<AffectedResult> {
return coroutineScope {
// Separate the projects to their distinct builds
val projectsMappedToBuilds = buildMap<String, MutableList<SquareProject>> {
projectList.forEach {
val list = getOrPut(it.namespace) { arrayListOf() }
list.add(it)
}
}

return@coroutineScope projectsMappedToBuilds.flatMap { (_, projects) ->
val slices = async(Dispatchers.Default) { projects.getReverseDependencies() }
val filesToDocs = async(Dispatchers.Default) {
filesToProjects(changedFiles, projects.associateBy { it.pathToProject })
}
return@flatMap findAffectedAddresses(slices.await(), filesToDocs.await())
val slices = async(Dispatchers.Default) { projectList.getReverseDependencies() }
val filesToDocs = async(Dispatchers.Default) {
filesToProjects(changedFiles, projectList.associateBy { it.pathToProject })
}
return@coroutineScope findAffectedAddresses(slices.await(), filesToDocs.await())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class AffectedPathsTest {
}
dependencies {
implementation(':library')
implementation project(':library')
}
""".trimIndent()
)
Expand Down Expand Up @@ -95,7 +95,7 @@ class AffectedPathsTest {
}
dependencies {
implementation(':library')
implementation project(':library')
}
""".trimIndent()
)
Expand Down Expand Up @@ -140,7 +140,7 @@ class AffectedPathsTest {
val result = analyzer.analyze()

assertContentEquals(listOf("build2/foobar", "app", "library"), result.projectMap.keys)
assertContentEquals(listOf("library"), result.affectedResults.flatMap { it.affectedProjectPaths }.distinct())
assertContentEquals(listOf("app", "app:debug:debugUnitTest", "library", "app:release:releaseUnitTest"), result.affectedResults.flatMap { it.affectedProjectPaths }.distinct())
}

@Test
Expand Down Expand Up @@ -168,7 +168,7 @@ class AffectedPathsTest {
}
dependencies {
implementation(':library')
implementation project(':library')
}
""".trimIndent()
)
Expand Down Expand Up @@ -214,7 +214,7 @@ class AffectedPathsTest {
val result = analyzer.analyze()

assertContentEquals(listOf("app", "library"), result.projectMap.keys)
assertContentEquals(listOf("library"), result.affectedResults.flatMap { it.affectedProjectPaths }.distinct())
assertContentEquals(listOf("app", "app:debug:debugUnitTest", "library", "app:release:releaseUnitTest"), result.affectedResults.flatMap { it.affectedProjectPaths }.distinct())
}

@Test
Expand Down Expand Up @@ -246,7 +246,7 @@ class AffectedPathsTest {
}
dependencies {
implementation(':library')
implementation project(':library')
implementation('com.squareup:blah:0.0.1')
}
""".trimIndent()
Expand Down Expand Up @@ -319,7 +319,7 @@ class AffectedPathsTest {
}
dependencies {
implementation(':library:foobar')
implementation project(':library:foobar')
implementation('com.squareup:blah:0.0.1')
}
""".trimIndent()
Expand Down Expand Up @@ -357,7 +357,7 @@ class AffectedPathsTest {
val result = analyzer.analyze()

assertContentEquals(listOf("app", "library", "library/foobar"), result.projectMap.keys)
assertContentEquals(listOf("library/foobar"), result.affectedResults.flatMap { it.affectedProjectPaths }.distinct())
assertContentEquals(listOf("app", "app:debug:debugUnitTest", "library/foobar", "app:release:releaseUnitTest"), result.affectedResults.flatMap { it.affectedProjectPaths }.distinct())
}

private fun createSettingsFile(rootDir: Path, contents: String) {
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8
kotlin.code.style=official

GROUP=com.squareup.affected.paths
VERSION_NAME=0.1.2
VERSION_NAME=0.1.3

POM_URL=https://github.com/squareup/affected-paths
POM_SCM_URL=https://github.com/squareup/affected-paths
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,25 @@ public fun generateLibraryBuild(temporaryFolder: File): File {
}
return buildDirectory
}

// Helper function for generating a bare-bone Android test project
public fun generateTestBuild(temporaryFolder: File): File {
val buildDirectory = temporaryFolder.canonicalFile.apply { mkdirs() }
File(buildDirectory, "build.gradle").apply {
writeText("""
apply plugin: 'com.android.test'
apply plugin: 'kotlin-android'
android {
targetProjectPath = ":app"
namespace 'com.squareup.test.library'
compileSdk 33
defaultConfig {
targetSdk 17
minSdk 17
}
}
""".trimIndent())
}
return buildDirectory
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package com.squareup.tooling.support.android

import com.android.build.gradle.AppExtension
import com.android.build.gradle.LibraryExtension
import com.android.build.gradle.TestExtension
import com.squareup.tooling.models.SquareProject
import com.squareup.tooling.models.SquareTestConfiguration
import com.squareup.tooling.support.core.extractors.relativePathToRootBuild
Expand Down Expand Up @@ -93,3 +94,36 @@ internal fun Project.extractLibraryModuleProject(): SquareProject {
}
)
}

/**
* Extracts a [SquareProject] using the Android [TestExtension].
*/
internal fun Project.extractTestModuleProject(): SquareProject {
val testExtension = requireNotNull(extensions.findByType(TestExtension::class.java))

// Gets the sources defined in the extension
val sourceIndex = testExtension.sourceIndexExtractor()

return SquareProject(
name = name,
pluginUsed = "android-test",
namespace = rootProject.name,
pathToProject = relativePathToRootBuild() ?: relativePathToRootProject(),
variants = testExtension.applicationVariants.associate { variant ->
val (srcs, deps) = variant.extractSquareVariantConfigurationParams(this, sourceIndex)
val tests = buildMap<String, SquareTestConfiguration> {
variant.testVariant?.let {
put(it.name, it.extractSquareTestConfiguration(this@extractTestModuleProject))
}
variant.unitTestVariant?.let {
put(it.name, it.extractSquareTestConfiguration(this@extractTestModuleProject))
}
}
variant.name to SquareVariantConfiguration(
srcs = srcs,
deps = deps,
tests = tests
)
}
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ internal class SquareProjectExtractorImpl : SquareProjectExtractor {
// Android library plugin logic
project.plugins.hasPlugin("com.android.library") -> project.extractLibraryModuleProject()

// Android test plugin logic
project.plugins.hasPlugin("com.android.test") -> project.extractTestModuleProject()

else -> null
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,40 @@ class SquareProjectModelBuilderTest {
assertEquals(expected, result)
}

@Test
fun `Ensure android test constructs SquareProject`() {
val projectExtractor = SquareProjectExtractorImpl()

val rootProject = ProjectBuilder
.builder()
.withName("com.squareup")
.build()

val childProject = ProjectBuilder
.builder()
.withName("test")
.withParent(rootProject)
.build()

val testProject = ProjectBuilder
.builder()
.withName("lib")
.withParent(childProject)
.build()

testProject.plugins.apply("com.android.test")

val result = projectExtractor.extractSquareProject(testProject)
val expected = SquareProject(
name = "lib",
namespace = "com.squareup",
pathToProject = "test/lib",
pluginUsed = "android-test",
variants = emptyMap()
)
assertEquals(expected, result)
}

@Test
fun `Ensure no plugins returns null`() {
val projectExtractor = SquareProjectExtractorImpl()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package com.squareup.tooling.support.builder
import com.squareup.test.support.forceEvaluate
import com.squareup.test.support.generateApplicationBuild
import com.squareup.test.support.generateLibraryBuild
import com.squareup.test.support.generateTestBuild
import com.squareup.tooling.models.SquareProject
import com.squareup.tooling.support.core.models.SquareDependency
import org.gradle.testfixtures.ProjectBuilder
Expand All @@ -29,7 +30,6 @@ import org.junit.jupiter.api.io.TempDir
import java.io.File
import kotlin.test.assertContains
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
import kotlin.test.assertFalse
import kotlin.test.assertTrue

Expand Down Expand Up @@ -288,6 +288,59 @@ class SquareProjectModelBuilderTest {
assertTrue(releaseUnitTest.deps.isEmpty())
}

@Test
fun `Ensure android test constructs SquareProject`() {
val projectModelBuilder = SquareProjectModelBuilder()

val rootProject = ProjectBuilder
.builder()
.withProjectDir(temporaryFolder)
.withName("com.squareup.test")
.build()

val libProject = ProjectBuilder
.builder()
.withName("test-lib")
.withProjectDir(generateTestBuild(File(rootProject.projectDir, "lib")))
.withParent(rootProject)
.build()

// Add in the ":app" project
ProjectBuilder
.builder()
.withName("app")
.withProjectDir(generateApplicationBuild(File(rootProject.projectDir, "app")))
.withParent(rootProject)
.build()

val expectedTestDependency = SquareDependency("/app", setOf("transitive"))

libProject.forceEvaluate()

val result = projectModelBuilder.buildAll(SquareProject::class.java.name, libProject) as SquareProject

// Check SquareProject properties
assertEquals("test-lib", result.name)
assertEquals("com.squareup.test", result.namespace)
assertEquals("lib", result.pathToProject)
assertEquals("android-test", result.pluginUsed)

// Check variant properties
assertTrue(result.variants.keys.containsAll(listOf("debug")))
val debugVariant = requireNotNull(result.variants["debug"])
assertTrue {
debugVariant.srcs.containsAll(
ANDROID_SRC_DIRECTORY_PATHS.map { "src/debug/$it" } +
ANDROID_SRC_DIRECTORY_PATHS.map { "src/main/$it" }
)
}
assertTrue(debugVariant.deps.contains(expectedTestDependency))

// Check test variant properties
val debugTestVariants = debugVariant.tests
assertTrue(debugTestVariants.keys.isEmpty())
}

@Test
fun `Do not throw exception if a non-Java or non-Android plugin is used`() {
val projectModelBuilder = SquareProjectModelBuilder()
Expand Down

0 comments on commit 253c22f

Please sign in to comment.