continuous-testing 0.0.1

A continue testing library
pub mod testing {
    use std::fs;
    use std::fs::File;
    use std::io::Write;
    use std::ops::Add;
    use std::path::Path;
    use std::process::Command;
    use std::thread::sleep;
    use std::time::Duration;

    pub const VAGRANTFILE: &str = "\
    Vagrant.configure(\"2\") do |config|\
        config.vm.box = {}\
        config.vm.disk :disk, size: \"100GB\", primary: true
        config.vm.provider \"virtualbox\" do |vb|
   	        vb.gui = false
  	        vb.memory = {}
	        vb.cpus =  {}
        end
        config.vm.provision \"shell\", path: \"bootstrap.sh\"\
    end";

    pub struct Again {}

    impl Again {

        ///
        ///
        ///
        ///
        pub fn on(directory: &str, os: &str, memory: &str, cpus: &str, command: &str) -> Result<String, String>
        {
            if String::from(directory).is_empty() {
                return Err(String::from("Missing directory name"));
            }

            if String::from(os).is_empty() {
                return Err(String::from("Missing os url"));
            }

            if String::from(memory).is_empty() {
                return Err(String::from("Missing memory size"));
            }

            if String::from(cpus).is_empty() {
                return Err(String::from("Missing cpus size"));
            }

            if String::from(command).is_empty() {
                return Err(String::from("Missing commands"));
            }

            Command::new("clear").spawn().expect("linux");
            sleep(Duration::from_secs(1));
            println!("\nDirectory : {}\nOperating system : {}\nMemory used : {}\nCpus used : {}\nCommand to run : {}\n", directory, os, memory, cpus, command);

            println!("{}", "Waiting...Press CTRL-C to cancel");
            sleep(Duration::from_secs(10));
            Command::new("clear").spawn().expect("linux");

            println!("[  OK  ] Starting configuration creation");


            if Path::new(directory).is_dir() {
                fs::remove_dir_all(directory).expect("failed to remove the directory");
                fs::create_dir(directory).expect("Failed to create the directory");
            } else {
                fs::create_dir(directory).expect("Failed to create the directory");
            }
            println!("[  OK  ] Directory has been created successfully");

            let binding = String::from(directory).add("/").add("Vagrantfile");

            let mut f = File::create(binding.as_str()).expect("");
            f.write_all(format!("Vagrant.configure(\"2\") do |config|\n\tconfig.vm.box = \"{}\"\n\tconfig.vm.disk :disk, size: \"100GB\", primary: true\n\n\tconfig.vm.provider \"virtualbox\" do |vb|\n\t\tvb.gui = false\n\t\tvb.memory = \"{}\"\n\t\tvb.cpus =  {}\n\tend\n\n\tconfig.vm.provision \"shell\", path: \"bootstrap.sh\"\nend", os, memory, cpus).as_bytes()).expect("failed to create vagrant fie content");
            println!("[  OK  ] Vagrantfile has been created successfully");

            let bootstrap = String::from(directory).add("/").add("bootstrap.sh");
            let boot = bootstrap.as_str();

            File::create(boot).expect("");
            let mut b = File::options().append(true).open(boot).expect("");
            writeln!(&mut b, "#!/bin/bash").expect("");
            writeln!(&mut b, "{}", command).expect("");

            println!("[  OK  ] bootstrap.sh has been created successfully");


            Command::new("chmod").arg("+x").arg(boot).spawn().expect("").wait().expect("");

            println!("[  OK  ] bootstrap.sh is now executable");

            println!("[  OK  ] Configuration ready to use");

            println!("[  OK  ] Running the virtual machine");
            Command::new("vagrant").arg("up").arg("--no-color").current_dir(directory).spawn().expect("").wait().expect("");
            println!("[  OK  ] Removing temporary files");
            Command::new("vagrant").arg("destroy").arg("-f").current_dir(directory).spawn().expect("").wait().expect("");
            fs::remove_dir_all(directory).expect("a");
            println!("[  OK  ] Temporary files removed successfully");
            println!("Bye {}", env!("USER"));
            Ok(String::from("success"))
        }
    }
}


#[cfg(test)]
mod tests {
    #[test]
    fn it_works() {}
}