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
use server::Server;
use definitions::registration::CucumberRegistrar;
use runner::WorldRunner;
use itertools::Itertools;
use std::process::{self, Command, Stdio};
use std::thread;
use std::time::Duration;
use std::env;
#[derive(Default)]
#[must_use = "CucumberConfig has to be consumed by start method!"]
pub struct CucumberConfig<'a, W: Send + 'static> {
world: W,
addr: &'static str,
registrar_fns: Vec<&'a Fn(&mut CucumberRegistrar<W>)>,
args: Vec<&'static str>,
}
pub fn create_config<'a, W: Send + 'static>(world: W) -> CucumberConfig<'a, W> {
CucumberConfig {
world: world,
addr: "127.0.0.1:7878",
registrar_fns: Vec::new(),
args: Vec::new(),
}
}
impl<'a, W: Send + 'static> CucumberConfig<'a, W> {
pub fn address(mut self, address: &'static str) -> CucumberConfig<'a, W> {
self.addr = address;
self
}
pub fn registrar_fns(mut self,
registrars: &'a [&Fn(&mut CucumberRegistrar<W>)])
-> CucumberConfig<'a, W> {
self.registrar_fns.extend_from_slice(registrars);
self
}
pub fn registrar_fn(mut self,
registrar: &'a Fn(&mut CucumberRegistrar<W>))
-> CucumberConfig<'a, W> {
self.registrar_fns.push(registrar);
self
}
pub fn args(mut self, args: &'a [&'static str]) -> CucumberConfig<'a, W> {
self.args.extend_from_slice(args);
self
}
pub fn arg(mut self, arg: &'static str) -> CucumberConfig<'a, W> {
self.args.push(arg);
self
}
#[allow(unused_variables)]
pub fn start(self) {
let mut runner = WorldRunner::new(self.world);
self.registrar_fns.iter().foreach(|fun| fun(&mut runner));
let server = Server::new(runner);
let (handle, stop_rx) = server.start(Some(self.addr));
let status = ruby_command(self.args)
.spawn()
.unwrap_or_else(|e| panic!("failed to execute process: {}. Is Cucumber on path?", e))
.wait()
.unwrap();
thread::sleep(Duration::new(2, 0));
process::exit(status.code().unwrap());
}
}
pub fn ruby_command(args: Vec<&'static str>) -> Command {
let cucumber_bin = if cfg!(target_os = "windows") {
"cucumber.bat"
} else {
"cucumber"
};
let mut command = Command::new(cucumber_bin);
command.stdout(Stdio::inherit());
command.stderr(Stdio::inherit());
env::args().skip(1).foreach(|a| {
command.arg(a);
});
command.args(args.as_slice());
command
}