Skip to content

Commit

Permalink
[fs] test init
Browse files Browse the repository at this point in the history
Signed-off-by: Colton J. McCurdy <[email protected]>
  • Loading branch information
mccurdyc committed Jun 11, 2023
1 parent 9ea0c0a commit 6b47083
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 7 deletions.
66 changes: 60 additions & 6 deletions src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use std::collections::HashMap;
use std::{env, fs, path::Path, path::PathBuf};
use walkdir::WalkDir;

const ENV_GITRS_ROOT: &str = "GITRS_ROOT";
const GITRS_ROOT_DEFAULT: &str = "src";

pub fn sync(root: PathBuf, repos: &HashMap<String, repo::Repo>, _clean_only: &bool) -> Result<()> {
Expand Down Expand Up @@ -104,17 +103,72 @@ pub fn init(p: Option<PathBuf>) -> Result<PathBuf> {
Ok(r.to_path_buf())
}

// TODO write tests for this function
fn root(p: Option<PathBuf>) -> PathBuf {
if let Some(r) = p {
return r;
}

if let Ok(v) = env::var(ENV_GITRS_ROOT) {
return PathBuf::from(v);
}

// defaults to $HOME/src
let h = home::home_dir().expect("couldn't get user's HOME directory");
return h.join(PathBuf::from(GITRS_ROOT_DEFAULT));
}

#[cfg(test)]
mod tests {
use super::*;
use tempfile::{tempdir, TempDir};
extern crate log;
use env_logger;

fn setup() -> TempDir {
// https://github.com/rust-cli/env_logger/blob/19e92ece73472ca3a0269c61c4f44399c6ea2366/examples/in_tests.rs#L21
let _ = env_logger::builder()
// Include all events in tests
.filter_level(log::LevelFilter::max())
// Ensure events are captured by `cargo test`
.is_test(true)
// Ignore errors initializing the logger if tests race to configure it
.try_init();

tempdir().expect("Failed to create tempdir")
}

fn cleanup(root: TempDir) {
// By closing the `TempDir` explicitly, we can check that it has
// been deleted successfully. If we don't close it explicitly,
// the directory will still be deleted when `dir` goes out
// of scope, but we won't know whether deleting the directory
// succeeded.
root.close().expect("Failed to close tempdir");
}

#[test]
fn test_init_from_input() {
let root = setup();
let p = root.path().to_path_buf();

init(Some(p.clone())).expect("init failed");
assert_eq!(p.exists(), true);

cleanup(root);
}

#[test]
fn test_init_from_default() {
let root = setup();
let old_home = env::var("HOME").expect("failed to get old home");
env::set_var("HOME", root.path().as_os_str());

let want = home::home_dir()
.expect("couldn't get user's HOME directory")
.join("src");

let got = init(None).expect("init failed");

assert_eq!(want.exists(), true);
assert_eq!(got, want);

env::set_var("HOME", old_home);
cleanup(root);
}
}
7 changes: 6 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use anyhow::{Context, Error};
use clap::{Parser, Subcommand};
extern crate log;
use env_logger;
use std::env;
use std::path::PathBuf;

pub mod config;
Expand Down Expand Up @@ -49,7 +50,11 @@ fn main() -> anyhow::Result<(), Error> {
run(cli)
}

fn run(c: Cli) -> anyhow::Result<(), Error> {
fn run(mut c: Cli) -> anyhow::Result<(), Error> {
if let Ok(root) = env::var("GITRS_ROOT") {
c.root = Some(PathBuf::from(root));
}

let r = fs::init(c.root).expect("failed to initialize root");
let mut cfg = config::Config::new(r, PathBuf::from(".gitrs.yaml"))?;

Expand Down

0 comments on commit 6b47083

Please sign in to comment.