Skip to content

Commit 241a836

Browse files
committed
Reject test programs that would overwrite executables
1 parent 11824df commit 241a836

6 files changed

Lines changed: 155 additions & 0 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44
> Each `## [<version>]` section is that release's notes. Entries are written in English
55
> from 2026.9.28.3 on; earlier entries remain as written.
66

7+
## [Unreleased]
8+
9+
### Fixed
10+
11+
- Test planning refuses a test program whose executable would overwrite an executable target
12+
from the same package. The diagnostic names both programs and their shared path (mcpp#1028).
13+
714
## [2026.11.27.1] - 2026-11-27
815

916
### Changed

‎docs/08-testing.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ the fixed directory, `mcpp test` notes how many stay programs of their own; when
7373
it forms no group at all, the note is a degradation, which `--strict` refuses
7474
(2026.11.13.1+).
7575

76+
Test planning refuses a test program whose executable would replace a program
77+
built by the same package (@NEXT@+). The diagnostic names both programs and
78+
the shared path. Rename the test program or the executable target.
79+
7680
`[test] programs = "package"` (2026.11.13.1+) makes every file the package
7781
discovers one program, named by the first glob's fixed directory (`tests`). At
7882
most one of its files defines `main`; two are refused, naming both. The program

‎docs/specs/test.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
`name` 相对于工作目录;`[target.<选择器>.test] files` 为该选择器选中的目标追加文件。工作目录的规则见 SPEC-002 §6.2.1
5757
(mcpp >= 2026.11.17.1)。
5858
7. `[test]` 中的其他键产生警告,在 `--strict` 下为错误。
59+
8. 若测试程序与同一包内的可执行目标产生相同路径,测试计划**必须**拒绝,并列出测试程序、可执行目标和共用路径(mcpp >= @NEXT@)。
5960

6061
### B2. 协议核心
6162

‎docs/zh/08-testing.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ discover = ["checks/**/*.cpp", "!checks/fixtures/**"]
6161
一次,后者正是由许多小文件组成的套件在构建上花费的时间。该设置留下直接位于固定目录中的文件时,`mcpp test` 给出一条
6262
note,说明有多少文件仍各自是一个程序;它不形成任何组时,该说明是一次降级,`--strict` 拒绝(2026.11.13.1+)。
6363

64+
若测试程序与同一包构建的可执行程序共用一个产物路径,测试计划会拒绝该配置(@NEXT@+)。
65+
诊断会列出两个程序及其共用路径。可重命名测试程序或可执行目标。
66+
6467
`[test] programs = "package"`(2026.11.13.1+)使包所发现的全部文件成为一个程序,以第一个 glob 的固定目录命名(`tests`)。
6568
其中定义 `main` 的文件至多一个,两个则被拒绝并点出两者。该程序仍按文件各起一个进程运行,以文件在固定目录之下的路径命名(`tests/unit/b.cpp` 为 `tests/unit/b`),隔离与并行
6669
与每文件一个程序相同,整个套件只链接一次。三个值是测试程序的粒度:`"file"`(默认)、`"directory"` 与 `"package"`。

‎src/build/plan.cppm‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1222,6 +1222,31 @@ std::filesystem::path target_output(const mcpp::manifest::Target& t,
12221222
std::format("{}{}", t.name, n.exeSuffix);
12231223
}
12241224

1225+
// Build and test select different targets, but share the product directory.
1226+
std::optional<std::string> test_output_collision(
1227+
const mcpp::manifest::Manifest& manifest,
1228+
const mcpp::toolchain::triple::ArtifactNaming& naming,
1229+
const std::filesystem::path& productDir,
1230+
bool applicationsAreExecutables,
1231+
const auto& placeProgram) {
1232+
using Target = mcpp::manifest::Target;
1233+
std::map<std::filesystem::path, std::string_view> programs;
1234+
for (const auto& t : manifest.targets) {
1235+
if (t.kind == Target::Binary || (t.kind == Target::Application && applicationsAreExecutables))
1236+
programs.emplace(placeProgram(target_output(t, naming)).lexically_normal(), t.name);
1237+
}
1238+
for (const auto& t : manifest.targets) {
1239+
if (t.kind != Target::TestBinary) continue;
1240+
const auto path = productDir / target_output(t, naming).lexically_relative("bin");
1241+
if (const auto it = programs.find(path.lexically_normal()); it != programs.end())
1242+
return std::format(
1243+
"[test-output-collision] package '{}': test program '{}' ({}) and executable target '{}' "
1244+
"both write '{}'. Rename the test program or the executable target",
1245+
manifest.package.name, t.name, t.main, it->second, path.generic_string());
1246+
}
1247+
return std::nullopt;
1248+
}
1249+
12251250
std::vector<std::filesystem::path> runtime_aliases_for_target(
12261251
const mcpp::manifest::Target& t,
12271252
const mcpp::toolchain::triple::ArtifactNaming& n) {
@@ -3935,6 +3960,11 @@ make_plan(const mcpp::manifest::Manifest& manifest,
39353960
for (auto& t : manifest.targets) {
39363961
if (t.kind == mcpp::manifest::Target::TestBinary) { inTestMode = true; break; }
39373962
}
3963+
const bool applicationsAreExecutables = mcpp::toolchain::triple::application_form(targetTriple)
3964+
== mcpp::toolchain::triple::ApplicationForm::Executable;
3965+
if (inTestMode)
3966+
if (auto why = test_output_collision(manifest, naming, "bin", applicationsAreExecutables, std::identity{}))
3967+
return std::unexpected(*why);
39383968
for (auto& t : manifest.targets) {
39393969
if (inTestMode && t.kind != mcpp::manifest::Target::TestBinary) continue;
39403970
// An entry target is the beginning of other programs, linked into
@@ -4505,6 +4535,9 @@ make_plan(const mcpp::manifest::Manifest& manifest,
45054535
bool memberTestMode = std::ranges::any_of(pkg.manifest.targets, [](auto const& t) {
45064536
return t.kind == mcpp::manifest::Target::TestBinary;
45074537
});
4538+
if (memberTestMode)
4539+
if (auto why = test_output_collision(pkg.manifest, naming, productDir, applicationsAreExecutables, place))
4540+
return std::unexpected(*why);
45084541
const auto memberExtTable = mcpp::extension_table_for(
45094542
pkg.manifest.buildConfig.moduleExtensions,
45104543
pkg.manifest.buildConfig.deviceExtensions);

‎tests/unit/test_test_targets.cpp‎

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11

22
import std;
33
import mcpplibs.mtest;
4+
import mcpp.build.plan;
45
import mcpp.build.test_targets;
6+
import mcpp.manifest;
7+
import mcpp.modgraph.graph;
8+
import mcpp.modgraph.scanner;
9+
import mcpp.project;
10+
import mcpp.toolchain.model;
511

612
using namespace mcpplibs::mtest;
713

@@ -47,6 +53,107 @@ version = "0.1.0"
4753
expect(eq(std::filesystem::path(result->targets[0].main), std::filesystem::path("tests") / "tagged" / "nested" / "smoke.cpp"));
4854
}};
4955

56+
UTest a_test_directory_cannot_replace_the_packages_executable{"TestTargets/ATestDirectoryCannotReplaceThePackagesExecutable", [] {
57+
Tmp tmp;
58+
write_manifest(tmp.path, R"([package]
59+
name = "collide"
60+
version = "0.1.0"
61+
62+
[targets.collide]
63+
kind = "bin"
64+
main = "src/main.cpp"
65+
66+
[test]
67+
programs = "directory"
68+
)");
69+
write_file(tmp.path / "src/main.cpp", "int main() { return 0; }\n");
70+
write_file(tmp.path / "tests/collide/one_test.cpp", "int main() { return 0; }\n");
71+
72+
auto project = mcpp::project::load_effective_manifest(tmp.path);
73+
if (auto&& failed = expect(fatal(project.has_value())); !failed) failed << project.error();
74+
auto discovered = mcpp::build::discover_test_targets(tmp.path, {});
75+
if (auto&& failed = expect(fatal(discovered.has_value())); !failed) failed << discovered.error();
76+
auto& manifest = project->manifest;
77+
manifest.targets.insert(manifest.targets.end(), discovered->targets.begin(), discovered->targets.end());
78+
mcpp::modgraph::PackageRoot root;
79+
root.root = tmp.path;
80+
root.manifest = manifest;
81+
82+
mcpp::toolchain::Toolchain toolchain;
83+
toolchain.compiler = mcpp::toolchain::CompilerId::Clang;
84+
for (const auto& [triple, binary] : {
85+
std::pair{"aarch64-apple-darwin", "bin/collide"},
86+
std::pair{"x86_64-w64-mingw32", "bin/collide.exe"}}) {
87+
toolchain.targetTriple = triple;
88+
const auto plan = mcpp::build::make_plan(manifest, toolchain, {}, {}, {}, {root},
89+
tmp.path, tmp.path / "target", {}, {});
90+
if (auto&& failed = expect(fatal(!plan.has_value())); !failed) failed << triple;
91+
const auto& error = plan.error();
92+
expect(error.contains("[test-output-collision]")) << error;
93+
expect(error.contains("test program 'collide'") && error.contains("executable target 'collide'")) << error;
94+
expect(error.contains(binary)) << error;
95+
}
96+
}};
97+
98+
UTest member_test_paths_keep_their_directories{"TestTargets/MemberTestPathsKeepTheirDirectories", [] {
99+
Tmp tmp;
100+
write_manifest(tmp.path, R"([workspace]
101+
members = ["member"]
102+
)");
103+
const auto memberDir = tmp.path / "member";
104+
write_manifest(memberDir, R"([package]
105+
name = "member"
106+
version = "0.1.0"
107+
108+
[targets."nested/tool"]
109+
kind = "bin"
110+
main = "src/main.cpp"
111+
112+
[test]
113+
programs = "directory"
114+
)");
115+
write_file(memberDir / "src/main.cpp", "int main() { return 0; }\n");
116+
write_file(memberDir / "tests/tool/main.cpp", "int main() { return 0; }\n");
117+
write_file(memberDir / "tests/nested/tool/main.cpp", "int main() { return 0; }\n");
118+
119+
auto workspace = mcpp::project::load_effective_manifest(tmp.path);
120+
if (auto&& failed = expect(fatal(workspace.has_value())); !failed) failed << workspace.error();
121+
auto project = mcpp::project::load_effective_manifest(memberDir);
122+
if (auto&& failed = expect(fatal(project.has_value())); !failed) failed << project.error();
123+
auto discovered = mcpp::build::discover_test_targets(tmp.path, "member");
124+
if (auto&& failed = expect(fatal(discovered.has_value())); !failed) failed << discovered.error();
125+
126+
mcpp::modgraph::PackageRoot root;
127+
root.root = tmp.path;
128+
root.manifest = mcpp::project::virtual_workspace_root(workspace->manifest, project->manifest, tmp.path);
129+
mcpp::modgraph::PackageRoot member;
130+
member.root = memberDir;
131+
member.selectedMember = true;
132+
member.memberProducts = "member";
133+
mcpp::toolchain::Toolchain toolchain;
134+
toolchain.compiler = mcpp::toolchain::CompilerId::Clang;
135+
toolchain.targetTriple = "aarch64-apple-darwin";
136+
137+
for (const auto& [name, collides] : {std::pair{"nested/tool", false}, std::pair{"tool", true}}) {
138+
const auto test = std::ranges::find(discovered->targets, name, &mcpp::manifest::Target::name);
139+
if (auto&& failed = expect(fatal(test != discovered->targets.end())); !failed) failed << name;
140+
member.manifest = project->manifest;
141+
member.manifest.targets.push_back(*test);
142+
const auto plan = mcpp::build::make_plan(root.manifest, toolchain, {}, {}, {}, {root, member},
143+
tmp.path, tmp.path / "target", {}, {});
144+
if (!collides) {
145+
if (auto&& failed = expect(fatal(plan.has_value())); !failed) failed << plan.error();
146+
expect(fatal(eq(plan->linkUnits.size(), 1u)));
147+
expect(eq(plan->linkUnits.front().output.generic_string(), "bin/member/nested/tool"));
148+
} else {
149+
if (auto&& failed = expect(fatal(!plan.has_value())); !failed) failed << name;
150+
expect(plan.error().contains("[test-output-collision]")) << plan.error();
151+
expect(plan.error().contains("executable target 'nested/tool'")) << plan.error();
152+
expect(plan.error().contains("'bin/member/tool'")) << plan.error();
153+
}
154+
}
155+
}};
156+
50157
UTest glob_flags_become_per_target_flags{"TestTargets/GlobFlagsBecomePerTargetFlags", [] {
51158
Tmp tmp;
52159
write_manifest(tmp.path, R"([package]

0 commit comments

Comments
 (0)