ProgressTask.GetPercentage() returns 100 when max value is 0 (#1694)

This commit is contained in:
Frank Ray 2024-11-22 11:44:00 +00:00 committed by GitHub
parent be45494d6e
commit 4515d89705
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 30 additions and 0 deletions

View File

@ -224,6 +224,11 @@ public sealed class ProgressTask : IProgress<double>
private double GetPercentage()
{
if (MaxValue == 0)
{
return 100;
}
var percentage = (Value / MaxValue) * 100;
percentage = Math.Min(100, Math.Max(0, percentage));
return percentage;

View File

@ -118,6 +118,31 @@ public sealed class ProgressTests
task.Value.ShouldBe(20);
}
[Fact]
public void Setting_Max_Value_To_Zero_Should_Make_Percentage_OneHundred()
{
// Given
var console = new TestConsole()
.Interactive();
var task = default(ProgressTask);
var progress = new Progress(console)
.Columns(new[] { new ProgressBarColumn() })
.AutoRefresh(false)
.AutoClear(false);
// When
progress.Start(ctx =>
{
task = ctx.AddTask("foo");
task.MaxValue = 0;
});
// Then
task.Value.ShouldBe(0);
task.Percentage.ShouldBe(100);
}
[Fact]
public void Setting_Value_Should_Override_Incremented_Value()
{