-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathlib.rs
More file actions
330 lines (292 loc) · 11.8 KB
/
lib.rs
File metadata and controls
330 lines (292 loc) · 11.8 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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
use std::{collections::BTreeMap, fmt::Display, ops::Range};
use bincode::{Decode, Encode};
use brush_parser::{
Parser, ParserOptions,
ast::{
AndOr, Assignment, AssignmentName, AssignmentValue, Command, CommandPrefix,
CommandPrefixOrSuffixItem, CommandSuffix, CompoundListItem, Pipeline, Program,
SeparatorOperator, SimpleCommand, SourceLocation, Word,
},
word::{WordPiece, WordPieceWithSource},
};
use diff::Diff;
use serde::{Deserialize, Serialize};
use vite_str::Str;
/// "FOO=BAR program arg1 arg2"
#[derive(Encode, Decode, Serialize, Deserialize, Debug, PartialEq, Eq, Diff, Clone)]
#[diff(attr(#[derive(Debug)]))]
pub struct TaskParsedCommand {
pub envs: BTreeMap<Str, Str>,
pub program: Str,
pub args: Vec<Str>,
}
impl Display for TaskParsedCommand {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
// BTreeMap ensures stable iteration order
for (name, value) in &self.envs {
Display::fmt(
&format_args!("{}={} ", name, shell_escape::escape(value.as_str().into())),
f,
)?;
}
Display::fmt(&shell_escape::escape(self.program.as_str().into()), f)?;
for arg in &self.args {
Display::fmt(" ", f)?;
Display::fmt(&shell_escape::escape(arg.as_str().into()), f)?;
}
Ok(())
}
}
/// Parser options matching those used in [`try_parse_as_and_list`].
const PARSER_OPTIONS: ParserOptions = ParserOptions {
enable_extended_globbing: false,
posix_mode: true,
sh_mode: true,
tilde_expansion: false,
};
/// Remove shell quoting from a word value, respecting quoting context.
///
/// Uses `brush_parser::word::parse` to properly handle nested quoting
/// (e.g. single quotes inside double quotes are preserved as literal characters).
/// Returns `None` if the word contains expansions that cannot be statically resolved
/// (parameter expansion, command substitution, arithmetic).
fn unquote(word: &Word) -> Option<Str> {
let Word { value, loc: _ } = word;
let pieces = brush_parser::word::parse(value.as_str(), &PARSER_OPTIONS).ok()?;
let mut result = Str::with_capacity(value.len());
flatten_pieces(&pieces, &mut result)?;
Some(result)
}
/// Recursively extract literal text from parsed word pieces.
///
/// Returns `None` if any piece requires runtime expansion.
fn flatten_pieces(pieces: &[WordPieceWithSource], result: &mut Str) -> Option<()> {
for piece in pieces {
match &piece.piece {
WordPiece::Text(s) | WordPiece::SingleQuotedText(s) | WordPiece::AnsiCQuotedText(s) => {
result.push_str(s);
}
// EscapeSequence contains the raw sequence (e.g. `\"` as two chars);
// the escaped character is everything after the leading backslash.
WordPiece::EscapeSequence(s) => {
result.push_str(s.strip_prefix('\\').unwrap_or(s));
}
WordPiece::DoubleQuotedSequence(inner)
| WordPiece::GettextDoubleQuotedSequence(inner) => {
flatten_pieces(inner, result)?;
}
// Tilde prefix, parameter expansion, command substitution, arithmetic
// cannot be statically resolved — bail out.
_ => return None,
}
}
Some(())
}
fn pipeline_to_command(pipeline: &Pipeline) -> Option<(TaskParsedCommand, Range<usize>)> {
let location = pipeline.location()?;
let range = location.start.index..location.end.index;
let Pipeline { timed: None, bang: false, seq } = pipeline else {
return None;
};
let [Command::Simple(simple_command)] = seq.as_slice() else {
return None;
};
let SimpleCommand { prefix, word_or_name: Some(program), suffix } = simple_command else {
return None;
};
let mut envs = BTreeMap::<Str, Str>::new();
if let Some(prefix) = prefix {
let CommandPrefix(items) = prefix;
for item in items {
let CommandPrefixOrSuffixItem::AssignmentWord(
Assignment { name, value, append: false, loc: _ },
_,
) = item
else {
return None;
};
let AssignmentName::VariableName(name) = name else {
return None;
};
let AssignmentValue::Scalar(value) = value else {
return None;
};
envs.insert(name.as_str().into(), unquote(value)?);
}
}
let mut args = Vec::<Str>::new();
if let Some(CommandSuffix(suffix_items)) = suffix {
for suffix_item in suffix_items {
let CommandPrefixOrSuffixItem::Word(word) = suffix_item else {
return None;
};
args.push(unquote(word)?);
}
}
Some((TaskParsedCommand { envs, program: unquote(program)?, args }, range))
}
#[must_use]
pub fn try_parse_as_and_list(cmd: &str) -> Option<Vec<(TaskParsedCommand, Range<usize>)>> {
let mut parser = Parser::new(cmd.as_bytes(), &PARSER_OPTIONS);
let Program { complete_commands } = parser.parse_program().ok()?;
let [compound_list] = complete_commands.as_slice() else {
return None;
};
let [CompoundListItem(and_or_list, SeparatorOperator::Sequence)] = compound_list.0.as_slice()
else {
return None;
};
let mut commands = Vec::<(TaskParsedCommand, Range<usize>)>::new();
commands.push(pipeline_to_command(&and_or_list.first)?);
for and_or in &and_or_list.additional {
let AndOr::And(pipeline) = and_or else {
return None;
};
commands.push(pipeline_to_command(pipeline)?);
}
Some(commands)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_parse_single_command() {
let source = r"A=B hello world";
let list = try_parse_as_and_list(source).unwrap();
assert_eq!(list.len(), 1);
let (cmd, range) = &list[0];
assert_eq!(&source[range.clone()], source);
assert_eq!(
cmd,
&TaskParsedCommand {
envs: [("A".into(), "B".into())].into(),
program: "hello".into(),
args: vec!["world".into()],
}
);
}
#[test]
fn test_parse_command() {
let source = r#"A=B hello world && FOO="BE\"R" program "arg1" "arg\"2" && zzz"#;
let list = try_parse_as_and_list(source).unwrap();
let commands = list.iter().map(|(cmd, _)| cmd).collect::<Vec<_>>();
assert_eq!(
commands,
vec![
&TaskParsedCommand {
envs: [("A".into(), "B".into())].into(),
program: "hello".into(),
args: vec!["world".into()],
},
&TaskParsedCommand {
envs: [("FOO".into(), "BE\"R".into())].into(),
program: "program".into(),
args: vec!["arg1".into(), "arg\"2".into()],
},
&TaskParsedCommand { envs: [].into(), program: "zzz".into(), args: vec![] }
]
);
let substrs = list.iter().map(|(_, range)| &source[range.clone()]).collect::<Vec<_>>();
assert_eq!(
substrs,
vec!["A=B hello world", r#"FOO="BE\"R" program "arg1" "arg\"2""#, "zzz"]
);
}
#[test]
fn test_task_parsed_command_stable_env_ordering() {
// Test that environment variables maintain stable ordering
let cmd = TaskParsedCommand {
envs: [
("ZEBRA".into(), "last".into()),
("ALPHA".into(), "first".into()),
("MIDDLE".into(), "middle".into()),
]
.into(),
program: "test".into(),
args: vec![],
};
// Convert to string multiple times and verify it's always the same
let str1 = cmd.to_string();
let str2 = cmd.to_string();
let str3 = cmd.to_string();
assert_eq!(str1, str2);
assert_eq!(str2, str3);
// Verify the order is alphabetical (BTreeMap sorts by key)
assert!(str1.starts_with("ALPHA=first MIDDLE=middle ZEBRA=last"));
}
#[test]
fn test_unquote_preserves_nested_quotes() {
// Single quotes inside double quotes are preserved
let cmd = r#"echo "hello 'world'""#;
let list = try_parse_as_and_list(cmd).unwrap();
assert_eq!(list[0].0.args[0].as_str(), "hello 'world'");
// Double quotes inside single quotes are preserved
let cmd = r#"echo 'hello "world"'"#;
let list = try_parse_as_and_list(cmd).unwrap();
assert_eq!(list[0].0.args[0].as_str(), "hello \"world\"");
// Backslash escaping in double quotes
let cmd = r#"echo "hello\"world""#;
let list = try_parse_as_and_list(cmd).unwrap();
assert_eq!(list[0].0.args[0].as_str(), "hello\"world");
// Backslash escaping outside quotes
let cmd = r"echo hello\ world";
let list = try_parse_as_and_list(cmd).unwrap();
assert_eq!(list[0].0.args[0].as_str(), "hello world");
}
#[test]
fn test_flatten_pieces_recursion() {
fn parse_and_flatten(input: &str) -> Option<Str> {
let pieces = brush_parser::word::parse(input, &PARSER_OPTIONS).ok()?;
let mut result = Str::default();
flatten_pieces(&pieces, &mut result)?;
Some(result)
}
// DoubleQuotedSequence containing Text + EscapeSequence + Text
assert_eq!(parse_and_flatten(r#""hello\"world""#).unwrap(), "hello\"world");
// DoubleQuotedSequence with single quotes preserved as literal text
assert_eq!(parse_and_flatten(r#""it's a 'test'""#).unwrap(), "it's a 'test'");
// Nested escape sequences inside double quotes
assert_eq!(parse_and_flatten(r#""a\\b""#).unwrap(), "a\\b");
// DoubleQuotedSequence bails on parameter expansion inside
assert!(parse_and_flatten(r#""hello $VAR""#).is_none());
// DoubleQuotedSequence bails on command substitution inside
assert!(parse_and_flatten(r#""hello $(cmd)""#).is_none());
}
#[test]
fn test_parse_urllib_prepare() {
let cmd = r#"node -e "const v = parseInt(process.versions.node, 10); if (v >= 20) require('child_process').execSync('vp config', {stdio: 'inherit'});""#;
let result = try_parse_as_and_list(cmd);
let (parsed, _) = &result.as_ref().unwrap()[0];
// Single quotes inside double quotes must be preserved as literal characters
assert_eq!(
parsed.args[1].as_str(),
"const v = parseInt(process.versions.node, 10); if (v >= 20) require('child_process').execSync('vp config', {stdio: 'inherit'});"
);
}
#[test]
fn test_task_parsed_command_serialization_stability() {
use bincode::{decode_from_slice, encode_to_vec};
// Create a command with multiple environment variables
let cmd = TaskParsedCommand {
envs: [
("VAR_C".into(), "value_c".into()),
("VAR_A".into(), "value_a".into()),
("VAR_B".into(), "value_b".into()),
]
.into(),
program: "program".into(),
args: vec!["arg1".into(), "arg2".into()],
};
// Serialize multiple times
let config = bincode::config::standard();
let bytes1 = encode_to_vec(&cmd, config).unwrap();
let bytes2 = encode_to_vec(&cmd, config).unwrap();
// Verify serialization is stable
assert_eq!(bytes1, bytes2);
// Verify deserialization works and maintains order
let (decoded, _): (TaskParsedCommand, _) = decode_from_slice(&bytes1, config).unwrap();
assert_eq!(decoded, cmd);
// Verify the decoded command still has stable string representation
assert_eq!(decoded.to_string(), cmd.to_string());
}
}