Skip to content

Commit

Permalink
Added test variants to the source sets.
Browse files Browse the repository at this point in the history
Changes:
- Added android resource directories only if the variant is not a unit test
- Updated GradleApiConnectorTest#testAndroidSourceSets to consider the all the test variants and their dependencies
- Addressed checkstyle issues
  • Loading branch information
Tanish-Ranjan committed Sep 3, 2024
1 parent 59b0568 commit 4542f3b
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ public static List<GradleSourceSet> getBuildVariantsAsGradleSourceSets(Project p
try {
Set<Object> variants = (Set<Object>) invokeMethod(androidExtension, methodName);
for (Object variant : variants) {
GradleSourceSet sourceSet = convertVariantToGradleSourceSet(project, variant);
GradleSourceSet sourceSet =
convertVariantToGradleSourceSet(project, variant, sourceSets, false);
if (sourceSet == null) {
continue;
}
Expand All @@ -116,8 +117,15 @@ public static List<GradleSourceSet> getBuildVariantsAsGradleSourceSets(Project p
*
* @param project Gradle project to populate GradleSourceSet properties
* @param variant Android Build Variant object to populate GradleSourceSet properties
* @param sourceSets List of source sets to which test variants will be added
* @param isUnitTest Indicates if the given variant is a unit test variant
*/
private static GradleSourceSet convertVariantToGradleSourceSet(Project project, Object variant) {
private static GradleSourceSet convertVariantToGradleSourceSet(
Project project,
Object variant,
List<GradleSourceSet> sourceSets,
boolean isUnitTest
) {

try {

Expand Down Expand Up @@ -154,16 +162,16 @@ private static GradleSourceSet convertVariantToGradleSourceSet(Project project,
String displayName = projectName + " [" + variantName + ']';
gradleSourceSet.setDisplayName(displayName);

List<String> compilerArgs = new ArrayList<>();

// module dependencies
addModuleDependencies(gradleSourceSet, project, variant);

// source and resource
addSourceAndResources(gradleSourceSet, variant);
addSourceAndResources(gradleSourceSet, variant, isUnitTest);

// resource outputs
addResourceOutputs(gradleSourceSet, variant);
addResourceOutputs(gradleSourceSet, variant, isUnitTest);

List<String> compilerArgs = new ArrayList<>();

// generated sources and source outputs
addGeneratedSourceAndSourceOutputs(gradleSourceSet, variant, compilerArgs);
Expand All @@ -175,7 +183,7 @@ private static GradleSourceSet convertVariantToGradleSourceSet(Project project,
gradleSourceSet.setArchiveOutputFiles(new HashMap<>());

// has tests
addTests(gradleSourceSet, variant);
addTests(gradleSourceSet, project, variant, sourceSets);

// extensions
addExtensions(gradleSourceSet, compilerArgs);
Expand All @@ -196,11 +204,16 @@ private static GradleSourceSet convertVariantToGradleSourceSet(Project project,
* @param variant Instance of Build Variant
*/
@SuppressWarnings("unchecked")
private static void addModuleDependencies(DefaultGradleSourceSet gradleSourceSet, Project project, Object variant) {
private static void addModuleDependencies(
DefaultGradleSourceSet gradleSourceSet,
Project project,
Object variant
) {

Set<GradleModuleDependency> moduleDependencies =
AndroidDependencyCollector.getModuleDependencies(project, variant);

try {
Set<GradleModuleDependency> moduleDependencies =
AndroidDependencyCollector.getModuleDependencies(project, variant);
// add Android SDK
Object androidComponents = getAndroidComponentExtension(project);
if (androidComponents != null) {
Expand Down Expand Up @@ -231,78 +244,94 @@ private static void addModuleDependencies(DefaultGradleSourceSet gradleSourceSet
moduleDependencies.add(mockModuleDependency(jarFile.toURI()));
}
}
gradleSourceSet.setModuleDependencies(moduleDependencies);
} catch (InvocationTargetException | NoSuchMethodException | IllegalAccessException e) {
// do nothing
}

gradleSourceSet.setModuleDependencies(moduleDependencies);

}

/**
* Add source and resource directories to the given GradleSourceSet.
*
* @param gradleSourceSet Instance of DefaultGradleSourceSet
* @param variant Instance of Build Variant
* @param isUnitTest Indicates if the given variant is a unit test variant
*/
@SuppressWarnings("unchecked")
private static void addSourceAndResources(DefaultGradleSourceSet gradleSourceSet, Object variant) {
private static void addSourceAndResources(
DefaultGradleSourceSet gradleSourceSet,
Object variant,
boolean isUnitTest
) {

Set<File> sourceDirs = new HashSet<>();
Set<File> resourceDirs = new HashSet<>();

try {
Object sourceSets = getProperty(variant, "sourceSets");
Set<File> sourceDirs = new HashSet<>();
Set<File> resourceDirs = new HashSet<>();
if (sourceSets instanceof Iterable) {
for (Object sourceSet : (Iterable<?>) sourceSets) {
Set<File> javaDirectories =
(Set<File>) getProperty(sourceSet, "javaDirectories");
Set<File> resDirectories =
(Set<File>) getProperty(sourceSet, "resDirectories");
Set<File> resourceDirectories =
(Set<File>) getProperty(sourceSet, "resourcesDirectories");
sourceDirs.addAll(javaDirectories);
resourceDirs.addAll(resDirectories);
resourceDirs.addAll(resourceDirectories);
if (!isUnitTest) {
resourceDirs.addAll((Set<File>) getProperty(sourceSet, "resDirectories"));
}
resourceDirs.addAll((Set<File>) getProperty(sourceSet, "resourcesDirectories"));
}
}
gradleSourceSet.setSourceDirs(sourceDirs);
gradleSourceSet.setResourceDirs(resourceDirs);
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
// do nothing
}

gradleSourceSet.setSourceDirs(sourceDirs);
gradleSourceSet.setResourceDirs(resourceDirs);

}

/**
* Add resource output directories to the given GradleSourceSet.
*
* @param gradleSourceSet Instance of DefaultGradleSourceSet
* @param variant Instance of Build Variant
* @param isUnitTest Indicates if the given variant is a unit test variant
*/
@SuppressWarnings("unchecked")
private static void addResourceOutputs(DefaultGradleSourceSet gradleSourceSet, Object variant) {
private static void addResourceOutputs(
DefaultGradleSourceSet gradleSourceSet,
Object variant,
boolean isUnitTest
) {

Set<File> resourceOutputs = new HashSet<>();

try {
Set<File> resourceOutputs = new HashSet<>();
Provider<Task> resourceProvider =
(Provider<Task>) getProperty(variant, "processJavaResourcesProvider");
if (resourceProvider != null) {
Task resTask = resourceProvider.get();
File outputDir = (File) invokeMethod(resTask, "getDestinationDir");
resourceOutputs.add(outputDir);
}
Provider<Task> resProvider =
(Provider<Task>) getProperty(variant, "mergeResourcesProvider");
if (resProvider != null) {
Task resTask = resProvider.get();
Object outputDir = invokeMethod(resTask, "getOutputDir");
File output = ((Provider<File>) invokeMethod(outputDir, "getAsFile")).get();
resourceOutputs.add(output);

if (!isUnitTest) {
Provider<Task> resProvider =
(Provider<Task>) getProperty(variant, "mergeResourcesProvider");
if (resProvider != null) {
Task resTask = resProvider.get();
Object outputDir = invokeMethod(resTask, "getOutputDir");
File output = ((Provider<File>) invokeMethod(outputDir, "getAsFile")).get();
resourceOutputs.add(output);
}
}
gradleSourceSet.setResourceOutputDirs(resourceOutputs);
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
// do nothing
}

gradleSourceSet.setResourceOutputDirs(resourceOutputs);

}

/**
Expand All @@ -313,11 +342,16 @@ private static void addResourceOutputs(DefaultGradleSourceSet gradleSourceSet, O
* @param compilerArgs List to be populated from the java compiler arguments.
*/
@SuppressWarnings("unchecked")
private static void addGeneratedSourceAndSourceOutputs(DefaultGradleSourceSet gradleSourceSet, Object variant, List<String> compilerArgs) {
private static void addGeneratedSourceAndSourceOutputs(
DefaultGradleSourceSet gradleSourceSet,
Object variant,
List<String> compilerArgs
) {

Set<File> generatedSources = new HashSet<>();
Set<File> sourceOutputs = new HashSet<>();

try {
Set<File> generatedSources = new HashSet<>();
Set<File> sourceOutputs = new HashSet<>();
Provider<Task> javaCompileProvider =
(Provider<Task>) getProperty(variant, "javaCompileProvider");
if (javaCompileProvider != null) {
Expand Down Expand Up @@ -346,12 +380,13 @@ private static void addGeneratedSourceAndSourceOutputs(DefaultGradleSourceSet gr
generatedSources.add(compileSource);
}
}
gradleSourceSet.setGeneratedSourceDirs(generatedSources);
gradleSourceSet.setSourceOutputDirs(sourceOutputs);
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
// do nothing
}

gradleSourceSet.setGeneratedSourceDirs(generatedSources);
gradleSourceSet.setSourceOutputDirs(sourceOutputs);

}

/**
Expand All @@ -362,26 +397,54 @@ private static void addGeneratedSourceAndSourceOutputs(DefaultGradleSourceSet gr
*/
@SuppressWarnings("unchecked")
private static void addClasspath(DefaultGradleSourceSet gradleSourceSet, Object variant) {

Set<File> classpathFiles = new HashSet<>();

try {
Object compileConfig = invokeMethod(variant, "getCompileConfiguration");
Set<File> classpathFiles = (Set<File>) invokeMethod(compileConfig, "getFiles");
gradleSourceSet.setCompileClasspath(new LinkedList<>(classpathFiles));
classpathFiles.addAll((Set<File>) invokeMethod(compileConfig, "getFiles"));
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
// do nothing
}

gradleSourceSet.setCompileClasspath(new LinkedList<>(classpathFiles));

}

/**
* Add classpath files to the given GradleSourceSet.
*
* @param gradleSourceSet Instance of DefaultGradleSourceSet
* @param project Instance of Project
* @param variant Instance of Build Variant
* @param sourceSets List of source sets to which test variants will be added
*/
private static void addTests(DefaultGradleSourceSet gradleSourceSet, Object variant) {
private static void addTests(
DefaultGradleSourceSet gradleSourceSet,
Project project,
Object variant,
List<GradleSourceSet> sourceSets
) {
try {
Object unitTestVariant = invokeMethod(variant, "getUnitTestVariant");
Object testVariant = invokeMethod(variant, "getTestVariant");
gradleSourceSet.setHasTests(unitTestVariant != null || testVariant != null);

if (unitTestVariant != null) {
GradleSourceSet unitTestSourceSet =
convertVariantToGradleSourceSet(project, unitTestVariant, sourceSets, true);
if (unitTestSourceSet != null) {
sourceSets.add(unitTestSourceSet);
}
}

if (testVariant != null) {
GradleSourceSet androidTestSourceSet =
convertVariantToGradleSourceSet(project, testVariant, sourceSets, false);
if (androidTestSourceSet != null) {
sourceSets.add(androidTestSourceSet);
}
}
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
// do nothing
}
Expand All @@ -393,7 +456,10 @@ private static void addTests(DefaultGradleSourceSet gradleSourceSet, Object vari
* @param gradleSourceSet Instance of DefaultGradleSourceSet
* @param compilerArgs List of compiler arguments needed to build the language extension.
*/
private static void addExtensions(DefaultGradleSourceSet gradleSourceSet, List<String> compilerArgs) {
private static void addExtensions(
DefaultGradleSourceSet gradleSourceSet,
List<String> compilerArgs
) {
Map<String, LanguageExtension> extensions = new HashMap<>();
boolean isJavaSupported = Arrays.stream(SourceSetUtils.getSupportedLanguages())
.anyMatch(l -> Objects.equals(l, SupportedLanguages.JAVA.getBspName()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,18 +100,19 @@ void testAndroidSourceSets() {
preferenceManager.setPreferences(new Preferences());
GradleApiConnector connector = new GradleApiConnector(preferenceManager);
GradleSourceSets gradleSourceSets = connector.getGradleSourceSets(projectDir.toURI(), null);
assertEquals(4, gradleSourceSets.getGradleSourceSets().size());
assertEquals(10, gradleSourceSets.getGradleSourceSets().size());
findSourceSet(gradleSourceSets, "app [debug]");
findSourceSet(gradleSourceSets, "app [debugUnitTest]");
findSourceSet(gradleSourceSets, "app [debugAndroidTest]");
findSourceSet(gradleSourceSets, "app [release]");
findSourceSet(gradleSourceSets, "app [releaseUnitTest]");
findSourceSet(gradleSourceSets, "mylibrary [debug]");
findSourceSet(gradleSourceSets, "mylibrary [debugUnitTest]");
findSourceSet(gradleSourceSets, "mylibrary [debugAndroidTest]");
findSourceSet(gradleSourceSets, "mylibrary [release]");
findSourceSet(gradleSourceSets, "mylibrary [releaseUnitTest]");
Set<GradleModuleDependency> combinedModuleDependencies = new HashSet<>();
for (GradleSourceSet sourceSet : gradleSourceSets.getGradleSourceSets()) {
assertEquals(2, sourceSet.getSourceDirs().size());
assertEquals(4, sourceSet.getResourceDirs().size());
assertEquals(0, sourceSet.getExtensions().size());
assertEquals(0, sourceSet.getArchiveOutputFiles().size());
assertTrue(sourceSet.hasTests());
combinedModuleDependencies.addAll(sourceSet.getModuleDependencies());
}
// This test can vary depending on the environment due to generated files.
Expand All @@ -120,9 +121,7 @@ void testAndroidSourceSets() {
// the R.jar files don't exist for the build targets and are not included.
// 2. ANDROID_HOME is not configured in which case the Android Component classpath
// is not added to module dependencies.

// 57 is the number of actual project module dependencies without test variant dependencies
assertTrue(combinedModuleDependencies.size() >= 57);
assertTrue(combinedModuleDependencies.size() >= 82);
}

private GradleSourceSet findSourceSet(GradleSourceSets gradleSourceSets, String displayName) {
Expand Down

0 comments on commit 4542f3b

Please sign in to comment.