Skip to content

Commit b08aa3c

Browse files
authored
Fix plugin loading dependencies (#40)
* All plugin tests now passing, apart from the one currently expected to fail (for the issue that needs resolving) * Plugin with dependencies test now working * Add GitHub Actions workflow for PR tests * Enhance PR test workflow: set timeout and enable detailed test output * Fix Razor template formatting in tests Added space before closing bracket in <TipContainer> tag and improved readability of the ChildContent property. * Add Blazor app initialization to test suite Implement functionality to initialize a Blazor app in `BlakeServeCommandTests.cs`. This includes handling the initialization of a project with a specified `.csproj` file, error handling for initialization failures, and the use of a cancellation token for command execution. * Add using directive for OptionsTest.Components Enhance Razor template in BlakeServeCommandTests.cs by including the @using OptionsTest.Components directive, allowing access to additional components for improved functionality. * Add CancellationToken support to async methods This commit introduces a `CancellationToken` parameter to various asynchronous methods across multiple files, enhancing control over task cancellation and improving responsiveness. Key updates include: - `InitProjectFile` in `ProjectFileBuilder.cs` now supports cancellation for file operations. - Similar updates in `SampleContentBuilder.cs`, `SiteGenerator.cs`, and `TemplateService.cs` for long-running tasks. - Logging statements have been modified to ensure consistent logging without null checks on the logger. - The main execution flow in `Program.cs` has been updated to manage cancellation tokens, allowing for graceful shutdowns. These changes collectively improve the application's resource management and logging consistency. * Enhance logging and standardize command execution Updated `Program.cs` to log the target path for baking the Blake site, improving traceability. Replaced instances of `RunBlakeFromDotnetAsync` with `RunBlakeCommandAsync` in `BlakeServeCommandTests.cs` for better argument handling. Adjusted test assertions to match the new logging format, enhancing clarity and maintainability. * Increase timeout for PR tests and simplify test verbosity
1 parent 194fbae commit b08aa3c

File tree

12 files changed

+296
-211
lines changed

12 files changed

+296
-211
lines changed

.github/workflows/pr-tests.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: PR Tests
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- main
7+
8+
jobs:
9+
test:
10+
runs-on: ubuntu-latest
11+
timeout-minutes: 20
12+
steps:
13+
- name: Checkout
14+
uses: actions/checkout@v4
15+
16+
- name: Setup .NET
17+
uses: actions/setup-dotnet@v4
18+
with:
19+
dotnet-version: 9.0.x
20+
21+
- name: Build
22+
run: |
23+
dotnet build
24+
25+
- name: Test
26+
run: |
27+
dotnet test

src/Blake.BuildTools/Generator/ProjectFileBuilder.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,20 @@ namespace Blake.BuildTools.Generator;
44

55
public static class ProjectFileBuilder
66
{
7-
public static async Task<int> InitProjectFile(string projectFilePath, ILogger? logger)
7+
public static async Task<int> InitProjectFile(string projectFilePath, ILogger logger, CancellationToken cancellationToken)
88
{
9-
var projectContent = await File.ReadAllTextAsync(projectFilePath);
9+
var projectContent = await File.ReadAllTextAsync(projectFilePath, cancellationToken);
1010

1111
if (!projectContent.Contains("<Project Sdk=\"Microsoft.NET.Sdk.BlazorWebAssembly\">"))
1212
{
13-
logger?.LogError("The specified project is not a Blazor WebAssembly app.");
13+
logger.LogError("The specified project is not a Blazor WebAssembly app.");
1414
return 1;
1515
}
1616

1717
// Check if the project already has Blake configured
1818
if (projectContent.Contains("<PackageReference Include=\"Blake.Types\""))
1919
{
20-
logger?.LogInformation("Blake is already configured in this project.");
20+
logger.LogInformation("Blake is already configured in this project.");
2121
return 0;
2222
}
2323

@@ -32,7 +32,7 @@ public static async Task<int> InitProjectFile(string projectFilePath, ILogger? l
3232
var itemGroupIndex = projectContent.LastIndexOf("</ItemGroup>", StringComparison.Ordinal);
3333
if (itemGroupIndex == -1)
3434
{
35-
logger?.LogError("Project file does not contain a valid ItemGroup.");
35+
logger.LogError("Project file does not contain a valid ItemGroup.");
3636
return 1;
3737
}
3838

@@ -49,7 +49,7 @@ public static async Task<int> InitProjectFile(string projectFilePath, ILogger? l
4949
var projectEndIndex = projectContent.LastIndexOf("</Project>", StringComparison.Ordinal);
5050
if (projectEndIndex == -1)
5151
{
52-
logger?.LogError("Project file does not end with </Project>.");
52+
logger.LogError("Project file does not end with </Project>.");
5353
return 1;
5454
}
5555

@@ -71,7 +71,7 @@ public static async Task<int> InitProjectFile(string projectFilePath, ILogger? l
7171

7272

7373
// Write the updated content back to the project file
74-
await File.WriteAllTextAsync(projectFilePath, projectContent);
74+
await File.WriteAllTextAsync(projectFilePath, projectContent, cancellationToken);
7575

7676
return 0;
7777
}

src/Blake.BuildTools/Generator/SampleContentBuilder.cs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace Blake.BuildTools.Generator;
44

55
public static class SampleContentBuilder
66
{
7-
public static async Task InitSampleContent(string projectFilePath, ILogger? logger)
7+
public static async Task InitSampleContent(string projectFilePath, ILogger logger, CancellationToken cancellationToken)
88
{
99
var pagesFolder = Path.Combine(Path.GetDirectoryName(projectFilePath) ?? string.Empty, "Pages");
1010
if (!Directory.Exists(pagesFolder))
@@ -16,24 +16,24 @@ public static async Task InitSampleContent(string projectFilePath, ILogger? logg
1616

1717
if (!File.Exists(samplePagePath))
1818
{
19-
await File.WriteAllTextAsync(samplePagePath, SamplePageContent);
20-
logger?.LogInformation("✅ Sample page created at: {samplePagePath}", samplePagePath);
19+
await File.WriteAllTextAsync(samplePagePath, SamplePageContent, cancellationToken);
20+
logger.LogInformation("✅ Sample page created at: {samplePagePath}", samplePagePath);
2121
}
2222
else
2323
{
24-
logger?.LogInformation("Sample page already exists at: {samplePagePath}", samplePagePath);
24+
logger.LogInformation("Sample page already exists at: {samplePagePath}", samplePagePath);
2525
}
2626

2727
// Add sample template
2828
var templatePath = Path.Combine(pagesFolder, "template.razor");
2929
if (!File.Exists(templatePath))
3030
{
31-
await File.WriteAllTextAsync(templatePath, SampleTemplate);
32-
logger?.LogInformation("✅ Sample template created at: {templatePath}", templatePath);
31+
await File.WriteAllTextAsync(templatePath, SampleTemplate, cancellationToken);
32+
logger.LogInformation("✅ Sample template created at: {templatePath}", templatePath);
3333
}
3434
else
3535
{
36-
logger?.LogInformation("Sample template already exists at: {templatePath}", templatePath);
36+
logger.LogInformation("Sample template already exists at: {templatePath}", templatePath);
3737
}
3838

3939
// Add sample component
@@ -47,20 +47,20 @@ public static async Task InitSampleContent(string projectFilePath, ILogger? logg
4747

4848
if (!File.Exists(sampleComponentPath))
4949
{
50-
await File.WriteAllTextAsync(sampleComponentPath, SampleComponent);
51-
logger?.LogInformation("✅ Sample component created at: {sampleComponentPath}", sampleComponentPath);
50+
await File.WriteAllTextAsync(sampleComponentPath, SampleComponent, cancellationToken);
51+
logger.LogInformation("✅ Sample component created at: {sampleComponentPath}", sampleComponentPath);
5252
}
5353
else
5454
{
55-
logger?.LogInformation("Sample component already exists at: {sampleComponentPath}", sampleComponentPath);
55+
logger.LogInformation("Sample component already exists at: {sampleComponentPath}", sampleComponentPath);
5656
}
5757

5858
// update the nav menu
5959
var navMenuPath = Path.Combine(Path.GetDirectoryName(projectFilePath) ?? string.Empty, "Layout", "NavMenu.razor");
6060

6161
if (File.Exists(navMenuPath))
6262
{
63-
var navMenuContent = await File.ReadAllTextAsync(navMenuPath);
63+
var navMenuContent = await File.ReadAllTextAsync(navMenuPath, cancellationToken);
6464
if (navMenuContent.Contains("</nav>"))
6565
{
6666
// Insert the new menu item before the closing </nav> tag
@@ -80,12 +80,12 @@ public static async Task InitSampleContent(string projectFilePath, ILogger? logg
8080
if (insertIndex != -1)
8181
{
8282
navMenuContent = navMenuContent.Insert(insertIndex, $"{Environment.NewLine}{newMenuItem}{Environment.NewLine}");
83-
await File.WriteAllTextAsync(navMenuPath, navMenuContent);
84-
logger?.LogInformation("✅ Updated NavMenu.razor with dynamic content links.");
83+
await File.WriteAllTextAsync(navMenuPath, navMenuContent, cancellationToken);
84+
logger.LogInformation("✅ Updated NavMenu.razor with dynamic content links.");
8585
}
8686
else
8787
{
88-
logger?.LogWarning("Could not find </nav> tag in NavMenu.razor to insert dynamic content links.");
88+
logger.LogWarning("Could not find </nav> tag in NavMenu.razor to insert dynamic content links.");
8989
}
9090
}
9191
}

0 commit comments

Comments
 (0)