test(lint): the ast and ast-module cases pass only when their rule ran
RunLint calls a rule that reads the AST only on a file it could parse, so when the parse failed, the checks inside the "ast" and "ast-module" rules went untried, and the test, which read nothing of the run but those checks, passed. That is how the "ast" case, whose file includes <string>, passed in crafter-build's Flatpak with #46 open: lint printed "'string' file not found" for that file, and the test exited 0. Both cases now also check that their rule ran and that the run had no error.
This commit is contained in:
parent
043b1a3015
commit
fe25d7aeff
1 changed files with 16 additions and 4 deletions
|
|
@ -629,7 +629,9 @@ int main() {
|
||||||
Scratch s("ast");
|
Scratch s("ast");
|
||||||
s.Write("f", Source);
|
s.Write("f", Source);
|
||||||
Configuration cfg = s.Config({"f"});
|
Configuration cfg = s.Config({"f"});
|
||||||
cfg.AddAstLintRule("ast", [](LintContext& ctx) {
|
bool ran = false;
|
||||||
|
cfg.AddAstLintRule("ast", [&ran](LintContext& ctx) {
|
||||||
|
ran = true;
|
||||||
Check(ctx.AstAvailable(), std::format("ast: parse succeeded ({})", ctx.AstUnavailableReason()));
|
Check(ctx.AstAvailable(), std::format("ast: parse succeeded ({})", ctx.AstUnavailableReason()));
|
||||||
std::span<const LintDecl> decls = ctx.Decls();
|
std::span<const LintDecl> decls = ctx.Decls();
|
||||||
Check(!decls.empty(), "ast: declarations found");
|
Check(!decls.empty(), "ast: declarations found");
|
||||||
|
|
@ -688,7 +690,13 @@ int main() {
|
||||||
Check(std::string_view(ctx.content).substr(count->begin, count->end - count->begin) == "int count", "ast: extent brackets exactly the declaration");
|
Check(std::string_view(ctx.content).substr(count->begin, count->end - count->begin) == "int count", "ast: extent brackets exactly the declaration");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
RunLint(cfg, Mode(LintMode::Report));
|
const LintSummary summary = RunLint(cfg, Mode(LintMode::Report));
|
||||||
|
// The driver runs a rule that reads the AST only on a file it could
|
||||||
|
// parse, so when the parse fails every check above goes untried and
|
||||||
|
// the run's error is all that is left to see. In crafter-build's
|
||||||
|
// Flatpak this file's <string> was not found, and the case passed
|
||||||
|
// (issue #46).
|
||||||
|
Check(ran && summary.errors == 0, "ast: the rule ran on the parsed file (why not is on stderr)");
|
||||||
}
|
}
|
||||||
|
|
||||||
// A module interface unit. libclang reports `export namespace X { … }` as a
|
// A module interface unit. libclang reports `export namespace X { … }` as a
|
||||||
|
|
@ -710,7 +718,9 @@ int main() {
|
||||||
std::array<fs::path, 1> ifaces = { "Demo" };
|
std::array<fs::path, 1> ifaces = { "Demo" };
|
||||||
std::array<fs::path, 0> impls = {};
|
std::array<fs::path, 0> impls = {};
|
||||||
cfg.GetInterfacesAndImplementations(ifaces, impls);
|
cfg.GetInterfacesAndImplementations(ifaces, impls);
|
||||||
cfg.AddAstLintRule("ast-module", [](LintContext& ctx) {
|
bool ran = false;
|
||||||
|
cfg.AddAstLintRule("ast-module", [&ran](LintContext& ctx) {
|
||||||
|
ran = true;
|
||||||
Check(ctx.AstAvailable(), std::format("ast-module: parse succeeded ({})", ctx.AstUnavailableReason()));
|
Check(ctx.AstAvailable(), std::format("ast-module: parse succeeded ({})", ctx.AstUnavailableReason()));
|
||||||
std::span<const LintDecl> decls = ctx.Decls();
|
std::span<const LintDecl> decls = ctx.Decls();
|
||||||
auto find = [&](LintDeclKind kind, std::string_view name) -> const LintDecl* {
|
auto find = [&](LintDeclKind kind, std::string_view name) -> const LintDecl* {
|
||||||
|
|
@ -732,7 +742,9 @@ int main() {
|
||||||
Check(count != nullptr && count->line == 18, "ast-module: field line matches");
|
Check(count != nullptr && count->line == 18, "ast-module: field line matches");
|
||||||
Check(exported != nullptr && exported->line == 20, "ast-module: exported variable line matches");
|
Check(exported != nullptr && exported->line == 20, "ast-module: exported variable line matches");
|
||||||
});
|
});
|
||||||
RunLint(cfg, Mode(LintMode::Report));
|
const LintSummary summary = RunLint(cfg, Mode(LintMode::Report));
|
||||||
|
// As in the case above: the checks only count if the rule ran.
|
||||||
|
Check(ran && summary.errors == 0, "ast-module: the rule ran on the parsed file (why not is on stderr)");
|
||||||
}
|
}
|
||||||
|
|
||||||
// An unavailable AST must fail the run, never look like a clean file. A
|
// An unavailable AST must fail the run, never look like a clean file. A
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue