use std::fmt;
use yaml_rust2::scanner::TScalarStyle;
use super::git_ref::SHA_LEN;
use super::{
CommitSha, LineOffsetTable, MAX_DIAGNOSTIC_VALUE_CHARS, PinResolution, TagIndex,
byte_span_to_range, extends_tag, is_partial_semver_shaped, markdown_code_span,
position_in_range, redact_name_for_diagnostic, sanitize_and_truncate_for_diagnostic, short_sha,
};
use crate::diagnostic::{Diagnostic, DiagnosticKind, Severity};
use crate::github::normalize_tag;
use crate::position::{Position, Range};
use crate::{ConcreteVersion, PackageName};
#[cfg(feature = "lsp-responses")]
use super::single_file_edit;
#[cfg(feature = "lsp-responses")]
use tower_lsp_server::ls_types::{CodeAction, CodeActionKind, WorkspaceEdit};
pub const SHA_COMMENT_MISMATCH_DIAGNOSTIC_CODE: &str = "sha-comment-mismatch";
const REST_OF_LINE_WINDOW_BYTES: usize = 4096;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CommentTag(String);
impl CommentTag {
#[must_use]
pub fn parse(text: &str) -> Option<Self> {
let safe = !text
.chars()
.any(|c| c.is_whitespace() || c.is_control() || c == '#');
(safe && is_partial_semver_shaped(text)).then(|| Self(text.to_string()))
}
#[must_use]
pub(crate) fn is_full_version(&self) -> bool {
semver::Version::parse(normalize_tag(&self.0)).is_ok()
}
fn is_printable_ascii(&self) -> bool {
self.0.chars().all(|c| c.is_ascii_graphic())
}
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
}
impl fmt::Display for CommentTag {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Delimiter {
DoubleQuote,
SingleQuote,
Space,
Tab,
CloseBrace,
}
impl Delimiter {
const fn as_char(self) -> char {
match self {
Self::DoubleQuote => '"',
Self::SingleQuote => '\'',
Self::Space => ' ',
Self::Tab => '\t',
Self::CloseBrace => '}',
}
}
const fn blank(c: char) -> Option<Self> {
match c {
' ' => Some(Self::Space),
'\t' => Some(Self::Tab),
_ => None,
}
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct ClosingDelimiters(Vec<Delimiter>);
impl ClosingDelimiters {
#[must_use]
pub fn parse(tail: &str, style: TScalarStyle) -> Self {
let opening = match style {
TScalarStyle::Plain => None,
TScalarStyle::SingleQuoted => Some(Delimiter::SingleQuote),
TScalarStyle::DoubleQuoted => Some(Delimiter::DoubleQuote),
TScalarStyle::Literal | TScalarStyle::Folded => return Self::default(),
};
let mut chars = tail.chars();
let mut delimiters = Vec::new();
if let Some(quote) = opening {
if chars.next() != Some(quote.as_char()) {
return Self::default();
}
delimiters.push(quote);
}
let rest = chars.as_str();
let blanks: Vec<Delimiter> = rest.chars().map_while(Delimiter::blank).collect();
if rest.chars().nth(blanks.len()) == Some(Delimiter::CloseBrace.as_char()) {
delimiters.extend(blanks);
delimiters.push(Delimiter::CloseBrace);
}
Self(delimiters)
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
#[must_use]
pub fn byte_len(&self) -> usize {
self.0.len()
}
}
impl fmt::Display for ClosingDelimiters {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0
.iter()
.try_for_each(|d| fmt::Write::write_char(f, d.as_char()))
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CommentRemainder {
Empty,
Text,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ShaPinComment {
pub tag: CommentTag,
pub tag_range: Range,
pub closing: ClosingDelimiters,
pub remainder: CommentRemainder,
}
impl ShaPinComment {
#[must_use]
pub const fn new(tag: CommentTag, closing: ClosingDelimiters, tag_range: Range) -> Self {
Self {
tag,
tag_range,
closing,
remainder: CommentRemainder::Text,
}
}
#[must_use]
pub const fn with_remainder(mut self, remainder: CommentRemainder) -> Self {
self.remainder = remainder;
self
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CommentSlot {
Appendable,
Unavailable,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ShaPinTail {
Commented(ShaPinComment),
Bare(CommentSlot),
}
impl ShaPinTail {
#[must_use]
pub const fn comment(&self) -> Option<&ShaPinComment> {
match self {
Self::Commented(comment) => Some(comment),
Self::Bare(_) => None,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ShaPinTailRead {
pub tail: ShaPinTail,
pub range_end: usize,
}
#[derive(Clone, Copy, PartialEq, Eq)]
enum WindowCoverage {
FullLine,
Truncated,
}
fn rest_of_line<'a>(
content: &'a str,
line_table: &LineOffsetTable,
ref_end: usize,
line: usize,
) -> (&'a str, WindowCoverage) {
let next_line_start = line_table.line_start(line).unwrap_or(content.len());
let line_end = match next_line_start.checked_sub(1) {
Some(i) if content.as_bytes().get(i) == Some(&b'\n') => i,
_ => next_line_start,
};
let desynced = line_end < ref_end;
if desynced {
tracing::debug!(
ref_end,
line_end,
line,
"line_table line_end is before ref_end; treating the rest-of-line window as truncated"
);
}
let capped_end = ref_end
.saturating_add(REST_OF_LINE_WINDOW_BYTES)
.min(line_end);
let coverage = if desynced || capped_end < line_end {
WindowCoverage::Truncated
} else {
WindowCoverage::FullLine
};
let capped_end = content.floor_char_boundary(capped_end).max(ref_end);
let rest = content.get(ref_end..capped_end).unwrap_or_default();
(rest, coverage)
}
fn is_last_token_on_line(rest: &str, window: WindowCoverage) -> bool {
let mut previous_is_blank = false;
for b in rest.bytes() {
if b == b'#' && previous_is_blank {
return true;
}
if !b.is_ascii_whitespace() {
return false;
}
previous_is_blank = true;
}
window == WindowCoverage::FullLine
}
fn extract_comment_tag(rest: &str, window: WindowCoverage) -> Option<(CommentTag, usize)> {
let mut previous_is_blank = false;
let hash = rest.bytes().position(|b| {
let found = b == b'#' && previous_is_blank;
previous_is_blank = b.is_ascii_whitespace();
found
})?;
let after_hash = rest.get(hash + 1..)?;
let after_ws = after_hash.trim_start();
let ws_len = after_hash.len() - after_ws.len();
let terminator = after_ws.find(char::is_whitespace);
if terminator.is_none() && window == WindowCoverage::Truncated {
return None;
}
let token_len = terminator.unwrap_or(after_ws.len());
let tag = CommentTag::parse(after_ws.get(..token_len)?)?;
Some((tag, hash + 1 + ws_len + token_len))
}
#[must_use]
pub fn ref_is_last_on_line(
content: &str,
line_table: &LineOffsetTable,
ref_end: usize,
line: usize,
) -> bool {
let (rest, window) = rest_of_line(content, line_table, ref_end, line);
is_last_token_on_line(rest, window)
}
fn comment_remainder(rest: &str, token_end: usize, window: WindowCoverage) -> CommentRemainder {
let blank = rest
.get(token_end..)
.is_some_and(|after| after.chars().all(char::is_whitespace));
if blank && window == WindowCoverage::FullLine {
CommentRemainder::Empty
} else {
CommentRemainder::Text
}
}
fn comment_slot(is_plain_scalar: bool, is_last_on_line: bool) -> CommentSlot {
if is_plain_scalar && is_last_on_line {
CommentSlot::Appendable
} else {
CommentSlot::Unavailable
}
}
#[must_use]
pub fn comment_slot_after(
content: &str,
line_table: &LineOffsetTable,
ref_end: usize,
line: usize,
is_plain_scalar: bool,
) -> CommentSlot {
comment_slot(
is_plain_scalar,
ref_is_last_on_line(content, line_table, ref_end, line),
)
}
#[must_use]
pub fn read_sha_pin_tail(
content: &str,
line_table: &LineOffsetTable,
ref_end: usize,
line: usize,
style: TScalarStyle,
is_plain_scalar: bool,
) -> ShaPinTailRead {
let (rest, window) = rest_of_line(content, line_table, ref_end, line);
let is_last = is_last_token_on_line(rest, window);
let closing = ClosingDelimiters::parse(rest, style);
let closer_len = closing.byte_len();
let comment = if closing.is_empty() {
(is_plain_scalar && is_last)
.then(|| extract_comment_tag(rest, window))
.flatten()
} else {
rest.get(closer_len..).and_then(|tail| {
is_last_token_on_line(tail, window)
.then(|| extract_comment_tag(tail, window))
.flatten()
.map(|(tag, end)| (tag, closer_len + end))
})
};
match comment {
Some((tag, token_end)) => {
let end = ref_end + token_end;
let tag_range = byte_span_to_range(content, line_table, end - tag.as_str().len(), end);
ShaPinTailRead {
tail: ShaPinTail::Commented(
ShaPinComment::new(tag, closing, tag_range)
.with_remainder(comment_remainder(rest, token_end, window)),
),
range_end: end,
}
}
None => ShaPinTailRead {
tail: ShaPinTail::Bare(comment_slot(is_plain_scalar, is_last)),
range_end: ref_end,
},
}
}
#[must_use]
pub fn sha_pin_rewrite(tail: &ShaPinTail, sha: &CommitSha, target: &ConcreteVersion) -> String {
let comment = CommentTag::parse(target.as_str()).filter(CommentTag::is_printable_ascii);
match (tail, comment) {
(ShaPinTail::Commented(existing), Some(tag)) => {
format!("{sha}{} # {tag}", existing.closing)
}
(ShaPinTail::Commented(existing), None) => match existing.remainder {
CommentRemainder::Empty => format!("{sha}{}", existing.closing),
CommentRemainder::Text => format!("{sha}{} #", existing.closing),
},
(ShaPinTail::Bare(CommentSlot::Appendable), Some(tag)) => format!("{sha} # {tag}"),
(ShaPinTail::Bare(CommentSlot::Appendable | CommentSlot::Unavailable), _) => {
sha.to_string()
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CommentCheck {
NoComment,
Unverifiable,
Confirmed,
Mismatch(CommentMismatch),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CommentMismatch {
ShaNotInIndex,
ShaIsOtherTag {
actual: ConcreteVersion,
},
CommentNamesOtherCommit,
}
fn comment_names_tag(comment: &str, actual: &str) -> bool {
let is_prerelease = |tag: &str| normalize_tag(tag).contains(['-', '+']);
normalize_tag(comment) == normalize_tag(actual)
|| (extends_tag(actual, comment) && (!is_prerelease(actual) || is_prerelease(comment)))
}
impl CommentCheck {
#[must_use]
pub fn evaluate(
index: Option<&TagIndex>,
sha: &CommitSha,
comment: Option<&ShaPinComment>,
) -> Self {
let Some(comment) = comment else {
return Self::NoComment;
};
let Some(index) = index.filter(|index| !index.is_empty()) else {
return Self::Unverifiable;
};
let commented = comment.tag.as_str();
if index
.sha_for_tag(commented)
.is_some_and(|commit| commit == sha)
{
return Self::Confirmed;
}
match index.pin_resolution(sha, Some(&comment.tag)) {
PinResolution::Resolved { pin, .. }
if comment_names_tag(commented, pin.version().as_str()) =>
{
Self::Confirmed
}
PinResolution::Resolved { pin, .. } => Self::Mismatch(CommentMismatch::ShaIsOtherTag {
actual: pin.version().clone(),
}),
PinResolution::Untagged => Self::Mismatch(CommentMismatch::ShaNotInIndex),
PinResolution::CommentContradicted => {
Self::Mismatch(CommentMismatch::CommentNamesOtherCommit)
}
PinResolution::Unresolved
| PinResolution::Unlisted
| PinResolution::Unpublished
| PinResolution::NotYetIndexed => Self::Unverifiable,
}
}
}
fn sanitize_for_message(value: &str) -> String {
sanitize_and_truncate_for_diagnostic(value, MAX_DIAGNOSTIC_VALUE_CHARS)
}
#[must_use]
pub fn sha_comment_mismatch_diagnostic(
range: Range,
name: &PackageName,
sha: &CommitSha,
comment: &CommentTag,
mismatch: &CommentMismatch,
severity: Severity,
) -> Diagnostic {
let name = redact_name_for_diagnostic(name);
let sha = short_sha(sha.as_str());
let comment = sanitize_for_message(comment.as_str());
let message = match mismatch {
CommentMismatch::ShaIsOtherTag { actual } => format!(
"{name}: SHA {sha} is not the commit of `{comment}` named in the comment \
(it is `{}`)",
sanitize_for_message(actual.as_str())
),
CommentMismatch::ShaNotInIndex => format!(
"{name}: SHA {sha} is not the commit of any release tag; the comment \
names `{comment}`"
),
CommentMismatch::CommentNamesOtherCommit => {
format!("{name}: SHA {sha} is not the commit of `{comment}` named in the comment")
}
};
Diagnostic::new(DiagnosticKind::ShaCommentMismatch, range, message).with_severity(severity)
}
#[must_use]
pub fn sha_comment_mismatch_hover_line(
sha: &CommitSha,
comment: &CommentTag,
mismatch: &CommentMismatch,
) -> String {
let sha = markdown_code_span(&format!("{}…", short_sha(sha.as_str())));
let comment = markdown_code_span(&sanitize_for_message(comment.as_str()));
match mismatch {
CommentMismatch::ShaIsOtherTag { actual } => format!(
"**Warning**: comment says {comment}, but SHA {sha} is {}",
markdown_code_span(&sanitize_for_message(actual.as_str()))
),
CommentMismatch::ShaNotInIndex => format!(
"**Warning**: SHA {sha} is not the commit of any release tag; comment says {comment}"
),
CommentMismatch::CommentNamesOtherCommit => {
format!("**Warning**: SHA {sha} is not the commit of {comment} named in the comment")
}
}
}
#[cfg(feature = "lsp-responses")]
#[must_use]
pub fn build_sha_comment_fix_action(
uri: &url::Url,
version_range: Range,
comment: &ShaPinComment,
mismatch: &CommentMismatch,
) -> Option<CodeAction> {
let CommentMismatch::ShaIsOtherTag { actual } = mismatch else {
return None;
};
let actual = actual.as_str();
if !is_partial_semver_shaped(actual)
|| sanitize_for_message(actual) != actual
|| comment
.tag
.as_str()
.ends_with(|c: char| c.is_ascii_punctuation())
{
return None;
}
let changes = single_file_edit(uri, comment.tag_range, actual.to_string());
Some(CodeAction {
title: format!("Correct version comment to `{actual}`"),
kind: Some(CodeActionKind::QUICKFIX),
edit: Some(WorkspaceEdit {
changes: Some(changes),
..Default::default()
}),
data: Some(serde_json::json!({
"diagnostic_codes": [SHA_COMMENT_MISMATCH_DIAGNOSTIC_CODE],
"diagnostic_range": tower_lsp_server::ls_types::Range::from(version_range),
})),
..Default::default()
})
}
#[must_use]
pub fn position_past_sha(range: Range, position: Position) -> bool {
let Ok(sha_len) = u32::try_from(SHA_LEN) else {
return false;
};
position_in_range(position, range)
&& position.character > range.start.character.saturating_add(sha_len)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::pagination::ListCoverage;
use std::assert_matches;
fn sha(c: char) -> CommitSha {
CommitSha::parse(&c.to_string().repeat(40)).unwrap()
}
fn read(content: &str, style: TScalarStyle, plain: bool, ref_end: usize) -> ShaPinTailRead {
let table = LineOffsetTable::new(content);
read_sha_pin_tail(content, &table, ref_end, 1, style, plain)
}
fn comment(tag: &str, closing: &str, style: TScalarStyle) -> ShaPinComment {
ShaPinComment::new(
CommentTag::parse(tag).unwrap(),
ClosingDelimiters::parse(closing, style),
Range::default(),
)
.with_remainder(CommentRemainder::Empty)
}
#[test]
fn test_comment_tag_parse_table() {
for ok in [
"v4",
"V4",
"v4.2",
"v4.2.0",
"4.2.0",
"v4.2.0-rc.1",
"v1+build",
] {
assert_eq!(CommentTag::parse(ok).unwrap().as_str(), ok, "{ok}");
}
for bad in [
"", "stable", "main", "1234", "20240501", "v1 2", "v1\tx", "v1-a\nb", "v1-#x",
"v1.2.3.4",
] {
assert!(CommentTag::parse(bad).is_none(), "{bad:?}");
}
}
#[test]
fn test_comment_tag_is_full_version() {
for full in ["v4.2.0", "V4.2.0", "4.2.0", "v4.2.0-rc.1", "v1.0.0+build"] {
assert!(CommentTag::parse(full).unwrap().is_full_version(), "{full}");
}
for alias in ["v4", "v4.2", "v4-beta", "v2.1-rc"] {
assert!(
!CommentTag::parse(alias).unwrap().is_full_version(),
"{alias}"
);
}
}
fn truncated_index_1762() -> TagIndex {
TagIndex::from_tags([("v4.2.0", &sha('b'))]).with_coverage(ListCoverage::Truncated)
}
#[test]
fn test_comment_check_truncated_index_contradicted_comment_is_mismatch() {
let index = truncated_index_1762();
let check = |tag: &str| {
CommentCheck::evaluate(
Some(&index),
&sha('a'),
Some(&comment(tag, "", TScalarStyle::Plain)),
)
};
assert_eq!(
check("v4.2.0"),
CommentCheck::Mismatch(CommentMismatch::CommentNamesOtherCommit)
);
for variant in ["4.2.0", "V4.2.0", "v4.2.0+build"] {
assert_eq!(
check(variant),
CommentCheck::Mismatch(CommentMismatch::CommentNamesOtherCommit),
"{variant}"
);
}
for benign in ["v4", "v9.9.9"] {
assert_eq!(check(benign), CommentCheck::Unverifiable, "{benign}");
}
assert_eq!(
CommentCheck::evaluate(
Some(&index),
&sha('b'),
Some(&comment("v4.2.0", "", TScalarStyle::Plain))
),
CommentCheck::Confirmed
);
}
#[test]
fn test_comment_names_other_commit_message_and_hover() {
let name = PackageName::new("a/b");
let tag = CommentTag::parse("v4.2.0").unwrap();
let diagnostic = sha_comment_mismatch_diagnostic(
Range::default(),
&name,
&sha('a'),
&tag,
&CommentMismatch::CommentNamesOtherCommit,
Severity::Warning,
);
assert_eq!(
diagnostic.code(),
Some(SHA_COMMENT_MISMATCH_DIAGNOSTIC_CODE)
);
assert!(diagnostic.message().contains("`v4.2.0`"));
assert!(
diagnostic
.message()
.contains("is not the commit of `v4.2.0`")
);
let hover = sha_comment_mismatch_hover_line(
&sha('a'),
&tag,
&CommentMismatch::CommentNamesOtherCommit,
);
assert!(hover.starts_with("**Warning**"));
assert!(hover.contains("v4.2.0"));
}
#[cfg(feature = "lsp-responses")]
#[test]
fn test_build_sha_comment_fix_action_offers_only_a_clean_other_tag() {
let uri = url::Url::parse("file:///repo/ci.yml").unwrap();
let range = Range::new(Position::new(0, 6), Position::new(0, 60));
let other = |tag: &str| CommentMismatch::ShaIsOtherTag {
actual: ConcreteVersion::new(tag),
};
let stale = comment("v3", "", TScalarStyle::Plain);
let action = build_sha_comment_fix_action(&uri, range, &stale, &other("v4.2.0")).unwrap();
assert_eq!(action.title, "Correct version comment to `v4.2.0`");
for refused in [
CommentMismatch::ShaNotInIndex,
CommentMismatch::CommentNamesOtherCommit,
other("stable"),
other("v4\u{202e}"),
] {
assert!(
build_sha_comment_fix_action(&uri, range, &stale, &refused).is_none(),
"{refused:?}"
);
}
let punctuated = ShaPinComment::new(
CommentTag::parse("v4-beta,").unwrap(),
ClosingDelimiters::default(),
Range::default(),
);
assert!(build_sha_comment_fix_action(&uri, range, &punctuated, &other("v4.2.0")).is_none());
}
#[test]
fn test_closing_delimiters_parse() {
use TScalarStyle::{DoubleQuoted, Folded, Literal, Plain, SingleQuoted};
let cases = [
(" # v4", Plain, ""),
("\" # v4", DoubleQuoted, "\""),
("' # v4", SingleQuoted, "'"),
("} # v4", Plain, "}"),
(" \t} # v4", Plain, " \t}"),
("\"} # v4", DoubleQuoted, "\"}"),
("\" } # v4", DoubleQuoted, "\" }"),
("\"}} # v4", DoubleQuoted, "\"}"),
(" , x: 1}", Plain, ""),
("# v4", Literal, ""),
("# v4", Folded, ""),
(" # v4", DoubleQuoted, ""),
("' # v4", DoubleQuoted, ""),
];
for (tail, style, expected) in cases {
let parsed = ClosingDelimiters::parse(tail, style);
assert_eq!(parsed.to_string(), expected, "{tail:?} {style:?}");
assert_eq!(parsed.byte_len(), expected.len());
assert_eq!(parsed.is_empty(), expected.is_empty());
}
}
#[test]
fn test_read_tail_plain_comment() {
let s = "a".repeat(40);
let content = format!("ref: {s} # v4.2.0\n");
let read = read(&content, TScalarStyle::Plain, true, 5 + 40);
assert_eq!(
read.tail,
ShaPinTail::Commented(ShaPinComment {
tag_range: Range::new(Position::new(0, 48), Position::new(0, 54)),
..comment("v4.2.0", "", TScalarStyle::Plain)
})
);
assert_eq!(
content.get(5..read.range_end).unwrap(),
format!("{s} # v4.2.0")
);
}
#[test]
fn test_read_tail_tag_range_counts_utf16_units() {
let s = "a".repeat(40);
let content = format!("😀: {s} # v4.2.0\n");
let read = read(&content, TScalarStyle::Plain, true, 4 + 2 + 40);
assert_eq!(
read.tail.comment().unwrap().tag_range,
Range::new(Position::new(0, 47), Position::new(0, 53))
);
}
#[test]
fn test_comment_slot_after_matches_read_tail_slot() {
let s = "a".repeat(40);
for (content, style, plain, ref_end) in [
(format!("ref: {s}\n"), TScalarStyle::Plain, true, 45),
(
format!("ref: \"{s}\"\n"),
TScalarStyle::DoubleQuoted,
false,
46,
),
(
format!("{{ref: {s}, x: 1}}\n"),
TScalarStyle::Plain,
true,
46,
),
] {
let table = LineOffsetTable::new(&content);
assert_eq!(
ShaPinTail::Bare(comment_slot_after(&content, &table, ref_end, 1, plain)),
read(&content, style, plain, ref_end).tail,
"{content:?}"
);
}
}
#[test]
fn test_read_tail_partial_precision_comments() {
let s = "a".repeat(40);
for tag in ["v4", "v4.2", "4.2.0"] {
let content = format!("ref: {s} # {tag}");
let read = read(&content, TScalarStyle::Plain, true, 45);
assert_eq!(read.tail.comment().unwrap().tag.as_str(), tag);
assert_eq!(read.range_end, content.len());
}
}
#[test]
fn test_read_tail_quoted_and_flow_closers() {
let s = "a".repeat(40);
let double = format!("ref: \"{s}\" # v4\n");
let read_double = read(&double, TScalarStyle::DoubleQuoted, false, 6 + 40);
assert_eq!(
read_double.tail,
ShaPinTail::Commented(ShaPinComment {
tag_range: Range::new(Position::new(0, 50), Position::new(0, 52)),
..comment("v4", "\"", TScalarStyle::DoubleQuoted)
})
);
assert_eq!(
double.get(6..read_double.range_end).unwrap(),
format!("{s}\" # v4")
);
let single = format!("ref: '{s}' # v4");
let read_single = read(&single, TScalarStyle::SingleQuoted, false, 6 + 40);
assert_eq!(read_single.tail.comment().unwrap().closing.to_string(), "'");
let flow = format!("{{ref: {s} }} # v4");
let read_flow = read(&flow, TScalarStyle::Plain, true, 6 + 40);
assert_eq!(read_flow.tail.comment().unwrap().closing.to_string(), " }");
let flow_tight = format!("{{ref: {s}}} # v4");
let read_tight = read(&flow_tight, TScalarStyle::Plain, true, 6 + 40);
assert_eq!(read_tight.tail.comment().unwrap().closing.to_string(), "}");
}
#[test]
fn test_read_tail_bare_slots() {
let s = "a".repeat(40);
let plain = format!("ref: {s}\n");
assert_eq!(
read(&plain, TScalarStyle::Plain, true, 45).tail,
ShaPinTail::Bare(CommentSlot::Appendable)
);
let plain_note = format!("ref: {s} # why\n");
assert_eq!(
read(&plain_note, TScalarStyle::Plain, true, 45).tail,
ShaPinTail::Bare(CommentSlot::Appendable)
);
let quoted = format!("ref: \"{s}\"\n");
assert_eq!(
read("ed, TScalarStyle::DoubleQuoted, false, 46).tail,
ShaPinTail::Bare(CommentSlot::Unavailable)
);
let flow = format!("{{ref: {s}, x: 1}}\n");
assert_eq!(
read(&flow, TScalarStyle::Plain, true, 46).tail,
ShaPinTail::Bare(CommentSlot::Unavailable)
);
let block = format!("ref: {s}\n");
assert_eq!(
read(&block, TScalarStyle::Literal, false, 45).tail,
ShaPinTail::Bare(CommentSlot::Unavailable)
);
}
#[test]
fn test_read_tail_range_end_is_sha_end_without_comment() {
let s = "a".repeat(40);
let content = format!("ref: {s} \n");
assert_eq!(read(&content, TScalarStyle::Plain, true, 45).range_end, 45);
}
#[test]
fn test_read_tail_hash_not_preceded_by_blank_is_not_a_comment() {
let s = "a".repeat(40);
let content = format!("ref: {s}#v4\n");
let read = read(&content, TScalarStyle::Plain, true, 45);
assert_eq!(read.tail, ShaPinTail::Bare(CommentSlot::Unavailable));
}
#[test]
fn test_read_tail_later_flow_token_comment_is_not_attributed() {
let s = "a".repeat(40);
let content = format!("{{ref: {s}, x: 1}} # v4\n");
let read = read(&content, TScalarStyle::Plain, true, 46);
assert_eq!(read.tail, ShaPinTail::Bare(CommentSlot::Unavailable));
}
#[test]
fn test_read_tail_non_version_comment_is_bare() {
let s = "a".repeat(40);
for note in ["cargo-deny", "main", "20240501", "1234"] {
let content = format!("ref: {s} # {note}\n");
let read = read(&content, TScalarStyle::Plain, true, 45);
assert_eq!(
read.tail,
ShaPinTail::Bare(CommentSlot::Appendable),
"{note}"
);
}
}
#[test]
fn test_read_tail_truncated_window_rejects_token_of_unknown_extent() {
let s = "a".repeat(40);
let padding = " ".repeat(REST_OF_LINE_WINDOW_BYTES - 8);
let content = format!("ref: {s}{padding}# v4.2.100 more\n");
let read = read(&content, TScalarStyle::Plain, true, 45);
assert_eq!(read.tail, ShaPinTail::Bare(CommentSlot::Appendable));
let fits = format!("ref: {s} # v4.2.100 more\n");
let read = read_tail_ok(&fits);
assert_eq!(read.tail.comment().unwrap().tag.as_str(), "v4.2.100");
}
fn read_tail_ok(content: &str) -> ShaPinTailRead {
read(content, TScalarStyle::Plain, true, 45)
}
#[test]
fn test_read_tail_truncated_all_blank_window_is_not_last_on_line() {
let s = "a".repeat(40);
let padding = " ".repeat(REST_OF_LINE_WINDOW_BYTES + 16);
let content = format!("{{ref: {s}{padding}, x: 1}}\n");
let read = read(&content, TScalarStyle::Plain, true, 46);
assert_eq!(read.tail, ShaPinTail::Bare(CommentSlot::Unavailable));
}
#[test]
fn test_read_tail_comment_found_within_truncated_window_is_definitive() {
let s = "a".repeat(40);
let tail = "x".repeat(REST_OF_LINE_WINDOW_BYTES * 2);
let content = format!("ref: {s} # v4 {tail}\n");
let read = read(&content, TScalarStyle::Plain, true, 45);
assert_eq!(read.tail.comment().unwrap().tag.as_str(), "v4");
}
#[test]
fn test_read_tail_multibyte_after_ref_does_not_panic() {
let s = "a".repeat(40);
let content = format!("ref: {s} # v4 {}\n", "é".repeat(REST_OF_LINE_WINDOW_BYTES));
let read = read(&content, TScalarStyle::Plain, true, 45);
assert_eq!(read.tail.comment().unwrap().tag.as_str(), "v4");
}
#[test]
fn test_read_tail_second_line_and_crlf() {
let s = "a".repeat(40);
let content = format!("x: 1\r\nref: {s} # v4\r\ny: 2\r\n");
let table = LineOffsetTable::new(&content);
let ref_end = 6 + 5 + 40;
let read = read_sha_pin_tail(&content, &table, ref_end, 2, TScalarStyle::Plain, true);
assert_eq!(read.tail.comment().unwrap().tag.as_str(), "v4");
assert_eq!(read.range_end, ref_end + " # v4".len());
}
#[test]
fn test_read_tail_ref_end_past_line_does_not_panic() {
let content = "ref: x\n";
let table = LineOffsetTable::new(content);
let read = read_sha_pin_tail(content, &table, 100, 1, TScalarStyle::Plain, true);
assert_eq!(read.range_end, 100);
assert_matches!(read.tail, ShaPinTail::Bare(_));
}
#[test]
fn test_ref_is_last_on_line() {
let cases = [
("ref: abc\n", 8, true),
("ref: abc \n", 8, true),
("ref: abc # note\n", 8, true),
("ref: abc#x\n", 8, false),
("{ref: abc, x: 1}\n", 9, false),
("{ref: abc}\n", 9, false),
("ref: abc", 8, true),
];
for (content, ref_end, expected) in cases {
let table = LineOffsetTable::new(content);
assert_eq!(
ref_is_last_on_line(content, &table, ref_end, 1),
expected,
"{content:?}"
);
}
}
#[test]
fn test_sha_pin_rewrite_table() {
use TScalarStyle::{DoubleQuoted, Plain, SingleQuoted};
let new = sha('b');
let n = new.as_str();
let target = ConcreteVersion::new("v4.3.0");
let cases = [
(
ShaPinTail::Commented(comment("v4.2.0", "", Plain)),
format!("{n} # v4.3.0"),
),
(
ShaPinTail::Commented(comment("v4", "\"", DoubleQuoted)),
format!("{n}\" # v4.3.0"),
),
(
ShaPinTail::Commented(comment("v4", "'", SingleQuoted)),
format!("{n}' # v4.3.0"),
),
(
ShaPinTail::Commented(comment("v4", " }", Plain)),
format!("{n} }} # v4.3.0"),
),
(
ShaPinTail::Bare(CommentSlot::Appendable),
format!("{n} # v4.3.0"),
),
(ShaPinTail::Bare(CommentSlot::Unavailable), n.to_string()),
];
for (tail, expected) in cases {
assert_eq!(sha_pin_rewrite(&tail, &new, &target), expected, "{tail:?}");
}
}
#[test]
fn test_sha_pin_rewrite_non_version_target_never_writes_a_comment() {
let new = sha('b');
let n = new.as_str();
for name in [
"stable",
"main",
"1234",
"v1 x",
"v1\n# x",
"v1-\u{202e}x",
"v4-\u{3b2}",
] {
let target = ConcreteVersion::new(name);
assert_eq!(
sha_pin_rewrite(&ShaPinTail::Bare(CommentSlot::Appendable), &new, &target),
n,
"{name:?}"
);
assert_eq!(
sha_pin_rewrite(
&ShaPinTail::Commented(
comment("v4", "\"", TScalarStyle::DoubleQuoted)
.with_remainder(CommentRemainder::Empty)
),
&new,
&target
),
format!("{n}\""),
"{name:?}"
);
}
}
fn rewrite_line(prefix: &str, quote: &str, comment_text: &str) -> String {
let old = "a".repeat(40);
let new = sha('b');
let style = if quote.is_empty() {
TScalarStyle::Plain
} else {
TScalarStyle::DoubleQuoted
};
let head = format!("{prefix}{quote}");
let content = format!("{head}{old}{quote}{comment_text}\n");
let start = head.len();
let table = LineOffsetTable::new(&content);
let read = read_sha_pin_tail(&content, &table, start + 40, 1, style, quote.is_empty());
let rewritten = sha_pin_rewrite(&read.tail, &new, &ConcreteVersion::new("stable"));
format!(
"{}{}{}",
content.get(..start).unwrap(),
rewritten,
content.get(read.range_end..).unwrap()
)
}
#[test]
fn test_sha_pin_rewrite_non_tag_target_keeps_trailing_comment_words_inside_comment() {
let n = "b".repeat(40);
let cases = [
(
"c: x@",
"",
" # 1.0.0 pinned for CVE",
format!("c: x@{n} # pinned for CVE\n"),
),
(
"ref: ",
"\"",
" # v1.0.0 pinned",
format!("ref: \"{n}\" # pinned\n"),
),
("ref: ", "", " # v1.0.0 \t", format!("ref: {n} \t\n")),
("ref: ", "", " # v1.0.0", format!("ref: {n}\n")),
("ref: ", "\"", " # v1.0.0", format!("ref: \"{n}\"\n")),
];
for (prefix, quote, text, expected) in cases {
assert_eq!(rewrite_line(prefix, quote, text), expected, "{text:?}");
}
}
#[test]
fn test_read_tail_records_comment_remainder() {
let s = "a".repeat(40);
let remainder = |text: &str| {
let content = format!("ref: {s}{text}\n");
read(&content, TScalarStyle::Plain, true, 45)
.tail
.comment()
.unwrap()
.remainder
};
assert_eq!(remainder(" # v4"), CommentRemainder::Empty);
assert_eq!(remainder(" # v4 "), CommentRemainder::Empty);
assert_eq!(remainder(" # v4 words"), CommentRemainder::Text);
}
#[test]
fn test_sha_pin_rewrite_is_idempotent_through_reparse() {
let s = sha('a');
let new = sha('b');
let target = ConcreteVersion::new("v4.3.0");
for (style, plain, quote) in [
(TScalarStyle::Plain, true, ""),
(TScalarStyle::DoubleQuoted, false, "\""),
] {
let content = format!("ref: {quote}{} # v4.2.0\n", s.as_str());
let first = read(&content, style, plain, 5 + quote.len() + 40);
let rewritten = sha_pin_rewrite(&first.tail, &new, &target);
let updated = format!("ref: {quote}{rewritten}\n");
let second = read(&updated, style, plain, 5 + quote.len() + 40);
assert_eq!(
sha_pin_rewrite(&second.tail, &new, &target),
rewritten,
"{style:?}"
);
assert_eq!(second.range_end, updated.trim_end().len());
}
}
fn check(index: Option<&TagIndex>, pin: &CommitSha, tag: Option<&str>) -> CommentCheck {
let comment = tag.map(|t| comment(t, "", TScalarStyle::Plain));
CommentCheck::evaluate(index, pin, comment.as_ref())
}
#[test]
fn test_comment_check_no_comment_and_unverifiable() {
let a = sha('a');
let index = TagIndex::from_tags([("v1.0.0", &a)]);
assert_eq!(check(Some(&index), &a, None), CommentCheck::NoComment);
assert_eq!(check(None, &a, None), CommentCheck::NoComment);
assert_eq!(check(None, &a, Some("v1.0.0")), CommentCheck::Unverifiable);
assert_eq!(
check(Some(&TagIndex::default()), &a, Some("v1.0.0")),
CommentCheck::Unverifiable
);
}
#[test]
fn test_comment_check_confirmed_forms() {
let a = sha('a');
let index = TagIndex::from_tags([("v4.3.1", &a), ("v4", &a)]);
for tag in ["v4.3.1", "4.3.1", "v4", "v4.3"] {
assert_eq!(
check(Some(&index), &a, Some(tag)),
CommentCheck::Confirmed,
"{tag}"
);
}
let upper = CommitSha::parse(&"A".repeat(40)).unwrap();
assert_eq!(
check(Some(&index), &upper, Some("v4")),
CommentCheck::Confirmed
);
}
#[test]
fn test_comment_check_mismatch_forms() {
let a = sha('a');
let b = sha('b');
let index = TagIndex::from_tags([("v1.0.0", &a), ("v2.0.0", &b)]);
assert_eq!(
check(Some(&index), &a, Some("v2.0.0")),
CommentCheck::Mismatch(CommentMismatch::ShaIsOtherTag {
actual: ConcreteVersion::new("v1.0.0")
})
);
assert_eq!(
check(Some(&index), &sha('c'), Some("v2.0.0")),
CommentCheck::Mismatch(CommentMismatch::ShaNotInIndex)
);
let truncated =
TagIndex::from_tags([("v1.0.0", &a)]).with_coverage(ListCoverage::Truncated);
assert_eq!(
check(Some(&truncated), &sha('c'), Some("v2.0.0")),
CommentCheck::Unverifiable
);
}
#[test]
fn test_comment_check_prerelease_commit_is_named_only_by_prerelease_comment() {
let a = sha('a');
let index = TagIndex::from_tags([("v4.3.1-rc.1", &a)]);
assert_matches!(
check(Some(&index), &a, Some("v4")),
CommentCheck::Mismatch(_)
);
assert_eq!(
check(Some(&index), &a, Some("v4.3.1-rc.1")),
CommentCheck::Confirmed
);
}
#[test]
fn test_mismatch_diagnostic_text_and_code() {
let range = Range::new(Position::new(1, 2), Position::new(1, 50));
let name = PackageName::new("a/b");
let a = sha('a');
let tag = CommentTag::parse("v2.0.0").unwrap();
let other = sha_comment_mismatch_diagnostic(
range,
&name,
&a,
&tag,
&CommentMismatch::ShaIsOtherTag {
actual: ConcreteVersion::new("v1.0.0"),
},
Severity::Warning,
);
assert_eq!(
other.message(),
"a/b: SHA aaaaaaa is not the commit of `v2.0.0` named in the comment (it is `v1.0.0`)"
);
assert_eq!(other.code(), Some(SHA_COMMENT_MISMATCH_DIAGNOSTIC_CODE));
let missing = sha_comment_mismatch_diagnostic(
range,
&name,
&a,
&tag,
&CommentMismatch::ShaNotInIndex,
Severity::Warning,
);
assert_eq!(
missing.message(),
"a/b: SHA aaaaaaa is not the commit of any release tag; the comment names `v2.0.0`"
);
}
#[test]
fn test_mismatch_hover_line_text() {
let a = sha('a');
let tag = CommentTag::parse("v2.0.0").unwrap();
assert_eq!(
sha_comment_mismatch_hover_line(
&a,
&tag,
&CommentMismatch::ShaIsOtherTag {
actual: ConcreteVersion::new("v1.0.0")
}
),
"**Warning**: comment says `v2.0.0`, but SHA `aaaaaaa…` is `v1.0.0`"
);
assert_eq!(
sha_comment_mismatch_hover_line(&a, &tag, &CommentMismatch::ShaNotInIndex),
"**Warning**: SHA `aaaaaaa…` is not the commit of any release tag; comment says `v2.0.0`"
);
}
#[test]
fn test_position_past_sha_boundaries() {
let range = Range::new(Position::new(2, 10), Position::new(2, 60));
assert!(position_past_sha(range, Position::new(2, 51)));
assert!(!position_past_sha(range, Position::new(2, 50)));
assert!(!position_past_sha(range, Position::new(2, 9)));
assert!(!position_past_sha(range, Position::new(2, 61)));
assert!(!position_past_sha(range, Position::new(1, 55)));
}
}