Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_text/tests/shell.rs

4.1 KiB, 3 runs

created by r1870400018:76849, which is this file's identity for as long as the history lasts, whatever it is later renamed to

download · who wrote it · its history

1//! `shell`: a command line read into words and simple commands the way a POSIX shell quotes it.
2//!
3//! Moved here from the Daimond engine's spend notice (8 Nov 2026), which names the file a
4//! command read, through `sh -c` and `cd`.
5
6use oxedyne_fe2o3_text::shell::{
7 commands,
8 join,
9 quote,
10 tokens,
11 Command,
12 Token,
13 Word,
14};
15
16
17fn texts(cmd: &Command) -> Vec<&str> {
18 cmd.words.iter().map(|w| w.text.as_str()).collect()
19}
20
21#[test]
22fn quoting_is_removed_as_the_shell_removes_it() {
23 let cmds = commands(r#"grep -n 'a b' "c \"d\" \x" e\ f '' g"#);
24 assert_eq!(1, cmds.len());
25 assert_eq!(vec!["grep", "-n", "a b", "c \"d\" \\x", "e f", "", "g"], texts(&cmds[0]));
26 assert!(cmds[0].words.iter().all(|w| w.literal));
27}
28
29#[test]
30fn an_expanded_word_is_not_literal() {
31 for (line, lit) in [
32 ("cat $HOME/a", false),
33 ("cat \"$x\"", false),
34 ("cat `pwd`", false),
35 ("cat *.rs", false),
36 ("cat a?.rs", false),
37 ("cat ~/a", false),
38 ("cat a~b", true),
39 ("cat '$x'", true),
40 ("cat '~/a'", true),
41 ] {
42 let cmds = commands(line);
43 assert_eq!(lit, cmds[0].words[1].literal, "{}", line);
44 }
45}
46
47#[test]
48fn operators_end_a_simple_command() {
49 let cmds = commands("cd web && cat a; ls | wc -l & (echo x)\nnl b # cat c\ntac d");
50 let progs: Vec<Option<&str>> = cmds.iter().map(|c| c.program()).collect();
51 assert_eq!(vec![Some("cd"), Some("cat"), Some("ls"), Some("wc"), Some("echo"), Some("nl"),
52 Some("tac")], progs);
53 assert_eq!(vec!["nl", "b"], texts(&cmds[5]));
54}
55
56#[test]
57fn redirections_are_not_operands() {
58 let cmds = commands("sort < in.txt > out.txt 2>/dev/null 2>&1 &>> log");
59 assert_eq!(1, cmds.len());
60 assert_eq!(vec!["sort"], texts(&cmds[0]));
61 assert_eq!(vec![Word::literal("in.txt")], cmds[0].read_from);
62
63 // A here-document, a here-string and a descriptor duplication read no file.
64 for line in ["cat << EOF", "cat <<< text", "cat <&3", "cat >| f"] {
65 let cmds = commands(line);
66 assert_eq!(vec!["cat"], texts(&cmds[0]), "{}", line);
67 assert!(cmds[0].read_from.is_empty(), "{}", line);
68 }
69 assert_eq!(vec![Token::Word(Word::literal("a")), Token::Redirect, Token::Word(Word::literal("b"))],
70 tokens("a 2>b"));
71 // A quoted number is a word, not a descriptor.
72 assert_eq!(vec!["echo", "2"], texts(&commands("echo '2'>f")[0]));
73}
74
75#[test]
76fn assignments_before_the_program_are_skipped() {
77 let cmds = commands("LANG=C A_1=x grep -n x=y f");
78 assert_eq!(Some("grep"), cmds[0].program());
79 assert_eq!(4, cmds[0].args().len());
80 assert_eq!(Some("cat"), commands("/usr/bin/cat f")[0].program());
81 assert_eq!(None, commands("$EDITOR f")[0].program());
82}
83
84#[test]
85fn a_shell_hands_over_its_dash_c_script() {
86 let cmd = Command::of_argv(&["bash".to_string(), "-lc".to_string(), "cd a && cat b".to_string()]);
87 assert!(cmd.is_shell());
88 assert_eq!(Some("cd a && cat b"), cmd.script().map(|w| w.text.as_str()));
89 assert_eq!(None, commands("sh run.sh")[0].script());
90 assert_eq!(None, commands("sh --norc run.sh")[0].script());
91 assert_eq!(None, commands("python3 -c 'print(1)'")[0].script());
92 assert!(!commands("sh -c \"cat $f\"")[0].script().map_or(true, |w| w.literal));
93}
94
95#[test]
96fn cd_moves_the_folder_or_loses_it() {
97 let mut dir = Some(String::new());
98 assert!(!commands("cat a")[0].cd(&mut dir));
99 assert!(commands("cd web")[0].cd(&mut dir));
100 assert_eq!(Some("web".to_string()), dir);
101 assert!(commands("cd apps/")[0].cd(&mut dir));
102 assert_eq!(Some("web/apps/".to_string()), dir);
103 assert_eq!(Some("web/apps/x.rs".to_string()), join(&dir, "x.rs"));
104 assert!(commands("cd /etc")[0].cd(&mut dir));
105 assert_eq!(Some("/etc".to_string()), dir);
106 for lost in ["cd", "cd -", "cd $HOME", "cd ~"] {
107 let mut dir = Some("/etc".to_string());
108 assert!(commands(lost)[0].cd(&mut dir));
109 assert_eq!(None, dir, "{}", lost);
110 assert_eq!(None, join(&dir, "x"));
111 assert_eq!(Some("/x".to_string()), join(&dir, "/x"));
112 }
113}
114
115#[test]
116fn quote_reads_back_as_written() {
117 for s in ["plain/path-1.rs", "my notes.md", "it's", "", "$x", "a\nb"] {
118 let q = quote(s);
119 let cmds = commands(&format!("cat {}", q));
120 assert_eq!(s, cmds[0].words[1].text, "{} quoted as {}", s, q);
121 assert!(cmds[0].words[1].literal, "{}", q);
122 }
123 assert_eq!("plain/path-1.rs", quote("plain/path-1.rs"));
124 assert_eq!("'it'\\''s'", quote("it's"));
125}