Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
> Each `## [<version>]` 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
Expand Down
4 changes: 4 additions & 0 deletions docs/08-testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions docs/specs/test.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. 协议核心

Expand Down
3 changes: 3 additions & 0 deletions docs/zh/08-testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"`。
Expand Down
33 changes: 33 additions & 0 deletions src/build/plan.cppm
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> 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<std::filesystem::path, std::string_view> 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<std::filesystem::path> runtime_aliases_for_target(
const mcpp::manifest::Target& t,
const mcpp::toolchain::triple::ArtifactNaming& n) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
107 changes: 107 additions & 0 deletions tests/unit/test_test_targets.cpp
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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]
Expand Down