Skip to content

Commit

Permalink
Changes telemetry config name, adds telemetry version to log files, a…
Browse files Browse the repository at this point in the history
…dds more telemetry tests.
  • Loading branch information
peschkaj committed Apr 18, 2016
1 parent bac8baf commit e754a35
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 18 deletions.
5 changes: 3 additions & 2 deletions src/multirust-cli/rustup_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ pub fn cli() -> App<'static, 'static> {
.subcommand(SubCommand::with_name("telemetry")
.about("Enable or disable rust telemetry")
.arg(Arg::with_name("enabled")
.hidden(true)
.takes_value(true)
.value_name("telemetry")
.help("Set telemetry 'on' or 'off'")
Expand Down Expand Up @@ -375,7 +376,7 @@ fn doc(cfg: &Cfg, m: &ArgMatches) -> Result<()> {
"index.html"
};

cfg.open_docs_for_dir(&try!(utils::current_dir()), doc_url)
cfg.open_docs_for_dir(&try!(utils::current_dir()), doc_url)
}

fn self_uninstall(m: &ArgMatches) -> Result<()> {
Expand All @@ -391,4 +392,4 @@ fn telemetry(cfg: &Cfg, m: &ArgMatches) -> Result<()> {
"off" => cfg.set_telemetry(false),
_ => Err(Error::Custom { id: "Telemetry".to_string(), desc: "Incorrect telemetry setting".to_string() }),
}
}
}
6 changes: 3 additions & 3 deletions src/multirust/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -425,15 +425,15 @@ impl Cfg {

try!(utils::write_file("temp", &work_file, ""));

try!(utils::rename_file("telemetry", &*work_file, &self.multirust_dir.join("telemetry_on")));
try!(utils::rename_file("telemetry", &*work_file, &self.multirust_dir.join("telemetry-on")));

self.notify_handler.call(Notification::SetTelemetry("on"));

Ok(())
}

fn disable_telemetry(&self) -> Result<()> {
let _ = utils::remove_file("telemetry_on", &self.multirust_dir.join("telemetry_on"));
let _ = utils::remove_file("telemetry-on", &self.multirust_dir.join("telemetry-on"));

self.notify_handler.call(Notification::SetTelemetry("off"));

Expand All @@ -449,7 +449,7 @@ impl Cfg {

fn find_telemetry(multirust_dir: &PathBuf) -> TelemetryMode {
// default telemetry should be off - if no telemetry file is found, it's off
let telemetry_file = multirust_dir.join("telemetry_on");
let telemetry_file = multirust_dir.join("telemetry-on");

if utils::is_file(telemetry_file) {
return TelemetryMode::On;
Expand Down
14 changes: 11 additions & 3 deletions src/multirust/telemetry.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use time;
use multirust_utils::utils;
use multirust_utils::raw;
use rustc_serialize::json;

use std::collections::HashSet;
use std::path::PathBuf;
use std::fs;

#[derive(Debug, PartialEq)]
pub enum TelemetryMode {
Expand All @@ -22,26 +24,32 @@ pub enum TelemetryEvent {
struct LogMessage {
log_time_s: i64,
event: TelemetryEvent,
version: i32,
}

#[derive(Debug)]
pub struct Telemetry {
telemetry_dir: PathBuf
}

const LOG_FILE_VERSION: i32 = 1;
const MAX_TELEMETRY_FILES: usize = 100;

impl Telemetry {
pub fn new(telemetry_dir: PathBuf) -> Telemetry {
Telemetry { telemetry_dir: telemetry_dir }
}
}

pub fn log_telemetry(&self, event: TelemetryEvent) {
let current_time = time::now_utc();
let ln = LogMessage { log_time_s: current_time.to_timespec().sec, event: event };
let ln = LogMessage { log_time_s: current_time.to_timespec().sec,
event: event,
version: LOG_FILE_VERSION };

let json = json::encode(&ln).unwrap();

let filename = format!("log-{}-{:02}-{:02}.json", current_time.tm_year + 1900, current_time.tm_mon + 1, current_time.tm_mday);

let _ = utils::append_file("telemetry", &self.telemetry_dir.join(&filename), &json);
}
}
}
6 changes: 3 additions & 3 deletions src/multirust/toolchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,9 @@ impl<'a> Toolchain<'a> {
}

pub fn install_from_dist(&self) -> Result<UpdateStatus> {
// if self.cfg.telemetry_enabled() {
// return self.install_from_dist_with_telemetry();
// }
if self.cfg.telemetry_enabled() {
return self.install_from_dist_with_telemetry();
}
self.install_from_dist_inner()
}

Expand Down
16 changes: 9 additions & 7 deletions tests/cli-exact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
extern crate multirust_dist;
extern crate multirust_mock;
extern crate multirust_utils;
extern crate time;

use multirust_mock::clitools::{self, Config, Scenario,
expect_ok, expect_ok_ex,
Expand Down Expand Up @@ -118,15 +120,15 @@ fn list_overrides() {
setup(&|config| {
let cwd = std::fs::canonicalize(env::current_dir().unwrap()).unwrap();
let mut cwd_formatted = format!("{}", cwd.display()).to_string();

if cfg!(windows) {
cwd_formatted = cwd_formatted[4..].to_owned();
}

let trip = this_host_triple();
expect_ok(config, &["rustup", "override", "add", "nightly"]);
expect_ok_ex(config, &["rustup", "override", "list"],
&format!("{:<40}\t{:<20}\n", cwd_formatted, &format!("nightly-{}", trip)), r"");
expect_ok_ex(config, &["rustup", "override", "list"],
&format!("{:<40}\t{:<20}\n", cwd_formatted, &format!("nightly-{}", trip)), r"");
});
}

Expand Down Expand Up @@ -200,7 +202,7 @@ info: installing component 'rust-std' for '{0}'
#[test]
fn enable_telemetry() {
setup(&|config| {
expect_ok_ex(config,
expect_ok_ex(config,
&["rustup", "telemetry", "on"],
r"",
&format!("info: telemetry set to 'on'\n"));
Expand All @@ -210,9 +212,9 @@ fn enable_telemetry() {
#[test]
fn disable_telemetry() {
setup(&|config| {
expect_ok_ex(config,
expect_ok_ex(config,
&["rustup", "telemetry", "off"],
r"",
&format!("info: telemetry set to 'off'\n"));
});
}
}
20 changes: 20 additions & 0 deletions tests/cli-misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
extern crate multirust_dist;
extern crate multirust_utils;
extern crate multirust_mock;
extern crate time;

use multirust_mock::clitools::{self, Config, Scenario,
expect_stdout_ok, expect_stderr_ok,
expect_ok, expect_err, run,
this_host_triple};
use multirust_utils::utils;

macro_rules! for_host { ($s: expr) => (&format!($s, this_host_triple())) }

Expand Down Expand Up @@ -757,3 +759,21 @@ fn proxies_pass_empty_args() {
expect_ok(config, &["rustup", "run", "nightly", "rustc", "--empty-arg-test", ""]);
});
}

#[test]
fn enabling_telemetry_and_compiling_creates_log() {
setup(&|config| {
expect_ok(config, &["rustup", "default", "stable"]);
expect_ok(config, &["rustup", "telemetry", "on"]);
expect_ok(config, &["rustc", "--version"]);

let telemetry_dir = config.rustupdir.join("telemetry");
let _ = utils::assert_is_directory(telemetry_dir.as_path());

let out = telemetry_dir.read_dir();
assert!(out.is_ok());

let contents = out.unwrap();
assert!(contents.count() > 0);
});
}

0 comments on commit e754a35

Please sign in to comment.