Skip to content

Commit e0f6f0f

Browse files
committed
Reuse PortAllocation worktree candidate list in tests via InternalsVisibleTo
1 parent 849be60 commit e0f6f0f

3 files changed

Lines changed: 55 additions & 1 deletion

File tree

application/shared-kernel/SharedKernel/Configuration/PortAllocation.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public sealed record PortAllocation(int BasePort)
1717
private const string PortFileName = "port.txt";
1818

1919
// Worktrees scan these in order and pick the first base port whose full allocation is free.
20-
private static readonly int[] WorktreeCandidateBasePorts = [9100, 9200, 9300, 9400, 9500, 9600, 9700, 9800, 9900];
20+
internal static readonly int[] WorktreeCandidateBasePorts = [9100, 9200, 9300, 9400, 9500, 9600, 9700, 9800, 9900];
2121

2222
public int AppGateway => BasePort;
2323

application/shared-kernel/SharedKernel/SharedKernel.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,8 @@
6060
<EmbeddedResource Include="Platform\platform-settings.jsonc"/>
6161
</ItemGroup>
6262

63+
<ItemGroup>
64+
<InternalsVisibleTo Include="SharedKernel.Tests"/>
65+
</ItemGroup>
66+
6367
</Project>
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using FluentAssertions;
2+
using SharedKernel.Configuration;
3+
using Xunit;
4+
5+
namespace SharedKernel.Tests.Configuration;
6+
7+
public sealed class PortAllocationTests : IDisposable
8+
{
9+
private const int DefaultBasePort = 9000;
10+
11+
private readonly string _temporaryDirectory = Path.Combine(Path.GetTempPath(), $"PortAllocationTests-{Guid.NewGuid():N}");
12+
13+
public PortAllocationTests()
14+
{
15+
Directory.CreateDirectory(_temporaryDirectory);
16+
}
17+
18+
public void Dispose()
19+
{
20+
if (Directory.Exists(_temporaryDirectory)) Directory.Delete(_temporaryDirectory, true);
21+
}
22+
23+
[Fact]
24+
public void LoadFrom_WhenPortFileMissingAndGitIsFile_ShouldPickFreeWorktreeCandidate()
25+
{
26+
// Arrange: a worktree has `.git` as a FILE (not a directory)
27+
File.WriteAllText(Path.Combine(_temporaryDirectory, ".git"), "gitdir: /some/other/path");
28+
29+
// Act
30+
var allocation = PortAllocation.LoadFrom(_temporaryDirectory);
31+
32+
// Assert
33+
PortAllocation.WorktreeCandidateBasePorts.Should().Contain(allocation.BasePort);
34+
File.Exists(Path.Combine(_temporaryDirectory, ".workspace", "port.txt")).Should().BeTrue();
35+
}
36+
37+
[Fact]
38+
public void LoadFrom_WhenPortFileMissingAndGitIsDirectory_ShouldPickDefaultBasePort()
39+
{
40+
// Arrange: a normal repository root has `.git` as a DIRECTORY
41+
Directory.CreateDirectory(Path.Combine(_temporaryDirectory, ".git"));
42+
43+
// Act
44+
var allocation = PortAllocation.LoadFrom(_temporaryDirectory);
45+
46+
// Assert
47+
allocation.BasePort.Should().Be(DefaultBasePort);
48+
File.Exists(Path.Combine(_temporaryDirectory, ".workspace", "port.txt")).Should().BeTrue();
49+
}
50+
}

0 commit comments

Comments
 (0)