Skip to content

Commit

Permalink
Add support for temporary Home dir, start testing TrustCache
Browse files Browse the repository at this point in the history
  • Loading branch information
LPGhatguy committed May 23, 2022
1 parent a3dd821 commit e5d9911
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 17 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ zip = "0.5.13"
winreg = "0.10.1"

[dev-dependencies]
tempfile = "3.3.0"
serde_json = "1.0.66"
11 changes: 5 additions & 6 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::path::PathBuf;
use anyhow::{bail, Context};
use structopt::StructOpt;

use crate::home::Home;
use crate::manifest::Manifest;
use crate::tool_alias::ToolAlias;
use crate::tool_name::ToolName;
Expand All @@ -18,12 +19,12 @@ pub struct Args {
}

impl Args {
pub fn run(self, tools: ToolStorage) -> anyhow::Result<()> {
pub fn run(self, home: &Home, tools: ToolStorage) -> anyhow::Result<()> {
match self.subcommand {
Subcommand::Init(sub) => sub.run(),
Subcommand::Add(sub) => sub.run(tools),
Subcommand::Install(sub) => sub.run(tools),
Subcommand::Trust(sub) => sub.run(tools),
Subcommand::Trust(sub) => sub.run(home),
Subcommand::SelfInstall(sub) => sub.run(tools),

Subcommand::List(_) => bail!("This command is not yet implemented."),
Expand Down Expand Up @@ -141,10 +142,8 @@ pub struct TrustSubcommand {
}

impl TrustSubcommand {
pub fn run(self, tools: ToolStorage) -> anyhow::Result<()> {
let trusted_path = tools.storage_dir.join("trusted.txt");

if TrustCache::add(&trusted_path, self.name.clone())? {
pub fn run(self, home: &Home) -> anyhow::Result<()> {
if TrustCache::add(home, self.name.clone())? {
log::info!("Added {} to the set of trusted tools.", self.name);
} else {
log::info!("{} was already a trusted tool.", self.name);
Expand Down
23 changes: 20 additions & 3 deletions src/home.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ use anyhow::format_err;
#[derive(Debug, Clone)]
pub struct Home {
path: Arc<Path>,

#[cfg(test)]
#[allow(unused)]
temp: Option<Arc<tempfile::TempDir>>,
}

impl Home {
Expand All @@ -25,13 +29,26 @@ impl Home {
dirs::home_dir().ok_or_else(|| format_err!("Home directory could not be found."))?;

path.push(".aftman");
Ok(Self { path: path.into() })

Ok(Self::from_path(path))
}

#[allow(unused)]
pub fn from_path<P: Into<PathBuf>>(path: P) -> Self {
#[cfg(test)]
pub fn new_temp() -> anyhow::Result<Self> {
let temp = tempfile::TempDir::new()?;

Ok(Self {
path: temp.path().to_path_buf().into(),
temp: Some(Arc::new(temp)),
})
}

fn from_path<P: Into<PathBuf>>(path: P) -> Self {
Self {
path: path.into().into(),

#[cfg(test)]
temp: None,
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ fn run() -> anyhow::Result<()> {

Manifest::init_global(&home)?;

Args::from_args().run(tool_storage)
Args::from_args().run(&home, tool_storage)
}

fn current_exe_name() -> anyhow::Result<String> {
Expand Down
5 changes: 2 additions & 3 deletions src/tool_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,7 @@ impl ToolStorage {
}

fn trust_check(&self, name: &ToolName, mode: TrustMode) -> anyhow::Result<()> {
let trusted_path = self.storage_dir.join("trusted.txt");
let trusted = TrustCache::read(&trusted_path)?;
let trusted = TrustCache::read(&self.home)?;
let is_trusted = trusted.tools.contains(name);

if !is_trusted {
Expand Down Expand Up @@ -315,7 +314,7 @@ impl ToolStorage {
}
}

TrustCache::add(&trusted_path, name.clone())?;
TrustCache::add(&self.home, name.clone())?;
}

Ok(())
Expand Down
35 changes: 31 additions & 4 deletions src/trust.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::collections::BTreeSet;
use std::fmt::Write;
use std::io;
use std::path::Path;

use anyhow::bail;

use crate::home::Home;
use crate::tool_name::ToolName;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
Expand All @@ -19,7 +19,9 @@ pub struct TrustCache {
}

impl TrustCache {
pub fn read(path: &Path) -> anyhow::Result<Self> {
pub fn read(home: &Home) -> anyhow::Result<Self> {
let path = home.path().join("trusted.txt");

let contents = match fs_err::read_to_string(path) {
Ok(v) => v,
Err(err) => {
Expand All @@ -39,15 +41,16 @@ impl TrustCache {
Ok(Self { tools })
}

pub fn add(path: &Path, name: ToolName) -> anyhow::Result<bool> {
let mut cache = Self::read(path)?;
pub fn add(home: &Home, name: ToolName) -> anyhow::Result<bool> {
let mut cache = Self::read(home)?;

if cache.tools.insert(name) {
let mut output = String::new();
for tool in cache.tools {
writeln!(&mut output, "{}", tool).unwrap();
}

let path = home.path().join("trusted.txt");
fs_err::write(path, output)?;

return Ok(true);
Expand All @@ -56,3 +59,27 @@ impl TrustCache {
Ok(false)
}
}

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

#[test]
fn get_and_add() -> anyhow::Result<()> {
let home = Home::new_temp()?;

let cache = TrustCache::read(&home)?;
assert!(cache.tools.is_empty());

let tool_name: ToolName = "foo/bar".parse()?;

let added = TrustCache::add(&home, tool_name.clone())?;
assert!(added);

let cache = TrustCache::read(&home)?;
assert!(cache.tools.len() == 1);
assert!(cache.tools.contains(&tool_name));

Ok(())
}
}

0 comments on commit e5d9911

Please sign in to comment.