Skip to content

Commit a26c792

Browse files
authored
fix: parse localized power plan output (#59)
Fixes #58
1 parent aa73d91 commit a26c792

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

‎Services/PowerPlanService.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class PowerPlanService : IPowerPlanService
1616
private static readonly Lazy<string> powerPlansPath = new(GetPowerPlansPath);
1717
private static readonly string powerCfgExecutablePath = Path.Combine(Environment.SystemDirectory, "powercfg.exe");
1818
private static readonly TimeSpan powerCfgTimeout = TimeSpan.FromSeconds(20);
19-
private static readonly Regex powerSchemeRegex = new(@"^Power Scheme GUID:\s*([0-9a-fA-F-]+)[ \t]+\((.*)\)[ \t]*(\*)?[ \t]*\r?$", RegexOptions.Multiline | RegexOptions.Compiled);
19+
private static readonly Regex powerSchemeRegex = new(@"^.*?\b([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})[ \t]+\((.*)\)[ \t]*(\*)?[ \t]*\r?$", RegexOptions.Multiline | RegexOptions.Compiled);
2020
private static readonly Regex pathTraversalRegex = new(@"(^|[\\/])\.\.([\\/]|$)", RegexOptions.Compiled);
2121

2222
private static string PowerPlansPath => powerPlansPath.Value;

‎Tests/ThreadPilot.Core.Tests/PowerPlanServiceTests.cs‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,28 @@ public async Task GetPowerPlansAsync_ParsesActiveMarkerWithOnePowerCfgCall()
7474
Assert.Equal(new[] { "/list" }, Assert.Single(runner.Invocations).Arguments);
7575
}
7676

77+
[Fact]
78+
public async Task GetPowerPlansAsync_ParsesLocalizedOutput()
79+
{
80+
const string activeGuid = "8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c";
81+
var runner = new RecordingProcessRunner
82+
{
83+
ResultFactory = _ => new ProcessRunResult(
84+
0,
85+
"Modes de gestion de l’alimentation existants (* Actif)\n" +
86+
"GUID du mode de gestion de l’alimentation : 381b4222-f694-41f0-9685-ff5bb260df2e (Utilisation normale)\n" +
87+
$"GUID du mode de gestion de l’alimentation : {activeGuid} (Haute performance) *",
88+
string.Empty),
89+
};
90+
var service = CreateService(runner);
91+
92+
var plans = await service.GetPowerPlansAsync();
93+
94+
Assert.Equal(2, plans.Count);
95+
Assert.Equal("Haute performance", plans.Single(plan => plan.Guid == activeGuid).Name);
96+
Assert.True(plans.Single(plan => plan.Guid == activeGuid).IsActive);
97+
}
98+
7799
[Fact]
78100
public async Task SetActivePowerPlanByGuidAsync_SkipsChange_WhenAlreadyActive()
79101
{

0 commit comments

Comments
 (0)