Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
heaths committed Jun 4, 2021
1 parent 250c336 commit 4075f13
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 4 deletions.
6 changes: 5 additions & 1 deletion sdk/core/build.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use rustc_version::version;

fn main() {
println!("cargo:rustc-env=AZSDK_RUSTC_VERSION={}", version().unwrap());
let version = match version() {
Ok(version) => version.to_string(),
Err(_) => "unknown".to_string(),
};
println!("cargo:rustc-env=AZSDK_RUSTC_VERSION={}", version);
}
88 changes: 85 additions & 3 deletions sdk/core/src/policies/telemetry_policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,15 @@ pub struct TelemetryPolicy {

impl TelemetryPolicy {
pub fn new(options: TelemetryOptions) -> Self {
let crate_name = env!("CARGO_PKG_NAME");
let crate_version = env!("CARGO_PKG_VERSION");
let platform_info = format!("({}; {}; {})", env!("AZSDK_RUSTC_VERSION"), OS, ARCH,);
Self::with_environment::<Env>(options)
}

fn with_environment<T: Environment>(options: TelemetryOptions) -> Self {
const UNKNOWN: &'static str = "unknown";
let crate_name = T::crate_name().unwrap_or(UNKNOWN);
let crate_version = T::crate_version().unwrap_or(UNKNOWN);
let rustc_version = T::rustc_version().unwrap_or(UNKNOWN);
let platform_info = format!("({}; {}; {})", rustc_version, OS, ARCH,);
let header = match options.application_id {
Some(application_id) => format!(
"{} azsdk-rust-{}/{} {}",
Expand All @@ -47,6 +53,23 @@ impl Default for TelemetryPolicy {
}
}

trait Environment {
fn crate_name() -> Option<&'static str> {
option_env!("CARGO_PKG_NAME")
}

fn crate_version() -> Option<&'static str> {
option_env!("CARGO_PKG_VERSION")
}

fn rustc_version() -> Option<&'static str> {
option_env!("AZSDK_RUSTC_VERSION")
}
}

struct Env;
impl Environment for Env {}

#[async_trait::async_trait]
impl Policy for TelemetryPolicy {
async fn send(
Expand All @@ -62,3 +85,62 @@ impl Policy for TelemetryPolicy {
next[0].send(ctx, request, &next[1..]).await
}
}

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

// tests assume cargo + rustc
const CRATE_NAME: &'static str = env!("CARGO_PKG_NAME");
const CRATE_VERSION: &'static str = env!("CARGO_PKG_VERSION");
const RUSTC_VERSION: &'static str = env!("AZSDK_RUSTC_VERSION");

struct EmptyEnv;
impl Environment for EmptyEnv {
fn crate_name() -> Option<&'static str> {
None
}

fn crate_version() -> Option<&'static str> {
None
}

fn rustc_version() -> Option<&'static str> {
None
}
}

#[test]
fn test_default() {
let policy = TelemetryPolicy::default();
assert_eq!(
policy.header,
format!(
"azsdk-rust-{}/{} ({}; {}; {})",
CRATE_NAME, CRATE_VERSION, RUSTC_VERSION, OS, ARCH
)
);
}

#[test]
fn test_with_application_id() {
let options = TelemetryOptions::new(Some("test".to_string()));
let policy = TelemetryPolicy::new(options);
assert_eq!(
policy.header,
format!(
"test azsdk-rust-{}/{} ({}; {}; {})",
CRATE_NAME, CRATE_VERSION, RUSTC_VERSION, OS, ARCH
)
);
}

#[test]
fn test_missing_env() {
let policy = TelemetryPolicy::with_environment::<EmptyEnv>(TelemetryOptions::default());
assert_eq!(
policy.header,
format!("azsdk-rust-unknown/unknown (unknown; {}; {})", OS, ARCH)
)
}
}

0 comments on commit 4075f13

Please sign in to comment.