Skip to content

Commit c66cddc

Browse files
authored
Merge pull request #29 from microsoft/alzollin/logRefactor
Switched to use dotnet's ILogger instead of Console.Out/Error.
2 parents 98eba72 + cb0a7f3 commit c66cddc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+1269
-1486
lines changed

.github/codeql/codeql-config.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
paths:
2+
- src
3+
paths-ignore:
4+
- '**/*Tests/*.cs'

.github/workflows/codeql.yml

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# For most projects, this workflow file will not need changing; you simply need
2+
# to commit it to your repository.
3+
#
4+
# You may wish to alter this file to override the set of languages analyzed,
5+
# or to provide custom queries or build logic.
6+
#
7+
# ******** NOTE ********
8+
# We have attempted to detect the languages in your repository. Please check
9+
# the `language` matrix defined below to confirm you have the correct set of
10+
# supported CodeQL languages.
11+
#
12+
name: "CodeQL Advanced"
13+
14+
on:
15+
push:
16+
branches: [ "main" ]
17+
pull_request:
18+
branches: [ "main" ]
19+
schedule:
20+
- cron: '35 20 * * 0'
21+
22+
jobs:
23+
analyze:
24+
name: Analyze (${{ matrix.language }})
25+
runs-on: ${{ (matrix.language == 'swift' && 'macos-latest') || 'ubuntu-latest' }}
26+
permissions:
27+
# required for all workflows
28+
security-events: write
29+
30+
# required to fetch internal or private CodeQL packs
31+
packages: read
32+
33+
# only required for workflows in private repositories
34+
actions: read
35+
contents: read
36+
37+
strategy:
38+
fail-fast: false
39+
matrix:
40+
include:
41+
- language: actions
42+
build-mode: none
43+
- language: c-cpp
44+
build-mode: none
45+
- language: csharp
46+
build-mode: none
47+
- language: javascript-typescript
48+
build-mode: none
49+
steps:
50+
- name: Checkout repository
51+
uses: actions/checkout@v5
52+
53+
# Initializes the CodeQL tools for scanning.
54+
- name: Initialize CodeQL
55+
uses: github/codeql-action/init@v4
56+
with:
57+
languages: ${{ matrix.language }}
58+
build-mode: ${{ matrix.build-mode }}
59+
config-file: ./.github/codeql/codeql-config.yml
60+
61+
- name: Perform CodeQL Analysis
62+
uses: github/codeql-action/analyze@v4
63+
with:
64+
category: "/language:${{matrix.language}}"

src/winsdk-CLI/.editorconfig

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ indent_style = space
88
indent_size = 4
99
trim_trailing_whitespace = true
1010

11-
[*.{cs,csx,vb,vbx}]
12-
indent_size = 4
13-
1411
[*.{csproj,vbproj,vcxproj,vcxproj.filters,proj,projitems,shproj}]
1512
indent_size = 2
1613

1714
[*.{json,yml,yaml}]
1815
indent_size = 2
1916

17+
[*.{cs,csx,vb,vbx}]
18+
indent_size = 4
19+
2020
# C# Code Style Rules
2121

2222
# Async method naming
@@ -71,7 +71,7 @@ dotnet_diagnostic.CA1515.severity = none
7171
dotnet_diagnostic.CA1031.severity = suggestion
7272

7373
# CA1848: Use the LoggerMessage delegates
74-
dotnet_diagnostic.CA1848.severity = suggestion
74+
dotnet_diagnostic.CA1848.severity = none
7575

7676
# CA2249: Consider using String.Contains instead of String.IndexOf
7777
dotnet_diagnostic.CA2249.severity = suggestion
@@ -89,7 +89,7 @@ dotnet_diagnostic.CA1016.severity = none
8989
dotnet_diagnostic.CA1031.severity = suggestion
9090

9191
# CA1032: Implement standard exception constructors
92-
dotnet_diagnostic.CA1032.severity = warning
92+
dotnet_diagnostic.CA1032.severity = suggestion
9393

9494
# CA1303: Do not pass literals as localized parameters
9595
dotnet_diagnostic.CA1303.severity = none

src/winsdk-CLI/Winsdk.Cli.Tests/BaseCommandTests.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,35 @@
11
using Microsoft.Extensions.DependencyInjection;
2+
using Microsoft.Extensions.Logging;
23
using Winsdk.Cli.Helpers;
34

45
namespace Winsdk.Cli.Tests;
56

67
public class BaseCommandTests : IDisposable
78
{
89
private ServiceProvider _serviceProvider;
10+
protected StringWriter ConsoleStdOut { get; } = new StringWriter();
11+
protected StringWriter ConsoleStdErr { get; } = new StringWriter();
912

1013
public BaseCommandTests()
1114
{
1215
var services = new ServiceCollection()
1316
.ConfigureServices()
14-
.ConfigureCommands();
17+
.ConfigureCommands()
18+
.AddLogging(b =>
19+
{
20+
b.ClearProviders();
21+
b.AddTextWriterLogger(ConsoleStdOut, ConsoleStdErr);
22+
b.SetMinimumLevel(LogLevel.Debug);
23+
});
1524

1625
_serviceProvider = services.BuildServiceProvider();
1726
}
1827

1928
public void Dispose()
2029
{
2130
_serviceProvider?.Dispose();
31+
ConsoleStdOut?.Dispose();
32+
ConsoleStdErr?.Dispose();
2233
GC.SuppressFinalize(this);
2334
}
2435

src/winsdk-CLI/Winsdk.Cli.Tests/BuildToolsServiceTests.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ public async Task RunBuildToolAsync_WithNonExistentTool_ThrowsFileNotFoundExcept
214214
// Act & Assert
215215
await Assert.ThrowsExactlyAsync<FileNotFoundException>(async () =>
216216
{
217-
await _buildToolsService.RunBuildToolAsync("nonexistent.exe", "", quiet: true);
217+
await _buildToolsService.RunBuildToolAsync("nonexistent.exe", "");
218218
});
219219
}
220220

@@ -225,7 +225,7 @@ public async Task EnsureBuildToolsAsync_WithNoExistingPackage_ShouldAttemptInsta
225225
// since we can't easily mock the package installation service in this test setup
226226

227227
// Act
228-
var result = await _buildToolsService.EnsureBuildToolsAsync(quiet: true);
228+
var result = await _buildToolsService.EnsureBuildToolsAsync();
229229

230230
// Assert - Result can be either null (if installation fails) or a path (if successful)
231231
// The important part is that the method completes without throwing
@@ -243,7 +243,7 @@ public async Task EnsureBuildToolsAsync_WithExistingPackage_ReturnsExistingPath(
243243
Directory.CreateDirectory(binDir);
244244

245245
// Act
246-
var result = await _buildToolsService.EnsureBuildToolsAsync(quiet: true);
246+
var result = await _buildToolsService.EnsureBuildToolsAsync();
247247

248248
// Assert - Should find and return the existing bin path
249249
Assert.AreEqual(binDir, result);
@@ -259,7 +259,7 @@ public async Task EnsureBuildToolsAsync_WithForceLatest_ShouldAttemptReinstallat
259259
Directory.CreateDirectory(binDir);
260260

261261
// Act - Force latest should attempt reinstallation even with existing package
262-
var result = await _buildToolsService.EnsureBuildToolsAsync(quiet: true, forceLatest: true);
262+
var result = await _buildToolsService.EnsureBuildToolsAsync(forceLatest: true);
263263

264264
// Assert - Result can be either null (if installation fails) or a path (if successful)
265265
// The important part is that the method completes and attempts reinstallation
@@ -279,7 +279,7 @@ public async Task EnsureBuildToolAvailableAsync_WithExistingTool_ReturnsToolPath
279279
File.WriteAllText(toolPath, "fake mt.exe");
280280

281281
// Act
282-
var result = await _buildToolsService.EnsureBuildToolAvailableAsync("mt.exe", quiet: true);
282+
var result = await _buildToolsService.EnsureBuildToolAvailableAsync("mt.exe");
283283

284284
// Assert
285285
Assert.AreEqual(toolPath, result);
@@ -298,7 +298,7 @@ public async Task EnsureBuildToolAvailableAsync_WithToolNameWithoutExtension_Add
298298
File.WriteAllText(toolPath, "fake mt.exe");
299299

300300
// Act - Request tool without .exe extension
301-
var result = await _buildToolsService.EnsureBuildToolAvailableAsync("mt", quiet: true);
301+
var result = await _buildToolsService.EnsureBuildToolAvailableAsync("mt");
302302

303303
// Assert
304304
Assert.AreEqual(toolPath, result);
@@ -313,7 +313,7 @@ public async Task EnsureBuildToolAvailableAsync_WithNoExistingPackageAndInstallS
313313
// Act
314314
try
315315
{
316-
var result = await _buildToolsService.EnsureBuildToolAvailableAsync("mt.exe", quiet: true);
316+
var result = await _buildToolsService.EnsureBuildToolAvailableAsync("mt.exe");
317317

318318
// Assert - If we get here, installation was successful and we got a path
319319
Assert.IsNotNull(result);
@@ -347,7 +347,7 @@ public async Task EnsureBuildToolAvailableAsync_WithNonExistentTool_ThrowsFileNo
347347
// Act & Assert
348348
await Assert.ThrowsExactlyAsync<FileNotFoundException>(async () =>
349349
{
350-
await _buildToolsService.EnsureBuildToolAvailableAsync("nonexistent.exe", quiet: true);
350+
await _buildToolsService.EnsureBuildToolAvailableAsync("nonexistent.exe");
351351
});
352352
}
353353

@@ -361,7 +361,7 @@ public async Task RunBuildToolAsync_WithNoExistingPackage_AutoInstallsAndRuns()
361361
{
362362
// Create a simple batch command that outputs something
363363
// This will either succeed (if BuildTools installs successfully) or throw an exception
364-
await _buildToolsService.RunBuildToolAsync("echo.cmd", "test", verbose: false, quiet: true);
364+
await _buildToolsService.RunBuildToolAsync("echo.cmd", "test");
365365

366366
// If we reach here, the auto-installation worked - test passes
367367
}
@@ -388,7 +388,7 @@ public async Task RunBuildToolAsync_WithExistingTool_RunsDirectly()
388388
File.WriteAllText(batchFile, "@echo Hello from test tool");
389389

390390
// Act
391-
var (stdout, stderr) = await _buildToolsService.RunBuildToolAsync("test.cmd", "", verbose: false, quiet: true);
391+
var (stdout, stderr) = await _buildToolsService.RunBuildToolAsync("test.cmd", "");
392392

393393
// Assert
394394
Assert.Contains("Hello from test tool", stdout);

src/winsdk-CLI/Winsdk.Cli.Tests/ManifestCommandTests.cs

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -267,28 +267,14 @@ public async Task ManifestGenerateCommandWithVerboseOptionShouldProduceOutput()
267267
"--yes" // Skip interactive prompts
268268
};
269269

270-
// Capture console output
271-
using var consoleOutput = new StringWriter();
272-
var originalConsoleOut = Console.Out;
273-
Console.SetOut(consoleOutput);
274-
275-
try
276-
{
277-
// Act
278-
var parseResult = generateCommand.Parse(args);
279-
var exitCode = await parseResult.InvokeAsync();
270+
var parseResult = generateCommand.Parse(args);
271+
var exitCode = await parseResult.InvokeAsync();
280272

281-
// Assert
282-
Assert.AreEqual(0, exitCode, "Generate command should complete successfully");
273+
// Assert
274+
Assert.AreEqual(0, exitCode, "Generate command should complete successfully");
283275

284-
var output = consoleOutput.ToString();
285-
Assert.Contains("Generating manifest", output, "Verbose output should contain generation message");
286-
}
287-
finally
288-
{
289-
// Restore console output
290-
Console.SetOut(originalConsoleOut);
291-
}
276+
var output = ConsoleStdOut.ToString();
277+
Assert.Contains("Generating manifest", output, "Verbose output should contain generation message");
292278
}
293279

294280
[TestMethod]

src/winsdk-CLI/Winsdk.Cli.Tests/PackageCommandTests.cs

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ public async Task PackageCommand_ToolDiscovery_FindsCommonBuildTools()
184184
var missingTools = new List<string>();
185185

186186
// Ensure BuildTools are installed
187-
var buildToolsPath = await _buildToolsService.EnsureBuildToolsAsync(quiet: true);
187+
var buildToolsPath = await _buildToolsService.EnsureBuildToolsAsync();
188188
if (buildToolsPath == null)
189189
{
190190
Assert.Fail("Cannot run test - BuildTools installation failed.");
@@ -249,7 +249,6 @@ public async Task CreateMsixPackageAsync_OutputPathHandling_WorksCorrectly(strin
249249
packageName: "TestPackage",
250250
skipPri: true,
251251
autoSign: false,
252-
verbose: true,
253252
cancellationToken: CancellationToken.None
254253
);
255254

@@ -273,7 +272,6 @@ await _msixService.CreateMsixPackageAsync(
273272
packageName: "TestPackage",
274273
skipPri: true,
275274
autoSign: false,
276-
verbose: true,
277275
cancellationToken: CancellationToken.None
278276
);
279277
}, "Expected DirectoryNotFoundException when input folder does not exist.");
@@ -298,7 +296,6 @@ await _msixService.CreateMsixPackageAsync(
298296
packageName: "TestPackage",
299297
skipPri: true,
300298
autoSign: false,
301-
verbose: true,
302299
cancellationToken: CancellationToken.None
303300
);
304301
}, "Expected FileNotFoundException when manifest file is missing.");
@@ -340,7 +337,6 @@ public async Task CreateMsixPackageAsync_ExternalManifestWithAssets_CopiesManife
340337
skipPri: true,
341338
autoSign: false,
342339
manifestPath: externalManifestPath,
343-
verbose: true,
344340
cancellationToken: CancellationToken.None
345341
);
346342

@@ -368,7 +364,7 @@ public async Task CreateMsixPackageAsync_WithSigningAndMatchingPublishers_Should
368364
const string testPublisher = "CN=TestPublisher"; // This matches StandardTestManifestContent
369365

370366
var certResult = await _certificateService.GenerateDevCertificateAsync(
371-
testPublisher, certPath, testPassword, verbose: true);
367+
testPublisher, certPath, testPassword);
372368

373369
// Create minimal winsdk.yaml
374370
await File.WriteAllTextAsync(_configService.ConfigPath, "packages: []");
@@ -382,7 +378,6 @@ public async Task CreateMsixPackageAsync_WithSigningAndMatchingPublishers_Should
382378
autoSign: true,
383379
certificatePath: certPath,
384380
certificatePassword: testPassword,
385-
verbose: true,
386381
cancellationToken: CancellationToken.None
387382
);
388383

@@ -405,7 +400,7 @@ public async Task CreateMsixPackageAsync_WithSigningAndMismatchedPublishers_Shou
405400
const string wrongPublisher = "CN=WrongPublisher"; // This does NOT match StandardTestManifestContent
406401

407402
var certResult = await _certificateService.GenerateDevCertificateAsync(
408-
wrongPublisher, certPath, testPassword, verbose: true);
403+
wrongPublisher, certPath, testPassword);
409404

410405
// Create minimal winsdk.yaml
411406
await File.WriteAllTextAsync(_configService.ConfigPath, "packages: []");
@@ -421,7 +416,6 @@ await _msixService.CreateMsixPackageAsync(
421416
autoSign: true,
422417
certificatePath: certPath,
423418
certificatePassword: testPassword,
424-
verbose: true,
425419
cancellationToken: CancellationToken.None
426420
);
427421
});
@@ -453,7 +447,7 @@ public async Task CreateMsixPackageAsync_WithExternalManifestAndMismatchedCertif
453447
const string wrongPublisher = "CN=DifferentPublisher";
454448

455449
await _certificateService.GenerateDevCertificateAsync(
456-
wrongPublisher, certPath, testPassword, verbose: true);
450+
wrongPublisher, certPath, testPassword);
457451

458452
// Create minimal winsdk.yaml
459453
await File.WriteAllTextAsync(_configService.ConfigPath, "packages: []");
@@ -470,7 +464,6 @@ await _msixService.CreateMsixPackageAsync(
470464
certificatePath: certPath,
471465
certificatePassword: testPassword,
472466
manifestPath: externalManifestPath,
473-
verbose: true,
474467
cancellationToken: CancellationToken.None
475468
);
476469
});
@@ -494,7 +487,7 @@ public void CertificateService_ExtractPublisherFromCertificate_ShouldReturnCorre
494487

495488
// Create a test certificate using the existing certificate service
496489
var certResult = _certificateService.GenerateDevCertificateAsync(
497-
testPublisherCN, certPath, testPassword, verbose: true).GetAwaiter().GetResult();
490+
testPublisherCN, certPath, testPassword).GetAwaiter().GetResult();
498491

499492
// Act
500493
var extractedPublisher = CertificateService.ExtractPublisherFromCertificate(certPath, testPassword);
@@ -525,7 +518,7 @@ public void CertificateService_ExtractPublisherFromCertificate_WithWrongPassword
525518
const string wrongPassword = "wrong123";
526519

527520
_certificateService.GenerateDevCertificateAsync(
528-
"CN=PasswordTestPublisher", certPath, correctPassword, verbose: true).GetAwaiter().GetResult();
521+
"CN=PasswordTestPublisher", certPath, correctPassword).GetAwaiter().GetResult();
529522

530523
// Act & Assert
531524
Assert.ThrowsExactly<InvalidOperationException>(() =>
@@ -545,7 +538,7 @@ public async Task CertificateService_ValidatePublisherMatch_WithMatchingPublishe
545538

546539
// Create certificate
547540
await _certificateService.GenerateDevCertificateAsync(
548-
commonPublisher, certPath, testPassword, verbose: true);
541+
commonPublisher, certPath, testPassword);
549542

550543
// Create manifest with same publisher
551544
var manifestContent = StandardTestManifestContent.Replace(
@@ -566,7 +559,7 @@ public async Task CertificateService_ValidatePublisherMatch_WithMismatchedPublis
566559

567560
// Create certificate with one publisher
568561
await _certificateService.GenerateDevCertificateAsync(
569-
"CN=CertificatePublisher", certPath, testPassword, verbose: true);
562+
"CN=CertificatePublisher", certPath, testPassword);
570563

571564
// Create manifest with different publisher
572565
var manifestContent = StandardTestManifestContent.Replace(

0 commit comments

Comments
 (0)