diff --git a/CHANGELOG.md b/CHANGELOG.md index ab53861f4..d0d8a9d21 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,13 @@ > Each `## []` section is that release's notes. Entries are written in English > from 2026.9.28.3 on; earlier entries remain as written. +## [Unreleased] + +### Fixed + +- Test planning refuses a test program whose executable would overwrite an executable target + from the same package. The diagnostic names both programs and their shared path (mcpp#1028). + ## [2026.11.27.1] - 2026-11-27 ### Changed diff --git a/docs/08-testing.md b/docs/08-testing.md index cf9917af9..431b4220b 100644 --- a/docs/08-testing.md +++ b/docs/08-testing.md @@ -73,6 +73,10 @@ the fixed directory, `mcpp test` notes how many stay programs of their own; when it forms no group at all, the note is a degradation, which `--strict` refuses (2026.11.13.1+). +Test planning refuses a test program whose executable would replace a program +built by the same package (@NEXT@+). The diagnostic names both programs and +the shared path. Rename the test program or the executable target. + `[test] programs = "package"` (2026.11.13.1+) makes every file the package discovers one program, named by the first glob's fixed directory (`tests`). At most one of its files defines `main`; two are refused, naming both. The program diff --git a/docs/specs/test.md b/docs/specs/test.md index 2d7869a42..e8a37d92e 100644 --- a/docs/specs/test.md +++ b/docs/specs/test.md @@ -56,6 +56,7 @@ `name` 相对于工作目录;`[target.<选择器>.test] files` 为该选择器选中的目标追加文件。工作目录的规则见 SPEC-002 §6.2.1 (mcpp >= 2026.11.17.1)。 7. `[test]` 中的其他键产生警告,在 `--strict` 下为错误。 +8. 若测试程序与同一包内的可执行目标产生相同路径,测试计划**必须**拒绝,并列出测试程序、可执行目标和共用路径(mcpp >= @NEXT@)。 ### B2. 协议核心 diff --git a/docs/zh/08-testing.md b/docs/zh/08-testing.md index c417c9f2d..5dfda1e6f 100644 --- a/docs/zh/08-testing.md +++ b/docs/zh/08-testing.md @@ -61,6 +61,9 @@ discover = ["checks/**/*.cpp", "!checks/fixtures/**"] 一次,后者正是由许多小文件组成的套件在构建上花费的时间。该设置留下直接位于固定目录中的文件时,`mcpp test` 给出一条 note,说明有多少文件仍各自是一个程序;它不形成任何组时,该说明是一次降级,`--strict` 拒绝(2026.11.13.1+)。 +若测试程序与同一包构建的可执行程序共用一个产物路径,测试计划会拒绝该配置(@NEXT@+)。 +诊断会列出两个程序及其共用路径。可重命名测试程序或可执行目标。 + `[test] programs = "package"`(2026.11.13.1+)使包所发现的全部文件成为一个程序,以第一个 glob 的固定目录命名(`tests`)。 其中定义 `main` 的文件至多一个,两个则被拒绝并点出两者。该程序仍按文件各起一个进程运行,以文件在固定目录之下的路径命名(`tests/unit/b.cpp` 为 `tests/unit/b`),隔离与并行 与每文件一个程序相同,整个套件只链接一次。三个值是测试程序的粒度:`"file"`(默认)、`"directory"` 与 `"package"`。 diff --git a/src/build/plan.cppm b/src/build/plan.cppm index d7440e9ad..df4257d55 100644 --- a/src/build/plan.cppm +++ b/src/build/plan.cppm @@ -1222,6 +1222,31 @@ std::filesystem::path target_output(const mcpp::manifest::Target& t, std::format("{}{}", t.name, n.exeSuffix); } +// Build and test select different targets, but share the product directory. +std::optional test_output_collision( + const mcpp::manifest::Manifest& manifest, + const mcpp::toolchain::triple::ArtifactNaming& naming, + const std::filesystem::path& productDir, + bool applicationsAreExecutables, + const auto& placeProgram) { + using Target = mcpp::manifest::Target; + std::map programs; + for (const auto& t : manifest.targets) { + if (t.kind == Target::Binary || (t.kind == Target::Application && applicationsAreExecutables)) + programs.emplace(placeProgram(target_output(t, naming)).lexically_normal(), t.name); + } + for (const auto& t : manifest.targets) { + if (t.kind != Target::TestBinary) continue; + const auto path = productDir / target_output(t, naming).lexically_relative("bin"); + if (const auto it = programs.find(path.lexically_normal()); it != programs.end()) + return std::format( + "[test-output-collision] package '{}': test program '{}' ({}) and executable target '{}' " + "both write '{}'. Rename the test program or the executable target", + manifest.package.name, t.name, t.main, it->second, path.generic_string()); + } + return std::nullopt; +} + std::vector runtime_aliases_for_target( const mcpp::manifest::Target& t, const mcpp::toolchain::triple::ArtifactNaming& n) { @@ -3935,6 +3960,11 @@ make_plan(const mcpp::manifest::Manifest& manifest, for (auto& t : manifest.targets) { if (t.kind == mcpp::manifest::Target::TestBinary) { inTestMode = true; break; } } + const bool applicationsAreExecutables = mcpp::toolchain::triple::application_form(targetTriple) + == mcpp::toolchain::triple::ApplicationForm::Executable; + if (inTestMode) + if (auto why = test_output_collision(manifest, naming, "bin", applicationsAreExecutables, std::identity{})) + return std::unexpected(*why); for (auto& t : manifest.targets) { if (inTestMode && t.kind != mcpp::manifest::Target::TestBinary) continue; // An entry target is the beginning of other programs, linked into @@ -4505,6 +4535,9 @@ make_plan(const mcpp::manifest::Manifest& manifest, bool memberTestMode = std::ranges::any_of(pkg.manifest.targets, [](auto const& t) { return t.kind == mcpp::manifest::Target::TestBinary; }); + if (memberTestMode) + if (auto why = test_output_collision(pkg.manifest, naming, productDir, applicationsAreExecutables, place)) + return std::unexpected(*why); const auto memberExtTable = mcpp::extension_table_for( pkg.manifest.buildConfig.moduleExtensions, pkg.manifest.buildConfig.deviceExtensions); diff --git a/tests/unit/test_test_targets.cpp b/tests/unit/test_test_targets.cpp index e7a5d2cf6..7c6180276 100644 --- a/tests/unit/test_test_targets.cpp +++ b/tests/unit/test_test_targets.cpp @@ -1,7 +1,13 @@ import std; import mcpplibs.mtest; +import mcpp.build.plan; import mcpp.build.test_targets; +import mcpp.manifest; +import mcpp.modgraph.graph; +import mcpp.modgraph.scanner; +import mcpp.project; +import mcpp.toolchain.model; using namespace mcpplibs::mtest; @@ -47,6 +53,107 @@ version = "0.1.0" expect(eq(std::filesystem::path(result->targets[0].main), std::filesystem::path("tests") / "tagged" / "nested" / "smoke.cpp")); }}; +UTest a_test_directory_cannot_replace_the_packages_executable{"TestTargets/ATestDirectoryCannotReplaceThePackagesExecutable", [] { + Tmp tmp; + write_manifest(tmp.path, R"([package] +name = "collide" +version = "0.1.0" + +[targets.collide] +kind = "bin" +main = "src/main.cpp" + +[test] +programs = "directory" +)"); + write_file(tmp.path / "src/main.cpp", "int main() { return 0; }\n"); + write_file(tmp.path / "tests/collide/one_test.cpp", "int main() { return 0; }\n"); + + auto project = mcpp::project::load_effective_manifest(tmp.path); + if (auto&& failed = expect(fatal(project.has_value())); !failed) failed << project.error(); + auto discovered = mcpp::build::discover_test_targets(tmp.path, {}); + if (auto&& failed = expect(fatal(discovered.has_value())); !failed) failed << discovered.error(); + auto& manifest = project->manifest; + manifest.targets.insert(manifest.targets.end(), discovered->targets.begin(), discovered->targets.end()); + mcpp::modgraph::PackageRoot root; + root.root = tmp.path; + root.manifest = manifest; + + mcpp::toolchain::Toolchain toolchain; + toolchain.compiler = mcpp::toolchain::CompilerId::Clang; + for (const auto& [triple, binary] : { + std::pair{"aarch64-apple-darwin", "bin/collide"}, + std::pair{"x86_64-w64-mingw32", "bin/collide.exe"}}) { + toolchain.targetTriple = triple; + const auto plan = mcpp::build::make_plan(manifest, toolchain, {}, {}, {}, {root}, + tmp.path, tmp.path / "target", {}, {}); + if (auto&& failed = expect(fatal(!plan.has_value())); !failed) failed << triple; + const auto& error = plan.error(); + expect(error.contains("[test-output-collision]")) << error; + expect(error.contains("test program 'collide'") && error.contains("executable target 'collide'")) << error; + expect(error.contains(binary)) << error; + } +}}; + +UTest member_test_paths_keep_their_directories{"TestTargets/MemberTestPathsKeepTheirDirectories", [] { + Tmp tmp; + write_manifest(tmp.path, R"([workspace] +members = ["member"] +)"); + const auto memberDir = tmp.path / "member"; + write_manifest(memberDir, R"([package] +name = "member" +version = "0.1.0" + +[targets."nested/tool"] +kind = "bin" +main = "src/main.cpp" + +[test] +programs = "directory" +)"); + write_file(memberDir / "src/main.cpp", "int main() { return 0; }\n"); + write_file(memberDir / "tests/tool/main.cpp", "int main() { return 0; }\n"); + write_file(memberDir / "tests/nested/tool/main.cpp", "int main() { return 0; }\n"); + + auto workspace = mcpp::project::load_effective_manifest(tmp.path); + if (auto&& failed = expect(fatal(workspace.has_value())); !failed) failed << workspace.error(); + auto project = mcpp::project::load_effective_manifest(memberDir); + if (auto&& failed = expect(fatal(project.has_value())); !failed) failed << project.error(); + auto discovered = mcpp::build::discover_test_targets(tmp.path, "member"); + if (auto&& failed = expect(fatal(discovered.has_value())); !failed) failed << discovered.error(); + + mcpp::modgraph::PackageRoot root; + root.root = tmp.path; + root.manifest = mcpp::project::virtual_workspace_root(workspace->manifest, project->manifest, tmp.path); + mcpp::modgraph::PackageRoot member; + member.root = memberDir; + member.selectedMember = true; + member.memberProducts = "member"; + mcpp::toolchain::Toolchain toolchain; + toolchain.compiler = mcpp::toolchain::CompilerId::Clang; + toolchain.targetTriple = "aarch64-apple-darwin"; + + for (const auto& [name, collides] : {std::pair{"nested/tool", false}, std::pair{"tool", true}}) { + const auto test = std::ranges::find(discovered->targets, name, &mcpp::manifest::Target::name); + if (auto&& failed = expect(fatal(test != discovered->targets.end())); !failed) failed << name; + member.manifest = project->manifest; + member.manifest.targets.push_back(*test); + const auto plan = mcpp::build::make_plan(root.manifest, toolchain, {}, {}, {}, {root, member}, + tmp.path, tmp.path / "target", {}, {}); + if (!collides) { + if (auto&& failed = expect(fatal(plan.has_value())); !failed) failed << plan.error(); + expect(fatal(eq(plan->linkUnits.size(), 1u))); + expect(eq(plan->linkUnits.front().output.generic_string(), "bin/member/nested/tool")); + } else { + if (auto&& failed = expect(fatal(!plan.has_value())); !failed) failed << name; + expect(plan.error().contains("[test-output-collision]")) << plan.error(); + expect(plan.error().contains("executable target 'nested/tool'")) << plan.error(); + expect(plan.error().contains("'bin/member/tool'")) << plan.error(); + } + } +}}; + UTest glob_flags_become_per_target_flags{"TestTargets/GlobFlagsBecomePerTargetFlags", [] { Tmp tmp; write_manifest(tmp.path, R"([package]