Skip to content

Commit c8c636a

Browse files
committed
cargo-rail: clean up duplicate methods; merge unify tests; move tests into the integration/ instead of orphaned out.
1 parent a9e2c81 commit c8c636a

File tree

6 files changed

+626
-670
lines changed

6 files changed

+626
-670
lines changed

src/commands/affected.rs

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//! - The minimal set of crates that need testing/building
77
88
use crate::change_detection::classify::{ChangeKind, classify_file};
9+
use crate::commands::common::OutputFormat;
910
use crate::config::ChangeDetectionConfig;
1011
use crate::error::{RailError, RailResult};
1112
use crate::git::detect_default_base_ref;
@@ -16,37 +17,6 @@ use glob::Pattern;
1617
use std::io::Write;
1718
use std::path::PathBuf;
1819

19-
/// Output format for affected command
20-
#[derive(Debug, Clone, Copy, PartialEq)]
21-
enum OutputFormat {
22-
Text,
23-
Json,
24-
NamesOnly,
25-
/// GitHub Actions output format for $GITHUB_OUTPUT
26-
GitHub,
27-
/// GitHub Actions matrix format for strategy.matrix
28-
GitHubMatrix,
29-
/// JSON Lines format (one object per line)
30-
JsonLines,
31-
}
32-
33-
impl OutputFormat {
34-
fn from_str(s: &str) -> RailResult<Self> {
35-
match s.to_lowercase().as_str() {
36-
"text" => Ok(Self::Text),
37-
"json" => Ok(Self::Json),
38-
"names" | "names-only" => Ok(Self::NamesOnly),
39-
"github" => Ok(Self::GitHub),
40-
"github-matrix" => Ok(Self::GitHubMatrix),
41-
"jsonl" | "json-lines" => Ok(Self::JsonLines),
42-
_ => Err(RailError::message(format!(
43-
"Unknown format '{}'. Valid formats: text, json, names-only, github, github-matrix, jsonl",
44-
s
45-
))),
46-
}
47-
}
48-
}
49-
5020
/// Run the affected command
5121
pub fn run_affected(
5222
ctx: &WorkspaceContext,
@@ -57,13 +27,10 @@ pub fn run_affected(
5727
all: bool,
5828
output: Option<PathBuf>,
5929
) -> RailResult<()> {
60-
let output_format = OutputFormat::from_str(&format)?;
30+
let output_format: OutputFormat = format.parse()?;
6131

6232
// JSON-like formats enable structured error output and suppress progress
63-
if matches!(
64-
output_format,
65-
OutputFormat::Json | OutputFormat::JsonLines | OutputFormat::GitHub | OutputFormat::GitHubMatrix
66-
) {
33+
if output_format.is_json_like() {
6734
crate::output::set_json_mode(true);
6835
}
6936

src/commands/common.rs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,39 @@ use crate::workspace::WorkspaceContext;
88
use std::path::PathBuf;
99
use std::str::FromStr;
1010

11-
/// Standard output format for most commands
11+
/// Standard output format for all commands
1212
///
13-
/// Use this for commands that just need text/json output.
14-
/// For commands with more formats (like `affected`), define a specialized enum.
13+
/// Supports both simple (text/json) and specialized formats (GitHub Actions, JSONL, etc.)
1514
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
1615
pub enum OutputFormat {
1716
/// Human-readable text output (default)
1817
#[default]
1918
Text,
2019
/// Machine-readable JSON output
2120
Json,
21+
/// Names only (one per line)
22+
NamesOnly,
23+
/// GitHub Actions output format for $GITHUB_OUTPUT
24+
GitHub,
25+
/// GitHub Actions matrix format for strategy.matrix
26+
GitHubMatrix,
27+
/// JSON Lines format (one object per line)
28+
JsonLines,
2229
}
2330

2431
impl OutputFormat {
2532
/// Check if this format is JSON
2633
pub fn is_json(&self) -> bool {
2734
matches!(self, Self::Json)
2835
}
36+
37+
/// Check if this format is a JSON-like structured format
38+
///
39+
/// Returns true for any format that produces structured output (json, jsonl, github, github-matrix).
40+
/// Used to enable JSON mode which suppresses progress messages.
41+
pub fn is_json_like(&self) -> bool {
42+
matches!(self, Self::Json | Self::JsonLines | Self::GitHub | Self::GitHubMatrix)
43+
}
2944
}
3045

3146
impl FromStr for OutputFormat {
@@ -35,8 +50,12 @@ impl FromStr for OutputFormat {
3550
match s.to_lowercase().as_str() {
3651
"text" => Ok(Self::Text),
3752
"json" => Ok(Self::Json),
53+
"names" | "names-only" => Ok(Self::NamesOnly),
54+
"github" => Ok(Self::GitHub),
55+
"github-matrix" => Ok(Self::GitHubMatrix),
56+
"jsonl" | "json-lines" => Ok(Self::JsonLines),
3857
_ => Err(RailError::message(format!(
39-
"Unknown format '{}'. Valid formats: text, json",
58+
"Unknown format '{}'. Valid formats: text, json, names-only, github, github-matrix, jsonl",
4059
s
4160
))),
4261
}
@@ -48,6 +67,10 @@ impl std::fmt::Display for OutputFormat {
4867
match self {
4968
Self::Text => write!(f, "text"),
5069
Self::Json => write!(f, "json"),
70+
Self::NamesOnly => write!(f, "names-only"),
71+
Self::GitHub => write!(f, "github"),
72+
Self::GitHubMatrix => write!(f, "github-matrix"),
73+
Self::JsonLines => write!(f, "jsonl"),
5174
}
5275
}
5376
}

tests/integration/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ mod helpers;
22
mod test_affected;
33
mod test_check;
44
mod test_classification;
5+
mod test_clean;
56
mod test_error_handling;
67
mod test_git_notes;
78
mod test_init;
@@ -10,6 +11,5 @@ mod test_runner;
1011
mod test_split;
1112
mod test_sync;
1213
mod test_unify;
13-
mod test_unify_comprehensive;
1414
mod test_unify_undo;
1515
mod test_unused_detection;

0 commit comments

Comments
 (0)