tome-scripts 0.12.3

Turn directories of shell scripts into full-blown CLIs
Documentation
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
use super::execute;

const EXAMPLE_DIR: &'static str = "./example";
const SHELL: &'static str = "bash";

fn _vec_str(args: Vec<&str>) -> Vec<String> {
    args.iter().map(|s| s.to_string()).collect()
}

/// test exec, which should execute a tome
/// command
#[test]
fn test_exec_simple_script() {
    assert_eq!(
        execute(_vec_str(vec![
            "tome",
            "command-execute",
            "-s",
            SHELL,
            EXAMPLE_DIR,
            "--",
            "exec",
            "file_example"
        ])),
        Ok(format!("'{}/file_example'", EXAMPLE_DIR))
    );
}

// test exec should work, even when called
// recursively.
#[test]
fn test_exec_recursive_simple_script() {
    assert_eq!(
        execute(_vec_str(vec![
            "tome",
            "command-execute",
            "-s",
            SHELL,
            EXAMPLE_DIR,
            "--",
            "exec",
            "exec",
            "file_example"
        ])),
        Ok(format!("'{}/file_example'", EXAMPLE_DIR))
    );
}

/// basic test for a simple script.
/// the output should be the path to the script itself.
#[test]
fn test_simple_script() {
    assert_eq!(
        execute(_vec_str(vec![
            "tome",
            "command-execute",
            "-s",
            SHELL,
            EXAMPLE_DIR,
            "--",
            "file_example"
        ])),
        Ok(format!("'{}/file_example'", EXAMPLE_DIR))
    );
}

/// the output should be the path to the script itself, with the passed arguments
#[test]
fn test_simple_script_with_args() {
    assert_eq!(
        execute(_vec_str(vec![
            "tome",
            "command-execute",
            "-s",
            SHELL,
            EXAMPLE_DIR,
            "--",
            "file_example",
            "x"
        ])),
        Ok(format!("'{}/file_example' 'x'", EXAMPLE_DIR))
    );
}

#[test]
fn test_simple_script_completion() {
    assert_eq!(
        execute(_vec_str(vec![
            "tome",
            "command-complete",
            "-s",
            SHELL,
            EXAMPLE_DIR,
            "--",
            "file_example",
        ])),
        Ok(String::from("file autocomplete example"))
    );
}

#[test]
fn test_script_completion_no_args() {
    assert_eq!(
        execute(_vec_str(vec![
            "tome",
            "command-complete",
            "-s",
            SHELL,
            EXAMPLE_DIR,
            "--",
            "dir_example",
            "baz",
        ])),
        Ok(String::from("--bar1 --bar2 --not-bar\n"))
    );
}

#[test]
fn test_script_completion_with_args() {
    assert_eq!(
        execute(_vec_str(vec![
            "tome",
            "command-complete",
            "-s",
            SHELL,
            EXAMPLE_DIR,
            "--",
            "dir_example",
            "baz",
            "foo",
            "bar",
        ])),
        Ok(String::from("completed with: foo bar\n"))
    );
}

/// Unless the file has the completion annotation
/// do not invoke completion on it and return nothing
/// instead.
#[test]
fn test_simple_script_no_completion() {
    assert_eq!(
        execute(_vec_str(vec![
            "tome",
            "command-complete",
            "-s",
            SHELL,
            EXAMPLE_DIR,
            "--",
            "test_files",
            "file_example_no_completion",
        ])),
        Ok(String::from(""))
    );
}

/// basic test for a script that should be sourced.
/// .source files are sourced when executed
#[test]
fn test_source() {
    assert_eq!(
        execute(_vec_str(vec![
            "tome",
            "command-execute",
            "-s",
            SHELL,
            EXAMPLE_DIR,
            "--",
            "source_example",
        ])),
        Ok(format!(
            "set --; '.' '{}/source_example.source'",
            EXAMPLE_DIR
        ))
    );
}

#[test]
fn test_source_completion() {
    assert_eq!(
        execute(_vec_str(vec![
            "tome",
            "command-complete",
            "-s",
            "bash",
            EXAMPLE_DIR,
            "--",
            "source_example",
        ])),
        Ok(String::from("foo baz\n"))
    );
}

/// if completion is requested on a directory,
/// return the list of file and directories in there.
#[test]
fn test_directory_completion() {
    assert_eq!(
        execute(_vec_str(vec![
            "tome",
            "command-complete",
            "-s",
            "bash",
            EXAMPLE_DIR,
            "--",
            "dir_example",
        ])),
        Ok("bar baz foo".to_string())
    );
}

/// the root directory should also be completed.
/// non-executable files should not appear in completions
#[test]
fn test_root_directory_completion() {
    assert_eq!(
        execute(_vec_str(vec!["tome", "command-complete", "-s", "bash", EXAMPLE_DIR])),
        // note that we also complete with builtins
        Ok("commands dir_example exec file_example help practical_examples quote read-from-root source_example source_example_fish test_files tome use-arg".to_string())
    );
}

/// the tome command is a no-op, and shouldn't
/// have completion
#[test]
fn test_tome_completion() {
    assert_eq!(
        execute(_vec_str(vec![
            "tome",
            "command-complete",
            "-s",
            SHELL,
            EXAMPLE_DIR,
            "--",
            "tome",
        ])),
        Ok(String::from(""))
    );
}

/// if completion is requested on a directory,
/// return the list of file and directories in there.
#[test]
fn test_script_in_directory() {
    assert_eq!(
        execute(_vec_str(vec![
            "tome",
            "command-execute",
            "-s",
            "bash",
            EXAMPLE_DIR,
            "--",
            "dir_example",
            "foo"
        ])),
        Ok(format!("'{}/dir_example/foo'", EXAMPLE_DIR))
    );
}

/// if the script is not found in the directory, provide
/// a clear error message.
#[test]
fn test_script_in_directory_not_found() {
    assert_eq!(
        execute(_vec_str(vec![
            "tome",
            "command-execute",
            "-s",
            "bash",
            EXAMPLE_DIR,
            "--",
            "dir_example",
            "foo-nonexistent",
            "baz"
        ])),
        Err(format!(
            "command foo-nonexistent not found in directory {}/dir_example",
            EXAMPLE_DIR
        ))
    );
}

/// completing to a directory emits the directory name and some help.
#[test]
fn test_script_directory_argument() {
    let result = execute(_vec_str(vec![
        "tome",
        "command-execute",
        "-s",
        "bash",
        EXAMPLE_DIR,
        "--",
        "dir_example",
    ]))
    .unwrap();
    assert!(result.contains("list of dir_example commands available are:"));
    assert!(result.contains("foo:"));
    assert!(result.contains("bar:"));
}

/// if there is no argument passed in for sourcing, an argument will
/// be added, to ensure that shells don't inherit arguments from the initial shell
/// command.
#[test]
fn test_use_arg() {
    assert_eq!(
        execute(_vec_str(vec![
            "tome",
            "command-execute",
            "-s",
            "bash",
            EXAMPLE_DIR,
            "--",
            "use-arg"
        ])),
        Ok(format!("set --; '.' '{}/use-arg.source'", EXAMPLE_DIR))
    );
}

/// to ensure that character sequences that have special meaning to
/// the shell are not interpreted as such, all values should be single quoted.
#[test]
fn test_dangerous_characters_quoted() {
    assert_eq!(
        execute(_vec_str(vec![
            "tome",
            "command-execute",
            "-s",
            "bash",
            EXAMPLE_DIR,
            "--",
            "use-arg"
        ])),
        Ok(format!("set --; '.' '{}/use-arg.source'", EXAMPLE_DIR))
    );
}

#[test]
fn test_execute_with_single_quotes() {
    assert_eq!(
        execute(_vec_str(vec![
            "tome",
            "command-execute",
            "-s",
            SHELL,
            EXAMPLE_DIR,
            "--",
            "file_example",
            "I'm the developer",
            "'hello'",
        ])),
        Ok(format!(
            "'{}/file_example' 'I'\\''m the developer' ''\\''hello'\\'''",
            EXAMPLE_DIR
        ))
    );
}

/// tome should add a "help" command into every instance, to
/// output help documentation.
#[test]
fn test_execute_help() {
    let result = execute(_vec_str(vec![
        "tome",
        "command-execute",
        "-s",
        "bash",
        EXAMPLE_DIR,
        "--",
        "help",
    ]))
    .unwrap();
    assert_is_help_text(&result)
}

/// tome should add a "commands" command which lists all
/// available commands.
#[test]
fn test_execute_commands() {
    let result = execute(_vec_str(vec![
        "tome",
        "command-execute",
        "-s",
        "bash",
        EXAMPLE_DIR,
        "--",
        "commands",
    ]))
    .unwrap();
    assert_is_help_text(&result)
}

/// help should be returned if help is called explicitly
#[test]
fn test_help_page() {
    let result = execute(_vec_str(vec!["tome", "command-help", EXAMPLE_DIR])).unwrap();
    assert_is_help_text(&result)
}

/// help should be returned if no arguments are passed.
#[test]
fn test_help_page_when_execute_no_args() {
    let result = execute(_vec_str(vec![
        "tome",
        "command-execute",
        "-s",
        "bash",
        EXAMPLE_DIR,
        "--",
    ]))
    .unwrap();
    assert_is_help_text(&result)
}

/// if an empty argument is passed in the middle of a path, it should
/// be treated as an argument to the script, rather than a no-op
/// directory.
#[test]
fn test_empty_arg_in_middle_of_path() {
    assert_eq!(
        execute(_vec_str(vec![
            "tome",
            "command-execute",
            "-s",
            "bash",
            EXAMPLE_DIR,
            "--",
            "",
            "file_example"
        ])),
        Err(format!("command  not found in directory {}", EXAMPLE_DIR))
    );
}

/// sourced scripts should be able to distinguish between zero args and one empty arg.
#[test]
fn test_source_arg_count() {
    // Zero args
    assert_eq!(
        execute(_vec_str(vec![
            "tome",
            "command-execute",
            "-s",
            "bash",
            EXAMPLE_DIR,
            "--",
            "source_example",
        ])),
        Ok(format!(
            "set --; '.' '{}/source_example.source'",
            EXAMPLE_DIR
        ))
    );
    // One empty arg
    assert_eq!(
        execute(_vec_str(vec![
            "tome",
            "command-execute",
            "-s",
            "bash",
            EXAMPLE_DIR,
            "--",
            "source_example",
            ""
        ])),
        Ok(format!("'.' '{}/source_example.source' ''", EXAMPLE_DIR))
    );
}

// helper function to assert that the output
// is indeed the help text.
fn assert_is_help_text(result: &str) {
    // uncomment to see output
    // println!("{}", result);
    assert_eq!(result.matches("'\\'").count(), 2);
    assert_eq!(result.matches("'").count(), 8);
    assert!(result.contains("echo -e"));
    // verify that builtin tome commands are present
    assert!(result.contains("commands:"));
    assert!(result.contains("exec:"));
    assert!(result.contains("help:"));
    assert!(result.contains("tome:"));
}

/// help for a specific script should print its summary and help text.
#[test]
fn test_help_for_script() {
    let result = execute(_vec_str(vec![
        "tome",
        "command-execute",
        "-s",
        "bash",
        EXAMPLE_DIR,
        "--",
        "help",
        "file_example",
    ]))
    .unwrap();
    assert!(result.contains("file_example"));
    assert!(result.contains("hey"));
    assert!(result.contains("this is an example of a simple script"));
}

/// help for a script via command-help should also print script-specific text.
#[test]
fn test_help_page_for_script() {
    let result = execute(_vec_str(vec![
        "tome",
        "command-help",
        EXAMPLE_DIR,
        "--",
        "file_example",
    ]))
    .unwrap();
    assert!(result.contains("file_example"));
    assert!(result.contains("hey"));
    assert!(result.contains("this is an example of a simple script"));
}

/// help for a directory prefix should list only commands in that directory.
#[test]
fn test_help_for_directory() {
    let result = execute(_vec_str(vec![
        "tome",
        "command-execute",
        "-s",
        "bash",
        EXAMPLE_DIR,
        "--",
        "help",
        "dir_example",
    ]))
    .unwrap();
    assert!(result.contains("foo"));
    assert!(result.contains("bar"));
}

/// help for an unknown command must return eval-safe output (wrapped in echo)
/// so the shell function does not misinterpret the error string as a command.
#[test]
fn test_help_unknown_command_is_eval_safe() {
    let result = execute(_vec_str(vec![
        "tome",
        "command-execute",
        "-s",
        "bash",
        EXAMPLE_DIR,
        "--",
        "help",
        "nonexistent_command",
    ]))
    .unwrap();
    assert!(
        result.starts_with("echo '"),
        "expected echo-wrapped output, got: {}",
        result
    );
    assert!(result.contains(">&2"));
}