Skip to content

Commit 22f9f98

Browse files
authored
Merge pull request #2568 from itowlson/override-data-dir
Override local data directory via env variable
2 parents 5b4879d + f420988 commit 22f9f98

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
@@ -941,7 +941,7 @@ route = "/..."
941941
"--yes",
942942
])
943943
// Ensure that spin installs the plugins into the temporary directory
944-
.env("TEST_PLUGINS_DIRECTORY", "./plugins");
944+
.env("SPIN_DATA_DIR", "./plugins");
945945
env.run_in(&mut install)?;
946946

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

971971
let mut execute = std::process::Command::new(spin_binary());
972-
execute
973-
.args(["example"])
974-
.env("TEST_PLUGINS_DIRECTORY", "./plugins");
972+
execute.args(["example"]).env("SPIN_DATA_DIR", "./plugins");
975973
let output = env.run_in(&mut execute)?;
976974

977975
// Verify plugin successfully wrote to output file
@@ -995,12 +993,11 @@ route = "/..."
995993
"example-plugin-manifest.json",
996994
"--yes",
997995
])
998-
.env("TEST_PLUGINS_DIRECTORY", "./plugins");
996+
.env("SPIN_DATA_DIR", "./plugins");
999997
env.run_in(&mut upgrade)?;
1000998

1001999
// Check plugin version
10021000
let installed_manifest = std::path::PathBuf::from("plugins")
1003-
.join("spin")
10041001
.join("plugins")
10051002
.join("manifests")
10061003
.join("example.json");
@@ -1020,7 +1017,7 @@ route = "/..."
10201017
login
10211018
.args(["login", "--help"])
10221019
// Ensure that spin installs the plugins into the temporary directory
1023-
.env("TEST_PLUGINS_DIRECTORY", "./plugins");
1020+
.env("SPIN_DATA_DIR", "./plugins");
10241021
let output = env.run_in(&mut login)?;
10251022

10261023
// Verify plugin successfully wrote to output file
@@ -1413,7 +1410,7 @@ route = "/..."
14131410

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

14181415
let plugin_dir = plugins_dir.join("trigger-timer");
14191416
fs::create_dir_all(&plugin_dir)?;
@@ -1440,7 +1437,7 @@ route = "/..."
14401437
&format!("{TIMER_TRIGGER_INTEGRATION_TEST}/spin.toml"),
14411438
"--test",
14421439
])
1443-
.env("TEST_PLUGINS_DIRECTORY", plugin_store_dir)
1440+
.env("SPIN_DATA_DIR", plugin_store_dir)
14441441
.output()?;
14451442
assert!(
14461443
out.status.success(),

0 commit comments

Comments
 (0)