Skip to content

Commit 1a40fa6

Browse files
committed
Refactor BlakeServeCommandTests for .NET CLI usage
Updated `BlakeServeCommandTests.cs` to replace calls to `RunBlakeCommandAsync` with `RunBlakeFromDotnetAsync`, shifting to the .NET CLI for executing the `blake serve` command. This change ensures proper process termination before test execution. Additionally, modified assertions to use `Assert.Contains` with predicates for improved readability and maintainability, enhancing the robustness of the integration tests.
1 parent 248149e commit 1a40fa6

File tree

1 file changed

+34
-33
lines changed

1 file changed

+34
-33
lines changed

tests/Blake.IntegrationTests/Commands/BlakeServeCommandTests.cs

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ namespace Blake.IntegrationTests.Commands;
88
/// </summary>
99
public class BlakeServeCommandTests : TestFixtureBase
1010
{
11+
// NOTE: All these must run using dotnet rather than the Blake CLI directly, as the processes need to be terminated before test execution proceeds
1112
[Fact]
1213
public async Task BlakeServe_WithNonExistentPath_ShowsError()
1314
{
@@ -16,7 +17,7 @@ public async Task BlakeServe_WithNonExistentPath_ShowsError()
1617

1718
// Act - Use a short timeout since serve command runs indefinitely
1819
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5));
19-
var result = await RunBlakeCommandAsync(["serve", nonExistentPath], cts.Token);
20+
var result = await RunBlakeFromDotnetAsync("serve", nonExistentPath, cancellationToken: cts.Token);
2021

2122
// Assert
2223
Assert.NotEqual(0, result.ExitCode);
@@ -45,20 +46,20 @@ public async Task BlakeServe_BakesBeforeServing()
4546

4647
// Act - Start serve command but cancel quickly
4748
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
48-
var result = await RunBlakeCommandAsync(["serve", testDir], cts.Token);
49+
var result = await RunBlakeFromDotnetAsync("serve", testDir, cancellationToken: cts.Token);
4950

5051
// Assert
5152
// Should have attempted to bake first
52-
Assert.Contains("Baking in:", result.OutputText);
53+
Assert.Contains(result.OutputText, o => o.Contains("Baking in:"));
5354

5455
// Should have created generated content
5556
FileSystemHelper.AssertDirectoryExists(Path.Combine(testDir, ".generated"));
5657

5758
// May show "Build completed successfully" or start showing dotnet run output
58-
Assert.True(
59-
result.OutputText.Contains("Build completed successfully") ||
60-
result.OutputText.Contains("Running app") ||
61-
result.ErrorText.Contains("was canceled") // Expected due to timeout
59+
Assert.Contains(result.OutputText, o =>
60+
o.Contains("Build completed successfully") ||
61+
o.Contains("Running app") ||
62+
o.Contains("was canceled") // Expected due to timeout
6263
);
6364
}
6465

@@ -78,7 +79,7 @@ public async Task BlakeServe_WithBakeFailure_DoesNotStartServer()
7879

7980
// Act
8081
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
81-
var result = await RunBlakeCommandAsync(["serve", testDir], cts.Token);
82+
var result = await RunBlakeFromDotnetAsync("serve", testDir, cancellationToken: cts.Token);
8283

8384
// Assert
8485
if (result.ExitCode != 0)
@@ -98,15 +99,15 @@ public async Task BlakeServe_WithValidProject_AttemptsToRunDotnetRun()
9899

99100
// Act
100101
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(15));
101-
var result = await RunBlakeCommandAsync(["serve", testDir], cts.Token);
102+
var result = await RunBlakeFromDotnetAsync("serve", testDir, cancellationToken: cts.Token);
102103

103104
// Assert
104105
// Should attempt to run the app (even if it fails due to missing dependencies in test environment)
105-
Assert.True(
106-
result.OutputText.Contains("Running app") ||
107-
result.OutputText.Contains("dotnet run") ||
108-
result.ErrorText.Contains("was canceled") || // Expected due to timeout
109-
result.ErrorText.Contains("run") // May show dotnet run errors
106+
Assert.Contains(result.OutputText, o =>
107+
o.Contains("Running app") ||
108+
o.Contains("dotnet run") ||
109+
o.Contains("was canceled") || // Expected due to timeout
110+
o.Contains("run") // May show dotnet run errors
110111
);
111112
}
112113

@@ -136,7 +137,7 @@ public async Task BlakeServe_PassesThroughOptions()
136137

137138
// Assert
138139
// Should have passed through the option to the bake step
139-
Assert.Contains("Baking in:", result.OutputText);
140+
Assert.Contains(result.OutputText, o => o.Contains("Baking in:"));
140141

141142
// Should have created generated content despite the option
142143
FileSystemHelper.AssertDirectoryExists(Path.Combine(testDir, ".generated"));
@@ -153,14 +154,14 @@ public async Task BlakeServe_WithMissingContentFolder_HandlesGracefully()
153154

154155
// Act
155156
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
156-
var result = await RunBlakeCommandAsync(["serve", testDir], cts.Token);
157+
var result = await RunBlakeFromDotnetAsync("serve", testDir, cancellationToken: cts.Token);
157158

158159
// Assert
159160
// Should handle missing content gracefully and still try to serve
160161
Assert.True(result.ExitCode == 0 || result.ErrorText.Contains("was canceled"));
161162

162163
// Should still attempt baking
163-
Assert.Contains("Baking in:", result.OutputText);
164+
Assert.Contains(result.OutputText, o => o.Contains("Baking in:"));
164165
}
165166

166167
[Fact]
@@ -172,7 +173,7 @@ public async Task BlakeServe_CreatesGeneratedFolder()
172173

173174
// Act
174175
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(8));
175-
var result = await RunBlakeCommandAsync(["serve", testDir], cts.Token);
176+
var result = await RunBlakeFromDotnetAsync("serve", testDir, cancellationToken: cts.Token);
176177

177178
// Assert
178179
// Should create .generated folder as part of the bake step
@@ -188,11 +189,11 @@ public async Task BlakeServe_UsesCurrentDirectoryWhenNoPathProvided()
188189

189190
// Act - Run blake serve without path argument from the project directory
190191
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(8));
191-
var result = await RunBlakeCommandAsync(["serve", testDir], cts.Token);
192+
var result = await RunBlakeFromDotnetAsync("serve", testDir, cancellationToken: cts.Token);
192193

193194
// Assert
194195
Assert.True(result.ExitCode == 0 || result.ErrorText.Contains("was canceled"));
195-
Assert.Contains("Baking in:", result.OutputText);
196+
Assert.Contains(result.OutputText, o => o.Contains("Baking in:"));
196197

197198
// Should create .generated in the working directory
198199
FileSystemHelper.AssertDirectoryExists(Path.Combine(testDir, ".generated"));
@@ -207,17 +208,17 @@ public async Task BlakeServe_ShowsProgressMessages()
207208

208209
// Act
209210
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
210-
var result = await RunBlakeCommandAsync(["serve", testDir], cts.Token);
211+
var result = await RunBlakeFromDotnetAsync("serve", testDir, cancellationToken: cts.Token);
211212

212213
// Assert
213214
// Should show baking progress
214-
Assert.Contains("Baking in:", result.OutputText);
215+
Assert.Contains(result.OutputText, o => o.Contains("Baking in:"));
215216

216217
// May show build completion or app startup messages
217-
Assert.True(
218-
result.OutputText.Contains("Build completed successfully") ||
219-
result.OutputText.Contains("Running app") ||
220-
result.ErrorText.Contains("was canceled") // Expected due to timeout
218+
Assert.Contains(result.OutputText, o =>
219+
o.Contains("Build completed successfully") ||
220+
o.Contains("Running app") ||
221+
o.Contains("was canceled") // Expected due to timeout
221222
);
222223
}
223224

@@ -245,7 +246,7 @@ public async Task BlakeServe_IntegrationWithBakeOptions()
245246

246247
// Act - Should not include drafts by default
247248
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(8));
248-
var result = await RunBlakeCommandAsync(["serve", testDir], cts.Token);
249+
var result = await RunBlakeFromDotnetAsync("serve", testDir, cancellationToken: cts.Token);
249250

250251
// Assert
251252
// Should have baked (drafts excluded by default)
@@ -264,20 +265,20 @@ public async Task BlakeServe_HandlesProjectWithoutCsproj()
264265

265266
// Act
266267
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5));
267-
var result = await RunBlakeCommandAsync(["serve", testDir], cts.Token);
268+
var result = await RunBlakeFromDotnetAsync("serve", testDir, cancellationToken: cts.Token);
268269

269270
// Assert
270271
// Should either fail gracefully or handle the missing project file
271272
if (result.ExitCode != 0)
272273
{
273-
Assert.True(
274-
result.ErrorText.Contains("project") ||
275-
result.ErrorText.Contains("csproj") ||
276-
result.ErrorText.Contains("build")
274+
Assert.Contains(result.ErrorText, e =>
275+
e.Contains("project") ||
276+
e.Contains("csproj") ||
277+
e.Contains("build")
277278
);
278279
}
279280

280281
// Should still attempt to bake first
281-
Assert.Contains("Baking in:", result.OutputText);
282+
Assert.Contains(result.OutputText, o => o.Contains("Baking in:"));
282283
}
283284
}

0 commit comments

Comments
 (0)