diff --git a/crates/pollard-cli/src/main.rs b/crates/pollard-cli/src/main.rs index 27b3c8f5..9aec33e8 100644 --- a/crates/pollard-cli/src/main.rs +++ b/crates/pollard-cli/src/main.rs @@ -85,6 +85,12 @@ enum MutateAction { #[arg(long)] mutant: Hash, }, + Describe { + #[arg(short, long)] + file: PathBuf, + #[arg(long)] + mutant: Hash, + }, } fn render_and_apply(actions: Vec, reports: Vec>, session: &Session) { @@ -191,6 +197,16 @@ fn main() { let (actions, report) = mutate::apply::run(&session.language, input, hash); (actions, vec![Box::new(report)]) } + Command::Mutate { + action: + MutateAction::Describe { + file: input, + mutant: hash, + }, + } => { + let (actions, report) = mutate::describe::run(&session.language, input, hash); + (actions, vec![Box::new(report)]) + } Command::Step { action: StepAction::FindFiles, } => { diff --git a/crates/pollard-cli/src/mutate/describe.rs b/crates/pollard-cli/src/mutate/describe.rs new file mode 100644 index 00000000..d44531f8 --- /dev/null +++ b/crates/pollard-cli/src/mutate/describe.rs @@ -0,0 +1,101 @@ +use crate::io::{Action, Render, Report, Style, hashed_path}; +use crate::mutate::find_description; +use pollard_core::config::LanguageId; +use pollard_core::{Hash, MutationKind, SourceFile}; +use serde::Serialize; +use std::path::{Path, PathBuf}; + +pub fn run(language: &LanguageId, input: &Path, hash: &Hash) -> (Vec, DescribeReport) { + let file = SourceFile::read(input).expect("failed to read input file"); + let desc = find_description(language, &file, hash); + + let report = DescribeReport { + path: desc.path, + kind: desc.kind, + start_line: desc.start_line, + start_char: desc.start_char, + end_line: desc.end_line, + end_char: desc.end_char, + original: desc.original, + replacement: desc.replacement, + mutated_hash: desc.mutated_hash, + }; + + (vec![], report) +} + +#[derive(Serialize)] +pub struct DescribeReport { + pub path: PathBuf, + pub kind: MutationKind, + pub mutated_hash: Hash, + pub start_line: usize, + pub start_char: usize, + pub end_line: usize, + pub end_char: usize, + pub original: String, + pub replacement: String, +} + +impl DescribeReport { + fn location(&self) -> String { + format!( + "{}:{}-{}:{}", + self.start_line + 1, + self.start_char + 1, + self.end_line + 1, + self.end_char + 1, + ) + } +} + +impl Render for DescribeReport { + fn render(&self, style: &Style, no_color: bool, depth: u8) { + let path = self.path.display(); + let loc = self.location(); + + match style { + Style::Json => { + println!( + "{}", + serde_json::to_string(self).expect("failed to serialize") + ); + } + Style::Markdown => { + let heading = "#".repeat((depth + 1).min(6) as usize); + println!("{heading} Mutation\n"); + print!("**Kind:** "); + self.kind.render(style, no_color, depth + 1); + println!("\n"); + println!("**File:** `{path}`\n"); + println!("**Location:** {loc}\n"); + println!("**Original:**\n```\n{}\n```\n", self.original); + println!("**Replacement:**\n```\n{}\n```", self.replacement); + } + Style::Pretty => { + print!("\x1b[1m"); + self.kind.render(style, no_color, depth + 1); + println!("\x1b[0m at \x1b[36m{path}:{loc}\x1b[0m"); + println!("\x1b[31m{}\x1b[0m", self.original); + println!("\x1b[32m{}\x1b[0m", self.replacement); + } + Style::Plain => { + self.kind.render(style, no_color, depth + 1); + println!(" at {path}:{loc}"); + println!("{}", self.original); + println!("{}", self.replacement); + } + } + } +} + +impl Report for DescribeReport { + fn get_dir(&self, session: &pollard_session::Session) -> PathBuf { + session.report_dir.join("mutate").join("describe") + } + + fn make_path(&self, session: &pollard_session::Session) -> PathBuf { + let content = serde_json::to_string(self).expect("failed to serialize"); + hashed_path(&self.get_dir(session), &content, "describe") + } +} diff --git a/crates/pollard-cli/src/mutate/mod.rs b/crates/pollard-cli/src/mutate/mod.rs index 707c78e2..09023616 100644 --- a/crates/pollard-cli/src/mutate/mod.rs +++ b/crates/pollard-cli/src/mutate/mod.rs @@ -1,4 +1,5 @@ pub mod apply; +pub mod describe; pub mod generate; pub mod view; @@ -6,8 +7,11 @@ use pollard_core::config::LanguageId; use pollard_core::languages::javascript::JavaScript; use pollard_core::languages::typescript::TypeScript; use pollard_core::{ - Hash, Language, MutatedFile, SourceFile, find_mutation_points, generate_mutation_substitutions, + Hash, Language, MutatedFile, MutationKind, SourceFile, find_mutation_points, + generate_mutation_substitutions, }; +use std::path::PathBuf; + fn find_mutated_by_hash<'a, L: Language>( file: &'a SourceFile, target: &Hash, @@ -37,3 +41,59 @@ fn find_mutated<'a>( std::process::exit(1); }) } + +pub struct MutationDescription { + pub path: PathBuf, + pub kind: MutationKind, + pub start_line: usize, + pub start_char: usize, + pub end_line: usize, + pub end_char: usize, + pub original: String, + pub replacement: String, + pub mutated_hash: Hash, +} + +fn find_description_by_hash( + file: &SourceFile, + target: &Hash, +) -> Option +where + L::Kind: Copy + Into, +{ + let points = find_mutation_points::(file); + for point in &points { + for (replacement, mutated) in generate_mutation_substitutions::(&point) { + if mutated.hash() == target { + return Some(MutationDescription { + path: file.path().to_owned(), + kind: point.kind.into(), + start_line: point.span.start.line, + start_char: point.span.start.char, + end_line: point.span.end.line, + end_char: point.span.end.char, + original: file.content()[point.span.start.byte..point.span.end.byte] + .to_string(), + replacement, + mutated_hash: mutated.hash().clone(), + }); + } + } + } + None +} + +fn find_description( + language: &LanguageId, + file: &SourceFile, + hash: &Hash, +) -> MutationDescription { + match language { + LanguageId::Javascript => find_description_by_hash::(file, hash), + LanguageId::Typescript => find_description_by_hash::(file, hash), + } + .unwrap_or_else(|| { + eprintln!("no mutation found with hash {hash}"); + std::process::exit(1); + }) +} diff --git a/crates/pollard-core/src/lib.rs b/crates/pollard-core/src/lib.rs index 67c45818..88ba4337 100644 --- a/crates/pollard-core/src/lib.rs +++ b/crates/pollard-core/src/lib.rs @@ -291,6 +291,121 @@ pub struct MutationSubstitution<'a, 'b, L: Language> { pub replacement: String, } +impl io::Render for BinaryOpKind { + fn render(&self, style: &io::Style, _no_color: bool, _depth: u8) { + match style { + io::Style::Json => { + println!( + "{}", + serde_json::to_string(self).expect("failed to serialize") + ); + } + _ => { + let label = match self { + BinaryOpKind::Add => "+", + BinaryOpKind::Sub => "-", + BinaryOpKind::Mul => "*", + BinaryOpKind::Div => "/", + BinaryOpKind::And => "&&", + BinaryOpKind::Or => "||", + BinaryOpKind::StrictEq => "===", + BinaryOpKind::StrictNeq => "!==", + BinaryOpKind::Eq => "==", + BinaryOpKind::Neq => "!=", + BinaryOpKind::Lt => "<", + BinaryOpKind::Lte => "<=", + BinaryOpKind::Gt => ">", + BinaryOpKind::Gte => ">=", + }; + print!("{label}"); + } + } + } +} + +impl io::Render for MutationKind { + fn render(&self, style: &io::Style, no_color: bool, depth: u8) { + match style { + io::Style::Json => { + println!( + "{}", + serde_json::to_string(self).expect("failed to serialize") + ); + } + _ => match self { + MutationKind::StatementBlock => print!("empty statement block"), + MutationKind::BinaryOp(op) => { + print!("binary operator "); + op.render(style, no_color, depth); + } + }, + } + } +} + +impl io::Render for MutationSubstitution<'_, '_, L> +where + L::Kind: Clone + Into, +{ + fn render(&self, style: &io::Style, no_color: bool, depth: u8) { + let point = self.point; + let original = + &point.file.content()[point.span.start.byte..point.span.end.byte]; + let kind: MutationKind = point.kind.clone().into(); + let path = point.file.path().display(); + let loc = format!( + "{}:{}-{}:{}", + point.span.start.line + 1, + point.span.start.char + 1, + point.span.end.line + 1, + point.span.end.char + 1, + ); + + match style { + io::Style::Json => { + println!( + "{}", + serde_json::to_string(&serde_json::json!({ + "path": path.to_string(), + "location": loc, + "kind": kind, + "original": original, + "replacement": self.replacement, + })) + .expect("failed to serialize") + ); + } + io::Style::Markdown => { + let heading = "#".repeat((depth + 1).min(6) as usize); + println!("{heading} Mutation\n"); + print!("**Kind:** "); + kind.render(style, no_color, depth + 1); + println!("\n"); + println!("**File:** `{path}`\n"); + println!("**Location:** {loc}\n"); + println!("**Original:**\n```\n{original}\n```\n"); + println!( + "**Replacement:**\n```\n{}\n```", + self.replacement + ); + } + io::Style::Pretty => { + print!("\x1b[1m"); + kind.render(style, no_color, depth + 1); + println!("\x1b[0m at \x1b[36m{path}:{loc}\x1b[0m"); + println!("\x1b[31m{original}\x1b[0m"); + println!("\x1b[32m{}\x1b[0m", self.replacement); + } + io::Style::Plain => { + kind.render(style, no_color, depth + 1); + println!(" at {path}:{loc}"); + println!("{original}"); + println!("{}", self.replacement); + } + } + } +} + pub fn generate_mutation_substitutions<'a, L: Language>( point: &MutationPoint<'a, L>, ) -> Vec<(String, MutatedFile<'a>)> {