11package com.gladed.gradle.androidgitversion
22
33import org.eclipse.jgit.api.Git
4+ import org.eclipse.jgit.lib.Repository
5+
46import org.gradle.api.Project
57import org.gradle.testfixtures.ProjectBuilder
68import org.junit.rules.TemporaryFolder
79
810class AndroidGitVersionTest extends GroovyTestCase {
911
10- def projectFolder = new TemporaryFolder ()
12+ @Lazy TemporaryFolder projectFolder = {
13+ TemporaryFolder folder = new TemporaryFolder ()
14+ folder. create()
15+ folder. newFile(" build.gradle" )
16+ return folder
17+ }()
18+
19+ @Lazy Project project = {
20+ ProjectBuilder . builder()
21+ .withProjectDir(projectFolder. root)
22+ .build()
23+ }()
24+
25+ @Lazy Git git = {
26+ return Git . init(). setDirectory(projectFolder. root). call();
27+ }()
1128
12- // These properties don't exist until touched
13- @Lazy Git git = { initGit() }()
14- @Lazy AndroidGitVersionExtension plugin = { makePlugin() }()
29+ @Lazy AndroidGitVersionExtension plugin = {
30+ project. pluginManager. apply ' com.gladed.androidgitversion'
31+ def extension = project. getExtensions(). getByName(' androidGitVersion' )
32+ assertTrue (extension instanceof AndroidGitVersionExtension )
33+ return (AndroidGitVersionExtension ) extension
34+ }()
1535
1636 void testNoGitRepo () {
1737 assertEquals (' unknown' , plugin. name())
@@ -177,8 +197,44 @@ class AndroidGitVersionTest extends GroovyTestCase {
177197 assertEquals (" 1.1-release" , plugin. name())
178198 }
179199
180- private Git initGit () {
181- return Git . init(). setDirectory(projectFolder. root). call();
200+ void testSubmodule () {
201+ // Set up a base repo
202+ addCommit()
203+ addTag(" 1.0" )
204+ assertEquals (" 1.0" , plugin. name())
205+
206+ TemporaryFolder libraryFolder = new TemporaryFolder ()
207+ libraryFolder. create()
208+ libraryFolder. newFile(" build.gradle" )
209+
210+ try {
211+ // Create a library repo with its own label
212+ Git libraryGit = Git . init(). setDirectory(libraryFolder. root). call()
213+ libraryGit. add(). addFilepattern(" build.gradle" ). call()
214+ libraryGit. commit(). setMessage(" addition" ). call()
215+ libraryGit. tag(). setName(" 2.0" ). call()
216+
217+ // Add the library repo to the base repo as a submodule
218+ Repository libraryRepo = git. submoduleAdd()
219+ .setPath(" library" )
220+ .setURI(libraryGit. getRepository(). getDirectory(). getCanonicalPath())
221+ .call()
222+ libraryRepo. close()
223+
224+ // Add the submodule as a subproject to the base project
225+ Project libraryProject = ProjectBuilder . builder()
226+ .withProjectDir(new File (projectFolder. root, " library" ))
227+ .withParent(project)
228+ .build()
229+ libraryProject. pluginManager. apply ' com.gladed.androidgitversion'
230+ AndroidGitVersionExtension libraryPlugin = (AndroidGitVersionExtension ) libraryProject. getExtensions(). getByName(' androidGitVersion' )
231+
232+ // Make sure the subproject gets its version number from the library repo
233+ // and NOT the base repo
234+ assertEquals (" 2.0" , libraryPlugin. name())
235+ } finally {
236+ libraryFolder. delete()
237+ }
182238 }
183239
184240 private void addCommit () {
@@ -199,23 +255,6 @@ class AndroidGitVersionTest extends GroovyTestCase {
199255 git. checkout(). setCreateBranch(true ). setName(branchName). call()
200256 }
201257
202- private AndroidGitVersionExtension makePlugin () {
203- Project project = ProjectBuilder . builder()
204- .withProjectDir(projectFolder. root)
205- .build()
206- project. pluginManager. apply ' com.gladed.androidgitversion'
207- def extension = project. getExtensions(). getByName(' androidGitVersion' )
208- assertTrue (extension instanceof AndroidGitVersionExtension )
209- return (AndroidGitVersionExtension ) extension
210- }
211-
212- @Override
213- protected void setUp () throws Exception {
214- projectFolder. create()
215- projectFolder. newFile(" build.gradle" )
216- // System.setProperty("user.dir", projectFolder.root.getAbsolutePath())
217- }
218-
219258 @Override
220259 protected void tearDown () throws Exception {
221260 projectFolder. delete()
0 commit comments