Skip to content

Commit 8cdda69

Browse files
authored
fix: clarify unsupported --json field selection (#68)
1 parent 2ad36ad commit 8cdda69

3 files changed

Lines changed: 106 additions & 7 deletions

File tree

src/cli.rs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,7 @@ fn parse_issue_comment_args(
437437
) -> Result<ParseOutcome<IssueCommentRequest>, CommandError> {
438438
map_parsed(parse_matches(issue_comment_command(), args), |matches| {
439439
let output = output_format(&matches);
440+
validate_json_field_selection(&output, "issue comment", &[])?;
440441
let repo = last_value(&matches, "repo");
441442
let body_values = values(&matches, "body");
442443
let body_file_values = values(&matches, "body_file");
@@ -488,6 +489,7 @@ fn parse_issue_create_args(
488489
) -> Result<ParseOutcome<IssueCreateRequest>, CommandError> {
489490
map_parsed(parse_matches(issue_create_command(), args), |matches| {
490491
let output = output_format(&matches);
492+
validate_json_field_selection(&output, "issue create", &[])?;
491493
let repo = last_value(&matches, "repo");
492494
let title = last_value(&matches, "title");
493495
let body_values = values(&matches, "body");
@@ -986,24 +988,35 @@ fn flag_count(matches: &ArgMatches, id: &str) -> usize {
986988
}
987989

988990
fn render_help(mut command: Command) -> CommandOutcome {
991+
let supports_json_field_selection = help_command_supports_json_field_selection(&command);
989992
let mut buffer = Vec::new();
990993
command
991994
.write_long_help(&mut buffer)
992995
.expect("writing clap help should succeed");
993996

994-
CommandOutcome::text(
995-
EXIT_OK,
996-
String::from_utf8(buffer)
997-
.expect("clap help should be utf-8")
998-
.trim_end()
999-
.to_string(),
1000-
)
997+
let body = String::from_utf8(buffer)
998+
.expect("clap help should be utf-8")
999+
.trim_end()
1000+
.to_string();
1001+
let body = if supports_json_field_selection {
1002+
body
1003+
} else {
1004+
body.replace("--json [<FIELDS>]", "--json")
1005+
};
1006+
1007+
CommandOutcome::text(EXIT_OK, body)
10011008
}
10021009

10031010
fn render_version() -> CommandOutcome {
10041011
CommandOutcome::text(EXIT_OK, format!("gitee {}", env!("CARGO_PKG_VERSION")))
10051012
}
10061013

1014+
fn help_command_supports_json_field_selection(command: &Command) -> bool {
1015+
let path = command.get_bin_name().unwrap_or(command.get_name());
1016+
let path = path.strip_prefix("gitee ").unwrap_or(path);
1017+
json_field_selection_for_help(path).is_some()
1018+
}
1019+
10071020
fn is_help_flag(arg: &str) -> bool {
10081021
matches!(arg, "--help" | "-h")
10091022
}

tests/help_cli.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,41 @@ fn help_can_render_text_for_a_nested_command_topic() {
6868
assert!(stdout.contains("--title <TITLE>"));
6969
}
7070

71+
#[test]
72+
fn help_text_describes_json_usage_per_command() {
73+
let pr_list_output = Command::cargo_bin("gitee")
74+
.unwrap()
75+
.args(["help", "pr", "list"])
76+
.output()
77+
.unwrap();
78+
79+
assert_eq!(pr_list_output.status.code(), Some(0));
80+
assert!(pr_list_output.stderr.is_empty());
81+
let pr_list_stdout = String::from_utf8_lossy(&pr_list_output.stdout);
82+
assert!(pr_list_stdout.contains("--json [<FIELDS>]"));
83+
84+
for topic in [
85+
["help", "issue", "create"],
86+
["help", "issue", "comment"],
87+
["help", "pr", "comment"],
88+
["help", "pr", "checkout"],
89+
["help", "repo", "clone"],
90+
] {
91+
let output = Command::cargo_bin("gitee")
92+
.unwrap()
93+
.args(topic)
94+
.output()
95+
.unwrap();
96+
97+
assert_eq!(output.status.code(), Some(0));
98+
assert!(output.stderr.is_empty());
99+
100+
let stdout = String::from_utf8_lossy(&output.stdout);
101+
assert!(stdout.contains("--json"));
102+
assert!(!stdout.contains("--json [<FIELDS>]"));
103+
}
104+
}
105+
71106
#[test]
72107
fn help_json_can_describe_a_single_nested_command() {
73108
let output = Command::cargo_bin("gitee")

tests/issue_cli.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,31 @@ fn issue_create_uses_local_repo_context_and_reports_stable_json_output() {
269269
create_mock.assert_hits(1);
270270
}
271271

272+
#[test]
273+
fn issue_create_rejects_json_field_selection_with_a_specific_usage_error() {
274+
let output = Command::cargo_bin("gitee")
275+
.unwrap()
276+
.args([
277+
"issue",
278+
"create",
279+
"--repo",
280+
"octo/demo",
281+
"--title",
282+
"Add issue create",
283+
"--json",
284+
"number",
285+
])
286+
.output()
287+
.unwrap();
288+
289+
assert_eq!(output.status.code(), Some(2));
290+
assert!(output.stdout.is_empty());
291+
assert_eq!(
292+
String::from_utf8_lossy(&output.stderr).trim(),
293+
"issue create does not support selecting JSON fields yet"
294+
);
295+
}
296+
272297
#[test]
273298
fn issue_create_uses_explicit_repo_and_renders_text_output() {
274299
let server = MockServer::start();
@@ -980,6 +1005,32 @@ fn issue_comment_rejects_removed_body_stdin_flag_as_unsupported() {
9801005
);
9811006
}
9821007

1008+
#[test]
1009+
fn issue_comment_rejects_json_field_selection_with_a_specific_usage_error() {
1010+
let output = Command::cargo_bin("gitee")
1011+
.unwrap()
1012+
.args([
1013+
"issue",
1014+
"comment",
1015+
"I123",
1016+
"--repo",
1017+
"octo/demo",
1018+
"--body",
1019+
"Posted from flag",
1020+
"--json",
1021+
"number",
1022+
])
1023+
.output()
1024+
.unwrap();
1025+
1026+
assert_eq!(output.status.code(), Some(2));
1027+
assert!(output.stdout.is_empty());
1028+
assert_eq!(
1029+
String::from_utf8_lossy(&output.stderr).trim(),
1030+
"issue comment does not support selecting JSON fields yet"
1031+
);
1032+
}
1033+
9831034
#[test]
9841035
fn issue_comment_rejects_whitespace_only_flag_body() {
9851036
let server = MockServer::start();

0 commit comments

Comments
 (0)