1use std::path::{Path, PathBuf};
2use std::process::{Child, Command, Stdio};
3
4use super::SubCommand;
5
6use crate::global::GlobalOpts;
7use crate::spawn::ParameterizedSpawn;
8
9#[cfg_attr(
10 feature = "lt2015_1",
11 doc = "`p4 [g-opts] dbverify [ -t db.tablename ] [-U]`"
12)]
13#[cfg_attr(
14 all(feature = "lt2016_1", not(feature = "lt2015_1")),
15 doc = "`p4 [g-opts] dbverify [-t db.tablename] [-U]`"
16)]
17#[cfg_attr(
18 not(feature = "lt2016_1"),
19 doc = "`p4 [g-opts] dbverify [-t db.tablename] [-U][-v]`"
20)]
21#[cfg_attr(
31 not(feature = "lt2016_1"),
32 doc = "",
33 doc = "Use [`Self::verbose`] to request verbose information on the verification."
34)]
35#[derive(Debug, Clone, Default)]
36pub struct DBVerify {
37 bin: PathBuf,
38
39 global_opts: GlobalOpts,
40
41 table: Option<String>,
42
43 less_detailed: bool,
44
45 #[cfg(not(feature = "lt2016_1"))]
46 verbose: bool,
47}
48
49impl DBVerify {
50 pub fn new(bin: impl Into<PathBuf>, global_opts: GlobalOpts) -> Self {
54 Self {
55 bin: bin.into(),
56 global_opts,
57 table: None,
58 less_detailed: false,
59 #[cfg(not(feature = "lt2016_1"))]
60 verbose: false,
61 }
62 }
63
64 pub fn get_bin(&self) -> &Path {
66 &self.bin
67 }
68
69 pub fn set_bin(&mut self, bin: impl Into<PathBuf>) -> &mut Self {
71 self.bin = bin.into();
72 self
73 }
74
75 pub fn bin(mut self, bin: impl Into<PathBuf>) -> Self {
77 self.set_bin(bin);
78 self
79 }
80
81 #[cfg_attr(
86 feature = "lt2014_2",
87 doc = "See the [Global Options](GlobalOpts) section."
88 )]
89 #[cfg_attr(
90 all(feature = "lt2015_1", not(feature = "lt2014_2")),
91 doc = "See the [“Global Options”](GlobalOpts) section."
92 )]
93 #[cfg_attr(
94 all(feature = "lt2017_1", not(feature = "lt2015_1")),
95 doc = "See [“Global Options”](GlobalOpts)."
96 )]
97 #[cfg_attr(
98 all(feature = "lt2018_2", not(feature = "lt2017_1")),
99 doc = "See [Global Options](GlobalOpts)."
100 )]
101 #[cfg_attr(not(feature = "lt2018_2"), doc = "See [Global options](GlobalOpts).")]
102 pub fn get_global_opts(&self) -> &GlobalOpts {
103 &self.global_opts
104 }
105
106 #[cfg_attr(
111 feature = "lt2014_2",
112 doc = "See the [Global Options](GlobalOpts) section."
113 )]
114 #[cfg_attr(
115 all(feature = "lt2015_1", not(feature = "lt2014_2")),
116 doc = "See the [“Global Options”](GlobalOpts) section."
117 )]
118 #[cfg_attr(
119 all(feature = "lt2017_1", not(feature = "lt2015_1")),
120 doc = "See [“Global Options”](GlobalOpts)."
121 )]
122 #[cfg_attr(
123 all(feature = "lt2018_2", not(feature = "lt2017_1")),
124 doc = "See [Global Options](GlobalOpts)."
125 )]
126 #[cfg_attr(not(feature = "lt2018_2"), doc = "See [Global options](GlobalOpts).")]
127 pub fn set_global_opts(&mut self, v: GlobalOpts) -> &mut Self {
128 self.global_opts = v;
129 self
130 }
131
132 #[cfg_attr(
137 feature = "lt2014_2",
138 doc = "See the [Global Options](GlobalOpts) section."
139 )]
140 #[cfg_attr(
141 all(feature = "lt2015_1", not(feature = "lt2014_2")),
142 doc = "See the [“Global Options”](GlobalOpts) section."
143 )]
144 #[cfg_attr(
145 all(feature = "lt2017_1", not(feature = "lt2015_1")),
146 doc = "See [“Global Options”](GlobalOpts)."
147 )]
148 #[cfg_attr(
149 all(feature = "lt2018_2", not(feature = "lt2017_1")),
150 doc = "See [Global Options](GlobalOpts)."
151 )]
152 #[cfg_attr(not(feature = "lt2018_2"), doc = "See [Global options](GlobalOpts).")]
153 pub fn global_opts(mut self, v: GlobalOpts) -> Self {
154 self.global_opts = v;
155 self
156 }
157
158 #[cfg_attr(feature = "lt2015_1", doc = "-t tablename")]
161 #[cfg_attr(not(feature = "lt2015_1"), doc = "-t db.tablename")]
162 pub fn get_table(&self) -> Option<&str> {
165 self.table.as_deref()
166 }
167
168 #[cfg_attr(feature = "lt2015_1", doc = "-t tablename")]
171 #[cfg_attr(not(feature = "lt2015_1"), doc = "-t db.tablename")]
172 pub fn set_table(&mut self, v: impl Into<String>) -> &mut Self {
175 self.table = Some(v.into());
176 self
177 }
178
179 #[cfg_attr(feature = "lt2015_1", doc = "-t tablename")]
182 #[cfg_attr(not(feature = "lt2015_1"), doc = "-t db.tablename")]
183 pub fn table(mut self, v: impl Into<String>) -> Self {
186 self.set_table(v);
187 self
188 }
189
190 pub fn get_less_detailed(&self) -> bool {
201 self.less_detailed
202 }
203
204 pub fn set_less_detailed(&mut self, v: bool) -> &mut Self {
215 self.less_detailed = v;
216 self
217 }
218
219 pub fn less_detailed(mut self, v: bool) -> Self {
230 self.set_less_detailed(v);
231 self
232 }
233
234 #[cfg(not(feature = "lt2016_1"))]
240 pub fn get_verbose(&self) -> bool {
241 self.verbose
242 }
243
244 #[cfg(not(feature = "lt2016_1"))]
250 pub fn set_verbose(&mut self, v: bool) -> &mut Self {
251 self.verbose = v;
252 self
253 }
254
255 #[cfg(not(feature = "lt2016_1"))]
261 pub fn verbose(mut self, v: bool) -> Self {
262 self.set_verbose(v);
263 self
264 }
265}
266
267impl SubCommand for DBVerify {
268 fn name(&self) -> &str {
269 "dbverify"
270 }
271
272 fn inject_local_args(&self, command: &mut Command) {
273 if let Some(table) = &self.table {
274 command.arg("-t").arg(table);
275 }
276
277 if self.less_detailed {
278 command.arg("-U");
279 }
280
281 #[cfg(not(feature = "lt2016_1"))]
282 if self.verbose {
283 command.arg("-v");
284 }
285 }
286
287 fn global_opts(&self) -> Option<&GlobalOpts> {
288 Some(&self.global_opts)
289 }
290}
291
292impl ParameterizedSpawn<()> for DBVerify {
293 type Output = Child;
294 type Error = std::io::Error;
295
296 fn spawn_with(&mut self, (): ()) -> Result<Self::Output, Self::Error> {
300 self.setup_command(&self.bin)
301 .stdout(Stdio::piped())
302 .stderr(Stdio::piped())
303 .spawn()
304 }
305}
306
307#[cfg(test)]
308mod tests {
309 use super::*;
310 use crate::cmd::args_of;
311
312 #[test]
313 fn bin_accessors_update_executable_path() {
314 let mut command = DBVerify::new("p4", GlobalOpts::new());
315 assert_eq!(command.get_bin(), Path::new("p4"));
316
317 command.set_bin("/usr/local/bin/p4");
318 assert_eq!(command.get_bin(), Path::new("/usr/local/bin/p4"));
319
320 let command = command.bin("p4.exe");
321 assert_eq!(command.get_bin(), Path::new("p4.exe"));
322 }
323
324 #[test]
327 fn without_options() {
328 let dbverify = DBVerify::new("p4", GlobalOpts::new());
329
330 assert_eq!(args_of(&dbverify.setup_command("p4")), ["dbverify"]);
331 }
332
333 #[test]
334 fn with_table() {
335 let dbverify = DBVerify::new("p4", GlobalOpts::new()).table("db.have");
336
337 assert_eq!(dbverify.get_table(), Some("db.have"));
338 assert_eq!(
339 args_of(&dbverify.setup_command("p4")),
340 ["dbverify", "-t", "db.have"]
341 );
342 }
343
344 #[test]
345 fn less_detailed_flag() {
346 let dbverify = DBVerify::new("p4", GlobalOpts::new()).less_detailed(true);
347
348 assert!(dbverify.get_less_detailed());
349 assert_eq!(args_of(&dbverify.setup_command("p4")), ["dbverify", "-U"]);
350 }
351
352 #[test]
353 fn table_and_less_detailed_in_syntax_order() {
354 let mut dbverify = DBVerify::new("p4", GlobalOpts::new()).less_detailed(true);
355 dbverify.set_table("db.have");
356
357 assert_eq!(
358 args_of(&dbverify.setup_command("p4")),
359 ["dbverify", "-t", "db.have", "-U"]
360 );
361 }
362
363 #[cfg(not(feature = "lt2016_1"))]
364 #[test]
365 fn verbose_flag() {
366 let dbverify = DBVerify::new("p4", GlobalOpts::new()).verbose(true);
367
368 assert!(dbverify.get_verbose());
369 assert_eq!(args_of(&dbverify.setup_command("p4")), ["dbverify", "-v"]);
370 }
371
372 #[cfg(not(feature = "lt2016_1"))]
373 #[test]
374 fn all_options_in_syntax_order() {
375 let dbverify = DBVerify::new("p4", GlobalOpts::new())
376 .table("db.have")
377 .less_detailed(true)
378 .verbose(true);
379
380 assert_eq!(
381 args_of(&dbverify.setup_command("p4")),
382 ["dbverify", "-t", "db.have", "-U", "-v"]
383 );
384 }
385
386 #[test]
387 fn global_opts_are_injected_once() {
388 let dbverify = DBVerify::new(
389 "p4",
390 GlobalOpts::new().port("localhost:1666").quiet_mode(true),
391 )
392 .table("db.have");
393
394 assert_eq!(
395 args_of(&dbverify.setup_command("p4")),
396 ["-p", "localhost:1666", "-q", "dbverify", "-t", "db.have"]
397 );
398 }
399}