Skip to content

Commit cd140aa

Browse files
committed
determing which to set cmd line args for + refactoring cleanup
1 parent 0811381 commit cd140aa

File tree

10 files changed

+95
-74
lines changed

10 files changed

+95
-74
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System.IO;
2+
using System.Linq;
3+
using FluentAssertions;
4+
using KS.RustAnalyzer.TestAdapter.Cargo;
5+
using KS.RustAnalyzer.Tests.Common;
6+
using Xunit;
7+
8+
namespace KS.RustAnalyzer.TestAdapter.UnitTests.Cargo;
9+
10+
public class ManifestExtensionsTests
11+
{
12+
[Theory]
13+
[InlineData(@"not_a_project\src\main.rs", "not_a_project", @"not_a_project\Cargo.toml", false)]
14+
[InlineData(@"not_a_project\src", "not_a_project", @"not_a_project\Cargo.toml", false)]
15+
[InlineData(@"hello_library\src\lib.rs", "hello_library", @"hello_library\Cargo.toml", true)]
16+
[InlineData(@"hello_library\Cargo.toml", "hello_library", @"hello_library\Cargo.toml", true)]
17+
[InlineData(@"hello_workspace\main\src\main.rs", "hello_workspace", @"hello_workspace\main\Cargo.toml", true)]
18+
[InlineData(@"hello_workspace\main\src", "hello_workspace", @"hello_workspace\main\Cargo.toml", true)]
19+
[InlineData(@"hello_workspace\main\Cargo.toml", "hello_workspace", @"hello_workspace\main\Cargo.toml", true)]
20+
[InlineData(@"workspace_with_example\lib\examples\eg1.rs", "workspace_with_example", @"workspace_with_example\lib\Cargo.toml", true)]
21+
[InlineData(@"c:\workspace_with_example\lib\examples\eg1.rs", "workspace_with_example", null, false)]
22+
public void GetContainingManifestOrThisTests(string fileOrFolder, string workspaceRootx, string parentCargoRelPath, bool foundParentManifest)
23+
{
24+
string path = Path.Combine(TestHelpers.ThisTestRoot, fileOrFolder);
25+
var workspaceRoot = Path.Combine(TestHelpers.ThisTestRoot, workspaceRootx);
26+
var found = path.TryGetParentManifestOrThisUnderWorkspace(workspaceRoot, out string parentCargoPath);
27+
28+
found.Should().Be(foundParentManifest);
29+
var expectedParentManifestpath = found ? Path.Combine(TestHelpers.ThisTestRoot, parentCargoRelPath) : null;
30+
parentCargoPath.Should().Be(expectedParentManifestpath);
31+
}
32+
33+
[Theory]
34+
[InlineData(@"not_a_project\src\main.rs", "not_a_project", false)]
35+
[InlineData(@"hello_library\src\lib.rs", "hello_library", false)]
36+
[InlineData(@"hello_library\Cargo.toml", "hello_library", false)]
37+
[InlineData(@"hello_workspace\main\src\main.rs", "hello_workspace", false)]
38+
[InlineData(@"hello_workspace\main\Cargo.toml", "hello_workspace", true)]
39+
[InlineData(@"workspace_with_example\lib\examples\eg1.rs", "workspace_with_example", true)]
40+
[InlineData(@"workspace_with_example\lib\examples\eg2\main.rs", "workspace_with_example", true)]
41+
[InlineData(@"workspace_with_example\lib\examples\eg2\utils.rs", "workspace_with_example", false)]
42+
[InlineData(@"does_not_exist\workspace_with_example\lib\examples\eg1.rs", "does_not_exist", false)]
43+
public void CanHaveExecutableTargetsTests(string relativePath, string relWorkspaceRoot, bool canHaveExecutableTargets)
44+
{
45+
var filePath = Path.Combine(TestHelpers.ThisTestRoot, relativePath);
46+
var workspaceRoot = Path.Combine(TestHelpers.ThisTestRoot, relWorkspaceRoot);
47+
48+
var res = filePath.CanHaveExecutableTargets(workspaceRoot);
49+
50+
res.Should().Be(canHaveExecutableTargets);
51+
}
52+
}

src/RustAnalyzer.TestAdapter.UnitTests/Cargo/ManifestTests.cs

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -93,25 +93,4 @@ public void GetTargetPathForProfileRelativeToPathTests(string manifestPath, stri
9393

9494
cargo.Targets.Single().GetPathRelativeTo("dev", Path.Combine(TestHelpers.ThisTestRoot, filePath)).Should().Be(ret);
9595
}
96-
97-
[Theory]
98-
[InlineData(@"not_a_project\src\main.rs", "not_a_project", @"not_a_project\Cargo.toml", false)]
99-
[InlineData(@"not_a_project\src", "not_a_project", @"not_a_project\Cargo.toml", false)]
100-
[InlineData(@"hello_library\src\lib.rs", "hello_library", @"hello_library\Cargo.toml", true)]
101-
[InlineData(@"hello_library\Cargo.toml", "hello_library", @"hello_library\Cargo.toml", true)]
102-
[InlineData(@"hello_workspace\main\src\main.rs", "hello_workspace", @"hello_workspace\main\Cargo.toml", true)]
103-
[InlineData(@"hello_workspace\main\src", "hello_workspace", @"hello_workspace\main\Cargo.toml", true)]
104-
[InlineData(@"hello_workspace\main\Cargo.toml", "hello_workspace", @"hello_workspace\main\Cargo.toml", true)]
105-
[InlineData(@"workspace_with_example\lib\examples\eg1.rs", "workspace_with_example", @"workspace_with_example\lib\Cargo.toml", true)]
106-
[InlineData(@"c:\workspace_with_example\lib\examples\eg1.rs", "workspace_with_example", null, false)]
107-
public void GetContainingManifestOrThisTests(string fileOrFolder, string workspaceRootx, string parentCargoRelPath, bool foundParentManifest)
108-
{
109-
string path = Path.Combine(TestHelpers.ThisTestRoot, fileOrFolder);
110-
var workspaceRoot = Path.Combine(TestHelpers.ThisTestRoot, workspaceRootx);
111-
var found = Manifest.TryGetParentManifestOrThisUnderWorkspace(workspaceRoot, path, out string parentCargoPath);
112-
113-
found.Should().Be(foundParentManifest);
114-
var expectedParentManifestpath = found ? Path.Combine(TestHelpers.ThisTestRoot, parentCargoRelPath) : null;
115-
parentCargoPath.Should().Be(expectedParentManifestpath);
116-
}
11796
}

src/RustAnalyzer.TestAdapter.UnitTests/RustAnalyzer.TestAdapter.UnitTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
</Compile>
4747
<Compile Include="Cargo\BuildJsonOutputParserTests.cs" />
4848
<Compile Include="Cargo\ExampleTargetTests.cs" />
49+
<Compile Include="Cargo\ManifestExtensionsTests.cs" />
4950
<Compile Include="Cargo\ManifestTests.cs" />
5051
<Compile Include="Cargo\TargetTests.cs" />
5152
<Compile Include="Properties\AssemblyInfo.cs" />

src/RustAnalyzer.TestAdapter/Cargo/ExampleTarget.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ private ExampleTarget(Manifest manifest, string name, string source)
1717
AdditionalBuildArgs = $"--example \"{name}\"";
1818
}
1919

20-
// TODO: DRY violation with is IsRustExample.
2120
public static IEnumerable<Target> GetAll(Manifest manifest)
2221
{
2322
var examplesFolder = Path.Combine(Path.GetDirectoryName(manifest.FullPath), "examples");

src/RustAnalyzer.TestAdapter/Cargo/Manifest.cs

Lines changed: 3 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@ private Manifest(string fullPath)
5858

5959
public bool Is(string filePath) => FullPath.Equals(filePath, StringComparison.OrdinalIgnoreCase);
6060

61-
public static Manifest Create(string parentCargoPath)
61+
public static Manifest Create(string filePath)
6262
{
6363
try
6464
{
65-
return new (parentCargoPath);
65+
return new (filePath);
6666
}
6767
catch
6868
{
@@ -71,44 +71,6 @@ public static Manifest Create(string parentCargoPath)
7171
}
7272
}
7373

74-
public static Manifest GetParentManifestOrThisUnderWorkspace(string workspaceRoot, string filePath)
75-
{
76-
if (TryGetParentManifestOrThisUnderWorkspace(workspaceRoot, filePath, out string parentManifestPath))
77-
{
78-
return Create(parentManifestPath);
79-
}
80-
81-
return null;
82-
}
83-
84-
public static bool TryGetParentManifestOrThisUnderWorkspace(string workspaceRoot, string fileOrFolderPath, out string parentCargoPath)
85-
{
86-
if (fileOrFolderPath.IsManifest())
87-
{
88-
parentCargoPath = fileOrFolderPath;
89-
return true;
90-
}
91-
92-
var currentPath = fileOrFolderPath;
93-
while (!currentPath.Equals(workspaceRoot, StringComparison.OrdinalIgnoreCase) && (currentPath = Path.GetDirectoryName(currentPath)) != null)
94-
{
95-
if (File.Exists(Path.Combine(currentPath, Constants.ManifestFileName)))
96-
{
97-
parentCargoPath = Path.Combine(currentPath, Constants.ManifestFileName);
98-
return true;
99-
}
100-
}
101-
102-
if (currentPath != null && File.Exists(Path.Combine(currentPath, Constants.ManifestFileName)))
103-
{
104-
parentCargoPath = Path.Combine(currentPath, Constants.ManifestFileName);
105-
return true;
106-
}
107-
108-
parentCargoPath = null;
109-
return false;
110-
}
111-
11274
public string GetPackageName()
11375
{
11476
Ensure.That(IsPackage, nameof(IsPackage)).IsTrue();
@@ -124,7 +86,7 @@ public string GetPackageName()
12486
private static string GetWorkspaceRoot(string fullPath)
12587
{
12688
var currentPath = fullPath;
127-
while (TryGetParentManifestOrThisUnderWorkspace(Path.GetDirectoryName(Path.GetDirectoryName(currentPath)), currentPath, out string parentCargoPath))
89+
while (currentPath.TryGetParentManifestOrThisUnderWorkspace(Path.GetDirectoryName(Path.GetDirectoryName(currentPath)), out string parentCargoPath))
12890
{
12991
var model = Toml.ToModel(File.ReadAllText(parentCargoPath));
13092
if (model.ContainsKey(KeyNameWorkspace))
Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
22
using System.IO;
3+
using System.Linq;
4+
using KS.RustAnalyzer.TestAdapter.Common;
35

46
namespace KS.RustAnalyzer.TestAdapter.Cargo;
57

@@ -11,26 +13,52 @@ public static bool IsManifest(this string @this)
1113
public static bool IsRustFile(this string @this)
1214
=> Path.GetExtension(@this).Equals(Constants.RustFileExtension, StringComparison.OrdinalIgnoreCase);
1315

14-
// TODO: unit test this.
15-
public static bool IsRustExample(this string @this)
16+
public static bool CanHaveExecutableTargets(this string @this, string workspaceRoot)
1617
{
17-
if (!@this.IsRustFile())
18+
if (!(@this.IsNotNullOrEmpty() && File.Exists(@this) && (@this.IsManifest() || @this.IsRustFile())))
1819
{
1920
return false;
2021
}
2122

22-
var parentDirName = Path.GetFileName(Path.GetDirectoryName(@this));
23-
if ("examples".Equals(parentDirName, StringComparison.OrdinalIgnoreCase))
23+
var manifest = @this.GetParentManifestOrThisUnderWorkspace(workspaceRoot);
24+
return manifest != null && manifest.Targets.Where(t => t.IsRunnable && t.Source.Equals(@this, StringComparison.OrdinalIgnoreCase)).Any();
25+
}
26+
27+
public static Manifest GetParentManifestOrThisUnderWorkspace(this string filePath, string workspaceRoot)
28+
{
29+
if (filePath.TryGetParentManifestOrThisUnderWorkspace(workspaceRoot, out string parentManifestPath))
30+
{
31+
return Manifest.Create(parentManifestPath);
32+
}
33+
34+
return null;
35+
}
36+
37+
public static bool TryGetParentManifestOrThisUnderWorkspace(this string fileOrFolderPath, string workspaceRoot, out string parentCargoPath)
38+
{
39+
if (fileOrFolderPath.IsManifest())
2440
{
41+
parentCargoPath = fileOrFolderPath;
2542
return true;
2643
}
2744

28-
var parentParentDirName = Path.GetFileName(Path.GetDirectoryName(parentDirName));
29-
if ("examples".Equals(parentParentDirName, StringComparison.OrdinalIgnoreCase))
45+
var currentPath = fileOrFolderPath;
46+
while (!currentPath.Equals(workspaceRoot, StringComparison.OrdinalIgnoreCase) && (currentPath = Path.GetDirectoryName(currentPath)) != null)
47+
{
48+
if (File.Exists(Path.Combine(currentPath, Constants.ManifestFileName)))
49+
{
50+
parentCargoPath = Path.Combine(currentPath, Constants.ManifestFileName);
51+
return true;
52+
}
53+
}
54+
55+
if (currentPath != null && File.Exists(Path.Combine(currentPath, Constants.ManifestFileName)))
3056
{
57+
parentCargoPath = Path.Combine(currentPath, Constants.ManifestFileName);
3158
return true;
3259
}
3360

61+
parentCargoPath = null;
3462
return false;
3563
}
3664
}

src/RustAnalyzer.TestAdapter/Constants.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace KS.RustAnalyzer.TestAdapter;
44

55
public static class Constants
66
{
7-
public const string ReleaseSummary = "Featuring ability to debug & run Cargo Examples from 'Select Startup Item' drop down & file context menu. NOTE: You may need to delete the '.vs' folder in your project.";
7+
public const string ReleaseSummary = "Right click on Cargo.toml or examples .rs files and set command line arguments for debugging.";
88

99
public const string RustLanguageContentType = "rust";
1010
public const string RustFileExtension = ".rs";

src/RustAnalyzer/VS/FileContextProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public Task<IReadOnlyCollection<FileContext>> GetContextsForFileAsync(string fil
3232

3333
public async Task<IReadOnlyCollection<FileContext>> GetContextsForFileAsync(string filePath, CancellationToken cancellationToken)
3434
{
35-
var parentManifest = Manifest.GetParentManifestOrThisUnderWorkspace(_workspaceRoot, filePath);
35+
var parentManifest = filePath.GetParentManifestOrThisUnderWorkspace(_workspaceRoot);
3636
if (parentManifest == null)
3737
{
3838
return await Task.FromResult(FileContext.EmptyFileContexts);

src/RustAnalyzer/VS/FileScanner.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public FileScanner(string workspaceRoot)
2323
public async Task<T> ScanContentAsync<T>(string filePath, CancellationToken cancellationToken)
2424
where T : class
2525
{
26-
var owningManifest = filePath.IsManifest() ? Manifest.Create(filePath) : Manifest.GetParentManifestOrThisUnderWorkspace(_workspaceRoot, filePath);
26+
var owningManifest = filePath.IsManifest() ? Manifest.Create(filePath) : filePath.GetParentManifestOrThisUnderWorkspace(_workspaceRoot);
2727
if (owningManifest == null)
2828
{
2929
return null;

src/RustAnalyzer/VS/NodeBrowseObjectProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public object ProvideBrowseObject(WorkspaceVisualNodeBase node)
4040
}
4141

4242
var relativePath = PathUtilities.MakeRelativePath(node.Workspace.Location, fsNode.FullPath);
43-
if (fsNode.FullPath.IsManifest() || fsNode.FullPath.IsRustExample())
43+
if (fsNode.FullPath.CanHaveExecutableTargets(node.Workspace.Location))
4444
{
4545
var cmdLineArgs = _settingsService.Get(SettingsService.KindDebugger, SettingsService.TypeCmdLineArgs, relativePath);
4646
_browseObject.ResetForNewNode(relativePath, cmdLineArgs);

0 commit comments

Comments
 (0)