From 386f34af802a812c2af8ece2cc427cfb5a7c1fe8 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sun, 1 Mar 2020 17:26:59 +0000 Subject: Implement `missing` and `env:VAR` imports --- dhall/build.rs | 13 ++----------- dhall/src/error/mod.rs | 2 ++ dhall/src/semantics/resolve/resolve.rs | 24 ++++++++++++++++-------- dhall/src/tests.rs | 2 ++ dhall/tests/import/failure/missing.txt | 1 + dhall/tests/import/failure/unit/EnvUnset.txt | 1 + 6 files changed, 24 insertions(+), 19 deletions(-) create mode 100644 dhall/tests/import/failure/missing.txt create mode 100644 dhall/tests/import/failure/unit/EnvUnset.txt (limited to 'dhall') diff --git a/dhall/build.rs b/dhall/build.rs index 6165be2..0ff5acc 100644 --- a/dhall/build.rs +++ b/dhall/build.rs @@ -258,18 +258,11 @@ fn generate_tests() -> std::io::Result<()> { || path == "hashFromCache" || path == "headerForwarding" || path == "noHeaderForwarding" - || path == "unit/asLocation/Remote" - || path == "unit/AlternativeChain1" - || path == "unit/AlternativeChain2" - || path == "unit/AlternativeEnv" || path == "unit/AlternativeHashMismatch" - || path == "unit/AlternativeImportError" - || path == "unit/AlternativeNoError3" - || path == "unit/AlternativeSubExpr" || path == "unit/AsText" - || path == "unit/EnvSet" || path == "unit/EnvSetAsText" || path == "unit/SimpleRemote" + || path == "unit/asLocation/Remote" }), input_type: FileType::Text, output_type: Some(FileType::Text), @@ -283,11 +276,9 @@ fn generate_tests() -> std::io::Result<()> { false || path == "alternativeEnv" || path == "alternativeEnvMissing" + || path == "customHeadersUsingBoundVariable" || path == "hashMismatch" - || path == "missing" || path == "referentiallyInsane" - || path == "customHeadersUsingBoundVariable" - || path == "unit/EnvUnset" || path == "unit/EnvUnsetAsText" }), input_type: FileType::Text, diff --git a/dhall/src/error/mod.rs b/dhall/src/error/mod.rs index 8829d47..5632ea6 100644 --- a/dhall/src/error/mod.rs +++ b/dhall/src/error/mod.rs @@ -26,6 +26,8 @@ pub(crate) enum ErrorKind { #[derive(Debug)] pub(crate) enum ImportError { + Missing, + MissingEnvVar, UnexpectedImport(Import<()>), ImportCycle(ImportStack, Import<()>), } diff --git a/dhall/src/semantics/resolve/resolve.rs b/dhall/src/semantics/resolve/resolve.rs index b27dd2c..bf7aabb 100644 --- a/dhall/src/semantics/resolve/resolve.rs +++ b/dhall/src/semantics/resolve/resolve.rs @@ -1,6 +1,7 @@ use itertools::Itertools; use std::borrow::Cow; -use std::path::{Path, PathBuf}; +use std::env; +use std::path::PathBuf; use crate::error::ErrorBuilder; use crate::error::{Error, ImportError}; @@ -47,8 +48,21 @@ fn resolve_one_import( FilePrefix::Here => cwd.join(path_buf), _ => unimplemented!("{:?}", import), }; - Ok(load_import(env, &path_buf)?) + + let parsed = Parsed::parse_file(&path_buf)?; + let typed = resolve_with_env(env, parsed)?.typecheck()?; + Ok((typed.normalize().to_hir(), typed.ty().clone())) + } + ImportLocation::Env(var_name) => { + let val = match env::var(var_name) { + Ok(val) => val, + Err(_) => Err(ImportError::MissingEnvVar)?, + }; + let parsed = Parsed::parse_str(&val)?; + let typed = resolve_with_env(env, parsed)?.typecheck()?; + Ok((typed.normalize().to_hir(), typed.ty().clone())) } + ImportLocation::Missing => Err(ImportError::Missing.into()), _ => unimplemented!("{:?}", import), } } @@ -122,12 +136,6 @@ fn resolve_one_import( } } -fn load_import(env: &mut ImportEnv, f: &Path) -> Result { - let parsed = Parsed::parse_file(f)?; - let typed = resolve_with_env(env, parsed)?.typecheck()?; - Ok((typed.normalize().to_hir(), typed.ty().clone())) -} - /// Desugar the first level of the expression. fn desugar(expr: &Expr) -> Cow<'_, Expr> { match expr.kind() { diff --git a/dhall/src/tests.rs b/dhall/src/tests.rs index 044b185..369c6cc 100644 --- a/dhall/src/tests.rs +++ b/dhall/src/tests.rs @@ -247,6 +247,8 @@ fn run_test(test: Test) -> Result<()> { env::set_current_dir( PathBuf::from(env!("CARGO_MANIFEST_DIR")).parent().unwrap(), )?; + // Set environment variable for import tests. + env::set_var("DHALL_TEST_VAR", "6 * 7"); match test { ParserSuccess(expr, expected) => { diff --git a/dhall/tests/import/failure/missing.txt b/dhall/tests/import/failure/missing.txt new file mode 100644 index 0000000..4666330 --- /dev/null +++ b/dhall/tests/import/failure/missing.txt @@ -0,0 +1 @@ +Missing diff --git a/dhall/tests/import/failure/unit/EnvUnset.txt b/dhall/tests/import/failure/unit/EnvUnset.txt new file mode 100644 index 0000000..482b68c --- /dev/null +++ b/dhall/tests/import/failure/unit/EnvUnset.txt @@ -0,0 +1 @@ +MissingEnvVar -- cgit v1.2.3