Skip to content

Commit

Permalink
Merge pull request #2568 from itowlson/override-data-dir
Browse files Browse the repository at this point in the history
Override local data directory via env variable
  • Loading branch information
itowlson authored Jun 17, 2024
2 parents 5b4879d + f420988 commit 22f9f98
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 20 deletions.
5 changes: 4 additions & 1 deletion crates/common/src/data_dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ use anyhow::{anyhow, Result};
use std::path::{Path, PathBuf};

/// Return the default data directory for Spin
pub fn default_data_dir() -> Result<PathBuf> {
pub fn data_dir() -> Result<PathBuf> {
if let Ok(data_dir) = std::env::var("SPIN_DATA_DIR") {
return Ok(PathBuf::from(data_dir));
}
if let Some(pkg_mgr_dir) = package_manager_data_dir() {
return Ok(pkg_mgr_dir);
}
Expand Down
9 changes: 2 additions & 7 deletions crates/plugins/src/store.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anyhow::{Context, Result};
use flate2::read::GzDecoder;
use spin_common::data_dir::default_data_dir;
use spin_common::data_dir::data_dir;
use std::{
ffi::OsStr,
fs::{self, File},
Expand All @@ -25,12 +25,7 @@ impl PluginStore {
}

pub fn try_default() -> Result<Self> {
let data_dir = if let Ok(test_dir) = std::env::var("TEST_PLUGINS_DIRECTORY") {
PathBuf::from(test_dir).join("spin")
} else {
default_data_dir()?
};
Ok(Self::new(data_dir.join("plugins")))
Ok(Self::new(data_dir()?.join("plugins")))
}

/// Gets the path to where Spin plugin are installed.
Expand Down
4 changes: 2 additions & 2 deletions crates/templates/src/store.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use anyhow::Context;
use spin_common::data_dir::default_data_dir;
use spin_common::data_dir::data_dir;
use std::path::{Path, PathBuf};

use crate::directory::subdirectories;
Expand All @@ -20,7 +20,7 @@ impl TemplateStore {
}

pub(crate) fn try_default() -> anyhow::Result<Self> {
Ok(Self::new(default_data_dir()?.join("templates")))
Ok(Self::new(data_dir()?.join("templates")))
}

pub(crate) fn get_directory(&self, id: impl AsRef<str>) -> PathBuf {
Expand Down
17 changes: 7 additions & 10 deletions tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -941,7 +941,7 @@ route = "/..."
"--yes",
])
// Ensure that spin installs the plugins into the temporary directory
.env("TEST_PLUGINS_DIRECTORY", "./plugins");
.env("SPIN_DATA_DIR", "./plugins");
env.run_in(&mut install)?;

/// Make sure that the plugin is uninstalled after the test
Expand All @@ -965,13 +965,11 @@ route = "/..."
"--yes",
])
// Ensure that spin installs the plugins into the temporary directory
.env("TEST_PLUGINS_DIRECTORY", "./plugins");
.env("SPIN_DATA_DIR", "./plugins");
env.run_in(&mut install)?;

let mut execute = std::process::Command::new(spin_binary());
execute
.args(["example"])
.env("TEST_PLUGINS_DIRECTORY", "./plugins");
execute.args(["example"]).env("SPIN_DATA_DIR", "./plugins");
let output = env.run_in(&mut execute)?;

// Verify plugin successfully wrote to output file
Expand All @@ -995,12 +993,11 @@ route = "/..."
"example-plugin-manifest.json",
"--yes",
])
.env("TEST_PLUGINS_DIRECTORY", "./plugins");
.env("SPIN_DATA_DIR", "./plugins");
env.run_in(&mut upgrade)?;

// Check plugin version
let installed_manifest = std::path::PathBuf::from("plugins")
.join("spin")
.join("plugins")
.join("manifests")
.join("example.json");
Expand All @@ -1020,7 +1017,7 @@ route = "/..."
login
.args(["login", "--help"])
// Ensure that spin installs the plugins into the temporary directory
.env("TEST_PLUGINS_DIRECTORY", "./plugins");
.env("SPIN_DATA_DIR", "./plugins");
let output = env.run_in(&mut login)?;

// Verify plugin successfully wrote to output file
Expand Down Expand Up @@ -1413,7 +1410,7 @@ route = "/..."

// Create a test plugin store so we don't modify the user's real one.
let plugin_store_dir = Path::new(concat!(env!("OUT_DIR"), "/plugin-store"));
let plugins_dir = plugin_store_dir.join("spin/plugins");
let plugins_dir = plugin_store_dir.join("plugins");

let plugin_dir = plugins_dir.join("trigger-timer");
fs::create_dir_all(&plugin_dir)?;
Expand All @@ -1440,7 +1437,7 @@ route = "/..."
&format!("{TIMER_TRIGGER_INTEGRATION_TEST}/spin.toml"),
"--test",
])
.env("TEST_PLUGINS_DIRECTORY", plugin_store_dir)
.env("SPIN_DATA_DIR", plugin_store_dir)
.output()?;
assert!(
out.status.success(),
Expand Down

0 comments on commit 22f9f98

Please sign in to comment.