1use std::fmt::Write as _;
4
5use crate::{
6 CodeClimateIssue, CodeClimateSeverity, DiffIndex, GitHubReviewComment, GitHubReviewSide,
7 GitLabReviewComment, GitLabReviewPosition, GitLabReviewPositionType, ReviewCheckConclusion,
8 ReviewComment, ReviewEnvelopeEvent, ReviewEnvelopeMeta, ReviewEnvelopeOutput,
9 ReviewEnvelopeSchema, ReviewEnvelopeSummary, ReviewProvider, default_marker_regex,
10 default_marker_regex_flags,
11};
12use serde_json::Value;
13
14#[derive(Clone, Copy, Debug, PartialEq, Eq)]
16pub enum CiProvider {
17 Github,
18 Gitlab,
19}
20
21impl CiProvider {
22 #[must_use]
23 pub const fn name(self) -> &'static str {
24 match self {
25 Self::Github => "GitHub",
26 Self::Gitlab => "GitLab",
27 }
28 }
29}
30
31#[derive(Clone, Debug, PartialEq, Eq)]
33pub struct CiIssue {
34 pub rule_id: String,
35 pub description: String,
36 pub severity: String,
37 pub path: String,
38 pub line: u64,
39 pub fingerprint: String,
40}
41
42pub struct PrCommentRenderInput<'a> {
44 pub command: &'a str,
45 pub provider: CiProvider,
46 pub issues: &'a [CiIssue],
47 pub marker_id: String,
48 pub max_comments: usize,
49 pub category_for_rule: &'a dyn Fn(&str) -> &'static str,
50}
51
52#[derive(Clone, Debug, PartialEq, Eq)]
54pub struct ReviewGitlabDiffRefs {
55 pub base_sha: String,
56 pub start_sha: String,
57 pub head_sha: String,
58}
59
60#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
62pub struct ReviewEnvelopeTruncation {
63 pub body: bool,
64 pub comment_limit: bool,
65}
66
67#[derive(Debug)]
69pub struct ReviewEnvelopeRenderResult {
70 pub envelope: ReviewEnvelopeOutput,
71 pub truncation: ReviewEnvelopeTruncation,
72}
73
74pub struct ReviewEnvelopeRenderInput<'a> {
76 pub command: &'a str,
77 pub provider: CiProvider,
78 pub issues: &'a [CiIssue],
79 pub diff_index: Option<&'a DiffIndex>,
80 pub max_comments: usize,
81 pub gitlab_diff_refs: Option<&'a ReviewGitlabDiffRefs>,
82 pub include_guidance: bool,
83 pub suggestion_block: &'a dyn Fn(CiProvider, &CiIssue) -> Option<String>,
84 pub guidance_block: &'a dyn Fn(&CiIssue) -> Option<String>,
85}
86
87pub const MARKER_PREFIX_V2: &str = "<!-- fallow-fingerprint:v2: ";
89
90pub const MARKER_SUFFIX_V2: &str = " -->";
92
93pub const MAX_COMMENT_BODY_BYTES: usize = 65_536;
94const TRUNCATION_SUFFIX: &str = "\n\n<!-- fallow-truncated -->\n> Body truncated by fallow.";
95
96#[must_use]
97pub fn issues_from_codeclimate(value: &Value) -> Vec<CiIssue> {
98 let mut issues = value
99 .as_array()
100 .into_iter()
101 .flatten()
102 .filter_map(issue_from_codeclimate)
103 .collect::<Vec<_>>();
104 sort_ci_issues(&mut issues);
105 issues
106}
107
108#[must_use]
109pub fn issues_from_codeclimate_issues(issues: &[CodeClimateIssue]) -> Vec<CiIssue> {
110 let mut issues = issues
111 .iter()
112 .map(issue_from_codeclimate_issue)
113 .collect::<Vec<_>>();
114 sort_ci_issues(&mut issues);
115 issues
116}
117
118fn issue_from_codeclimate(value: &Value) -> Option<CiIssue> {
119 let path = value.pointer("/location/path")?.as_str()?.to_string();
120 let line = value
121 .pointer("/location/lines/begin")
122 .and_then(Value::as_u64)
123 .unwrap_or(1);
124 Some(CiIssue {
125 rule_id: value
126 .get("check_name")
127 .and_then(Value::as_str)
128 .unwrap_or("fallow/finding")
129 .to_string(),
130 description: value
131 .get("description")
132 .and_then(Value::as_str)
133 .unwrap_or("Fallow finding")
134 .to_string(),
135 severity: value
136 .get("severity")
137 .and_then(Value::as_str)
138 .unwrap_or("minor")
139 .to_string(),
140 fingerprint: value
141 .get("fingerprint")
142 .and_then(Value::as_str)
143 .unwrap_or("")
144 .to_string(),
145 path,
146 line,
147 })
148}
149
150fn issue_from_codeclimate_issue(issue: &CodeClimateIssue) -> CiIssue {
151 CiIssue {
152 rule_id: issue.check_name.clone(),
153 description: issue.description.clone(),
154 severity: codeclimate_severity_label(issue.severity).to_owned(),
155 path: issue.location.path.clone(),
156 line: u64::from(issue.location.lines.begin),
157 fingerprint: issue.fingerprint.clone(),
158 }
159}
160
161const fn codeclimate_severity_label(severity: CodeClimateSeverity) -> &'static str {
162 match severity {
163 CodeClimateSeverity::Info => "info",
164 CodeClimateSeverity::Minor => "minor",
165 CodeClimateSeverity::Major => "major",
166 CodeClimateSeverity::Critical => "critical",
167 CodeClimateSeverity::Blocker => "blocker",
168 }
169}
170
171fn sort_ci_issues(issues: &mut [CiIssue]) {
172 issues
173 .sort_by(|a, b| (&a.path, a.line, &a.fingerprint).cmp(&(&b.path, b.line, &b.fingerprint)));
174}
175
176fn fingerprint_hash(parts: &[&str]) -> String {
177 crate::codeclimate_fingerprint_hash(parts)
178}
179
180#[must_use]
181#[expect(clippy::expect_used, reason = "formatting into String is infallible")]
182pub fn render_pr_comment(input: &PrCommentRenderInput<'_>) -> String {
183 let marker = format!("<!-- fallow-id: {} -->", input.marker_id);
184 let title = command_title(input.command);
185 let count = input.issues.len();
186 let noun = if count == 1 { "finding" } else { "findings" };
187
188 let mut out = String::new();
189 out.push_str(&marker);
190 out.push('\n');
191 write!(&mut out, "### Fallow {title}\n\n").expect("write to string");
192 if count == 0 {
193 writeln!(
194 &mut out,
195 "No {provider} PR/MR findings.",
196 provider = input.provider.name()
197 )
198 .expect("write to string");
199 } else {
200 write!(&mut out, "Found **{count}** {noun}.\n\n").expect("write to string");
201 let groups = group_by_category(input.issues, input.category_for_rule);
202 if let [(_, group_issues)] = groups.as_slice() {
203 render_findings_table(&mut out, group_issues, input.max_comments, "Details");
204 } else {
205 for (category, group_issues) in &groups {
206 let summary_label = summary_label(category, group_issues.len(), input.max_comments);
207 render_findings_table(&mut out, group_issues, input.max_comments, &summary_label);
208 }
209 }
210 }
211 out.push_str("\nGenerated by fallow.");
212 out
213}
214
215pub const PROJECT_LEVEL_RULE_IDS: &[&str] = &[
218 "fallow/unused-catalog-entry",
219 "fallow/empty-catalog-group",
220 "fallow/unresolved-catalog-reference",
221 "fallow/unused-dependency-override",
222 "fallow/misconfigured-dependency-override",
223 "fallow/unused-dependency",
224 "fallow/unused-dev-dependency",
225 "fallow/unused-optional-dependency",
226 "fallow/type-only-dependency",
227 "fallow/test-only-dependency",
228];
229
230#[must_use]
231pub fn is_project_level_rule(rule_id: &str) -> bool {
232 PROJECT_LEVEL_RULE_IDS.contains(&rule_id)
233}
234
235const CATEGORY_ORDER: [&str; 6] = [
236 "Dead code",
237 "Dependencies",
238 "Duplication",
239 "Health",
240 "Architecture",
241 "Suppressions",
242];
243
244fn group_by_category<'a>(
245 issues: &'a [CiIssue],
246 category_for_rule: &dyn Fn(&str) -> &'static str,
247) -> Vec<(&'static str, Vec<&'a CiIssue>)> {
248 let mut buckets: std::collections::BTreeMap<&'static str, Vec<&CiIssue>> =
249 std::collections::BTreeMap::new();
250 for issue in issues {
251 let category = category_for_rule(&issue.rule_id);
252 buckets.entry(category).or_default().push(issue);
253 }
254 let mut ordered: Vec<(&'static str, Vec<&CiIssue>)> = Vec::with_capacity(buckets.len());
255 for category in CATEGORY_ORDER {
256 if let Some(items) = buckets.remove(category) {
257 ordered.push((category, items));
258 }
259 }
260 for (category, items) in buckets {
261 ordered.push((category, items));
262 }
263 ordered
264}
265
266#[must_use]
267pub fn summary_label(category: &str, total: usize, max: usize) -> String {
268 if total > max {
269 format!("{category} ({total}, showing {max})")
270 } else {
271 format!("{category} ({total})")
272 }
273}
274
275#[expect(clippy::expect_used, reason = "formatting into String is infallible")]
276fn render_findings_table(out: &mut String, issues: &[&CiIssue], max: usize, summary: &str) {
277 writeln!(out, "<details>\n<summary>{summary}</summary>\n").expect("write to string");
278 out.push_str("| Severity | Rule | Location | Description |\n");
279 out.push_str("| --- | --- | --- | --- |\n");
280 for issue in issues.iter().take(max) {
281 writeln!(
282 out,
283 "| {} | `{}` | `{}`:{} | {} |",
284 escape_md(&issue.severity),
285 escape_md(&issue.rule_id),
286 escape_md(&issue.path),
287 issue.line,
288 escape_md(&issue.description),
289 )
290 .expect("write to string");
291 }
292 if issues.len() > max {
293 writeln!(
294 out,
295 "\nShowing {max} of {} findings. Run fallow locally or inspect the CI output for the full report.",
296 issues.len(),
297 )
298 .expect("write to string");
299 }
300 out.push_str("\n</details>\n\n");
301}
302
303#[must_use]
304pub fn command_title(command: &str) -> &'static str {
305 match command {
306 "dead-code" | "check" => "dead-code report",
307 "dupes" => "duplication report",
308 "health" => "health report",
309 "audit" => "audit report",
310 "" | "combined" => "combined report",
311 _ => "report",
312 }
313}
314
315#[must_use]
317pub fn escape_md(value: &str) -> String {
318 let collapsed = value.replace('\n', " ");
319 let mut out = String::with_capacity(collapsed.len());
320 for ch in collapsed.chars() {
321 if matches!(
322 ch,
323 '\\' | '`'
324 | '*'
325 | '_'
326 | '['
327 | ']'
328 | '('
329 | ')'
330 | '!'
331 | '<'
332 | '>'
333 | '#'
334 | '|'
335 | '~'
336 | '&'
337 ) {
338 out.push('\\');
339 }
340 out.push(ch);
341 }
342 out.trim().to_owned()
343}
344
345#[must_use]
347pub fn render_review_envelope(input: &ReviewEnvelopeRenderInput<'_>) -> ReviewEnvelopeRenderResult {
348 let grouped = group_review_issues_by_path_line(input.issues, input.max_comments);
349
350 let comments: Vec<ReviewComment> = grouped
351 .groups
352 .iter()
353 .map(|group| {
354 render_review_comment_for_group(&ReviewCommentRenderInput {
355 provider: input.provider,
356 group,
357 gitlab_diff_refs: input.gitlab_diff_refs,
358 diff_index: input.diff_index,
359 include_guidance: input.include_guidance,
360 suggestion_block: input.suggestion_block,
361 guidance_block: input.guidance_block,
362 })
363 })
364 .collect();
365
366 let summary_text =
367 review_summary_text(input.command, input.provider, comments.len(), input.issues);
368 let summary_fp = summary_fingerprint(&summary_text);
369 let summary_marker = format!("\n\n{MARKER_PREFIX_V2}{summary_fp}{MARKER_SUFFIX_V2}");
370 let body = format!("{summary_text}{summary_marker}");
371 let summary = ReviewEnvelopeSummary {
372 body: body.clone(),
373 fingerprint: summary_fp,
374 };
375
376 let truncation = ReviewEnvelopeTruncation {
377 body: comments.iter().any(review_comment_truncated),
378 comment_limit: grouped.truncated,
379 };
380
381 ReviewEnvelopeRenderResult {
382 envelope: build_review_envelope_output(
383 input.provider,
384 body,
385 summary,
386 comments,
387 input.issues,
388 ),
389 truncation,
390 }
391}
392
393fn review_summary_text(
394 command: &str,
395 provider: CiProvider,
396 comment_count: usize,
397 issues: &[CiIssue],
398) -> String {
399 let verdict = review_summary_verdict(issues);
400 format!(
401 "### Fallow {}\n\n**{}**\n\n{} inline finding{} selected for {} review.\n\n<!-- fallow-review -->",
402 command_title(command),
403 verdict,
404 comment_count,
405 if comment_count == 1 { "" } else { "s" },
406 provider.name(),
407 )
408}
409
410fn review_summary_verdict(issues: &[CiIssue]) -> &'static str {
411 match github_check_conclusion(issues) {
412 ReviewCheckConclusion::Failure => "Quality gate failed",
413 ReviewCheckConclusion::Neutral => "Review needed",
414 ReviewCheckConclusion::Success => "Quality gate passed",
415 }
416}
417
418#[derive(Debug, PartialEq, Eq)]
419pub struct GroupedReviewIssues<'a> {
420 pub groups: Vec<Vec<&'a CiIssue>>,
421 pub truncated: bool,
422}
423
424#[must_use]
427pub fn group_review_issues_by_path_line(
428 issues: &[CiIssue],
429 max_groups: usize,
430) -> GroupedReviewIssues<'_> {
431 if max_groups == 0 {
432 return GroupedReviewIssues {
433 groups: Vec::new(),
434 truncated: !issues.is_empty(),
435 };
436 }
437 let mut groups: Vec<Vec<&CiIssue>> = Vec::with_capacity(max_groups.min(issues.len()));
438 let mut current: Vec<&CiIssue> = Vec::new();
439 let mut current_key: Option<(&str, u64)> = None;
440 for issue in issues {
441 let key = (issue.path.as_str(), issue.line);
442 if Some(key) != current_key {
443 if !current.is_empty() {
444 groups.push(std::mem::take(&mut current));
445 if groups.len() == max_groups {
446 return GroupedReviewIssues {
447 groups,
448 truncated: true,
449 };
450 }
451 }
452 current_key = Some(key);
453 }
454 current.push(issue);
455 }
456 if !current.is_empty() && groups.len() < max_groups {
457 groups.push(current);
458 }
459 GroupedReviewIssues {
460 groups,
461 truncated: false,
462 }
463}
464
465fn review_comment_truncated(comment: &ReviewComment) -> bool {
466 match comment {
467 ReviewComment::GitHub(comment) => comment.truncated,
468 ReviewComment::GitLab(comment) => comment.truncated,
469 }
470}
471
472pub struct ReviewCommentRenderInput<'a, 'group> {
473 pub provider: CiProvider,
474 pub group: &'a [&'group CiIssue],
475 pub gitlab_diff_refs: Option<&'a ReviewGitlabDiffRefs>,
476 pub diff_index: Option<&'a DiffIndex>,
477 pub include_guidance: bool,
478 pub suggestion_block: &'a dyn Fn(CiProvider, &CiIssue) -> Option<String>,
479 pub guidance_block: &'a dyn Fn(&CiIssue) -> Option<String>,
480}
481
482#[must_use]
484pub fn render_review_comment_for_group(input: &ReviewCommentRenderInput<'_, '_>) -> ReviewComment {
485 assert!(
486 !input.group.is_empty(),
487 "group_review_issues_by_path_line never yields empty"
488 );
489 let representative = input.group[0];
490 let fingerprint = if input.group.len() == 1 {
491 representative.fingerprint.clone()
492 } else {
493 let constituents: Vec<&str> = input.group.iter().map(|i| i.fingerprint.as_str()).collect();
494 composite_fingerprint(&constituents)
495 };
496
497 let content = build_merged_comment_content(input);
498 let marker_line = format!("\n\n{MARKER_PREFIX_V2}{fingerprint}{MARKER_SUFFIX_V2}");
499 let (body, truncated) = cap_body_with_marker(&content, &marker_line);
500
501 build_review_comment(ReviewCommentInput {
502 provider: input.provider,
503 representative,
504 gitlab_diff_refs: input.gitlab_diff_refs,
505 diff_index: input.diff_index,
506 body,
507 fingerprint,
508 truncated,
509 })
510}
511
512#[expect(clippy::expect_used, reason = "formatting into String is infallible")]
513fn build_merged_comment_content(input: &ReviewCommentRenderInput<'_, '_>) -> String {
514 let mut content = String::new();
515 for (index, issue) in input.group.iter().enumerate() {
516 let label = review_label_from_codeclimate(&issue.severity);
517 if index > 0 {
518 content.push_str("\n\n");
519 }
520 write!(
521 content,
522 "**{}** `{}`: {}",
523 label,
524 escape_md(&issue.rule_id),
525 escape_md(&issue.description)
526 )
527 .expect("write to String is infallible");
528 if let Some(suggestion) = (input.suggestion_block)(input.provider, issue) {
529 content.push_str(&suggestion);
530 }
531 if input.include_guidance
532 && let Some(guidance) = (input.guidance_block)(issue)
533 {
534 content.push_str(&guidance);
535 }
536 }
537 content
538}
539
540struct ReviewCommentInput<'a> {
541 provider: CiProvider,
542 representative: &'a CiIssue,
543 gitlab_diff_refs: Option<&'a ReviewGitlabDiffRefs>,
544 diff_index: Option<&'a DiffIndex>,
545 body: String,
546 fingerprint: String,
547 truncated: bool,
548}
549
550fn build_review_comment(input: ReviewCommentInput<'_>) -> ReviewComment {
551 let ReviewCommentInput {
552 provider,
553 representative,
554 gitlab_diff_refs,
555 diff_index,
556 body,
557 fingerprint,
558 truncated,
559 } = input;
560 match provider {
561 CiProvider::Github => ReviewComment::GitHub(GitHubReviewComment {
562 path: representative.path.clone(),
563 line: u32::try_from(representative.line).unwrap_or(u32::MAX),
564 side: GitHubReviewSide::Right,
565 body,
566 fingerprint,
567 truncated,
568 }),
569 CiProvider::Gitlab => {
570 let new_path = representative.path.clone();
571 let old_path = diff_index
572 .and_then(|di| di.old_path_for(&new_path))
573 .map_or_else(|| new_path.clone(), str::to_owned);
574 let position = GitLabReviewPosition {
575 base_sha: gitlab_diff_refs.map(|r| r.base_sha.clone()),
576 start_sha: gitlab_diff_refs.map(|r| r.start_sha.clone()),
577 head_sha: gitlab_diff_refs.map(|r| r.head_sha.clone()),
578 position_type: GitLabReviewPositionType::Text,
579 old_path,
580 new_path,
581 new_line: u32::try_from(representative.line).unwrap_or(u32::MAX),
582 };
583 ReviewComment::GitLab(GitLabReviewComment {
584 body,
585 position,
586 fingerprint,
587 truncated,
588 })
589 }
590 }
591}
592
593#[must_use]
594pub fn cap_body_with_marker(content: &str, marker_line: &str) -> (String, bool) {
595 let intact_len = content.len() + marker_line.len();
596 if intact_len <= MAX_COMMENT_BODY_BYTES {
597 let mut out = String::with_capacity(intact_len);
598 out.push_str(content);
599 out.push_str(marker_line);
600 return (out, false);
601 }
602 let reserved = marker_line.len() + TRUNCATION_SUFFIX.len();
603 let budget = MAX_COMMENT_BODY_BYTES.saturating_sub(reserved);
604 let mut cut = budget.min(content.len());
605 while cut > 0 && !content.is_char_boundary(cut) {
606 cut -= 1;
607 }
608 let mut out = String::with_capacity(MAX_COMMENT_BODY_BYTES);
609 out.push_str(&content[..cut]);
610 out.push_str(TRUNCATION_SUFFIX);
611 out.push_str(marker_line);
612 (out, true)
613}
614
615#[must_use]
616pub const fn review_label_from_codeclimate(severity_name: &str) -> &'static str {
617 match severity_name.as_bytes() {
618 b"major" | b"critical" | b"blocker" => "error",
619 _ => "warn",
620 }
621}
622
623#[must_use]
624pub fn github_check_conclusion(issues: &[CiIssue]) -> ReviewCheckConclusion {
625 if issues
626 .iter()
627 .any(|issue| matches!(issue.severity.as_str(), "major" | "critical" | "blocker"))
628 {
629 ReviewCheckConclusion::Failure
630 } else if issues.is_empty() {
631 ReviewCheckConclusion::Success
632 } else {
633 ReviewCheckConclusion::Neutral
634 }
635}
636
637fn build_review_envelope_output(
638 provider: CiProvider,
639 body: String,
640 summary: ReviewEnvelopeSummary,
641 comments: Vec<ReviewComment>,
642 issues: &[CiIssue],
643) -> ReviewEnvelopeOutput {
644 match provider {
645 CiProvider::Github => ReviewEnvelopeOutput {
646 event: Some(ReviewEnvelopeEvent::Comment),
647 body,
648 summary,
649 comments,
650 marker_regex: default_marker_regex(),
651 marker_regex_flags: default_marker_regex_flags(),
652 meta: ReviewEnvelopeMeta {
653 schema: ReviewEnvelopeSchema::V2,
654 provider: ReviewProvider::Github,
655 check_conclusion: Some(github_check_conclusion(issues)),
656 },
657 },
658 CiProvider::Gitlab => ReviewEnvelopeOutput {
659 event: None,
660 body,
661 summary,
662 comments,
663 marker_regex: default_marker_regex(),
664 marker_regex_flags: default_marker_regex_flags(),
665 meta: ReviewEnvelopeMeta {
666 schema: ReviewEnvelopeSchema::V2,
667 provider: ReviewProvider::Gitlab,
668 check_conclusion: None,
669 },
670 },
671 }
672}
673
674#[must_use]
675pub fn summary_fingerprint(body: &str) -> String {
676 fingerprint_hash(&[body])
677}
678
679#[must_use]
680pub fn composite_fingerprint(constituents: &[&str]) -> String {
681 let mut sorted: Vec<&str> = constituents.to_vec();
682 sorted.sort_unstable();
683 let joined = sorted.join(":");
684 format!("merged:{}", fingerprint_hash(&[joined.as_str()]))
685}
686
687#[cfg(test)]
688mod tests {
689 use super::*;
690 use crate::{CodeClimateIssueKind, CodeClimateLines, CodeClimateLocation};
691
692 fn category_for_rule(rule_id: &str) -> &'static str {
693 match rule_id {
694 "fallow/code-duplication" => "Duplication",
695 "fallow/high-complexity" => "Health",
696 "fallow/unused-dependency" => "Dependencies",
697 _ => "Dead code",
698 }
699 }
700
701 #[test]
702 fn extracts_issues_from_codeclimate() {
703 let value = serde_json::json!([{
704 "check_name": "fallow/unused-export",
705 "description": "Export x is never imported",
706 "severity": "minor",
707 "fingerprint": "abc",
708 "location": { "path": "src/a.ts", "lines": { "begin": 7 } }
709 }]);
710 let issues = issues_from_codeclimate(&value);
711 assert_eq!(issues.len(), 1);
712 assert_eq!(issues[0].path, "src/a.ts");
713 assert_eq!(issues[0].line, 7);
714 }
715
716 #[test]
717 fn typed_codeclimate_issues_extract_like_json_codeclimate() {
718 let severities = [
719 (CodeClimateSeverity::Info, "info"),
720 (CodeClimateSeverity::Minor, "minor"),
721 (CodeClimateSeverity::Major, "major"),
722 (CodeClimateSeverity::Critical, "critical"),
723 (CodeClimateSeverity::Blocker, "blocker"),
724 ];
725 let typed = severities
726 .iter()
727 .enumerate()
728 .map(|(index, (severity, _))| CodeClimateIssue {
729 kind: CodeClimateIssueKind::Issue,
730 check_name: format!("fallow/rule-{index}"),
731 description: format!("Finding {index}"),
732 categories: vec!["Complexity".to_owned()],
733 severity: *severity,
734 fingerprint: format!("fp-{index}"),
735 location: CodeClimateLocation {
736 path: format!("src/{index}.ts"),
737 lines: CodeClimateLines {
738 begin: u32::try_from(index + 1).expect("small fixture index"),
739 },
740 },
741 owner: None,
742 group: None,
743 })
744 .collect::<Vec<_>>();
745 let value = serde_json::to_value(&typed).expect("typed fixture serializes");
746
747 assert_eq!(
748 issues_from_codeclimate_issues(&typed),
749 issues_from_codeclimate(&value)
750 );
751 let typed_labels = issues_from_codeclimate_issues(&typed)
752 .into_iter()
753 .map(|issue| issue.severity)
754 .collect::<Vec<_>>();
755 let expected_labels = severities
756 .iter()
757 .map(|(_, label)| (*label).to_owned())
758 .collect::<Vec<_>>();
759 assert_eq!(typed_labels, expected_labels);
760 }
761
762 #[test]
763 fn renders_default_empty_comment() {
764 let body = render_pr_comment(&PrCommentRenderInput {
765 command: "check",
766 provider: CiProvider::Github,
767 issues: &[],
768 marker_id: "fallow-results".to_owned(),
769 max_comments: 50,
770 category_for_rule: &category_for_rule,
771 });
772 assert!(body.contains("<!-- fallow-id: fallow-results"));
773 assert!(body.contains("No GitHub PR/MR findings."));
774 }
775
776 #[test]
777 fn escape_md_escapes_inline_commonmark_specials() {
778 let raw = "foo*bar_baz [a](u) `c` <h> #x !i ~s | p";
779 let escaped = escape_md(raw);
780 for ch in [
781 '*', '_', '[', ']', '(', ')', '`', '<', '>', '#', '!', '~', '|',
782 ] {
783 let raw_count = raw.chars().filter(|c| c == &ch).count();
784 let escaped_count = escaped.matches(&format!("\\{ch}")).count();
785 assert_eq!(
786 raw_count, escaped_count,
787 "char {ch:?}: raw {raw_count} occurrences, escaped {escaped_count} in {escaped:?}"
788 );
789 }
790 }
791
792 #[test]
793 fn escape_md_escapes_ampersand_to_block_numeric_entity_bypass() {
794 let raw = "value *suspicious* here";
795 let escaped = escape_md(raw);
796 assert!(escaped.contains(r"\&"), "got: {escaped}");
797 assert!(escaped.contains(r"\#"), "got: {escaped}");
798 assert!(!escaped.contains(" *suspicious"), "got: {escaped}");
799 }
800
801 #[test]
802 fn summary_label_foreshadows_truncation() {
803 assert_eq!(
804 summary_label("Duplication", 160, 50),
805 "Duplication (160, showing 50)"
806 );
807 assert_eq!(summary_label("Health", 12, 50), "Health (12)");
808 assert_eq!(summary_label("Dependencies", 50, 50), "Dependencies (50)");
809 }
810
811 #[test]
812 fn escape_md_does_not_escape_block_only_markers() {
813 let raw = "fallow/test-only-dependency package.json:12";
814 let escaped = escape_md(raw);
815 assert!(!escaped.contains("\\-"), "should not escape `-`");
816 assert!(!escaped.contains("\\."), "should not escape `.`");
817 assert_eq!(escaped, raw);
818 }
819
820 #[test]
821 fn escape_md_collapses_newlines_to_spaces() {
822 let raw = "first\nsecond\nthird";
823 assert_eq!(escape_md(raw), "first second third");
824 }
825
826 #[test]
827 fn escape_md_leaves_safe_chars_unchanged() {
828 let raw = "Export 'helperFn' is never imported by other modules";
829 assert_eq!(
830 escape_md(raw),
831 r"Export 'helperFn' is never imported by other modules"
832 );
833 }
834
835 #[test]
836 fn is_project_level_rule_covers_config_anchored_dependency_findings() {
837 for rule_id in PROJECT_LEVEL_RULE_IDS {
838 assert!(
839 is_project_level_rule(rule_id),
840 "{rule_id} must be project-level"
841 );
842 }
843 for rule_id in [
844 "fallow/unused-file",
845 "fallow/unused-export",
846 "fallow/unused-type",
847 "fallow/unused-enum-member",
848 "fallow/unused-class-member",
849 "fallow/unused-store-member",
850 "fallow/unresolved-import",
851 "fallow/unlisted-dependency",
852 "fallow/duplicate-export",
853 "fallow/circular-dependency",
854 "fallow/re-export-cycle",
855 "fallow/boundary-violation",
856 "fallow/stale-suppression",
857 "fallow/private-type-leak",
858 "fallow/high-complexity",
859 "fallow/high-crap-score",
860 ] {
861 assert!(
862 !is_project_level_rule(rule_id),
863 "{rule_id} must NOT be project-level"
864 );
865 }
866 }
867
868 #[test]
869 fn escape_md_double_apply_is_safe() {
870 let raw = "code with `backticks` and *stars*";
871 let once = escape_md(raw);
872 let twice = escape_md(&once);
873 assert!(twice.contains(r"\\"));
874 }
875}