| 1 | # rusty-fork |
| 2 | |
| 3 | [](https://crates.io/crates/rusty-fork) |
| 4 | Rusty-fork provides a way to "fork" unit tests into separate processes. |
| 5 | |
| 6 | There are a number of reasons to want to run some tests in isolated |
| 7 | processes: |
| 8 | |
| 9 | - When tests share a process, if any test causes the process to abort, |
| 10 | segfault, overflow the stack, etc., the entire test runner process dies. If |
| 11 | the test is in a subprocess, only the subprocess dies and the test runner |
| 12 | simply fails the test. |
| 13 | |
| 14 | - Isolating a test to a subprocess makes it possible to add a timeout to |
| 15 | the test and forcibly terminate it and produce a normal test failure. |
| 16 | |
| 17 | - Tests which need to interact with some inherently global property, such |
| 18 | as the current working directory, can do so without interfering with other |
| 19 | tests. |
| 20 | |
| 21 | This crate itself provides two things: |
| 22 | |
| 23 | - The [`rusty_fork_test!`](macro.rusty_fork_test.html) macro, which is a |
| 24 | simple way to wrap standard Rust tests to be run in subprocesses with |
| 25 | optional timeouts. |
| 26 | |
| 27 | - The [`fork`](fn.fork.html) function which can be used as a building block |
| 28 | to make other types of process isolation strategies. |
| 29 | |
| 30 | ## Quick Start |
| 31 | |
| 32 | If you just want to run normal Rust tests in isolated processes, getting |
| 33 | started is pretty quick. |
| 34 | |
| 35 | In `Cargo.toml`, add |
| 36 | |
| 37 | ```toml |
| 38 | [dev-dependencies] |
| 39 | rusty-fork = "0.3.0" |
| 40 | ``` |
| 41 | |
| 42 | Then, you can simply wrap any test(s) to be isolated with the |
| 43 | [`rusty_fork_test!`](macro.rusty_fork_test.html) macro. |
| 44 | |
| 45 | ```rust |
| 46 | use rusty_fork::rusty_fork_test; |
| 47 | |
| 48 | rusty_fork_test! { |
| 49 | #[test] |
| 50 | fn my_test() { |
| 51 | assert_eq!(2, 1 + 1); |
| 52 | } |
| 53 | |
| 54 | // more tests... |
| 55 | } |
| 56 | ``` |
| 57 | |
| 58 | For more advanced usage, have a look at the [`fork`](fn.fork.html) |
| 59 | function. |
| 60 | |
| 61 | ## How rusty-fork works |
| 62 | |
| 63 | Unix-style process forking isn't really viable within the standard Rust |
| 64 | test environment for a number of reasons. |
| 65 | |
| 66 | - While true process forking can be done on Windows, it's neither fast nor |
| 67 | reliable. |
| 68 | |
| 69 | - The Rust test environment is multi-threaded, so attempting to do anything |
| 70 | non-trivial after a process fork would result in undefined behaviour. |
| 71 | |
| 72 | Rusty-fork instead works by _spawning_ a fresh instance of the current |
| 73 | process, after adjusting the command-line to ensure that only the desired |
| 74 | test is entered. Some additional coordination establishes the parent/child |
| 75 | branches and (not quite seamlessly) integrates the child's output with the |
| 76 | test output capture system. |
| 77 | |
| 78 | Coordination between the processes is performed via environment variables, |
| 79 | since there is otherwise no way to pass parameters to a test. |
| 80 | |
| 81 | Since it needs to spawn new copies of the test runner executable, |
| 82 | rusty-fork does need to know about the meaning of every flag passed by the |
| 83 | user. If any unknown flags are encountered, forking will fail. Please do |
| 84 | not hesitate to file |
| 85 | [issues](https://github.com/AltSysrq/rusty-fork/issues) if rusty-fork fails |
| 86 | to recognise any valid flags passed to the test runner. |
| 87 | |
| 88 | It is possible to inform rusty-fork of new flags without patching by |
| 89 | setting environment variables. For example, if a new `--frob-widgets` flag |
| 90 | were added to the test runner, you could set `RUSTY_FORK_FLAG_FROB_WIDGETS` |
| 91 | to one of the following: |
| 92 | |
| 93 | - `pass` — Pass the flag (just the flag) to the child process |
| 94 | - `pass-arg` — Pass the flag and its following argument to the child process |
| 95 | - `drop` — Don't pass the flag to the child process |
| 96 | - `drop-arg` — Don't pass the flag to the child process, and ignore whatever |
| 97 | argument follows. |
| 98 | |
| 99 | In general, arguments that affect which tests are run should be dropped, |
| 100 | and others should be passed. |
| 101 | |
| 102 | |
| 103 | ## Contribution |
| 104 | |
| 105 | Unless you explicitly state otherwise, any contribution intentionally submitted |
| 106 | for inclusion in the work by you, as defined in the Apache-2.0 license, shall |
| 107 | be dual licensed as above, without any additional terms or conditions. |