Skip to content

Commit 6dacbdb

Browse files
committed
Fix ConsoleApp.Run(async) / ConsoleApp.RunAsync(sync) code does not generate correctly
1 parent 7cf52a5 commit 6dacbdb

File tree

3 files changed

+46
-8
lines changed

3 files changed

+46
-8
lines changed
Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
using ConsoleAppFramework;
22

3-
var app = ConsoleApp.Create();
43

5-
app.ConfigureServices(_ => { Console.WriteLine("ConfigureServices"); });
6-
app.Add("test", () => Console.WriteLine("test output"));
4+
args = ["--x", "3", "--y", "5"];
75

8-
for (var i = 0; i < 3; i++)
6+
await ConsoleApp.RunAsync(args, async (int x, int y) => { });
7+
8+
static void Foo(int x, int y)
99
{
10-
app.Run(["test"], startHost: false, stopHost: false, disposeServiceProvider: false);
10+
Console.WriteLine(x + y);
1111
}
12-
13-

src/ConsoleAppFramework/ConsoleAppGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ static void EmitConsoleAppRun(SourceProductionContext sourceProductionContext, C
221221
{
222222
var emitter = new Emitter(null);
223223
var withId = new Emitter.CommandWithId(null, command, -1);
224-
emitter.EmitRun(sb, withId, command.IsAsync, null);
224+
emitter.EmitRun(sb, withId, commandContext.IsAsync, null);
225225
}
226226
sourceProductionContext.AddSource("ConsoleApp.Run.g.cs", sb.ToString().ReplaceLineEndings());
227227

tests/ConsoleAppFramework.GeneratorTests/RunTest.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,4 +284,44 @@ public void ArgumentLastParams()
284284

285285
verifier.Execute(code, "--opt1 abc a b c d", "abc, a|b|c|d");
286286
}
287+
288+
[Fact]
289+
public void RunAndRunAsyncOverloads()
290+
{
291+
verifier.Execute("""
292+
ConsoleApp.Run(args, () => Console.Write("sync"));
293+
""", "", "sync");
294+
295+
verifier.Execute("""
296+
ConsoleApp.Run(args, async () => Console.Write("async"));
297+
""", "", "async");
298+
299+
verifier.Execute("""
300+
await ConsoleApp.RunAsync(args, () => Console.Write("sync"));
301+
""", "", "sync");
302+
303+
verifier.Execute("""
304+
await ConsoleApp.RunAsync(args, async () => Console.Write("async"));
305+
""", "", "async");
306+
}
307+
308+
[Fact]
309+
public void RunAndRunAsyncOverloadsWithCancellationToken()
310+
{
311+
verifier.Execute("""
312+
ConsoleApp.Run(args, (CancellationToken cancellationToken) => Console.Write("sync"));
313+
""", "", "sync");
314+
315+
verifier.Execute("""
316+
ConsoleApp.Run(args, async (CancellationToken cancellationToken) => Console.Write("async"));
317+
""", "", "async");
318+
319+
verifier.Execute("""
320+
await ConsoleApp.RunAsync(args, (CancellationToken cancellationToken) => Console.Write("sync"));
321+
""", "", "sync");
322+
323+
verifier.Execute("""
324+
await ConsoleApp.RunAsync(args, async (CancellationToken cancellationToken) => Console.Write("async"));
325+
""", "", "async");
326+
}
287327
}

0 commit comments

Comments
 (0)