| 1 | use std::path::PathBuf; |
| 2 | use std::process::Command; |
| 3 | |
| 4 | pub fn retrieve_less_version(less_path: PathBuf) -> Option<usize> { |
| 5 | let cmd = Command::new(less_path).arg("--version").output().ok()?; |
| 6 | parse_less_version(&cmd.stdout) |
| 7 | } |
| 8 | |
| 9 | fn parse_less_version(output: &[u8]) -> Option<usize> { |
| 10 | if output.starts_with(b"less ") { |
| 11 | let version = std::str::from_utf8(&output[5..]).ok()?; |
| 12 | let end = version.find(|c: char| !c.is_ascii_digit())?; |
| 13 | version[..end].parse::<usize>().ok() |
| 14 | } else { |
| 15 | None |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | #[cfg(test)] |
| 20 | mod tests { |
| 21 | use super::parse_less_version; |
| 22 | |
| 23 | #[test] |
| 24 | fn test_parse_less_version_487() { |
| 25 | let output = b"less 487 (GNU regular expressions) |
| 26 | Copyright (C) 1984-2016 Mark Nudelman |
| 27 | |
| 28 | less comes with NO WARRANTY, to the extent permitted by law. |
| 29 | For information about the terms of redistribution, |
| 30 | see the file named README in the less distribution. |
| 31 | Homepage: http://www.greenwoodsoftware.com/less"; |
| 32 | |
| 33 | assert_eq!(Some(487), parse_less_version(output)); |
| 34 | } |
| 35 | |
| 36 | #[test] |
| 37 | fn test_parse_less_version_529() { |
| 38 | let output = b"less 529 (Spencer V8 regular expressions) |
| 39 | Copyright (C) 1984-2017 Mark Nudelman |
| 40 | |
| 41 | less comes with NO WARRANTY, to the extent permitted by law. |
| 42 | For information about the terms of redistribution, |
| 43 | see the file named README in the less distribution. |
| 44 | Homepage: http://www.greenwoodsoftware.com/less"; |
| 45 | |
| 46 | assert_eq!(Some(529), parse_less_version(output)); |
| 47 | } |
| 48 | |
| 49 | #[test] |
| 50 | fn test_parse_less_version_551() { |
| 51 | let output = b"less 551 (PCRE regular expressions) |
| 52 | Copyright (C) 1984-2019 Mark Nudelman |
| 53 | |
| 54 | less comes with NO WARRANTY, to the extent permitted by law. |
| 55 | For information about the terms of redistribution, |
| 56 | see the file named README in the less distribution. |
| 57 | Home page: http://www.greenwoodsoftware.com/less"; |
| 58 | |
| 59 | assert_eq!(Some(551), parse_less_version(output)); |
| 60 | } |
| 61 | |
| 62 | #[test] |
| 63 | fn test_parse_less_version_wrong_program() { |
| 64 | let output = b"more from util-linux 2.34"; |
| 65 | |
| 66 | assert_eq!(None, parse_less_version(output)); |
| 67 | } |
| 68 | } |