Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions src/winapp-CLI/WinApp.Cli.Tests/NewCommandHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1116,6 +1116,8 @@ public async Task Handler_AppTemplate_PrintsWinappRunNextStep()
$"App templates should suggest 'winapp run' as the next step (it builds and launches the project). Output:\n{TestAnsiConsole.Output}");
Assert.IsFalse(TestAnsiConsole.Output.Contains("dotnet run", StringComparison.Ordinal),
$"App templates must not suggest 'dotnet run' now that 'winapp run' is the documented next step. Output:\n{TestAnsiConsole.Output}");
Assert.IsFalse(TestAnsiConsole.Output.Contains("MyApp.cs", StringComparison.Ordinal),
$"Project-based apps run from their folder; the single-file hint must not appear. Output:\n{TestAnsiConsole.Output}");
}

[TestMethod]
Expand Down Expand Up @@ -1166,6 +1168,80 @@ public async Task Handler_UnitTestTemplate_PrintsPackagedRunNextStep()
"The unittest next step must not recommend 'dotnet test' — the packaged MSTest app runs its tests on launch.");
}

/// <summary>
/// Scripts a pack holding the Reactor single-file template with <paramref name="tags"/> in the Tags
/// column. When <paramref name="createSingleFile"/> is set, the scaffold step writes
/// <c>MyApp\MyApp.cs</c> (and no project file), as the real template does.
/// </summary>
private void ScriptSingleFileTemplate(string tags, bool createSingleFile)
{
(string Name, string Short, string Lang, string Type, string Author, string Tags)[] templates =
[
("Reactor Single-File App (Experimental)", "reactor-singlefile,winui-reactor-singlefile", "[C#]", "project", "Microsoft", tags),
("WinUI Blank App", "winui,winui3,wasdk-single", "[C#]", "project", "Microsoft", "Windows/WinUI/Desktop/XAML"),
];
var list = BuildListTable(templates);
var uninstall = BuildUninstallOutput("0.0.6-alpha", templates);
_dotnet.RunDotnetArgumentListHandler = args =>
{
if (args.Count >= 1 && args[0] == "--version")
{
return (0, "10.0.100\n", string.Empty);
}
if (args.Count >= 2 && args[0] == "new" && args[1] == "uninstall")
{
return (0, uninstall, string.Empty);
}
if (args.Count >= 2 && args[0] == "new" && args[1] == "list")
{
return (0, list, string.Empty);
}
if (args.Count >= 2 && args[0] == "new" && args[1] == "update")
{
return (0, "All template packages are up-to-date.", string.Empty);
}
if (args.Count >= 2 && args[0] == "new" && args[1] == "install")
{
return (0, "ok", string.Empty);
}
if (createSingleFile)
{
var dir = Directory.CreateDirectory(Path.Join(_tempDirectory.FullName, "MyApp"));
File.WriteAllText(Path.Join(dir.FullName, "MyApp.cs"), "// file-based app");
}
return (0, "The template was created successfully.", string.Empty);
};
}

[TestMethod]
public async Task Handler_SingleFileTemplate_PrintsRunWithCsFileNextStep()
{
ScriptSingleFileTemplate("Windows/WinUI/Desktop/Reactor/SingleFile/Experimental", createSingleFile: false);
TestAnsiConsole.Profile.Width = 500;
var command = GetRequiredService<NewCommand>();

var exitCode = await ParseAndInvokeWithCaptureAsync(command, ["--use-defaults", "--template", "reactor-singlefile", "--name", "MyApp"]);

Assert.AreEqual(NewCommand.ExitSuccess, exitCode, TestAnsiConsole.Output);
Assert.IsTrue(TestAnsiConsole.Output.Contains("winapp run \"MyApp.cs\"", StringComparison.Ordinal),
$"A single-file app only runs with its .cs path, so the next step must be 'winapp run \"MyApp.cs\"'. Output:\n{TestAnsiConsole.Output}");
}

[TestMethod]
public async Task Handler_SingleFileTemplate_TruncatedTags_DetectsScaffoldedCsFile()
{
// dotnet new list can cut the Tags column short, dropping the SingleFile segment.
ScriptSingleFileTemplate("Windows/WinUI/Desktop/Re...", createSingleFile: true);
TestAnsiConsole.Profile.Width = 500;
var command = GetRequiredService<NewCommand>();

var exitCode = await ParseAndInvokeWithCaptureAsync(command, ["--use-defaults", "--template", "reactor-singlefile", "--name", "MyApp"]);

Assert.AreEqual(NewCommand.ExitSuccess, exitCode, TestAnsiConsole.Output);
Assert.IsTrue(TestAnsiConsole.Output.Contains("winapp run \"MyApp.cs\"", StringComparison.Ordinal),
$"A scaffolded MyApp.cs with no project file is a single-file app even when the tag is cut off. Output:\n{TestAnsiConsole.Output}");
}

[TestMethod]
public async Task Handler_ScaffoldPinsTargetFrameworkToInstalledSdk()
{
Expand Down
27 changes: 27 additions & 0 deletions src/winapp-CLI/WinApp.Cli/Commands/NewCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1277,12 +1277,39 @@ private void WriteSuccess(WinUiTemplateEntry entry, string name, DirectoryInfo o
{
ansiConsole.MarkupLineInterpolated($"{UiSymbols.Next} Next: [blue]cd \"{relative}\"[/] then [blue]winapp run[/] — this packaged MSTest app runs its tests when launched.");
}
else if (IsSingleFileApp(entry, name, outputDir))
{
ansiConsole.MarkupLineInterpolated($"{UiSymbols.Next} Next: [blue]cd \"{relative}\"[/] then [blue]winapp run \"{name}.cs\"[/].");
}
else
{
ansiConsole.MarkupLineInterpolated($"{UiSymbols.Next} Next: [blue]cd \"{relative}\"[/] then [blue]winapp run[/].");
}
}

/// <summary>
/// True when the template produced a .NET file-based app (one <c>&lt;name&gt;.cs</c>, no project file),
/// which <c>winapp run</c> only runs when given the <c>.cs</c> path. The scaffolded files are checked
/// as well as the <c>SingleFile</c> tag because <c>dotnet new list</c> can cut off the Tags column.
/// </summary>
private static bool IsSingleFileApp(WinUiTemplateEntry entry, string name, DirectoryInfo outputDir)
{
if (entry.HasTag("SingleFile"))
{
return true;
}

try
{
return File.Exists(Path.Join(outputDir.FullName, name + ".cs"))
&& !Directory.EnumerateFiles(outputDir.FullName, "*.csproj").Any();
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
{
return false;
}
}

/// <summary>Writes the human-readable template list for <c>--list</c>.</summary>
private void WriteTemplateList(IReadOnlyList<WinUiTemplateEntry> templates, string? packVersion)
{
Expand Down
Loading