Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
52 changes: 52 additions & 0 deletions .github/workflows/dotnet-build-different-locale.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: .NET

on:
pull_request:
branches: ["main"]

jobs:
modularpipeline:
strategy:
matrix:
locale: [fr-FR, pl-PL, de-DE]
fail-fast: true
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v5
with:
fetch-depth: 0

- name: Setup .NET 8
uses: actions/setup-dotnet@v5
with:
dotnet-version: 8.0.x

- name: Setup .NET 9
uses: actions/setup-dotnet@v5
with:
dotnet-version: 9.0.x

- name: Setup .NET 10
uses: actions/setup-dotnet@v5
with:
dotnet-version: 10.0.x

- name: Generate and set locale for subsequent steps
run: |
# Convert hyphen (fr-FR) to underscore (fr_FR) which is the correct locale name on Ubuntu
LOCALE=${{ matrix.locale }}
LOCALE=${LOCALE/-/_}

sudo apt-get update
sudo apt-get install -y locales
sudo locale-gen "${LOCALE}.UTF-8"
sudo update-locale LANG="${LOCALE}.UTF-8"

# Export for subsequent GitHub Actions steps
echo "LANG=${LOCALE}.UTF-8" >> $GITHUB_ENV
echo "LC_ALL=${LOCALE}.UTF-8" >> $GITHUB_ENV

- name: Build
run: dotnet build -c Release
working-directory: TUnit.TestProject
Comment on lines +9 to +52

Check warning

Code scanning / CodeQL

Workflow does not contain permissions Medium

Actions job or workflow does not limit the permissions of the GITHUB_TOKEN. Consider setting an explicit permissions block, using the following as a minimal starting point: {contents: read}

Copilot Autofix

AI about 2 months ago

To fix the problem and follow least-privilege guidelines, explicitly set the minimal permissions for the workflow or the modularpipeline job. Since there is only one job, and it does not need write permissions, setting

permissions:
  contents: read

either at the workflow root (applies to all jobs), or within the job (applies just to that job) will resolve the issue and satisfy CodeQL. For clarity and maintainability, put this at the root, after name: .NET, so it applies globally and is easily discoverable by future maintainers. No other changes required.

Suggested changeset 1
.github/workflows/dotnet-build-different-locale.yml

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/.github/workflows/dotnet-build-different-locale.yml b/.github/workflows/dotnet-build-different-locale.yml
--- a/.github/workflows/dotnet-build-different-locale.yml
+++ b/.github/workflows/dotnet-build-different-locale.yml
@@ -1,4 +1,6 @@
 name: .NET
+permissions:
+  contents: read
 
 on:
   pull_request:
EOF
@@ -1,4 +1,6 @@
name: .NET
permissions:
contents: read

on:
pull_request:
Copilot is powered by AI and may make mistakes. Always verify output.
17 changes: 12 additions & 5 deletions TUnit.Analyzers/TestDataAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -952,12 +952,19 @@ private static bool CanConvert(SymbolAnalysisContext context, TypedConstant argu

if (methodParameterType?.SpecialType == SpecialType.System_Decimal &&
argument.Type?.SpecialType == SpecialType.System_String &&
argument.Value is string strValue &&
decimal.TryParse(strValue, out _))
argument.Value is string strValue)
{
// Allow string literals for decimal parameters for values that can't be expressed as C# numeric literals
// e.g. [Arguments("79228162514264337593543950335")] for decimal.MaxValue
return true;
// For string-to-decimal conversions in attributes, be permissive at compile time
// The runtime will handle culture-specific parsing with proper fallback
// Try both dot and comma as decimal separators since these are the most common
var normalizedValue = strValue.Replace(',', '.');
if (decimal.TryParse(normalizedValue, System.Globalization.NumberStyles.Any,
System.Globalization.CultureInfo.InvariantCulture, out _))
{
// Allow string literals for decimal parameters
// e.g. [Arguments("123.456")] or [Arguments("123,456")]
return true;
}
}

return CanConvert(context, argument.Type, methodParameterType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ private string FormatPrimitiveForCode(object? value, ITypeSymbol? targetType)
}
if (value is string s)
{
return $"decimal.Parse(\"{s.ToInvariantString()}\", global::System.Globalization.CultureInfo.InvariantCulture)";
return $"global::TUnit.Core.Helpers.DecimalParsingHelper.ParseDecimalWithCultureFallback(\"{s.ToInvariantString()}\")";
}
if (value is double d)
{
Expand Down
38 changes: 38 additions & 0 deletions TUnit.Core/Helpers/DecimalParsingHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System.Globalization;

namespace TUnit.Core.Helpers;

/// <summary>
/// Helper methods for parsing decimal values with culture fallback support
/// </summary>
public static class DecimalParsingHelper
{
/// <summary>
/// Tries to parse a decimal value from a string, first using the current culture,
/// then falling back to the invariant culture if that fails.
/// This is useful for handling decimal values in attributes that might be written
/// with different decimal separators (e.g., "123.456" vs "123,456").
/// </summary>
public static decimal ParseDecimalWithCultureFallback(string value)
{
// First, try parsing with the current culture
if (decimal.TryParse(value, NumberStyles.Any, CultureInfo.CurrentCulture, out var result))
{
return result;
}

// If that fails, try with the invariant culture
if (decimal.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out result))
{
return result;
}

// If both fail, throw an exception with helpful details
throw new FormatException(
$"Could not parse '{value}' as a decimal value. " +
$"Tried both CurrentCulture ({CultureInfo.CurrentCulture.Name}) " +
$"and InvariantCulture. " +
$"Valid decimal formats include: 123.456 (invariant) or locale-specific format."
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1963,6 +1963,10 @@ namespace .Helpers
"ute\' in target method or type.", Justification="We handle specific known tuple types without reflection")]
public static object?[] UnwrapTupleAot(object? value) { }
}
public static class DecimalParsingHelper
{
public static decimal ParseDecimalWithCultureFallback(string value) { }
}
public static class GenericTypeHelper
{
public static GetGenericTypeDefinition( type) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1963,6 +1963,10 @@ namespace .Helpers
"ute\' in target method or type.", Justification="We handle specific known tuple types without reflection")]
public static object?[] UnwrapTupleAot(object? value) { }
}
public static class DecimalParsingHelper
{
public static decimal ParseDecimalWithCultureFallback(string value) { }
}
public static class GenericTypeHelper
{
public static GetGenericTypeDefinition( type) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1863,6 +1863,10 @@ namespace .Helpers
public static object?[] UnwrapTuple<T1, T2, T3, T4, T5, T6, T7>(<T1, T2, T3, T4, T5, T6, T7> tuple) { }
public static object?[] UnwrapTupleAot(object? value) { }
}
public static class DecimalParsingHelper
{
public static decimal ParseDecimalWithCultureFallback(string value) { }
}
public static class GenericTypeHelper
{
public static GetGenericTypeDefinition( type) { }
Expand Down
11 changes: 10 additions & 1 deletion TUnit.TestProject/DecimalArgumentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,15 @@ public async Task MultipleDecimals(decimal a, decimal b, decimal c)
[Arguments(1)]
public void Test(decimal test)
{
return;
}


[Test]
[Arguments(50, 75, 70, 5, 0, true)]
[Arguments(70, 75, 70, 5, 5, true)]
[Arguments(70, 75, 70, 5, 0, false)]
public void TransactionDiscountCalculations(decimal amountPaying, decimal invoiceBalance,
decimal invoiceBalanceDue, decimal discountAmount, decimal appliedDiscountAmount, bool discountAllowedForUser)
{
}
}
Loading