Skip to content

Commit b7624bf

Browse files
committed
Add file and directory info to runtime config
Add tests
1 parent 47d0bba commit b7624bf

File tree

2 files changed

+130
-2
lines changed

2 files changed

+130
-2
lines changed

src/Cli/dotnet/Commands/Run/VirtualProjectBuildingCommand.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ public VirtualProjectBuildingCommand(
8787
public override int Execute()
8888
{
8989
Debug.Assert(!(NoRestore && NoBuild));
90-
9190
var consoleLogger = RunCommand.MakeTerminalLogger(Verbosity);
9291
var binaryLogger = GetBinaryLogger(BinaryLoggerArgs);
9392

@@ -625,6 +624,15 @@ public static void WriteProjectFile(
625624
626625
""");
627626

627+
var targetDirectory = Path.GetDirectoryName(targetFilePath) ?? "";
628+
writer.WriteLine($"""
629+
<ItemGroup>
630+
<RuntimeHostConfigurationOption Include="EntryPointFilePath" Value="{EscapeValue(targetFilePath)}" />
631+
<RuntimeHostConfigurationOption Include="EntryPointFileDirectoryPath" Value="{EscapeValue(targetDirectory)}" />
632+
</ItemGroup>
633+
634+
""");
635+
628636
foreach (var sdk in sdkDirectives)
629637
{
630638
WriteImport(writer, "Sdk.targets", sdk);

test/dotnet.Tests/CommandTests/Run/RunFileTests.cs

Lines changed: 121 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Licensed to the .NET Foundation under one or more agreements.
1+
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.Runtime.Versioning;
@@ -1225,6 +1225,11 @@ public void Api()
12251225
<Compile Include="{programPath}" />
12261226
</ItemGroup>
12271227
1228+
<ItemGroup>
1229+
<RuntimeHostConfigurationOption Include="EntryPointFilePath" Value="{programPath}" />
1230+
<RuntimeHostConfigurationOption Include="EntryPointFileDirectoryPath" Value="{testInstance.Path}" />
1231+
</ItemGroup>
1232+
12281233
<Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk" />
12291234
<Import Project="Sdk.targets" Sdk="Aspire.Hosting.Sdk" Version="9.1.0" />
12301235
@@ -1308,6 +1313,11 @@ public void Api_Diagnostic_01()
13081313
<Compile Include="{programPath}" />
13091314
</ItemGroup>
13101315
1316+
<ItemGroup>
1317+
<RuntimeHostConfigurationOption Include="EntryPointFilePath" Value="{programPath}" />
1318+
<RuntimeHostConfigurationOption Include="EntryPointFileDirectoryPath" Value="{testInstance.Path}" />
1319+
</ItemGroup>
1320+
13111321
<Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk" />
13121322
13131323
<!--
@@ -1394,6 +1404,11 @@ public void Api_Diagnostic_02()
13941404
<Compile Include="{programPath}" />
13951405
</ItemGroup>
13961406
1407+
<ItemGroup>
1408+
<RuntimeHostConfigurationOption Include="EntryPointFilePath" Value="{programPath}" />
1409+
<RuntimeHostConfigurationOption Include="EntryPointFileDirectoryPath" Value="{testInstance.Path}" />
1410+
</ItemGroup>
1411+
13971412
<Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk" />
13981413
13991414
<!--
@@ -1449,4 +1464,109 @@ public void Api_Error()
14491464
.And.HaveStdOutContaining("Unknown1")
14501465
.And.HaveStdOutContaining("Unknown2");
14511466
}
1467+
1468+
[Fact]
1469+
public void EntryPointFilePath()
1470+
{
1471+
var testInstance = _testAssetsManager.CreateTestDirectory();
1472+
var filePath = Path.Join(testInstance.Path, "Program.cs");
1473+
File.WriteAllText(filePath, """"
1474+
var entryPointFilePath = AppContext.GetData("EntryPointFilePath") as string;
1475+
Console.WriteLine($"""EntryPointFilePath: {entryPointFilePath}""");
1476+
"""");
1477+
1478+
new DotnetCommand(Log, "run", "Program.cs")
1479+
.WithWorkingDirectory(testInstance.Path)
1480+
.Execute()
1481+
.Should().Pass()
1482+
.And.HaveStdOut($"EntryPointFilePath: {filePath}");
1483+
}
1484+
1485+
[Fact]
1486+
public void EntryPointFileDirectoryPath()
1487+
{
1488+
var testInstance = _testAssetsManager.CreateTestDirectory();
1489+
File.WriteAllText(Path.Join(testInstance.Path, "Program.cs"), """"
1490+
var entryPointFileDirectoryPath = AppContext.GetData("EntryPointFileDirectoryPath") as string;
1491+
Console.WriteLine($"""EntryPointFileDirectoryPath: {entryPointFileDirectoryPath}""");
1492+
"""");
1493+
1494+
new DotnetCommand(Log, "run", "Program.cs")
1495+
.WithWorkingDirectory(testInstance.Path)
1496+
.Execute()
1497+
.Should().Pass()
1498+
.And.HaveStdOut($"EntryPointFileDirectoryPath: {testInstance.Path}");
1499+
}
1500+
1501+
[Fact]
1502+
public void EntryPointFilePath_WithRelativePath()
1503+
{
1504+
var testInstance = _testAssetsManager.CreateTestDirectory();
1505+
var fileName = "Program.cs";
1506+
File.WriteAllText(Path.Join(testInstance.Path, fileName), """
1507+
var entryPointFilePath = AppContext.GetData("EntryPointFilePath") as string;
1508+
Console.WriteLine($"EntryPointFilePath: {entryPointFilePath}");
1509+
""");
1510+
1511+
var relativePath = Path.GetRelativePath(Directory.GetCurrentDirectory(), Path.Join(testInstance.Path, fileName));
1512+
new DotnetCommand(Log, "run", relativePath)
1513+
.WithWorkingDirectory(Directory.GetCurrentDirectory())
1514+
.Execute()
1515+
.Should().Pass()
1516+
.And.HaveStdOut($"EntryPointFilePath: {Path.GetFullPath(relativePath)}");
1517+
}
1518+
1519+
[Fact]
1520+
public void EntryPointFilePath_WithSpacesInPath()
1521+
{
1522+
var testInstance = _testAssetsManager.CreateTestDirectory();
1523+
var dirWithSpaces = Path.Join(testInstance.Path, "dir with spaces");
1524+
Directory.CreateDirectory(dirWithSpaces);
1525+
var filePath = Path.Join(dirWithSpaces, "Program.cs");
1526+
File.WriteAllText(filePath, """
1527+
var entryPointFilePath = AppContext.GetData("EntryPointFilePath") as string;
1528+
Console.WriteLine($"EntryPointFilePath: {entryPointFilePath}");
1529+
""");
1530+
1531+
new DotnetCommand(Log, "run", filePath)
1532+
.WithWorkingDirectory(testInstance.Path)
1533+
.Execute()
1534+
.Should().Pass()
1535+
.And.HaveStdOut($"EntryPointFilePath: {filePath}");
1536+
}
1537+
1538+
[Fact]
1539+
public void EntryPointFileDirectoryPath_WithDotSlash()
1540+
{
1541+
var testInstance = _testAssetsManager.CreateTestDirectory();
1542+
var fileName = "Program.cs";
1543+
File.WriteAllText(Path.Join(testInstance.Path, fileName), """
1544+
var entryPointFileDirectoryPath = AppContext.GetData("EntryPointFileDirectoryPath") as string;
1545+
Console.WriteLine($"EntryPointFileDirectoryPath: {entryPointFileDirectoryPath}");
1546+
""");
1547+
1548+
new DotnetCommand(Log, "run", $"./{fileName}")
1549+
.WithWorkingDirectory(testInstance.Path)
1550+
.Execute()
1551+
.Should().Pass()
1552+
.And.HaveStdOut($"EntryPointFileDirectoryPath: {testInstance.Path}");
1553+
}
1554+
1555+
[Fact]
1556+
public void EntryPointFilePath_WithUnicodeCharacters()
1557+
{
1558+
var testInstance = _testAssetsManager.CreateTestDirectory();
1559+
var unicodeFileName = "Программа.cs";
1560+
var filePath = Path.Join(testInstance.Path, unicodeFileName);
1561+
File.WriteAllText(filePath, """
1562+
var entryPointFilePath = AppContext.GetData("EntryPointFilePath") as string;
1563+
Console.WriteLine($"EntryPointFilePath: {entryPointFilePath}");
1564+
""");
1565+
1566+
new DotnetCommand(Log, "run", unicodeFileName)
1567+
.WithWorkingDirectory(testInstance.Path)
1568+
.Execute()
1569+
.Should().Pass()
1570+
.And.HaveStdOut($"EntryPointFilePath: {filePath}");
1571+
}
14521572
}

0 commit comments

Comments
 (0)