Skip to content

Commit 4515d89

Browse files
authored
ProgressTask.GetPercentage() returns 100 when max value is 0 (#1694)
1 parent be45494 commit 4515d89

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/Spectre.Console/Live/Progress/ProgressTask.cs

+5
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,11 @@ private void Update(
224224

225225
private double GetPercentage()
226226
{
227+
if (MaxValue == 0)
228+
{
229+
return 100;
230+
}
231+
227232
var percentage = (Value / MaxValue) * 100;
228233
percentage = Math.Min(100, Math.Max(0, percentage));
229234
return percentage;

src/Tests/Spectre.Console.Tests/Unit/Live/Progress/ProgressTests.cs

+25
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,31 @@ public void Setting_Max_Value_Should_Set_The_MaxValue_And_Cap_Value()
118118
task.Value.ShouldBe(20);
119119
}
120120

121+
[Fact]
122+
public void Setting_Max_Value_To_Zero_Should_Make_Percentage_OneHundred()
123+
{
124+
// Given
125+
var console = new TestConsole()
126+
.Interactive();
127+
128+
var task = default(ProgressTask);
129+
var progress = new Progress(console)
130+
.Columns(new[] { new ProgressBarColumn() })
131+
.AutoRefresh(false)
132+
.AutoClear(false);
133+
134+
// When
135+
progress.Start(ctx =>
136+
{
137+
task = ctx.AddTask("foo");
138+
task.MaxValue = 0;
139+
});
140+
141+
// Then
142+
task.Value.ShouldBe(0);
143+
task.Percentage.ShouldBe(100);
144+
}
145+
121146
[Fact]
122147
public void Setting_Value_Should_Override_Incremented_Value()
123148
{

0 commit comments

Comments
 (0)