дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-05 17:47:08 +02:00
Run configuration detection and its .sln / .slnx reader only accepted .csproj, so F# and Visual Basic projects got no run configurations. .fsproj and .vbproj now follow the C# rules. Visual Basic keeps launch profiles in My Project/launchSettings.json, where dotnet run looks, so profiles and the gutter run icons in that file read it. The real dotnet CLI test is ignored by default (no SDK in CI). Closes #7631 Co-authored-by: wildcatDownstairs <[email protected]>
287 рядки
15 KiB
Rust
287 рядки
15 KiB
Rust
use ide_run::{
|
|
detect,
|
|
dotnet::{parse_profiles, parse_project, resolve, solution_projects},
|
|
};
|
|
use std::fs;
|
|
use std::path::PathBuf;
|
|
|
|
const CONSOLE: &str = r#"<Project Sdk="Microsoft.NET.Sdk"><PropertyGroup><OutputType>Exe</OutputType><TargetFrameworks>net10.0; net9.0</TargetFrameworks></PropertyGroup></Project>"#;
|
|
const PROFILES: &str = r#"{
|
|
// Only Project profiles can be run with dotnet.
|
|
"profiles": {
|
|
"http": {"commandName":"Project", "applicationUrl":"http://localhost:5165", "commandLineArgs":"--message \"hello world\"", "workingDirectory":"work", "environmentVariables":{"DEMO":"profile"}},
|
|
"https": {"commandName":"Project", "environmentVariables":{"DEMO":"https"}},
|
|
"IIS Express": {"commandName":"IISExpress"},
|
|
"external": {"commandName":"Executable"},
|
|
"broken": {"commandName":"Project", "environmentVariables":7}
|
|
}
|
|
}"#;
|
|
|
|
fn write(root: &std::path::Path, file: &str, src: &str) {
|
|
let path = root.join(file);
|
|
fs::create_dir_all(path.parent().unwrap()).unwrap();
|
|
fs::write(path, src).unwrap();
|
|
}
|
|
|
|
#[test]
|
|
fn project_properties_and_sdk_defaults() {
|
|
let p = parse_project(CONSOLE).unwrap();
|
|
assert!(p.executable && !p.test);
|
|
assert_eq!(p.frameworks, ["net10.0", "net9.0"]);
|
|
for (sdk, output, executable) in [
|
|
("Microsoft.NET.Sdk", "", false),
|
|
("Microsoft.NET.Sdk", "<OutputType>WinExe</OutputType>", true),
|
|
("Microsoft.NET.Sdk.Web", "", true),
|
|
("Microsoft.NET.Sdk.Web", "<OutputType>Library</OutputType>", false),
|
|
] {
|
|
let xml = format!(
|
|
r#"<Project Sdk="{sdk}"><PropertyGroup>{output}<TargetFramework>net10.0</TargetFramework></PropertyGroup></Project>"#
|
|
);
|
|
let p = parse_project(&xml).unwrap();
|
|
assert_eq!(p.executable, executable, "{xml}");
|
|
assert_eq!(p.frameworks, ["net10.0"]);
|
|
}
|
|
let p = parse_project(r#"<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"><Sdk Name="Microsoft.NET.Sdk.Web"/><PropertyGroup><TargetFramework>net10.0</TargetFramework></PropertyGroup><PropertyGroup Condition="'$(Configuration)'=='Release'"><OutputType>Library</OutputType></PropertyGroup><ItemGroup><PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.0" /></ItemGroup></Project>"#).unwrap();
|
|
assert!(p.executable && p.test);
|
|
assert!(parse_project("<Project><PropertyGroup>").is_none());
|
|
assert!(parse_project("garbage").is_none());
|
|
assert!(
|
|
parse_project(
|
|
r#"<Project><ItemGroup><PackageReference Update="microsoft.net.test.sdk" /></ItemGroup></Project>"#
|
|
)
|
|
.unwrap()
|
|
.test
|
|
);
|
|
assert!(
|
|
!parse_project(r#"<Project><!-- <PackageReference Include="Microsoft.NET.Test.Sdk"/> --></Project>"#)
|
|
.unwrap()
|
|
.test
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn testing_platform_projects_are_test_projects_without_the_test_sdk() {
|
|
let project = |body: &str| {
|
|
let xml = format!(
|
|
r#"<Project Sdk="Microsoft.NET.Sdk"><PropertyGroup><OutputType>Exe</OutputType><TargetFramework>net11.0</TargetFramework></PropertyGroup>{body}</Project>"#
|
|
);
|
|
parse_project(&xml).unwrap()
|
|
};
|
|
// xUnit v3 on Microsoft.Testing.Platform: an executable with no Microsoft.NET.Test.Sdk.
|
|
let xunit_v3 = project(
|
|
r#"<PropertyGroup><UseMicrosoftTestingPlatformRunner>true</UseMicrosoftTestingPlatformRunner></PropertyGroup><ItemGroup><PackageReference Include="xunit.v3" Version="3.2.0" /></ItemGroup>"#,
|
|
);
|
|
assert!(xunit_v3.executable && xunit_v3.test);
|
|
for body in [
|
|
r#"<ItemGroup><PackageReference Include="xunit.v3" /></ItemGroup>"#,
|
|
r#"<ItemGroup><PackageReference Include="xunit.v3.mtp-v2" /></ItemGroup>"#,
|
|
r#"<ItemGroup><PackageReference Include="TUnit" /></ItemGroup>"#,
|
|
r#"<ItemGroup><PackageReference Include="MSTest" /></ItemGroup>"#,
|
|
r#"<PropertyGroup><IsTestProject>true</IsTestProject></PropertyGroup>"#,
|
|
r#"<PropertyGroup><IsTestingPlatformApplication>true</IsTestingPlatformApplication></PropertyGroup>"#,
|
|
r#"<PropertyGroup><EnableMSTestRunner>true</EnableMSTestRunner></PropertyGroup>"#,
|
|
] {
|
|
assert!(project(body).test, "{body}");
|
|
}
|
|
let mstest_sdk = parse_project(r#"<Project Sdk="MSTest.Sdk/3.8.0"><PropertyGroup><TargetFramework>net10.0</TargetFramework></PropertyGroup></Project>"#).unwrap();
|
|
assert!(mstest_sdk.test);
|
|
// A project that opts out, a library of test helpers, and a framework's companion package.
|
|
for body in [
|
|
r#"<PropertyGroup><IsTestProject>false</IsTestProject></PropertyGroup><ItemGroup><PackageReference Include="xunit.v3" /></ItemGroup>"#,
|
|
r#"<ItemGroup><PackageReference Include="xunit.v3.assert" /></ItemGroup>"#,
|
|
r#"<PropertyGroup Condition="'$(CI)'=='true'"><IsTestProject>true</IsTestProject></PropertyGroup>"#,
|
|
] {
|
|
assert!(!project(body).test, "{body}");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn profiles_filter_and_preserve_launch_properties() {
|
|
let p = parse_profiles(PROFILES);
|
|
assert_eq!(p.keys().map(String::as_str).collect::<Vec<_>>(), ["http", "https"]);
|
|
assert_eq!(p["http"].application_url, "http://localhost:5165");
|
|
assert_eq!(p["http"].environment_variables["DEMO"], "profile");
|
|
assert_eq!(p["http"].command_line_args, "--message \"hello world\"");
|
|
assert_eq!(p["http"].working_directory, "work");
|
|
assert!(parse_profiles("invalid").is_empty());
|
|
// Visual Studio saves launchSettings.json with a byte order mark.
|
|
assert_eq!(parse_profiles(&format!("\u{feff}{PROFILES}")).len(), 2);
|
|
}
|
|
|
|
#[test]
|
|
fn solution_paths_support_windows_separators_folders_and_entities() {
|
|
let sln = "Project(\"{guid}\") = \"App\", \"src\\App\\App.csproj\", \"{guid}\"\nEndProject\nProject(\"{folder}\") = \"Folder\", \"Folder\", \"{guid}\"\nProject(\"{guid}\") = \"External\", \"..\\other\\Other.csproj\", \"{guid}\"";
|
|
assert_eq!(
|
|
solution_projects(sln, false),
|
|
[PathBuf::from("src/App/App.csproj"), PathBuf::from("../other/Other.csproj")]
|
|
);
|
|
let slnx = r#"<Solution><Folder Name="/src/"><Project Path="src\A&B\App.csproj" /></Folder><Project Path="other.vbproj" /></Solution>"#;
|
|
assert_eq!(solution_projects(slnx, true), [PathBuf::from("src/A&B/App.csproj"), PathBuf::from("other.vbproj")]);
|
|
// F# and Visual Basic projects next to C#; other project kinds are left out.
|
|
let mixed = "Project(\"{cs}\") = \"A\", \"A\\A.csproj\", \"{1}\"\nEndProject\nProject(\"{fs}\") = \"B\", \"B\\B.FSPROJ\", \"{2}\"\nEndProject\nProject(\"{vb}\") = \"C\", \"C\\C.vbproj\", \"{3}\"\nEndProject\nProject(\"{cpp}\") = \"D\", \"D\\D.vcxproj\", \"{4}\"";
|
|
assert_eq!(solution_projects(mixed, false), ["A/A.csproj", "B/B.FSPROJ", "C/C.vbproj"].map(PathBuf::from));
|
|
assert!(solution_projects("<Solution><Project", true).is_empty());
|
|
}
|
|
|
|
#[test]
|
|
fn detection_deduplicates_projects_and_generates_commands() {
|
|
let temp = tempfile::tempdir().unwrap();
|
|
let root = temp.path();
|
|
write(root, "src/App/App.csproj", CONSOLE);
|
|
write(root, "src/App/Properties/launchSettings.json", PROFILES);
|
|
write(
|
|
root,
|
|
"demo.slnx",
|
|
r#"<Solution><Project Path="src/App/App.csproj"/><Project Path="../external/Missing.csproj"/></Solution>"#,
|
|
);
|
|
write(
|
|
root,
|
|
"src/Test/Test.csproj",
|
|
r#"<Project Sdk="Microsoft.NET.Sdk"><PropertyGroup><TargetFramework>net10.0</TargetFramework></PropertyGroup><ItemGroup><PackageReference Include="Microsoft.NET.Test.Sdk"/></ItemGroup></Project>"#,
|
|
);
|
|
write(root, "bin/Ignored.csproj", CONSOLE);
|
|
write(root, "obj/Ignored.csproj", CONSOLE);
|
|
write(root, "node_modules/Ignored.csproj", CONSOLE);
|
|
write(root, "Lib.csproj", "<Project Sdk=\"Microsoft.NET.Sdk\" />");
|
|
let configs = detect(root);
|
|
assert_eq!(configs.len(), 4);
|
|
let app = configs.iter().find(|c| c.extras.params.get("launchProfile").is_some_and(|p| p == "http")).unwrap();
|
|
assert_eq!(
|
|
app.args,
|
|
["run", "--project", "src/App/App.csproj", "--framework", "net10.0", "--launch-profile", "http"]
|
|
);
|
|
assert_eq!(app.debug_adapter.as_deref(), Some("netcoredbg"));
|
|
assert_eq!(app.extras.before_launch[0].kind, "dotnetBuild");
|
|
let test = configs.iter().find(|c| c.is_test).unwrap();
|
|
assert_eq!(test.kind, "dotnet-test");
|
|
assert_eq!(test.args, ["test", "src/Test/Test.csproj", "--framework", "net10.0"]);
|
|
assert!(test.debug_adapter.is_none());
|
|
let mut edited = app.clone();
|
|
edited.env.insert("DEMO".into(), "override".into());
|
|
let resolved = resolve(root, edited.clone()).unwrap();
|
|
assert_eq!(resolved.env["DEMO"], "override");
|
|
assert_eq!(resolved.env["ASPNETCORE_URLS"], "http://localhost:5165");
|
|
assert!(resolved.cwd.unwrap().ends_with("src/App/work"));
|
|
edited.extras.params.insert("launchProfile".into(), "https".into());
|
|
let resolved = resolve(root, edited).unwrap();
|
|
assert!(!resolved.env.contains_key("ASPNETCORE_URLS"));
|
|
assert!(resolved.cwd.unwrap().ends_with("src/App"));
|
|
}
|
|
|
|
#[test]
|
|
fn solution_references_can_reach_projects_outside_the_open_directory() {
|
|
let temp = tempfile::tempdir().unwrap();
|
|
write(temp.path(), "external/App.csproj", CONSOLE);
|
|
write(temp.path(), "open/demo.sln", "Project(\"{guid}\") = \"App\", \"..\\external\\App.csproj\", \"{guid}\"");
|
|
let configs = detect(&temp.path().join("open"));
|
|
assert_eq!(configs.len(), 1);
|
|
assert!(configs[0].args[2].ends_with("external/App.csproj"));
|
|
}
|
|
|
|
#[test]
|
|
fn an_xunit_v3_testing_platform_project_gets_a_test_configuration() {
|
|
let temp = tempfile::tempdir().unwrap();
|
|
let root = temp.path();
|
|
write(
|
|
root,
|
|
"tests/App.Tests/App.Tests.csproj",
|
|
r#"<Project Sdk="Microsoft.NET.Sdk"><PropertyGroup><OutputType>Exe</OutputType><TargetFramework>net11.0</TargetFramework><UseMicrosoftTestingPlatformRunner>true</UseMicrosoftTestingPlatformRunner></PropertyGroup><ItemGroup><PackageReference Include="xunit.v3" Version="3.2.0" /></ItemGroup></Project>"#,
|
|
);
|
|
let configs = detect(root);
|
|
assert_eq!(configs.len(), 1, "a test project is not also run as an application");
|
|
assert_eq!(configs[0].kind, "dotnet-test");
|
|
assert!(configs[0].is_test);
|
|
assert_eq!(configs[0].extras.params["project"], "tests/App.Tests/App.Tests.csproj");
|
|
assert_eq!(configs[0].args, ["test", "tests/App.Tests/App.Tests.csproj", "--framework", "net11.0"]);
|
|
}
|
|
|
|
/// The project files `dotnet new` writes for F# and Visual Basic (SDK 11), by kind.
|
|
fn templates(lang: &str) -> [(&'static str, String, bool, bool); 5] {
|
|
let exe = r#"<Project Sdk="Microsoft.NET.Sdk"><PropertyGroup><OutputType>Exe</OutputType><TargetFramework>net11.0</TargetFramework></PropertyGroup></Project>"#;
|
|
let winexe = r#"<Project Sdk="Microsoft.NET.Sdk"><PropertyGroup><OutputType>WinExe</OutputType><TargetFrameworks>net11.0;net10.0</TargetFrameworks></PropertyGroup></Project>"#;
|
|
let library = r#"<Project Sdk="Microsoft.NET.Sdk"><PropertyGroup><TargetFramework>net11.0</TargetFramework></PropertyGroup></Project>"#;
|
|
let web = r#"<Project Sdk="Microsoft.NET.Sdk.Web"><PropertyGroup><TargetFramework>net11.0</TargetFramework></PropertyGroup></Project>"#;
|
|
let test = format!(
|
|
r#"<Project Sdk="Microsoft.NET.Sdk"><PropertyGroup><TargetFramework>net11.0</TargetFramework><IsPackable>false</IsPackable></PropertyGroup><ItemGroup><Compile Include="{lang}"/></ItemGroup><ItemGroup><PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1"/><PackageReference Include="xunit" Version="2.9.3"/></ItemGroup></Project>"#
|
|
);
|
|
[
|
|
("Exe", exe.into(), true, false),
|
|
("WinExe", winexe.into(), true, false),
|
|
("Library", library.into(), false, false),
|
|
("Web", web.into(), true, false),
|
|
("Test", test, false, true),
|
|
]
|
|
}
|
|
|
|
#[test]
|
|
fn fsharp_and_vb_projects_follow_the_csharp_rules() {
|
|
for ext in ["fsproj", "vbproj"] {
|
|
let temp = tempfile::tempdir().unwrap();
|
|
let root = temp.path();
|
|
for (kind, xml, _, _) in templates(ext) {
|
|
write(root, &format!("{kind}/{kind}.{ext}"), &xml);
|
|
}
|
|
// Not a project file.
|
|
write(root, &format!("Exe/Exe.{ext}.user"), "<Project/>");
|
|
let configs = detect(root);
|
|
for (kind, _, run, test) in templates(ext) {
|
|
let own: Vec<_> =
|
|
configs.iter().filter(|c| c.extras.params["project"] == format!("{kind}/{kind}.{ext}")).collect();
|
|
assert_eq!(own.len(), usize::from(run || test), "{ext} {kind}");
|
|
for c in own {
|
|
assert_eq!(c.kind, if test { "dotnet-test" } else { "dotnet" }, "{ext} {kind}");
|
|
assert_eq!(c.debug_adapter.is_some(), run, "{ext} {kind}");
|
|
}
|
|
}
|
|
let multi = configs.iter().find(|c| c.name == "WinExe (.NET Project)").unwrap();
|
|
assert_eq!(multi.extras.params["frameworks"], "net11.0;net10.0");
|
|
assert_eq!(multi.args[3..5], ["--framework", "net11.0"]);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn launch_profiles_are_read_where_dotnet_run_reads_them() {
|
|
let temp = tempfile::tempdir().unwrap();
|
|
let root = temp.path();
|
|
let exe = templates("x")[0].1.clone();
|
|
for (project, folder) in [("f/F.fsproj", "f/Properties"), ("v/V.vbproj", "v/My Project")] {
|
|
write(root, project, &exe);
|
|
write(root, &format!("{folder}/launchSettings.json"), PROFILES);
|
|
}
|
|
// `dotnet run` ignores a Visual Basic project's `Properties` folder.
|
|
write(root, "w/W.vbproj", &exe);
|
|
write(root, "w/Properties/launchSettings.json", PROFILES);
|
|
let configs = detect(root);
|
|
let names: Vec<_> = configs.iter().map(|c| c.name.as_str()).collect();
|
|
assert_eq!(names.iter().filter(|n| !n.starts_with("W")).count(), 6, "the default and two profiles each: {names:?}");
|
|
assert_eq!(names.iter().filter(|n| n.starts_with("W")).count(), 1, "{names:?}");
|
|
for project in ["f/F.fsproj", "v/V.vbproj"] {
|
|
let config = configs
|
|
.iter()
|
|
.find(|c| c.extras.params["project"] == project && c.extras.params["launchProfile"] == "http")
|
|
.unwrap();
|
|
let resolved = resolve(root, config.clone()).unwrap();
|
|
assert_eq!(resolved.env["DEMO"], "profile", "{project}");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn mixed_solutions_with_spaces_and_non_ascii_paths() {
|
|
let temp = tempfile::tempdir().unwrap();
|
|
let root = temp.path().join("open");
|
|
let exe = templates("x")[0].1.clone();
|
|
for project in ["我的 应用/应用.fsproj", "Ünï code/Vb App.vbproj", "cs/Cs.csproj"] {
|
|
write(temp.path(), &format!("outside/{project}"), &exe);
|
|
}
|
|
write(
|
|
&root,
|
|
"all.slnx",
|
|
r#"<Solution><Folder Name="/src/"><Project Path="../outside/我的 应用/应用.fsproj"/></Folder><Project Path="..\outside\cs\Cs.csproj"/></Solution>"#,
|
|
);
|
|
write(&root, "vb.sln", "Project(\"{vb}\") = \"Vb App\", \"..\\outside\\Ünï code\\Vb App.vbproj\", \"{1}\"");
|
|
let configs = detect(&root);
|
|
let mut names: Vec<_> = configs.iter().map(|c| c.name.as_str()).collect();
|
|
names.sort_unstable();
|
|
assert_eq!(names, ["Cs (.NET Project)", "Vb App (.NET Project)", "应用 (.NET Project)"]);
|
|
}
|