diff --git a/tokio-console/src/state/mod.rs b/tokio-console/src/state/mod.rs index 51913dd92..d40dac014 100644 --- a/tokio-console/src/state/mod.rs +++ b/tokio-console/src/state/mod.rs @@ -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)), + "/tokio-1.0.1/src/lib.rs" + ); + assert_eq!( + format_location(Some(location2)), + "/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), ""); + } + + #[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)), + "/tokio-1.0.1/src/lib.rs" + ); + assert_eq!( + format_location(Some(location5)), + "/tokio-1.0.1/src/lib.rs" + ); + assert_eq!( + format_location(Some(location6)), + "/Users/user/projects/tokio-1.0.1/src/lib.rs" + ); + } +}