use std::path::Path;
use crate::error::{Result, TuicrError};
use crate::model::{DiffFile, DiffLine, FileStatus};
use crate::syntax::SyntaxHighlighter;
use super::traits::{VcsBackend, VcsInfo};
pub struct PrNoopVcs {
info: VcsInfo,
}
impl PrNoopVcs {
pub fn new(info: VcsInfo) -> Self {
Self { info }
}
}
impl VcsBackend for PrNoopVcs {
fn info(&self) -> &VcsInfo {
&self.info
}
fn get_working_tree_diff(&self, _highlighter: &SyntaxHighlighter) -> Result<Vec<DiffFile>> {
Err(TuicrError::UnsupportedOperation(
"PR mode does not read from the local working tree".to_string(),
))
}
fn fetch_context_lines(
&self,
_file_path: &Path,
_file_status: FileStatus,
_start_line: u32,
_end_line: u32,
) -> Result<Vec<DiffLine>> {
Ok(Vec::new())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::vcs::traits::VcsType;
use std::path::PathBuf;
fn info() -> VcsInfo {
VcsInfo {
root_path: PathBuf::from("forge:github.com/a/b"),
head_commit: "abc".to_string(),
branch_name: Some("main".to_string()),
vcs_type: VcsType::File,
}
}
#[test]
fn should_return_empty_context_lines_from_noop_backend() {
let vcs = PrNoopVcs::new(info());
let lines = vcs
.fetch_context_lines(&PathBuf::from("x"), FileStatus::Modified, 1, 5)
.unwrap();
assert!(lines.is_empty());
}
#[test]
fn should_reject_working_tree_diff_on_noop_backend() {
let vcs = PrNoopVcs::new(info());
let err = vcs
.get_working_tree_diff(&SyntaxHighlighter::default())
.unwrap_err();
assert!(
err.to_string()
.contains("PR mode does not read from the local working tree"),
"unexpected error: {err}"
);
}
}