extern crate ellipsis; use std::{env,fs}; extern crate serde_json; #[test] fn test_config_command() { // Setup: create a spoof home directory, and make our program // think it is the correct one let fakehome = env::temp_dir().join("rust-fake-home");; assert!(fs::create_dir(&fakehome).is_ok()); env::set_var("HOME", &fakehome); let cfgfile = fakehome.join(".dot.json"); assert!(!cfgfile.exists()); // Run the base program -> should just create a blank config file run_cmd("ellipsis config"); // Make sure we created the file, and that it has the right contents assert!(cfgfile.exists()); assert_eq!(fs::read(&cfgfile).unwrap(), b"{}"); // dummy url for our remote let url = "https://github.com/ndrewtl/dotfiles.git"; run_cmd(&format!("ellipsis config --remote {}", url)); assert!(cfgfile.exists()); let json : serde_json::Value = serde_json::from_reader( fs::File::open(&cfgfile).unwrap() ).unwrap(); assert_eq!(json["remote"], url); // Cleanup // FIXME these cleanup functions don't get run if the test // fails at any point, making cleanup fail // Idea: Implement a struct whose destructor destroys these // files assert!(fs::remove_file(&cfgfile).is_ok()); assert!(fs::remove_dir(&fakehome).is_ok()); } fn run_cmd(cmd : &str) { let matches = ellipsis::app().get_matches_from(cmd.split_whitespace()); assert!(ellipsis::run(&matches).is_ok()); }