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

feat: add monorepo support #121

Merged
merged 15 commits into from
Apr 30, 2024
Merged
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

All notable changes to this project will be documented in this file. See [versionize](https://github.com/versionize/versionize) for commit guidelines.

<a name="1.23.0"></a>
## [1.23.0](https://www.github.com/versionize/versionize/releases/tag/v1.23.0) (2024-04-29)

### Features

* add commit parser options ([#120](https://www.github.com/versionize/versionize/issues/120)) ([b2c137a](https://www.github.com/versionize/versionize/commit/b2c137a8616c651361c5e837795bfedfb862a25f))

<a name="1.22.0"></a>
## [1.22.0](https://www.github.com/versionize/versionize/releases/tag/v1.22.0) (2024-03-02)

Expand Down
31 changes: 29 additions & 2 deletions Versionize.Tests/ConventionalCommitParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ public void ShouldSupportExclamationMarkToSignifyingBreakingChanges(string commi
conventionalCommit.Notes[0].Title.ShouldBe("BREAKING CHANGE");
conventionalCommit.Notes[0].Text.ShouldBe(string.Empty);
}

[Theory]
[InlineData("fix: subject text #64", new[] {"64"})]
[InlineData("fix: subject text #64", new[] { "64" })]
[InlineData("fix: subject #64 text", new[] { "64" })]
[InlineData("fix: #64 subject text", new[] { "64" })]
[InlineData("fix: subject text. #64 #65", new[] { "64", "65" })]
Expand All @@ -95,6 +95,33 @@ public void ShouldExtractCommitIssues(string commitMessage, string[] expectedIss
Assert.Equal(issue.Token, $"#{expectedIssue}");
}
}

[Theory]
[InlineData("fix: subject text #64", "", "fix", "subject text #64")]
[InlineData("feat(scope): subject text", "scope", "feat", "subject text")]
[InlineData("Merged PR 123: fix: subject text #64", "", "fix", "subject text #64")]
[InlineData("Merged PR 321: feat(scope): subject text", "scope", "feat", "subject text")]
[InlineData("Pull Request 11792: fix: subject text #64", "", "fix", "subject text #64")]
[InlineData("Pull Request 11792: feat(scope): subject text", "scope", "feat", "subject text")]
public void ShouldParseCommitWithExtraHeaderPatterns(string commitMessage,
string scope, string type, string subject)
{
var testCommit = new TestCommit("c360d6a307909c6e571b29d4a329fd786c5d4543", commitMessage);
var conventionalCommit = ConventionalCommitParser.Parse(
testCommit,
new CommitParserOptions
{
HeaderPatterns = new []
{
"^Merged PR \\d+: (?<type>\\w*)(?:\\((?<scope>.*)\\))?(?<breakingChangeMarker>!)?: (?<subject>.*)$",
"^Pull Request \\d+: (?<type>\\w*)(?:\\((?<scope>.*)\\))?(?<breakingChangeMarker>!)?: (?<subject>.*)$"
}
});

Assert.Equal(conventionalCommit.Scope, scope);
Assert.Equal(conventionalCommit.Type, type);
Assert.Equal(conventionalCommit.Subject, subject);
}
}

public class TestCommit : Commit
Expand Down
223 changes: 221 additions & 2 deletions Versionize.Tests/ProgramTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using Shouldly;
using System.Text;
using LibGit2Sharp;
using Newtonsoft.Json;
using Shouldly;
using Versionize.CommandLine;
using Versionize.Tests.TestSupport;
using Xunit;
Expand All @@ -13,7 +16,6 @@ public class ProgramTests : IDisposable
public ProgramTests()
{
_testSetup = TestSetup.Create();
TempProject.CreateCsharpProject(_testSetup.WorkingDirectory, "1.1.0");

_testPlatformAbstractions = new TestPlatformAbstractions();
CommandLineUI.Platform = _testPlatformAbstractions;
Expand All @@ -22,6 +24,8 @@ public ProgramTests()
[Fact]
public void ShouldRunVersionizeWithDryRunOption()
{
TempProject.CreateCsharpProject(_testSetup.WorkingDirectory, "1.1.0");

var exitCode = Program.Main(new[] { "--workingDir", _testSetup.WorkingDirectory, "--dry-run", "--skip-dirty" });

exitCode.ShouldBe(0);
Expand All @@ -31,6 +35,8 @@ public void ShouldRunVersionizeWithDryRunOption()
[Fact]
public void ShouldVersionizeDesiredReleaseVersion()
{
TempProject.CreateCsharpProject(_testSetup.WorkingDirectory, "1.1.0");

var exitCode = Program.Main(new[] { "--workingDir", _testSetup.WorkingDirectory, "--dry-run", "--skip-dirty", "--release-as", "2.0.0" });

exitCode.ShouldBe(0);
Expand All @@ -40,6 +46,8 @@ public void ShouldVersionizeDesiredReleaseVersion()
[Fact]
public void ShouldPrintTheCurrentVersionWithInspectCommand()
{
TempProject.CreateCsharpProject(_testSetup.WorkingDirectory, "1.1.0");

var exitCode = Program.Main(new[] { "--workingDir", _testSetup.WorkingDirectory, "inspect" });

exitCode.ShouldBe(0);
Expand All @@ -62,8 +70,219 @@ public void ShouldReadConfigurationFromConfigFile()
_testSetup.Repository.Commits.Count().ShouldBe(1);
}

[Fact]
public void ShouldPrintTheCurrentMonoRepoVersionWithInspectCommand()
{
var projects = new[]
{
(
new ProjectOptions
{
Name = "Project1",
Path = "project1",
},
"1.2.3"),
(
new ProjectOptions
{
Name = "Project2",
Path = "project2"
},
"3.2.1")
};

var config = new ConfigurationContract
{
Projects = projects.Select(x => x.Item1).ToArray()
};

File.WriteAllText(Path.Join(_testSetup.WorkingDirectory, ".versionize"),
JsonConvert.SerializeObject(config));

foreach (var (project, version) in projects)
{
TempProject.CreateCsharpProject(
Path.Combine(_testSetup.WorkingDirectory, project.Path),
version);
}

foreach (var (project, version) in projects)
{
var output = new TestPlatformAbstractions();
CommandLineUI.Platform = output;

var exitCode = Program.Main(
new[]
{
"--workingDir", _testSetup.WorkingDirectory,
"--proj-name", project.Name,
"inspect"
});

exitCode.ShouldBe(0);
output.Messages.ShouldHaveSingleItem();
output.Messages[0].ShouldBe(version);
}
}


[Fact]
public void ShouldSupportMonoRepo()
{
var projects = new[]
{
new ProjectOptions
{
Name = "Project1",
Path = "project1",
Changelog = ChangelogOptions.Default with
{
Header = "Project1 header"
}
},
new ProjectOptions
{
Name = "Project2",
Path = "project2"
}
};

var config = new ConfigurationContract
{
Projects = projects,
Changelog = new ChangelogOptions
{
Header = "Default custom header"
}
};

File.WriteAllText(Path.Join(_testSetup.WorkingDirectory, ".versionize"),
JsonConvert.SerializeObject(config));

var fileCommitter = new FileCommitter(_testSetup);

foreach (var project in projects)
{
TempProject.CreateCsharpProject(
Path.Combine(_testSetup.WorkingDirectory, project.Path));

foreach (var commitMessage in new[]
{
$"feat: new feature at {project.Name}"
})
{
fileCommitter.CommitChange(commitMessage, project.Path);
}
}

foreach (var project in projects)
{
var exitCode = Program.Main(new[] {"-w", _testSetup.WorkingDirectory, "--proj-name", project.Name});
exitCode.ShouldBe(0);

var changelogFile = Path.Join(_testSetup.WorkingDirectory, project.Path, "CHANGELOG.md");
File.Exists(changelogFile).ShouldBeTrue();

var changelog = File.ReadAllText(changelogFile, Encoding.UTF8);

changelog.ShouldStartWith(project.Changelog.Header ??
config.Changelog?.Header ??
ChangelogOptions.Default.Header);

foreach (var checkPName in projects.Select(x => x.Name))
{
foreach (var commitMessage in new[]
{
$"new feature at {checkPName}"
})
{
if (checkPName == project.Name)
{
changelog.ShouldContain(commitMessage);
}
else
{
changelog.ShouldNotContain(commitMessage);
}
}
}
}
}


[Fact]
public void ShouldExtraCommitHeaderPatternOptionsFromConfigFile()
{
TempProject.CreateCsharpProject(_testSetup.WorkingDirectory);

File.WriteAllText(Path.Join(_testSetup.WorkingDirectory, ".versionize"),
@"
{
""CommitParser"":{
""HeaderPatterns"":[
""^Merged PR \\d+: (?<type>\\w*)(?:\\((?<scope>.*)\\))?(?<breakingChangeMarker>!)?: (?<subject>.*)$"",
""^Pull Request \\d+: (?<type>\\w*)(?:\\((?<scope>.*)\\))?(?<breakingChangeMarker>!)?: (?<subject>.*)$""
]
}
}
");

_ = new[]
{
"feat(git-case): subject text",
"Merged PR 123: fix(squash-azure-case): subject text #64",
"Pull Request 11792: feat(azure-case): subject text"
}
.Select(
x => _testSetup.Repository.Commit(x,
new Signature("versionize", "test@versionize.com", DateTimeOffset.Now),
new Signature("versionize", "test@versionize.com", DateTimeOffset.Now),
new CommitOptions { AllowEmptyCommit = true }))
.ToArray();

var exitCode = Program.Main(new[] { "-w", _testSetup.WorkingDirectory, "--skip-dirty" });

exitCode.ShouldBe(0);
_testSetup.Repository.Commits.Count().ShouldBe(4);

var changelogFile = Path.Join(_testSetup.WorkingDirectory, "CHANGELOG.md");
File.Exists(changelogFile).ShouldBeTrue();

var changelog = File.ReadAllText(changelogFile, Encoding.UTF8);
changelog.ShouldContain("git-case");
changelog.ShouldContain("squash-azure-case");
changelog.ShouldContain("azure-case");
}

public void Dispose()
{
_testSetup.Dispose();
}

private static void CommitAll(IRepository repository, string message = "feat: Initial commit")
{
var author = new Signature("Gitty McGitface", "noreply@git.com", DateTime.Now);
Commands.Stage(repository, "*");
repository.Commit(message, author, author);
}

class FileCommitter
{
private readonly TestSetup _testSetup;

public FileCommitter(TestSetup testSetup)
{
_testSetup = testSetup;
}

public void CommitChange(string commitMessage, string changeOnDirectory = "")
{
var directory = Path.Join(_testSetup.WorkingDirectory, changeOnDirectory);
Directory.CreateDirectory(directory);

var workingFilePath = Path.Join(directory, "hello.txt");
File.WriteAllText(workingFilePath, Guid.NewGuid().ToString());
CommitAll(_testSetup.Repository, commitMessage);
}
}
}
Loading
Loading