@@ -321,4 +321,149 @@ void apply_CommitWithLabelTagGivesExpectedLabel() throws GitAPIException, IOExce
321321 // then
322322 assertThat (project .getVersion ()).isEqualTo ("677-SNAPSHOT" );
323323 }
324+
325+ @ Test
326+ void apply_TagWithInvalidFormatGiveInitialVersion () throws GitAPIException , IOException {
327+ // given
328+ Git git = Git .init ().setInitialBranch (MASTER ).setDirectory (projectDir .toFile ()).call ();
329+ git .commit ().setMessage ("initial commit" ).setAllowEmpty (true ).call ();
330+ String givenTag = "successfulBuild" ;
331+ git .tag ().setName (givenTag ).call ();
332+
333+ Project project = ProjectBuilder .builder ().withProjectDir (projectDir .toFile ()).build ();
334+
335+ project .getPluginManager ().apply (GitVersioningPlugin .class );
336+
337+ GitVersioningPluginExtension extension = (GitVersioningPluginExtension ) project .getExtensions ()
338+ .getByName ("gitVersioning" );
339+
340+ GitVersioningPluginConfig config = new GitVersioningPluginConfig () {{
341+ refs .branch (".*" , patch -> {
342+ patch .version = "${describe.tag.version.major}.${describe.tag.version.minor}.${describe.tag.version.patch}" ;
343+ });
344+ }};
345+
346+ // when
347+ extension .apply (config );
348+
349+ // then
350+ assertThat (project .getVersion ()).isEqualTo ("0.0.0" );
351+ }
352+
353+ @ Test
354+ void apply_TagWithSingleNonDigitPrefixGivesExpectedVersion () throws GitAPIException , IOException {
355+ // given
356+ Git git = Git .init ().setInitialBranch (MASTER ).setDirectory (projectDir .toFile ()).call ();
357+ git .commit ().setMessage ("initial commit" ).setAllowEmpty (true ).call ();
358+ String givenTag = "v2.0.4" ;
359+ git .tag ().setName (givenTag ).call ();
360+
361+ Project project = ProjectBuilder .builder ().withProjectDir (projectDir .toFile ()).build ();
362+
363+ project .getPluginManager ().apply (GitVersioningPlugin .class );
364+
365+ GitVersioningPluginExtension extension = (GitVersioningPluginExtension ) project .getExtensions ()
366+ .getByName ("gitVersioning" );
367+
368+ GitVersioningPluginConfig config = new GitVersioningPluginConfig () {{
369+ refs .branch (".*" , patch -> {
370+ patch .version = "${describe.tag.version.major}.${describe.tag.version.minor}.${describe.tag.version.patch}" ;
371+ });
372+ }};
373+
374+ // when
375+ extension .apply (config );
376+
377+ // then
378+ assertThat (project .getVersion ()).isEqualTo ("2.0.4" );
379+ }
380+
381+ @ Test
382+ void apply_TagWithSingleWordPrefixGivesExpectedVersion () throws GitAPIException , IOException {
383+ // given
384+ Git git = Git .init ().setInitialBranch (MASTER ).setDirectory (projectDir .toFile ()).call ();
385+ git .commit ().setMessage ("initial commit" ).setAllowEmpty (true ).call ();
386+ String givenTag = "alpha1.2.3" ;
387+ git .tag ().setName (givenTag ).call ();
388+
389+ Project project = ProjectBuilder .builder ().withProjectDir (projectDir .toFile ()).build ();
390+
391+ project .getPluginManager ().apply (GitVersioningPlugin .class );
392+
393+ GitVersioningPluginExtension extension = (GitVersioningPluginExtension ) project .getExtensions ()
394+ .getByName ("gitVersioning" );
395+
396+ GitVersioningPluginConfig config = new GitVersioningPluginConfig () {{
397+ refs .branch (".*" , patch -> {
398+ patch .version = "${describe.tag.version.major}.${describe.tag.version.minor}.${describe.tag.version.patch}" ;
399+ });
400+ }};
401+
402+ // when
403+ extension .apply (config );
404+
405+ // then
406+ assertThat (project .getVersion ()).isEqualTo ("1.2.3" );
407+ }
408+
409+ @ Test
410+ void apply_TwoCommitsSinceLastTagGivesExpectedPatchDistanceAndBranch () throws GitAPIException , IOException {
411+ // given
412+ Git git = Git .init ().setInitialBranch ("featureA" ).setDirectory (projectDir .toFile ()).call ();
413+ git .commit ().setMessage ("initial commit" ).setAllowEmpty (true ).call ();
414+ String givenTag = "v2.0.4" ;
415+ git .tag ().setName (givenTag ).call ();
416+ git .commit ().setMessage ("commit two" ).setAllowEmpty (true ).call ();
417+ git .commit ().setMessage ("commit three" ).setAllowEmpty (true ).call ();
418+
419+ Project project = ProjectBuilder .builder ().withProjectDir (projectDir .toFile ()).build ();
420+
421+ project .getPluginManager ().apply (GitVersioningPlugin .class );
422+
423+ GitVersioningPluginExtension extension = (GitVersioningPluginExtension ) project .getExtensions ()
424+ .getByName ("gitVersioning" );
425+
426+ GitVersioningPluginConfig config = new GitVersioningPluginConfig () {{
427+ refs .branch (".*" , patch -> {
428+ patch .version = "${describe.tag.version.major}.${describe.tag.version.minor}.${describe.tag.version.patch.next}-${describe.distance}-${ref.slug}" ;
429+ });
430+ }};
431+
432+ // when
433+ extension .apply (config );
434+
435+ // then
436+ assertThat (project .getVersion ()).isEqualTo ("2.0.5-2-featureA" );
437+ }
438+
439+ @ Test
440+ void apply_GivenTwoVersionTagsUseTagMatchingDescribePattern () throws GitAPIException , IOException {
441+ // given
442+ Git git = Git .init ().setInitialBranch ("featureA" ).setDirectory (projectDir .toFile ()).call ();
443+ git .commit ().setMessage ("initial commit" ).setAllowEmpty (true ).call ();
444+ String givenTag = "v2.0.4" ;
445+ git .tag ().setName (givenTag ).call ();
446+ String givenSecondaryTag = "35.1.3" ;
447+ git .tag ().setName (givenSecondaryTag ).call ();
448+
449+ Project project = ProjectBuilder .builder ().withProjectDir (projectDir .toFile ()).build ();
450+
451+ project .getPluginManager ().apply (GitVersioningPlugin .class );
452+
453+ GitVersioningPluginExtension extension = (GitVersioningPluginExtension ) project .getExtensions ()
454+ .getByName ("gitVersioning" );
455+
456+ GitVersioningPluginConfig config = new GitVersioningPluginConfig () {{
457+ describeTagPattern = "v2.0.*" ;
458+ refs .branch (".*" , patch -> {
459+ patch .version = "${describe.tag.version.core}" ;
460+ });
461+ }};
462+
463+ // when
464+ extension .apply (config );
465+
466+ // then
467+ assertThat (project .getVersion ()).isEqualTo ("2.0.4" );
468+ }
324469}
0 commit comments