Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Format no format #4657

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

Open
9swampy wants to merge 6 commits into GitTools:main
base: main
Choose a base branch
Loading
from 9swampy:FormatNoFormat
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Red
  • Loading branch information
9swampy committed Aug 13, 2025
commit 32b8c057b0d93d5004d49dcb4fc10c31981b81d7
244 changes: 244 additions & 0 deletions src/GitVersion.Core.Tests/Formatting/BackwardCompatibilityTests.cs
View file Open in desktop
Copy link
Member

Choose a reason for hiding this comment

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

I love the added tests! 🤩 Could we please have a test for the combination of ;; and ?? as well?

Original file line number Diff line number Diff line change
@@ -0,0 +1,244 @@
using System.Globalization;
using GitVersion.Core.Tests.Helpers;
using GitVersion.Formatting;

namespace GitVersion.Core.Tests.Formatting;

/// <summary>
/// Tests for backward compatibility with legacy .NET composite format syntax (;;)
/// These tests document the expected behavior before implementing the fix.
/// </summary>
[TestFixture]
public class LegacyFormattingSyntaxTests
{
/// <summary>
/// Test that the old ;;'' syntax for zero-value fallbacks still works
/// This is the exact case from issue #4654
/// </summary>
[Test]
public void FormatWith_LegacyZeroFallbackSyntax_ShouldWork()
{
// Arrange
var semanticVersion = new SemanticVersion
{
Major = 6,
Minor = 13,
Patch = 54,
PreReleaseTag = new SemanticVersionPreReleaseTag("gv6", 1, true),
BuildMetaData = new SemanticVersionBuildMetaData()
{
Branch = "feature/gv6",
VersionSourceSha = "versionSourceSha",
Sha = "489a0c0ab425214def918e36399f3cc3c9a9c42d",
ShortSha = "489a0c0",
CommitsSinceVersionSource = 2,
CommitDate = DateTimeOffset.Parse("2025年08月12日", CultureInfo.InvariantCulture)
}
};

// The exact template from the issue
const string template = "{MajorMinorPatch}{PreReleaseLabelWithDash}{CommitsSinceVersionSource:0000;;''}";
const string expected = "6.13.54-gv60002"; // Should format CommitsSinceVersionSource as 0002, not show literal text

// Act
var actual = template.FormatWith(semanticVersion, new TestEnvironment());

// Assert
actual.ShouldBe(expected);
}

/// <summary>
/// Test that legacy positive/negative/zero section syntax works
/// </summary>
[Test]
public void FormatWith_LegacyThreeSectionSyntax_ShouldWork()
{
// Arrange
var testObject = new { Value = -5 };
const string template = "{Value:positive;negative;zero}";
const string expected = "negative";

// Act
var actual = template.FormatWith(testObject, new TestEnvironment());

// Assert
actual.ShouldBe(expected);
}

/// <summary>
/// Test that legacy two-section syntax works (positive;negative)
/// </summary>
[Test]
public void FormatWith_LegacyTwoSectionSyntax_ShouldWork()
{
// Arrange
var testObject = new { Value = -10 };
const string template = "{Value:positive;negative}";
const string expected = "negative";

// Act
var actual = template.FormatWith(testObject, new TestEnvironment());

// Assert
actual.ShouldBe(expected);
}

/// <summary>
/// Test that zero values use the third section in legacy syntax
/// </summary>
[Test]
public void FormatWith_LegacyZeroValue_ShouldUseThirdSection()
{
// Arrange
var testObject = new { Value = 0 };
const string template = "{Value:pos;neg;ZERO}";
const string expected = "ZERO";

// Act
var actual = template.FormatWith(testObject, new TestEnvironment());

// Assert
actual.ShouldBe(expected);
}

/// <summary>
/// Test mixed usage: some properties with legacy syntax, others with new syntax
/// </summary>
[Test]
public void FormatWith_MixedLegacyAndNewSyntax_ShouldWork()
{
// Arrange
var testObject = new
{
OldStyle = 0,
NewStyle = 42,
RegularProp = "test"
};
const string template = "{OldStyle:pos;neg;''}{NewStyle:0000 ?? 'fallback'}{RegularProp}";
const string expected = "0042test"; // Empty string for zero, 0042 for 42, test as-is

// Act
var actual = template.FormatWith(testObject, new TestEnvironment());

// Assert
actual.ShouldBe(expected);
}

/// <summary>
/// Test that complex legacy format with actual .NET format specifiers works
/// </summary>
[Test]
public void FormatWith_LegacyWithStandardFormatSpecifiers_ShouldWork()
{
// Arrange
var testObject = new { Amount = 1234.56 };
const string template = "{Amount:C2;(C2);'No Amount'}";
const string expected = "¤1,234.56"; // Should format as currency

// Act
var actual = template.FormatWith(testObject, new TestEnvironment());

// Assert
actual.ShouldBe(expected);
}

/// <summary>
/// Test that the original failing case from issue #4654 works exactly as expected
/// </summary>
[Test]
public void FormatWith_Issue4654ExactCase_ShouldWork()
{
// Arrange - recreate the exact scenario from the issue
var semanticVersion = new SemanticVersion
{
Major = 6,
Minor = 13,
Patch = 54,
PreReleaseTag = new SemanticVersionPreReleaseTag("gv6", 1, true),
BuildMetaData = new SemanticVersionBuildMetaData("Branch.feature-gv6")
{
CommitsSinceVersionSource = 2
}
};

// This should work on main branch where PreReleaseLabelWithDash would be empty
var mainBranchVersion = new SemanticVersion
{
Major = 6,
Minor = 13,
Patch = 54,
PreReleaseTag = new SemanticVersionPreReleaseTag(string.Empty, 0, true),
BuildMetaData = new SemanticVersionBuildMetaData()
{
CommitsSinceVersionSource = 0
}
};

const string template = "{MajorMinorPatch}{PreReleaseLabelWithDash}{CommitsSinceVersionSource:0000;;''}";

// Act & Assert for feature branch
var featureResult = template.FormatWith(semanticVersion, new TestEnvironment());
featureResult.ShouldBe("6.13.54-gv60002");

// Act & Assert for main branch (zero commits should show empty string)
var mainResult = template.FormatWith(mainBranchVersion, new TestEnvironment());
mainResult.ShouldBe("6.13.54"); // Empty PreReleaseLabelWithDash and empty string for zero commits
}
}

/// <summary>
/// Tests specifically for the regex pattern changes to ensure backward compatibility
/// </summary>
[TestFixture]
public class LegacyRegexPatternTests
{
/// <summary>
/// Test that the ExpandTokensRegex can parse legacy semicolon syntax
/// </summary>
[Test]
public void ExpandTokensRegex_ShouldParseLegacySemicolonSyntax()
{
// Arrange
const string input = "{CommitsSinceVersionSource:0000;;''}";

// Act
var matches = RegexPatterns.Common.ExpandTokensRegex().Matches(input);

// Assert
matches.Count.ShouldBe(1);
var match = matches[0];
match.Groups["member"].Value.ShouldBe("CommitsSinceVersionSource");

// The format group should capture the entire format including semicolons
// This test documents what should happen - the format might need to be "0000;;''"
// or the regex might need to separate format and fallback parts
match.Groups["format"].Success.ShouldBeTrue();
// The exact capture will depend on implementation - this test will guide the regex design
}

/// <summary>
/// Test that both new and old syntax can coexist in the same template
/// </summary>
[Test]
public void ExpandTokensRegex_ShouldHandleMixedSyntax()
{
// Arrange
const string input = "{NewStyle:0000 ?? 'fallback'} {OldStyle:pos;neg;zero}";

// Act
var matches = RegexPatterns.Common.ExpandTokensRegex().Matches(input);

// Assert
matches.Count.ShouldBe(2);

// First match: new syntax
var newMatch = matches[0];
newMatch.Groups["member"].Value.ShouldBe("NewStyle");
newMatch.Groups["fallback"].Value.ShouldBe("fallback");

// Second match: old syntax
var oldMatch = matches[1];
oldMatch.Groups["member"].Value.ShouldBe("OldStyle");
// Format handling for legacy syntax TBD based on implementation approach
}
}
Loading

AltStyle によって変換されたページ (->オリジナル) /