Skip to content

Commit

Permalink
test: add cases for format_location (#541)
Browse files Browse the repository at this point in the history
Because this function uses a regex,
it's better to add some tests to make sure
we don't break anything if we want to change the regex.
  • Loading branch information
Rustin170506 authored Apr 18, 2024
1 parent 74331e1 commit 4d91f5d
Showing 1 changed file with 75 additions and 0 deletions.
75 changes: 75 additions & 0 deletions tokio-console/src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -538,3 +538,78 @@ fn pb_duration(dur: prost_types::Duration) -> Duration {
let nanos = u64::try_from(dur.nanos).expect("duration should not be negative!");
Duration::from_secs(secs) + Duration::from_nanos(nanos)
}

#[cfg(test)]
mod tests {
use super::*;

// This test should be run on all platforms. The console can display instrumentation data
// from different console subscribers that may be running on different operating systems.
// For instance, the console could be running on Windows, while the application is running on Linux.
// Therefore, it's important to ensure that paths, which can differ between operating systems,
// are displayed correctly in the console.
#[test]
fn test_format_location_linux() {
// Linux style paths.
let location1 = proto::Location {
file: Some(
"/home/user/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.0.1/src/lib.rs"
.to_string(),
),
..Default::default()
};
let location2 = proto::Location {
file: Some("/home/user/.cargo/git/checkouts/tokio-1.0.1/src/lib.rs".to_string()),
..Default::default()
};
let location3 = proto::Location {
file: Some("/home/user/projects/tokio-1.0.1/src/lib.rs".to_string()),
..Default::default()
};

assert_eq!(
format_location(Some(location1)),
"<cargo>/tokio-1.0.1/src/lib.rs"
);
assert_eq!(
format_location(Some(location2)),
"<cargo>/tokio-1.0.1/src/lib.rs"
);
assert_eq!(
format_location(Some(location3)),
"/home/user/projects/tokio-1.0.1/src/lib.rs"
);

assert_eq!(format_location(None), "<unknown location>");
}

#[test]
fn test_format_location_macos() {
// macOS style paths.
let location4 = proto::Location {
file: Some("/Users/user/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.0.1/src/lib.rs".to_string()),
..Default::default()
};
let location5 = proto::Location {
file: Some("/Users/user/.cargo/git/checkouts/tokio-1.0.1/src/lib.rs".to_string()),
..Default::default()
};
let location6 = proto::Location {
file: Some("/Users/user/projects/tokio-1.0.1/src/lib.rs".to_string()),
..Default::default()
};

assert_eq!(
format_location(Some(location4)),
"<cargo>/tokio-1.0.1/src/lib.rs"
);
assert_eq!(
format_location(Some(location5)),
"<cargo>/tokio-1.0.1/src/lib.rs"
);
assert_eq!(
format_location(Some(location6)),
"/Users/user/projects/tokio-1.0.1/src/lib.rs"
);
}
}

0 comments on commit 4d91f5d

Please sign in to comment.