Skip to content

Commit 5978ec0

Browse files
committed
feat: add --tee-json option
1 parent f9aba24 commit 5978ec0

File tree

4 files changed

+63
-1
lines changed

4 files changed

+63
-1
lines changed

src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use render::{render_lint_messages, render_lint_messages_json};
1010
use std::collections::HashMap;
1111
use std::collections::HashSet;
1212
use std::convert::TryFrom;
13+
use std::fs::OpenOptions;
1314
use std::sync::{Arc, Mutex};
1415
use std::thread;
1516

@@ -155,6 +156,7 @@ pub fn do_lint(
155156
render_opt: RenderOpt,
156157
enable_spinners: bool,
157158
revision_opt: RevisionOpt,
159+
tee_json: Option<String>,
158160
) -> Result<i32> {
159161
debug!(
160162
"Running linters: {:?}",
@@ -255,6 +257,15 @@ pub fn do_lint(
255257
RenderOpt::Oneline => render_lint_messages_oneline(&mut stdout, &all_lints)?,
256258
};
257259

260+
if let Some(tee_json) = tee_json {
261+
let mut file = OpenOptions::new()
262+
.write(true)
263+
.create_new(true)
264+
.open(tee_json)
265+
.context("Couldn't open file for --tee-json")?;
266+
render_lint_messages_json(&mut file, &all_lints)?;
267+
}
268+
258269
if should_apply_patches {
259270
stdout.write_line("Successfully applied all patches.")?;
260271
}

src/main.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ struct Args {
8888
/// application data (e.g. $XDG_DATA_HOME for UNIX systems.)
8989
#[clap(long, global = true)]
9090
data_path: Option<String>,
91+
92+
93+
/// If set, output json to the provided path as well as the terminal.
94+
#[clap(long, global=true)]
95+
tee_json: Option<String>,
9196
}
9297

9398
#[derive(Debug, Parser)]
@@ -211,6 +216,7 @@ fn do_main() -> Result<i32> {
211216
PathsOpt::Auto
212217
};
213218

219+
214220
let res = match cmd {
215221
SubCommand::Init { dry_run } => {
216222
// Just run initialization commands, don't actually lint.
@@ -225,6 +231,7 @@ fn do_main() -> Result<i32> {
225231
args.output,
226232
enable_spinners,
227233
revision_opt,
234+
args.tee_json,
228235
)
229236
}
230237
SubCommand::Lint => {
@@ -237,6 +244,7 @@ fn do_main() -> Result<i32> {
237244
args.output,
238245
enable_spinners,
239246
revision_opt,
247+
args.tee_json,
240248
)
241249
}
242250
SubCommand::Rage { invocation } => do_rage(&persistent_data_store, invocation),

tests/integration_test.rs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use anyhow::Result;
22
use assert_cmd::Command;
3-
use insta::assert_yaml_snapshot;
3+
use insta::{assert_yaml_snapshot, assert_snapshot};
44
use lintrunner::lint_message::{LintMessage, LintSeverity};
55
use regex::Regex;
66
use serde_json;
@@ -666,3 +666,40 @@ fn rage_command_basic() -> Result<()> {
666666
cmd.assert().success();
667667
Ok(())
668668
}
669+
670+
#[test]
671+
fn tee_json() -> Result<()> {
672+
let data_path = tempfile::tempdir()?;
673+
let lint_message = LintMessage {
674+
path: Some("tests/fixtures/fake_source_file.rs".to_string()),
675+
line: Some(9),
676+
char: Some(1),
677+
code: "DUMMY".to_string(),
678+
name: "dummy failure".to_string(),
679+
severity: LintSeverity::Advice,
680+
original: None,
681+
replacement: None,
682+
description: Some("A dummy linter failure".to_string()),
683+
};
684+
let config = temp_config_returning_msg(lint_message)?;
685+
686+
let mut cmd = Command::cargo_bin("lintrunner")?;
687+
cmd.arg(format!("--config={}", config.path().to_str().unwrap()));
688+
cmd.arg(format!(
689+
"--data-path={}",
690+
data_path.path().to_str().unwrap()
691+
));
692+
cmd.arg(format!(
693+
"--tee-json={}",
694+
data_path.path().join("foo.json").display()
695+
));
696+
// Run on a file to ensure that the linter is run.
697+
cmd.arg("README.md");
698+
cmd.assert().failure();
699+
700+
let tee_json = std::fs::read_to_string(data_path.path().join("foo.json"))?;
701+
702+
assert_snapshot!("tee_json", tee_json);
703+
704+
Ok(())
705+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
source: tests/integration_test.rs
3+
expression: tee_json
4+
---
5+
{"path":"tests/fixtures/fake_source_file.rs","line":9,"char":1,"code":"DUMMY","severity":"advice","name":"dummy failure","description":"A dummy linter failure"}
6+

0 commit comments

Comments
 (0)