Skip to content

Commit

Permalink
Switched to regex matching in test
Browse files Browse the repository at this point in the history
  • Loading branch information
AnirbanHalder654322 committed May 9, 2024
1 parent c8fa0bb commit 46a9f8c
Showing 1 changed file with 17 additions and 56 deletions.
73 changes: 17 additions & 56 deletions tests/by-util/test_ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1381,77 +1381,38 @@ fn test_ls_long_symlink_color() {
/// using 'ln' to create a dangling symlink
/// Only tests whether the specific color of the target and the dangling_symlink are equal
/// Modified version of the "test_ls_long_symlink_color" test. Please see that for more details
//#[cfg(all(features = "ln"))]
#[test]
fn test_ls_long_dangling_symlink_color() {
let ts = TestScenario::new(util_name!());
let at = &ts.fixtures;

at.mkdir("dir1");
ts.ccmd("ln")
.arg("-s")
.arg("foo")
.arg("dir1/dangling_symlink")
.succeeds();

// The string between \x1b[ and m
type Color = String;

// The string between the color start and the color end is the file name itself.
type Name = String;

at.mkdir("dir2");
at.symlink_dir("foo", "dir1/dangling_symlink");
let result = ts
.ucmd()
.terminal_simulation(true)
.arg("-l")
.arg("--color=always")
.arg("dir1/dangling_symlink")
.succeeds();

let stdout = result.stdout_str();
// stdout contains output like in the below sequence. We match for the color i.e. 01;36
// \x1b[0m\x1b[01;36mdir1/dangling_symlink\x1b[0m -> \x1b[01;36mfoo\x1b[0m
let color_regex = Regex::new(r"(\d\d;)\d\dm").unwrap();
// colors_vec[0] contains the symlink color and style and colors_vec[1] contains the color and style of the file the
// symlink points to.
let colors_vec: Vec<_> = color_regex
.find_iter(stdout)
.map(|color| color.as_str())
.collect();
// constructs the string of file path with the color code
let symlink_color_name = colors_vec[0].to_owned() + "dir1/dangling_symlink\x1b";
let target_color_name = colors_vec[1].to_owned() + at.plus_as_string("foo\x1b").as_str();

//Helper function to isolate the sequence in the string from where the ANSI C escape code begins
fn isolate_colored_sequence(stdout: &str) -> (Name, Name) {
let name = String::from("\x1b")
+ stdout
.split(" -> ")
.next()
.unwrap()
.split(" \x1b")
.last()
.unwrap();
// `target` is whatever comes after the ar.
let target = stdout.split(" -> ").last().unwrap().to_string();

(name, target)
}

let (name, target) = isolate_colored_sequence(stdout);

let (symlink_color, symlink_name) = capture_colored_string(&name);
let (target_color, target_name) = capture_colored_string(&target);

assert_eq!(
(symlink_name, target_name),
("dir1/dangling_symlink".to_string(), "foo".to_string())
);
assert_eq!(symlink_color, target_color);

// Helper function for capturing the color from the escape sequence and name of file
// Please see the helper function of the same name in "test_ls_long_symlink_color"
fn capture_colored_string(input: &str) -> (Color, Name) {
let colored_name = Regex::new(r"(?:\x1b\[0m\x1b)?\[([0-9;]+)m(.+)\x1b\[0m").unwrap();
match colored_name.captures(input) {
Some(captures) => {
dbg!(captures.get(1).unwrap().as_str().to_string());
dbg!(captures.get(2).unwrap().as_str().to_string());
return (
captures.get(1).unwrap().as_str().to_string(),
captures.get(2).unwrap().as_str().to_string(),
);
}
None => (String::new(), input.to_string()),
}
}
assert!(stdout.contains(&symlink_color_name));
assert!(stdout.contains(&target_color_name));
}

#[test]
Expand Down

0 comments on commit 46a9f8c

Please sign in to comment.