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

WIP Add support for Junit tests #1811

Closed
wants to merge 3 commits into from
Closed

Conversation

yigit
Copy link

@yigit yigit commented Apr 13, 2020

Android Transform API does not work for junit tests which breaks robolectric tests that use Hilt.

This CL adds a new optional task that will run the transform on the compiler output and feed it into the unit test task as the first classpath item (hence shadow regular output).
I've added an activity launcher test to the sample app and also a kotlin sample app since this task requires special handling for kotlin output.

Unfortunately, this does not work from Android Studio as it computes its own test classpath and does not use the gradle to run the test.

@yigit
Copy link
Author

yigit commented Apr 13, 2020

related studio bug: https://issuetracker.google.com/37003772

outputSourceRootDir: File,
skipIfNotHilt : Boolean = false
) {
outputSourceRootDir.mkdirs()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Check return value.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually the return value for mkdirs is not straightforward as it will return false if it already exists and that is a valid case here.

val outputFileCollection = project.objects.fileCollection()
val classpathKey = unitTestVariant.registerPreJavacGeneratedBytecode(
outputFileCollection.apply {
from(outputDir)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should be able to replace this with task output i.e. pass task.outputDir.getAsFileTree() to registerPreJavacGeneratedBytecode. That avoid usage of .builtBy on line 113.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm not sure how to do that, maybe i'm missing something here about gradle.

I don't have the task until it is configured but to configure the task i need to know the output directory. what am i missing here ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and if i try to register when task is configured then gradle won't really have a reason to configure it, but maybe i can add it as a dependency manually? i feel like i'm missing some gradle APIs here 🤷

}
}
// parse the classpath in reverse so that we respect overrides, if it ever happens
compiledClasses.files.filter { it.isDirectory }.reversed().forEach {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you filter directories? In 4.1, for the unit test variant the tested code (i.e. main) will be in the classpath as jar.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the code i'm re-using here is from transform API which does ignore jars.
and yea i just tried w/ 4.1 and it fails, we were using 3.6.3 here. i'll see what we can do there.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done. added transform for jars but only here. i'm guessing the regular transform API still receives .class files ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually seems to be working fine but i'll keep this open in case we should also process contents of jar files for the transform API. FYI we are only interested in files from the compiled module/app.

Android transforms do not run for junit tests which breaks hilt.
This CL adds a new custom test transform just for tests so that Hilt
can run fine for AndroidJUnit tests
@cpovirk cpovirk mentioned this pull request Apr 30, 2020
cpovirk pushed a commit that referenced this pull request Apr 30, 2020
Android transforms are not applied for JUnit tests which breaks when using
the Hilt Plugin and its transform. This CL adds a new custom test transform
task just for tests so that transformed classes are used in Android JUnit tests.

The task does not transform test sources, therefore temporarily allow specifying
a base class in @androidentrypoint that will be used even if the superclass
validation option is given to the processor. This helps us still have test-only
Android Entry Points.

Closes #1811

RELNOTES=n/a

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=309143731
cpovirk pushed a commit that referenced this pull request Apr 30, 2020
Android transforms are not applied for JUnit tests which breaks when using
the Hilt Plugin and its transform. This CL adds a new custom test transform
task just for tests so that transformed classes are used in Android JUnit tests.

The task does not transform test sources, therefore temporarily allow specifying
a base class in @androidentrypoint that will be used even if the superclass
validation option is given to the processor. This helps us still have test-only
Android Entry Points.

Closes #1811

RELNOTES=n/a

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=309143731
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants