-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClaudeCliLocatorTests.cs
More file actions
152 lines (128 loc) · 5.62 KB
/
ClaudeCliLocatorTests.cs
File metadata and controls
152 lines (128 loc) · 5.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
using ManagedCode.ClaudeCodeSharpSDK.Internal;
using ManagedCode.ClaudeCodeSharpSDK.Tests.Shared;
namespace ManagedCode.ClaudeCodeSharpSDK.Tests.Unit;
public class ClaudeCliLocatorTests
{
private const string AnthropicScopeDirectory = "@anthropic-ai";
private const string CustomOverridePath = "/tmp/custom-claude";
private const string FirstPathEntryName = "first";
private const string GuidFormat = "N";
private const string NodeModulesDirectory = "node_modules";
private const string PackageDirectory = "claude-code";
private const string SandboxPrefix = "ClaudeCliLocatorTests-";
private const string SecondPathEntryName = "second";
private const string UnixPathEntryName = "unix";
[Test]
public async Task GetPathExecutableCandidates_Windows_IncludeCommandWrappers()
{
var candidates = ClaudeCliLocator.GetPathExecutableCandidates(isWindows: true);
await Assert.That(candidates).IsEquivalentTo(
[
ClaudeCliLocator.ClaudeWindowsExecutableName,
ClaudeCliLocator.ClaudeWindowsCommandName,
ClaudeCliLocator.ClaudeWindowsBatchName,
ClaudeCliLocator.ClaudeExecutableName,
]);
}
[Test]
public async Task TryResolvePathExecutable_Windows_ResolvesCmdWhenExeMissing()
{
var sandboxDirectory = CreateSandboxDirectory();
try
{
var firstPathEntry = Path.Combine(sandboxDirectory, FirstPathEntryName);
var secondPathEntry = Path.Combine(sandboxDirectory, SecondPathEntryName);
Directory.CreateDirectory(firstPathEntry);
Directory.CreateDirectory(secondPathEntry);
var cmdPath = Path.Combine(secondPathEntry, ClaudeCliLocator.ClaudeWindowsCommandName);
await File.WriteAllTextAsync(cmdPath, TestConstants.EchoOffScript);
var pathVariable = string.Join(Path.PathSeparator, firstPathEntry, secondPathEntry);
var resolved = ClaudeCliLocator.TryResolvePathExecutable(pathVariable, isWindows: true, out var executablePath);
await Assert.That(resolved).IsTrue();
await Assert.That(executablePath).IsEqualTo(cmdPath);
}
finally
{
Directory.Delete(sandboxDirectory, recursive: true);
}
}
[Test]
public async Task TryResolvePathExecutable_Unix_DoesNotTreatCmdAsExecutable()
{
var sandboxDirectory = CreateSandboxDirectory();
try
{
var pathEntry = Path.Combine(sandboxDirectory, UnixPathEntryName);
Directory.CreateDirectory(pathEntry);
await File.WriteAllTextAsync(Path.Combine(pathEntry, ClaudeCliLocator.ClaudeWindowsCommandName), TestConstants.BashShebang);
var resolved = ClaudeCliLocator.TryResolvePathExecutable(pathEntry, isWindows: false, out var executablePath);
await Assert.That(resolved).IsFalse();
await Assert.That(executablePath).IsEqualTo(TestConstants.EmptyString);
}
finally
{
Directory.Delete(sandboxDirectory, recursive: true);
}
}
[Test]
public async Task FindClaudePath_ReturnsOverrideWithoutFurtherResolution()
{
var resolved = ClaudeCliLocator.FindClaudePath(CustomOverridePath);
await Assert.That(resolved).IsEqualTo(CustomOverridePath);
}
[Test]
public async Task TryResolveNodeModulesBinary_Windows_IgnoresPackageCliEntryWithoutCommandWrapper()
{
var sandboxDirectory = CreateSandboxDirectory();
try
{
var packageDirectory = Path.Combine(
sandboxDirectory,
NodeModulesDirectory,
AnthropicScopeDirectory,
PackageDirectory);
Directory.CreateDirectory(packageDirectory);
await File.WriteAllTextAsync(Path.Combine(packageDirectory, TestConstants.CliJsFileName), TestConstants.NodeShebang);
var resolved = ClaudeCliLocator.TryResolveNodeModulesBinary([sandboxDirectory], isWindows: true, out var executablePath);
await Assert.That(resolved).IsFalse();
await Assert.That(executablePath).IsEqualTo(TestConstants.EmptyString);
}
finally
{
Directory.Delete(sandboxDirectory, recursive: true);
}
}
[Test]
public async Task TryResolveNodeModulesBinary_Unix_UsesPackageCliEntryWhenWrapperMissing()
{
var sandboxDirectory = CreateSandboxDirectory();
try
{
var packageDirectory = Path.Combine(
sandboxDirectory,
NodeModulesDirectory,
AnthropicScopeDirectory,
PackageDirectory);
Directory.CreateDirectory(packageDirectory);
var cliPath = Path.Combine(packageDirectory, TestConstants.CliJsFileName);
await File.WriteAllTextAsync(cliPath, TestConstants.NodeShebang);
var resolved = ClaudeCliLocator.TryResolveNodeModulesBinary([sandboxDirectory], isWindows: false, out var executablePath);
await Assert.That(resolved).IsTrue();
await Assert.That(executablePath).IsEqualTo(cliPath);
}
finally
{
Directory.Delete(sandboxDirectory, recursive: true);
}
}
private static string CreateSandboxDirectory()
{
var sandboxDirectory = Path.Combine(
Environment.CurrentDirectory,
TestConstants.TestsDirectoryName,
TestConstants.SandboxDirectoryName,
string.Concat(SandboxPrefix, Guid.NewGuid().ToString(GuidFormat)));
Directory.CreateDirectory(sandboxDirectory);
return sandboxDirectory;
}
}