Skip to content

Commit

Permalink
Add test for static fs not creating directory
Browse files Browse the repository at this point in the history
Signed-off-by: itowlson <[email protected]>
  • Loading branch information
itowlson committed Jul 20, 2023
1 parent dcff90f commit f4da5ac
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 1 deletion.
3 changes: 3 additions & 0 deletions crates/templates/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,6 @@ pub use manager::*;
pub use run::{Run, RunOptions};
pub use source::TemplateSource;
pub use template::{Template, TemplateVariantInfo};

#[cfg(test)]
mod test_built_ins;
6 changes: 5 additions & 1 deletion crates/templates/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,11 @@ impl TemplateManager {
/// Creates a `TemplateManager` for the default install location.
pub fn try_default() -> anyhow::Result<Self> {
let store = TemplateStore::try_default()?;
Ok(Self { store })
Ok(Self::new(store))
}

pub(crate) fn new(store: TemplateStore) -> Self {
Self { store }
}

/// Installs templates from the specified source.
Expand Down
73 changes: 73 additions & 0 deletions crates/templates/src/test_built_ins/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#![cfg(test)]

// Module for unit-testing the built-in templates when a full e2e test would be overkill.
// If your test involves invoking the Spin CLI, or builds or runs an application, use
// an e2e test.

use std::{collections::HashMap, path::PathBuf};

use super::*;

struct DiscardingReporter;

impl ProgressReporter for DiscardingReporter {
fn report(&self, _: impl AsRef<str>) {}
}

#[tokio::test]
async fn add_fileserver_does_not_create_dir() -> anyhow::Result<()> {
let built_ins_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../..");
let built_ins_src = TemplateSource::File(built_ins_dir);

let store_dir = tempfile::tempdir()?;
let store = store::TemplateStore::new(store_dir.path());
let manager = TemplateManager::new(store);

manager
.install(
&built_ins_src,
&InstallOptions::default(),
&DiscardingReporter,
)
.await?;

let app_dir = tempfile::tempdir()?;

// Create an app to add the fileserver into
let new_empty_options = RunOptions {
variant: TemplateVariantInfo::NewApplication,
name: "add-fs-dir-test".to_owned(),
output_path: app_dir.path().to_owned(),
values: HashMap::new(),
accept_defaults: true,
};
manager
.get("http-empty")?
.expect("http-empty template should exist")
.run(new_empty_options)
.silent()
.await?;

// Add the fileserver to that app
let manifest_path = app_dir.path().join("spin.toml");
let add_fs_options = RunOptions {
variant: TemplateVariantInfo::AddComponent { manifest_path },
name: "fs".to_owned(),
output_path: app_dir.path().join("fs"),
values: HashMap::new(),
accept_defaults: true,
};
manager
.get("static-fileserver")?
.expect("static-fileserver template should exist")
.run(add_fs_options)
.silent()
.await?;

// Finally!
assert!(
!app_dir.path().join("fs").exists(),
"<app_dir>/fs should not have been created"
);
Ok(())
}

0 comments on commit f4da5ac

Please sign in to comment.