-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathlib.rs
More file actions
246 lines (225 loc) · 10.6 KB
/
Copy pathlib.rs
File metadata and controls
246 lines (225 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#![recursion_limit = "256"]
mod cli;
pub use crate::cli::{Cli, Command, CommandOutcome};
mod cloud;
mod compare;
mod compile;
mod convert;
mod db;
mod demo;
pub mod errors;
mod execute;
mod graph;
mod init;
mod lint;
pub mod logging;
mod mcp_codemode;
mod merge;
mod new;
mod open;
mod options;
mod outputs;
mod pull;
mod push;
mod render;
mod site;
mod status;
mod sync;
mod uninstall;
mod unwatch;
pub mod upgrade;
mod watch;
#[allow(clippy::print_stderr)]
#[cfg(test)]
mod tests {
use super::*;
use clap::Parser;
use stencila_cli_utils::strip_ansi_escapes;
/// Test that all CLI example commands in AFTER_LONG_HELP strings parse correctly
///
/// This test:
/// 1. Collects all AFTER_LONG_HELP constants from all CLI modules
/// 2. Extracts example commands starting with "stencila"
/// 3. Strips ANSI escape sequences and parses quoted arguments
/// 4. Validates each command using Cli::try_parse_from()
/// 5. Skips commands that fail due to validation constraints (not parsing issues)
#[test]
fn test_after_long_help_examples() {
let help_strings = collect_all_after_long_help();
let mut successful_commands = 0;
for (command_name, help_text) in help_strings {
let examples = extract_stencila_commands(help_text);
for example in examples {
let args = parse_command_line(&example);
// Special handling for help commands
if args.len() >= 2 && (args[1] == "--help" || args[1] == "-h") {
continue;
}
match Cli::try_parse_from(&args) {
Ok(_) => {
successful_commands += 1;
}
Err(e) => {
panic!("Command `{command_name}` example `{example}` failed: {e}");
}
}
}
}
eprintln!("✅ Validated {successful_commands} CLI example commands successfully!");
}
#[rustfmt::skip]
fn collect_all_after_long_help() -> Vec<(&'static str, &'static str)> {
vec![
// CLI module help strings
("cli", crate::cli::CLI_AFTER_LONG_HELP),
("new", crate::new::CLI_AFTER_LONG_HELP),
("convert", crate::convert::CLI_AFTER_LONG_HELP),
("compare", crate::compare::CLI_AFTER_LONG_HELP),
("merge", crate::merge::CLI_AFTER_LONG_HELP),
("sync", crate::sync::CLI_AFTER_LONG_HELP),
("push", crate::push::CLI_AFTER_LONG_HELP),
("watch", crate::watch::CLI_AFTER_LONG_HELP),
("unwatch", crate::unwatch::CLI_AFTER_LONG_HELP),
("compile", crate::compile::CLI_AFTER_LONG_HELP),
("lint", crate::lint::CLI_AFTER_LONG_HELP),
("execute", crate::execute::CLI_AFTER_LONG_HELP),
("render", crate::render::CLI_AFTER_LONG_HELP),
("graph", crate::graph::CLI_AFTER_LONG_HELP),
("preview", crate::open::CLI_AFTER_LONG_HELP),
("demo", crate::demo::DEMO_AFTER_LONG_HELP),
("upgrade", crate::upgrade::CLI_AFTER_LONG_HELP),
("uninstall", crate::uninstall::CLI_AFTER_LONG_HELP),
("init", stencila_config::cli::INIT_AFTER_LONG_HELP),
("config", stencila_config::cli::CLI_AFTER_LONG_HELP),
// Document module help strings
("status", crate::status::CLI_AFTER_LONG_HELP),
("document::move", stencila_document::cli::MOVE_AFTER_LONG_HELP),
("document::track", stencila_document::cli::TRACK_AFTER_LONG_HELP),
("document::untrack", stencila_document::cli::UNTRACK_AFTER_LONG_HELP),
("document::clean", stencila_document::cli::CLEAN_AFTER_LONG_HELP),
// DB module help strings
("db", crate::db::CLI_AFTER_LONG_HELP),
("db::push", crate::db::PUSH_AFTER_LONG_HELP),
("db::pull", crate::db::PULL_AFTER_LONG_HELP),
("db::status", crate::db::STATUS_AFTER_LONG_HELP),
// Prompts module help strings
("prompts::cli", stencila_prompts::cli::CLI_AFTER_LONG_HELP),
("prompts::list", stencila_prompts::cli::LIST_AFTER_LONG_HELP),
("prompts::show", stencila_prompts::cli::SHOW_AFTER_LONG_HELP),
("prompts::infer", stencila_prompts::cli::INFER_AFTER_LONG_HELP),
("prompts::update", stencila_prompts::cli::UPDATE_AFTER_LONG_HELP),
("prompts::reset", stencila_prompts::cli::RESET_AFTER_LONG_HELP),
// Workflows module help strings
("workflows::cli", stencila_workflows::cli::CLI_AFTER_LONG_HELP),
("workflows::list", stencila_workflows::cli::LIST_AFTER_LONG_HELP),
("workflows::show", stencila_workflows::cli::SHOW_AFTER_LONG_HELP),
("workflows::validate", stencila_workflows::cli::VALIDATE_AFTER_LONG_HELP),
("workflows::create", stencila_workflows::cli::CREATE_AFTER_LONG_HELP),
("workflows::save", stencila_workflows::cli::SAVE_AFTER_LONG_HELP),
("workflows::discard", stencila_workflows::cli::DISCARD_AFTER_LONG_HELP),
// Agents module help strings
("agents::cli", stencila_agents::cli::CLI_AFTER_LONG_HELP),
("agents::list", stencila_agents::cli::LIST_AFTER_LONG_HELP),
("agents::show", stencila_agents::cli::SHOW_AFTER_LONG_HELP),
("agents::validate", stencila_agents::cli::VALIDATE_AFTER_LONG_HELP),
("agents::create", stencila_agents::cli::CREATE_AFTER_LONG_HELP),
("agents::resolve", stencila_agents::cli::RESOLVE_AFTER_LONG_HELP),
("agents::run", stencila_agents::cli::RUN_AFTER_LONG_HELP),
// Models module help strings
("models::cli", stencila_models3::cli::CLI_AFTER_LONG_HELP),
("models::list", stencila_models3::cli::LIST_AFTER_LONG_HELP),
("models::run", stencila_models3::cli::RUN_AFTER_LONG_HELP),
// Kernels module help strings
("kernels::cli", stencila_kernels::cli::CLI_AFTER_LONG_HELP),
("kernels::list", stencila_kernels::cli::LIST_AFTER_LONG_HELP),
("kernels::info", stencila_kernels::cli::INFO_AFTER_LONG_HELP),
("kernels::packages", stencila_kernels::cli::PACKAGES_AFTER_LONG_HELP),
("kernels::execute", stencila_kernels::cli::EXECUTE_AFTER_LONG_HELP),
("kernels::evaluate", stencila_kernels::cli::EVALUATE_AFTER_LONG_HELP),
// Linters module help strings
("linters::list", stencila_linters::cli::LIST_AFTER_LONG_HELP),
("linters::lint", stencila_linters::cli::LINT_AFTER_LONG_HELP),
// Codecs module help strings
("formats::cli", stencila_codecs::cli::CLI_AFTER_LONG_HELP),
("formats::list", stencila_codecs::cli::LIST_AFTER_LONG_HELP),
// Secrets module help strings
("secrets::cli", stencila_secrets::cli::CLI_AFTER_LONG_HELP),
("secrets::set", stencila_secrets::cli::SET_AFTER_LONG_HELP),
("secrets::delete", stencila_secrets::cli::DELETE_AFTER_LONG_HELP),
// MCP module help strings
("mcp::cli", stencila_mcp::cli::CLI_AFTER_LONG_HELP),
("mcp::list", stencila_mcp::cli::LIST_AFTER_LONG_HELP),
("mcp::show", stencila_mcp::cli::SHOW_AFTER_LONG_HELP),
("mcp::add", stencila_mcp::cli::ADD_AFTER_LONG_HELP),
("mcp::remove", stencila_mcp::cli::REMOVE_AFTER_LONG_HELP),
("mcp::codemode", stencila_mcp::cli::CODEMODE_AFTER_LONG_HELP),
// Tools module help strings
("tools::cli", stencila_tools::cli::CLI_AFTER_LONG_HELP),
("tools::list", stencila_tools::cli::LIST_AFTER_LONG_HELP),
("tools::show", stencila_tools::cli::SHOW_AFTER_LONG_HELP),
("tools::install", stencila_tools::cli::INSTALL_AFTER_LONG_HELP),
("tools::env", stencila_tools::cli::ENV_AFTER_LONG_HELP),
("tools::run", stencila_tools::cli::RUN_AFTER_LONG_HELP),
// Cloud module help strings
("cloud::cli", crate::cloud::CLI_AFTER_LONG_HELP),
("cloud::signin", crate::cloud::SIGNIN_AFTER_LONG_HELP),
("cloud::signout", crate::cloud::SIGNOUT_AFTER_LONG_HELP),
("cloud::status", crate::cloud::STATUS_AFTER_LONG_HELP),
("cloud::logs", crate::cloud::LOGS_AFTER_LONG_HELP),
// Site module help strings
("site::cli", crate::site::AFTER_LONG_HELP),
("site::show", crate::site::SHOW_AFTER_LONG_HELP),
("site::access", crate::site::ACCESS_AFTER_LONG_HELP),
("site::access::password", crate::site::ACCESS_PASSWORD_AFTER_LONG_HELP),
("site::access::team", crate::site::ACCESS_TEAM_AFTER_LONG_HELP),
// Outputs module help strings
("outputs::cli", crate::outputs::CLI_AFTER_LONG_HELP),
("outputs::list", crate::outputs::LIST_AFTER_LONG_HELP),
("outputs::add", crate::outputs::ADD_AFTER_LONG_HELP),
("outputs::remove", crate::outputs::REMOVE_AFTER_LONG_HELP),
("outputs::push", crate::outputs::PUSH_AFTER_LONG_HELP),
]
}
fn extract_stencila_commands(help_text: &str) -> Vec<String> {
let mut commands = Vec::new();
for line in help_text.lines() {
let trimmed = line.trim();
// First strip ANSI sequences to see the clean text
let clean_line = strip_ansi_escapes(trimmed);
// Look for lines that start with stencila (ignoring leading whitespace and comments)
if clean_line.trim_start().starts_with("stencila") {
// Extract the command from the clean line
let command = clean_line.trim().to_string();
commands.push(command);
}
}
commands
}
fn parse_command_line(command: &str) -> Vec<String> {
// Handle quoted arguments properly
let mut args = Vec::new();
let mut current_arg = String::new();
let mut in_quotes = false;
let chars = command.chars().peekable();
for ch in chars {
match ch {
'"' => {
in_quotes = !in_quotes;
}
' ' if !in_quotes => {
if !current_arg.is_empty() {
args.push(current_arg.clone());
current_arg.clear();
}
}
_ => {
current_arg.push(ch);
}
}
}
if !current_arg.is_empty() {
args.push(current_arg);
}
args
}
}