-
Notifications
You must be signed in to change notification settings - Fork 1
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
Add the ability to provide SDML Catalog path via an environment variable #18
Open
mchaloupka
wants to merge
3
commits into
sdm-lang:main
Choose a base branch
from
mchaloupka:feature/catalog-from-env
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ use sdml_errors::{Error, FileId}; | |
use search_path::SearchPath; | ||
use serde::{Deserialize, Serialize}; | ||
use std::collections::HashMap; | ||
use std::env; | ||
use std::fs::File; | ||
use std::io::Read; | ||
use std::path::{Path, PathBuf}; | ||
|
@@ -51,6 +52,9 @@ pub const SDML_FILE_EXTENSION_LONG: &str = "sdml"; | |
/// The name used for resolver catalog files. | ||
pub const SDML_CATALOG_FILE_NAME: &str = "sdml-catalog.json"; | ||
|
||
/// The environment variable used to override resolver catalog file location. | ||
pub const SDML_CATALOG_FILE_VARIABLE: &str = "SDML_CATALOG"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would make this |
||
|
||
/// | ||
/// The loader is used to manage the process of creating an in-memory model from file-system resources. | ||
/// | ||
|
@@ -149,8 +153,20 @@ impl Default for FsModuleResolver { | |
// 2. Add the current directory to the search path | ||
search_path.prepend_cwd(); | ||
|
||
// 3. Load any catalog file found in the search path | ||
let catalog = ModuleCatalog::load_from_current(true); | ||
// 3. Load catalog file | ||
let catalog = match env::var(SDML_CATALOG_FILE_VARIABLE) { | ||
// If the environment variable is provided, load it from the location provided | ||
Ok(catalog_file) => { | ||
let catalog_file_path = PathBuf::from(catalog_file); | ||
let module_catalog = ModuleCatalog::load_from_file(catalog_file_path.as_path()); | ||
if module_catalog.is_none() { | ||
error!("The path to module catalog was provided through environment variable, yet it failed to load."); | ||
} | ||
module_catalog | ||
}, | ||
// If the environment variable is not provided, load it from the current directory (or any parent directory) | ||
_ => ModuleCatalog::load_from_current(true), | ||
}; | ||
|
||
let _self = Self { | ||
catalog, | ||
|
@@ -435,6 +451,7 @@ impl ModuleCatalog { | |
/// exist. | ||
/// | ||
fn load_from_file(file: &Path) -> Option<Self> { | ||
trace!("ModuleCatalog::load_from_file({file:?})"); | ||
match std::fs::read_to_string(file) { | ||
Ok(source) => match serde_json::from_str::<ModuleCatalog>(&source) { | ||
Ok(mut catalog) => { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"base": "https://examples.sdml.io/", | ||
"entries": { | ||
"campaign": { | ||
"item": { | ||
"relative_url": "campaign#", | ||
"relative_path": "../examples/entity_empty.sdm" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
use sdml_core::{load::ModuleLoader, model::identifiers::Identifier, store::ModuleStore}; | ||
use sdml_parse::load::SDML_CATALOG_FILE_VARIABLE; | ||
use serial_test::serial; | ||
use std::str::FromStr; | ||
use url::Url; | ||
|
||
const MANIFEST_PATH: &str = env!("CARGO_MANIFEST_DIR"); | ||
const TEST_PATH: &str = "tests/catalog_examples"; | ||
|
||
const CATALOG_FILE: &str = "custom-catalog.json"; | ||
const MODULE_NAME: &str = "campaign"; | ||
|
||
fn set_env_variable(env_key: &str, env_value: Option<String>) { | ||
match env_value { | ||
Some(v) => std::env::set_var(env_key, v), | ||
None => std::env::remove_var(env_key), | ||
} | ||
} | ||
|
||
fn with_env_variable<F>(env_key: &str, env_value: Option<&str>, test: F) | ||
where | ||
F : FnOnce() + std::panic::UnwindSafe, | ||
{ | ||
// Set the environment variable | ||
let old_value = std::env::var(env_key).ok(); | ||
|
||
set_env_variable(env_key, env_value.map(|x| { String::from(x)})); | ||
|
||
// Run the test, catching any panic | ||
let result = std::panic::catch_unwind(|| { | ||
test(); | ||
}); | ||
|
||
// Clean-up / restore environment variable | ||
set_env_variable(env_key, old_value); | ||
|
||
// Propagate the panic if it occurred | ||
if let Err(err) = result { | ||
std::panic::resume_unwind(err); | ||
} | ||
} | ||
|
||
#[test] | ||
#[serial] | ||
fn test_load_without_catalogue() { | ||
with_env_variable(SDML_CATALOG_FILE_VARIABLE, None, || { | ||
let mut cache = ::sdml_core::store::InMemoryModuleCache::default().with_stdlib(); | ||
let mut loader = ::sdml_parse::load::FsModuleLoader::default(); | ||
let module_name = Identifier::from_str(MODULE_NAME).unwrap(); | ||
|
||
loader.load( | ||
&module_name, | ||
loader.get_file_id(&module_name), | ||
&mut cache, | ||
true, | ||
).expect_err("Error: Should have failed to load the module."); | ||
}); | ||
} | ||
|
||
#[test] | ||
#[serial] | ||
fn test_load_with_catalogue() { | ||
let catalog_path = ::std::path::PathBuf::from( | ||
format!( | ||
"{}/{}/{}", | ||
MANIFEST_PATH, | ||
TEST_PATH, | ||
CATALOG_FILE, | ||
)); | ||
|
||
with_env_variable(SDML_CATALOG_FILE_VARIABLE, catalog_path.to_str(), || { | ||
let mut cache = ::sdml_core::store::InMemoryModuleCache::default().with_stdlib(); | ||
let mut loader = ::sdml_parse::load::FsModuleLoader::default(); | ||
let module_name = Identifier::from_str(MODULE_NAME).unwrap(); | ||
|
||
loader.load( | ||
&module_name, | ||
loader.get_file_id(&module_name), | ||
&mut cache, | ||
true, | ||
).expect("Error: Should have been able to load the module."); | ||
|
||
let module = cache | ||
.get(&module_name) | ||
.expect("Error: Module not found in cache."); | ||
|
||
let url = Url::from_str("https://examples.sdml.io/campaign#").ok(); | ||
|
||
assert_eq!(module.base_uri().map(|x| { x.value().clone() }), url); | ||
}); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move this down to dev-dependencies