Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Override local data directory via env variable #2568

Merged
merged 1 commit into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -903,7 +903,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 @@ -927,13 +927,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 @@ -957,12 +955,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 @@ -984,7 +981,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 @@ -1361,7 +1358,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 @@ -1388,7 +1385,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
Loading