/dns_utility/tests/utils.rs

https://github.com/SubstratumNetwork/SubstratumNode · Rust · 107 lines · 97 code · 9 blank · 1 comment · 6 complexity · 4c6f3992f445d299bb54e18cc877f941 MD5 · raw file

  1. // Copyright (c) 2017-2019, Substratum LLC (https://substratum.net) and/or its affiliates. All rights reserved.
  2. use std::env;
  3. use std::io::Read;
  4. use std::ops::Drop;
  5. use std::process::Child;
  6. use std::process::Command;
  7. use std::process::Stdio;
  8. pub struct TestCommand {
  9. command: String,
  10. child: Child,
  11. }
  12. impl Drop for TestCommand {
  13. fn drop(&mut self) {
  14. self.kill();
  15. }
  16. }
  17. impl TestCommand {
  18. pub fn start(command: &str, parameters: Vec<&str>) -> TestCommand {
  19. let mut command_object = TestCommand::make_command(command, parameters.clone());
  20. command_object.stdout(Stdio::piped());
  21. command_object.stderr(Stdio::piped());
  22. let child = command_object.spawn().unwrap();
  23. TestCommand {
  24. command: format!(
  25. "{}{}",
  26. command,
  27. parameters
  28. .iter()
  29. .fold(String::new(), |so_far, parameter| format!(
  30. "{} {}",
  31. so_far, parameter
  32. ))
  33. ),
  34. child,
  35. }
  36. }
  37. pub fn wait(&mut self) -> Option<i32> {
  38. match self.child.wait() {
  39. Err(e) => panic!("{:?}", e),
  40. Ok(exit_status) => exit_status.code(),
  41. }
  42. }
  43. pub fn kill(&mut self) {
  44. let _ = self.child.kill(); // can't do anything special for failure
  45. }
  46. pub fn output(&mut self) -> String {
  47. let mut stdout = String::new();
  48. let child_stdout = match self.child.stdout.as_mut() {
  49. Some(cso) => cso,
  50. None => panic!(
  51. "Could not get standard output from command: {}",
  52. self.command
  53. ),
  54. };
  55. child_stdout.read_to_string(&mut stdout).unwrap();
  56. let mut stderr = String::new();
  57. let child_stderr = match self.child.stderr.as_mut() {
  58. Some(cse) => cse,
  59. None => panic!(
  60. "Could not get standard error from command: {}",
  61. self.command
  62. ),
  63. };
  64. child_stderr.read_to_string(&mut stderr).unwrap();
  65. format!(
  66. "STANDARD OUTPUT:\n{}\nSTANDARD ERROR:\n{}\n",
  67. stdout, stderr
  68. )
  69. }
  70. #[cfg(target_os = "windows")]
  71. fn make_command(command: &str, parameters: Vec<&str>) -> Command {
  72. let test_command = env::args().next().unwrap();
  73. let debug_or_release = test_command
  74. .split("\\")
  75. .skip_while(|s| s != &"target")
  76. .skip(1)
  77. .next()
  78. .unwrap();
  79. let command_to_start = &format!("target\\{}\\{}", debug_or_release, command);
  80. let mut command = Command::new(command_to_start);
  81. command.args(parameters);
  82. command
  83. }
  84. #[cfg(not(target_os = "windows"))]
  85. fn make_command(command: &str, parameters: Vec<&str>) -> Command {
  86. let test_command = env::args().next().unwrap();
  87. let debug_or_release = test_command
  88. .split("/")
  89. .skip_while(|s| s != &"target")
  90. .skip(1)
  91. .next()
  92. .unwrap();
  93. let command_to_start = format!("target/{}/{}", debug_or_release, command);
  94. let mut command = Command::new(command_to_start);
  95. command.args(parameters);
  96. command
  97. }
  98. }