Skip to content

Commit f420988

Browse files
committed
Override local data directory via env variable
Signed-off-by: itowlson <[email protected]>
1 parent 13a133f commit f420988

File tree

4 files changed

+15
-20
lines changed

4 files changed

+15
-20
lines changed

crates/common/src/data_dir.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ use anyhow::{anyhow, Result};
44
use std::path::{Path, PathBuf};
55

66
/// Return the default data directory for Spin
7-
pub fn default_data_dir() -> Result<PathBuf> {
7+
pub fn data_dir() -> Result<PathBuf> {
8+
if let Ok(data_dir) = std::env::var("SPIN_DATA_DIR") {
9+
return Ok(PathBuf::from(data_dir));
10+
}
811
if let Some(pkg_mgr_dir) = package_manager_data_dir() {
912
return Ok(pkg_mgr_dir);
1013
}

crates/plugins/src/store.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use anyhow::{Context, Result};
22
use flate2::read::GzDecoder;
3-
use spin_common::data_dir::default_data_dir;
3+
use spin_common::data_dir::data_dir;
44
use std::{
55
ffi::OsStr,
66
fs::{self, File},
@@ -25,12 +25,7 @@ impl PluginStore {
2525
}
2626

2727
pub fn try_default() -> Result<Self> {
28-
let data_dir = if let Ok(test_dir) = std::env::var("TEST_PLUGINS_DIRECTORY") {
29-
PathBuf::from(test_dir).join("spin")
30-
} else {
31-
default_data_dir()?
32-
};
33-
Ok(Self::new(data_dir.join("plugins")))
28+
Ok(Self::new(data_dir()?.join("plugins")))
3429
}
3530

3631
/// Gets the path to where Spin plugin are installed.

crates/templates/src/store.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use anyhow::Context;
2-
use spin_common::data_dir::default_data_dir;
2+
use spin_common::data_dir::data_dir;
33
use std::path::{Path, PathBuf};
44

55
use crate::directory::subdirectories;
@@ -20,7 +20,7 @@ impl TemplateStore {
2020
}
2121

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

2626
pub(crate) fn get_directory(&self, id: impl AsRef<str>) -> PathBuf {

tests/integration.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -903,7 +903,7 @@ route = "/..."
903903
"--yes",
904904
])
905905
// Ensure that spin installs the plugins into the temporary directory
906-
.env("TEST_PLUGINS_DIRECTORY", "./plugins");
906+
.env("SPIN_DATA_DIR", "./plugins");
907907
env.run_in(&mut install)?;
908908

909909
/// Make sure that the plugin is uninstalled after the test
@@ -927,13 +927,11 @@ route = "/..."
927927
"--yes",
928928
])
929929
// Ensure that spin installs the plugins into the temporary directory
930-
.env("TEST_PLUGINS_DIRECTORY", "./plugins");
930+
.env("SPIN_DATA_DIR", "./plugins");
931931
env.run_in(&mut install)?;
932932

933933
let mut execute = std::process::Command::new(spin_binary());
934-
execute
935-
.args(["example"])
936-
.env("TEST_PLUGINS_DIRECTORY", "./plugins");
934+
execute.args(["example"]).env("SPIN_DATA_DIR", "./plugins");
937935
let output = env.run_in(&mut execute)?;
938936

939937
// Verify plugin successfully wrote to output file
@@ -957,12 +955,11 @@ route = "/..."
957955
"example-plugin-manifest.json",
958956
"--yes",
959957
])
960-
.env("TEST_PLUGINS_DIRECTORY", "./plugins");
958+
.env("SPIN_DATA_DIR", "./plugins");
961959
env.run_in(&mut upgrade)?;
962960

963961
// Check plugin version
964962
let installed_manifest = std::path::PathBuf::from("plugins")
965-
.join("spin")
966963
.join("plugins")
967964
.join("manifests")
968965
.join("example.json");
@@ -984,7 +981,7 @@ route = "/..."
984981
login
985982
.args(["login", "--help"])
986983
// Ensure that spin installs the plugins into the temporary directory
987-
.env("TEST_PLUGINS_DIRECTORY", "./plugins");
984+
.env("SPIN_DATA_DIR", "./plugins");
988985
let output = env.run_in(&mut login)?;
989986

990987
// Verify plugin successfully wrote to output file
@@ -1361,7 +1358,7 @@ route = "/..."
13611358

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

13661363
let plugin_dir = plugins_dir.join("trigger-timer");
13671364
fs::create_dir_all(&plugin_dir)?;
@@ -1388,7 +1385,7 @@ route = "/..."
13881385
&format!("{TIMER_TRIGGER_INTEGRATION_TEST}/spin.toml"),
13891386
"--test",
13901387
])
1391-
.env("TEST_PLUGINS_DIRECTORY", plugin_store_dir)
1388+
.env("SPIN_DATA_DIR", plugin_store_dir)
13921389
.output()?;
13931390
assert!(
13941391
out.status.success(),

0 commit comments

Comments
 (0)