conch-runtime 0.1.2

A library for evaluating/executing programs written in the shell programming language.
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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
extern crate conch_runtime;
extern crate futures;
extern crate tempdir;
extern crate tokio_core;
extern crate void;

use self::conch_runtime::STDOUT_FILENO;
use self::conch_runtime::error::IsFatalError;
use self::conch_runtime::io::{FileDesc, FileDescWrapper};
use self::futures::future::FutureResult;
use self::futures::future::result as future_result;
use self::tempdir::TempDir;
use self::void::{unreachable, Void};
use std::borrow::Borrow;
use std::fs::OpenOptions;
use std::rc::Rc;
use std::sync::Arc;

// Convenience re-exports
pub use self::conch_runtime::{ExitStatus, EXIT_SUCCESS, EXIT_ERROR, Spawn};
pub use self::conch_runtime::env::*;
pub use self::conch_runtime::error::*;
pub use self::conch_runtime::eval::*;
pub use self::conch_runtime::future::*;
pub use self::conch_runtime::path::*;
pub use self::conch_runtime::spawn::*;
pub use self::futures::{Async, Future, Poll};
pub use self::tokio_core::reactor::Core;

/// Poor man's mktmp. A macro for creating "unique" test directories.
#[macro_export]
macro_rules! mktmp {
    () => {
        mktmp_impl(concat!("test-", module_path!(), "-", line!(), "-", column!()))
    };
}

pub fn mktmp_impl(path: &str) -> TempDir {
    if cfg!(windows) {
        TempDir::new(&path.replace(":", "_")).unwrap()
    } else {
        TempDir::new(path).unwrap()
    }
}

#[macro_export]
macro_rules! test_cancel {
    ($future:expr) => { test_cancel!($future, ()) };
    ($future:expr, $env:expr) => {{
        ::support::test_cancel_impl($future, &mut $env);
    }};
}

pub fn test_cancel_impl<F: EnvFuture<E>, E: ?Sized>(mut future: F, env: &mut E) {
    let _ = future.poll(env); // Give a chance to init things
    future.cancel(env); // Cancel the operation
    drop(future);
}

#[cfg(unix)]
pub const DEV_NULL: &str = "/dev/null";

#[cfg(windows)]
pub const DEV_NULL: &str = "NUL";

pub fn dev_null() -> Rc<FileDesc> {
    let fdes = OpenOptions::new()
        .read(true)
        .write(true)
        .open(DEV_NULL)
        .unwrap()
        .into();

    Rc::new(fdes)
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum MockErr {
    Fatal(bool),
    ExpansionError(ExpansionError),
    RedirectionError(Arc<RedirectionError>),
    CommandError(Arc<CommandError>),
}

impl self::conch_runtime::error::IsFatalError for MockErr {
    fn is_fatal(&self) -> bool {
        match *self {
            MockErr::Fatal(fatal) => fatal,
            MockErr::ExpansionError(ref e) => e.is_fatal(),
            MockErr::RedirectionError(ref e) => e.is_fatal(),
            MockErr::CommandError(ref e) => e.is_fatal(),
        }
    }
}

impl ::std::error::Error for MockErr {
    fn description(&self) -> &str {
        "mock error"
    }
}

impl ::std::fmt::Display for MockErr {
    fn fmt(&self, fmt: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
        write!(fmt, "mock {}fatal error", if self.is_fatal() { "non-" } else { "" })
    }
}

impl From<RuntimeError> for MockErr {
    fn from(err: RuntimeError) -> Self {
        MockErr::Fatal(err.is_fatal())
    }
}

impl From<ExpansionError> for MockErr {
    fn from(err: ExpansionError) -> Self {
        MockErr::ExpansionError(err)
    }
}

impl From<RedirectionError> for MockErr {
    fn from(err: RedirectionError) -> Self {
        MockErr::RedirectionError(Arc::new(err))
    }
}

impl From<CommandError> for MockErr {
    fn from(err: CommandError) -> Self {
        MockErr::CommandError(Arc::new(err))
    }
}

impl From<::std::io::Error> for MockErr {
    fn from(_: ::std::io::Error) -> Self {
        MockErr::Fatal(false)
    }
}

impl From<Void> for MockErr {
    fn from(void: Void) -> Self {
        unreachable(void)
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
#[must_use = "futures do nothing unless polled"]
pub struct MustCancel {
    /// Did we get polled at least once (i.e. did we get fully "spawned")
    was_polled: bool,
    /// Did we ever get a "cancel" signal
    was_canceled: bool,
}

impl MustCancel {
    pub fn new() -> Self {
        MustCancel {
            was_polled: false,
            was_canceled: false,
        }
    }

    pub fn poll<T, E>(&mut self) -> Poll<T, E> {
        assert!(!self.was_canceled, "cannot poll after canceling");
        self.was_polled = true;
        Ok(Async::NotReady)
    }

    pub fn cancel(&mut self) {
        assert!(!self.was_canceled, "cannot cancel twice");
        self.was_canceled = true;
    }
}

impl Drop for MustCancel {
    fn drop(&mut self) {
        if self.was_polled {
            assert!(self.was_canceled, "MustCancel future was not canceled!");
        }
    }
}

#[must_use = "futures do nothing unless polled"]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum MockCmd {
    Status(ExitStatus),
    Error(MockErr),
    Panic(&'static str),
    MustCancel(MustCancel),
}

pub fn mock_status(status: ExitStatus) -> MockCmd {
    MockCmd::Status(status)
}

pub fn mock_error(fatal: bool) -> MockCmd {
    MockCmd::Error(MockErr::Fatal(fatal))
}

pub fn mock_panic(msg: &'static str) -> MockCmd {
    MockCmd::Panic(msg)
}

pub fn mock_must_cancel() -> MockCmd {
    MockCmd::MustCancel(MustCancel::new())
}

impl<E: ?Sized> Spawn<E> for MockCmd {
    type Error = MockErr;
    type EnvFuture = Self;
    type Future = FutureResult<ExitStatus, Self::Error>;

    fn spawn(self, _: &E) -> Self::EnvFuture {
        self
    }
}

impl<'a, E: ?Sized> Spawn<E> for &'a MockCmd {
    type Error = MockErr;
    type EnvFuture = MockCmd;
    type Future = FutureResult<ExitStatus, Self::Error>;

    fn spawn(self, _: &E) -> Self::EnvFuture {
        self.clone()
    }
}

impl<E: ?Sized> EnvFuture<E> for MockCmd {
    type Item = FutureResult<ExitStatus, Self::Error>;
    type Error = MockErr;

    fn poll(&mut self, _: &mut E) -> Poll<Self::Item, Self::Error> {
        match *self {
            MockCmd::Status(s) => Ok(Async::Ready(future_result(Ok(s)))),
            MockCmd::Error(ref e) => Err(e.clone()),
            MockCmd::Panic(msg) => panic!("{}", msg),
            MockCmd::MustCancel(ref mut mc) => mc.poll(),
        }
    }

    fn cancel(&mut self, _env: &mut E) {
        match *self {
            MockCmd::Status(_) |
            MockCmd::Error(_) |
            MockCmd::Panic(_) => {},
            MockCmd::MustCancel(ref mut mc) => mc.cancel(),
        }
    }
}

pub fn mock_word_fields(fields: Fields<String>) -> MockWord {
    MockWord::Fields(Some(fields))
}

pub fn mock_word_error(fatal: bool) -> MockWord {
    MockWord::Error(MockErr::Fatal(fatal))
}

pub fn mock_word_must_cancel() -> MockWord {
    MockWord::MustCancel(MustCancel::new())
}

pub fn mock_word_assert_cfg(cfg: WordEvalConfig) -> MockWord {
    MockWord::AssertCfg(cfg, None)
}

pub fn mock_word_assert_cfg_with_fields(
    fields: Fields<String>,
    cfg: WordEvalConfig,
) -> MockWord {
    MockWord::AssertCfg(cfg, Some(fields))
}

pub fn mock_word_panic(msg: &'static str) -> MockWord {
    MockWord::Panic(msg)
}

#[must_use = "futures do nothing unless polled"]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum MockWord {
    Fields(Option<Fields<String>>),
    Error(MockErr),
    MustCancel(MustCancel),
    AssertCfg(WordEvalConfig, Option<Fields<String>>),
    Panic(&'static str),
}

impl<E: ?Sized> WordEval<E> for MockWord {
    type EvalResult = String;
    type Error = MockErr;
    type EvalFuture = Self;

    fn eval_with_config(self, _: &E, cfg: WordEvalConfig) -> Self::EvalFuture {
        if let MockWord::AssertCfg(expected, _) = self {
            assert_eq!(expected, cfg);
        }

        self
    }
}

impl<'a, E: ?Sized> WordEval<E> for &'a MockWord {
    type EvalResult = String;
    type Error = MockErr;
    type EvalFuture = MockWord;

    fn eval_with_config(self, _: &E, cfg: WordEvalConfig) -> Self::EvalFuture {
        if let MockWord::AssertCfg(ref expected, _) = *self {
            assert_eq!(*expected, cfg);
        }

        self.clone()
    }
}

impl<E: ?Sized> EnvFuture<E> for MockWord {
    type Item = Fields<String>;
    type Error = MockErr;

    fn poll(&mut self, _: &mut E) -> Poll<Self::Item, Self::Error> {
        match *self {
            MockWord::Fields(ref mut f) => Ok(Async::Ready(f.take().expect("polled twice"))),
            MockWord::Error(ref mut e) => Err(e.clone()),
            MockWord::MustCancel(ref mut mc) => mc.poll(),
            MockWord::AssertCfg(_, ref mut fields) => {
                let ret = fields.take().unwrap_or(Fields::Zero);
                Ok(Async::Ready(ret))
            },
            MockWord::Panic(msg) => panic!("{}", msg),
        }
    }

    fn cancel(&mut self, _: &mut E) {
        match *self {
            MockWord::Fields(_) |
            MockWord::Error(_) |
            MockWord::AssertCfg(_, _) => {},
            MockWord::MustCancel(ref mut mc) => mc.cancel(),
            MockWord::Panic(msg) => panic!("{}", msg),
        }
    }
}

#[derive(Debug, Clone)]
pub enum MockParam {
    FieldsWithName(Option<Fields<String>>, String),
    Fields(Option<Fields<String>>),
    Split(bool /* expect_split */, Fields<String>),
}

impl ::std::fmt::Display for MockParam {
    fn fmt(&self, fmt: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
        write!(fmt, "MockParam")
    }
}

impl<E: ?Sized> ParamEval<E> for MockParam {
    type EvalResult = String;

    fn eval(&self, split_fields_further: bool, _: &E) -> Option<Fields<Self::EvalResult>> {
        match *self {
            MockParam::Fields(ref f) |
            MockParam::FieldsWithName(ref f, _) => f.clone(),
            MockParam::Split(expect_split, ref f) => {
                assert_eq!(expect_split, split_fields_further);
                Some(f.clone())
            },
        }
    }

    fn assig_name(&self) -> Option<Self::EvalResult> {
        match *self {
            MockParam::Fields(_) |
            MockParam::Split(..) => None,
            MockParam::FieldsWithName(_, ref name) => Some(name.clone()),
        }
    }
}

#[derive(Debug, Clone)]
pub enum MockOutCmd {
    Out(&'static str),
    Cmd(MockCmd),
}

impl<E: ?Sized> Spawn<E> for MockOutCmd
    where E: AsyncIoEnvironment + FileDescEnvironment,
          E::FileHandle: Clone + FileDescWrapper,
          E::WriteAll: 'static + Send + Sync,
{
    type Error = MockErr;
    type EnvFuture = Self;
    type Future = Box<'static + Future<Item = ExitStatus, Error = Self::Error> + Send + Sync>;

    fn spawn(self, _: &E) -> Self::EnvFuture {
        self
    }
}

impl<'a, E: ?Sized> Spawn<E> for &'a MockOutCmd
    where E: AsyncIoEnvironment + FileDescEnvironment,
          E::FileHandle: Clone + FileDescWrapper,
          E::WriteAll: 'static + Send + Sync,
{
    type Error = MockErr;
    type EnvFuture = MockOutCmd;
    type Future = Box<'static + Future<Item = ExitStatus, Error = Self::Error> + Send + Sync>;

    fn spawn(self, _: &E) -> Self::EnvFuture {
        self.clone()
    }
}

impl<E: ?Sized> EnvFuture<E> for MockOutCmd
    where E: AsyncIoEnvironment + FileDescEnvironment,
          E::FileHandle: Clone + FileDescWrapper,
          E::WriteAll: 'static + Send + Sync,
{
    type Item = Box<'static + Future<Item = ExitStatus, Error = Self::Error> + Send + Sync>;
    type Error = MockErr;

    fn poll(&mut self, env: &mut E) -> Poll<Self::Item, Self::Error> {
        let msg = match *self {
            MockOutCmd::Out(ref m) => m,
            MockOutCmd::Cmd(ref mut c) => match c.poll(env) {
                Ok(Async::Ready(f)) => return Ok(Async::Ready(Box::new(f))),
                Ok(Async::NotReady) => return Ok(Async::NotReady),
                Err(e) => return Err(e),
            },
        };

        let fd = env.file_desc(STDOUT_FILENO)
            .expect("failed to get stdout")
            .0
            .borrow()
            .duplicate()
            .expect("failed to duplicate stdout handle");

        let future = env.write_all(fd, msg.as_bytes().into())
            .then(|result| {
                result.expect("unexpected failure");
                Ok(EXIT_SUCCESS)
            });

        Ok(Async::Ready(Box::new(future)))
    }

    fn cancel(&mut self, env: &mut E) {
        match *self {
            MockOutCmd::Out(_) => {},
            MockOutCmd::Cmd(ref mut c) => c.cancel(env),
        };
    }
}

#[must_use = "futures do nothing unless polled"]
#[derive(Debug, Clone)]
pub enum MockRedirect<T> {
    Action(Option<RedirectAction<T>>),
    MustCancel(MustCancel),
    Error(Option<MockErr>),
}

pub fn mock_redirect<T>(action: RedirectAction<T>) -> MockRedirect<T> {
    MockRedirect::Action(Some(action))
}

pub fn mock_redirect_must_cancel<T>() -> MockRedirect<T> {
    MockRedirect::MustCancel(MustCancel::new())
}

pub fn mock_redirect_error<T>(fatal: bool) -> MockRedirect<T> {
    MockRedirect::Error(Some(MockErr::Fatal(fatal)))
}

impl<T, E: ?Sized> RedirectEval<E> for MockRedirect<T> {
    type Handle = T;
    type Error = MockErr;
    type EvalFuture = Self;

    fn eval(self, _: &E) -> Self::EvalFuture {
        self
    }
}

impl<'a, T, E: ?Sized> RedirectEval<E> for &'a MockRedirect<T>
    where T: Clone,
{
    type Handle = T;
    type Error = MockErr;
    type EvalFuture = MockRedirect<T>;

    fn eval(self, _: &E) -> Self::EvalFuture {
        self.clone()
    }
}

impl<T, E: ?Sized> EnvFuture<E> for MockRedirect<T> {
    type Item = RedirectAction<T>;
    type Error = MockErr;

    fn poll(&mut self, _: &mut E) -> Poll<Self::Item, Self::Error> {
        match *self {
            MockRedirect::Action(ref mut a) => Ok(Async::Ready(a.take().expect("polled twice"))),
            MockRedirect::MustCancel(ref mut mc) => mc.poll(),
            MockRedirect::Error(ref mut e) => Err(e.take().expect("polled twice")),
        }
    }

    fn cancel(&mut self, _: &mut E) {
        match *self {
            MockRedirect::Action(_) |
            MockRedirect::Error(_) => {},
            MockRedirect::MustCancel(ref mut mc) => mc.cancel(),
        }
    }
}

pub fn new_env() -> (Core, DefaultEnvRc) {
    new_env_with_threads(1)
}

pub fn new_env_with_threads(threads: usize) -> (Core, DefaultEnvRc) {
    let lp = Core::new().expect("failed to create Core loop");
    let env = DefaultEnvRc::new(lp.remote(), Some(threads)).expect("failed to create env");
    (lp, env)
}

pub fn new_env_with_no_fds() -> (Core, DefaultEnvRc) {
    let lp = Core::new().expect("failed to create Core loop");
    let mut cfg = DefaultEnvConfigRc::new(lp.remote(), Some(1)).expect("failed to create env cfg");
    cfg.file_desc_env = FileDescEnv::new();
    let env = DefaultEnvRc::with_config(cfg);
    (lp, env)
}

#[macro_export]
macro_rules! run {
    ($cmd:expr) => {{
        let (lp, env) = ::support::new_env();
        run!($cmd, lp, env)
    }};

    ($cmd:expr, $lp:expr, $env:expr) => {{
        let mut lp = $lp;
        let env = $env;
        let cmd = $cmd;

        #[allow(deprecated)]
        let ret_ref = run(&cmd, env.sub_env(), &mut lp);
        #[allow(deprecated)]
        let ret = run(cmd, env, &mut lp);

        assert_eq!(ret_ref, ret);
        ret
    }};
}

/// Spawns and syncronously runs the provided command to completion.
#[deprecated(note = "use `run!` macro instead, to cover spawning T and &T")]
pub fn run<T: Spawn<E>, E>(cmd: T, env: E, lp: &mut Core) -> Result<ExitStatus, T::Error> {
    let future = cmd.spawn(&env)
        .pin_env(env)
        .flatten();

    lp.run(future)
}

#[macro_export]
macro_rules! run_cancel {
    ($cmd:expr) => {{
        let cmd = $cmd;
        #[allow(deprecated)]
        let ret_ref = run_cancel(&cmd);
        #[allow(deprecated)]
        let ret = run_cancel(cmd);
        assert_eq!(ret_ref, ret);
        ret
    }}
}

/// Spawns the provided command and polls it a single time to give it a
/// chance to get initialized. Then cancels and drops the future.
///
/// It is up to the caller to set up the command in a way that failure to
/// propagate cancel messages results in a panic.
#[deprecated(note = "use `run!` macro instead, to cover spawning T and &T")]
pub fn run_cancel<T: Spawn<DefaultEnvRc>>(cmd: T) {
    let (_, mut env) = new_env();
    let env_future = cmd.spawn(&env);
    test_cancel_impl(env_future, &mut env);
}

#[macro_export]
macro_rules! eval {
    ($word:expr, $cfg:expr) => { eval_with_thread_pool!($word, $cfg, 1) }
}

#[macro_export]
macro_rules! eval_with_thread_pool {
    ($word:expr, $cfg:expr, $threads:expr) => {{
        let word = $word;
        let cfg = $cfg;
        #[allow(deprecated)]
        let ret_ref = eval_word(&word, cfg, $threads);
        #[allow(deprecated)]
        let ret = eval_word(word, cfg, $threads);
        assert_eq!(ret_ref, ret);
        ret
    }}
}

/// Evaluates a word to completion.
#[deprecated(note = "use `eval!` macro instead, to cover spawning T and &T")]
pub fn eval_word<W: WordEval<DefaultEnv<String>>>(word: W, cfg: WordEvalConfig, threads: usize)
    -> Result<Fields<W::EvalResult>, W::Error>
{
    let mut lp = Core::new().expect("failed to create Core loop");
    let env = DefaultEnv::<String>::new(lp.remote(), Some(threads)).expect("failed to create env");
    let future = word.eval_with_config(&env, cfg)
        .pin_env(env);

    lp.run(future)
}

pub fn bin_path(s: &str) -> ::std::path::PathBuf {
    let mut me = ::std::env::current_exe().unwrap();
    me.pop();
    if me.ends_with("deps") {
        me.pop();
    }
    me.push(s);
    me
}