Move from source to source-env (#6277)

* start working on source-env

* WIP

* Get most tests working, still one to go

* Fix file-relative paths; Report parser error

* Fix merge conflicts; Restore source as deprecated

* Tests: Use source-env; Remove redundant tests

* Fmt

* Respect hidden env vars

* Fix file-relative eval for source-env

* Add file-relative eval to "overlay use"

* Use FILE_PWD only in source-env and "overlay use"

* Ignore new tests for now

This will be another issue

* Throw an error if setting FILE_PWD manually

* Fix source-related test failures

* Fix nu-check to respect FILE_PWD

* Fix corrupted spans in source-env shell errors

* Fix up some references to old source

* Remove deprecation message

* Re-introduce deleted tests

Co-authored-by: kubouch <kubouch@gmail.com>
This commit is contained in:
JT
2022-09-01 08:32:56 +12:00
committed by GitHub
parent 11531b7630
commit c52d45cb97
33 changed files with 726 additions and 175 deletions

View File

@ -1 +1 @@
alias deactivate-foo = source deactivate-foo.nu
export alias deactivate-foo = overlay hide activate-foo

View File

@ -1 +0,0 @@
hide deactivate-foo

View File

@ -1,3 +1,3 @@
def greet [] {
export def greet [] {
"hello"
}

View File

@ -367,7 +367,7 @@ fn env_change_block_condition_pwd() {
&env_change_hook_code_condition(
"PWD",
r#"{|before, after| ($after | path basename) == samples }"#,
r#"'source .nu-env'"#,
r#"'source-env .nu-env'"#,
),
"cd samples",
"$env.SPAM",

View File

@ -83,35 +83,6 @@ fn module_private_import_decl_not_public() {
})
}
// TODO -- doesn't work because modules are never evaluated
#[ignore]
#[test]
fn module_private_import_env() {
Playground::setup("module_private_import_env", |dirs, sandbox| {
sandbox
.with_files(vec![FileWithContentToBeTrimmed(
"main.nu",
r#"
use spam.nu FOO_HELPER
export def foo [] { $env.FOO_HELPER }
"#,
)])
.with_files(vec![FileWithContentToBeTrimmed(
"spam.nu",
r#"
export env FOO_HELPER { "foo" }
"#,
)]);
let inp = &[r#"use main.nu foo"#, r#"foo"#];
let actual = nu!(cwd: dirs.test(), pipeline(&inp.join("; ")));
assert_eq!(actual.out, "foo");
})
}
#[test]
fn module_public_import_decl() {
Playground::setup("module_public_import_decl", |dirs, sandbox| {
@ -163,33 +134,6 @@ fn module_public_import_alias() {
})
}
// TODO -- doesn't work because modules are never evaluated
#[ignore]
#[test]
fn module_public_import_env() {
Playground::setup("module_public_import_decl", |dirs, sandbox| {
sandbox
.with_files(vec![FileWithContentToBeTrimmed(
"main.nu",
r#"
export use spam.nu FOO
"#,
)])
.with_files(vec![FileWithContentToBeTrimmed(
"spam.nu",
r#"
export env FOO { "foo" }
"#,
)]);
let inp = &[r#"use main.nu FOO"#, r#"$env.FOO"#];
let actual = nu!(cwd: dirs.test(), pipeline(&inp.join("; ")));
assert_eq!(actual.out, "foo");
})
}
#[test]
fn module_nested_imports() {
Playground::setup("module_nested_imports", |dirs, sandbox| {
@ -347,16 +291,50 @@ fn module_nested_imports_in_dirs_prefixed() {
}
#[test]
fn module_eval_export_env() {
Playground::setup("module_eval_export_env", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContentToBeTrimmed(
"spam.nu",
r#"
export-env { let-env FOO = 'foo' }
"#,
)]);
fn module_import_env_1() {
Playground::setup("module_imprt_env_1", |dirs, sandbox| {
sandbox
.with_files(vec![FileWithContentToBeTrimmed(
"main.nu",
r#"
export-env { source-env spam.nu }
let inp = &[r#"source spam.nu"#, r#"$env.FOO"#];
export def foo [] { $env.FOO_HELPER }
"#,
)])
.with_files(vec![FileWithContentToBeTrimmed(
"spam.nu",
r#"
export-env { let-env FOO_HELPER = "foo" }
"#,
)]);
let inp = &[r#"source-env main.nu"#, r#"use main.nu foo"#, r#"foo"#];
let actual = nu!(cwd: dirs.test(), pipeline(&inp.join("; ")));
assert_eq!(actual.out, "foo");
})
}
#[test]
fn module_import_env_2() {
Playground::setup("module_import_env_2", |dirs, sandbox| {
sandbox
.with_files(vec![FileWithContentToBeTrimmed(
"main.nu",
r#"
export-env { source-env spam.nu }
"#,
)])
.with_files(vec![FileWithContentToBeTrimmed(
"spam.nu",
r#"
export-env { let-env FOO = "foo" }
"#,
)]);
let inp = &[r#"source-env main.nu"#, r#"$env.FOO"#];
let actual = nu!(cwd: dirs.test(), pipeline(&inp.join("; ")));

View File

@ -1,3 +1,5 @@
use nu_test_support::fs::Stub::FileWithContentToBeTrimmed;
use nu_test_support::playground::Playground;
use nu_test_support::{nu, nu_repl_code, pipeline};
#[test]
@ -767,3 +769,97 @@ fn overlay_use_export_env_hide() {
assert!(actual.err.contains("did you mean"));
assert!(actual_repl.err.contains("did you mean"));
}
#[test]
fn overlay_use_do_cd() {
Playground::setup("overlay_use_do_cd", |dirs, sandbox| {
sandbox
.mkdir("test1/test2")
.with_files(vec![FileWithContentToBeTrimmed(
"test1/test2/spam.nu",
r#"
export-env { cd test1/test2 }
"#,
)]);
let inp = &[
r#"overlay use test1/test2/spam.nu"#,
r#"$env.PWD | path basename"#,
];
let actual = nu!(cwd: dirs.test(), pipeline(&inp.join("; ")));
assert_eq!(actual.out, "test2");
})
}
#[test]
fn overlay_use_do_cd_file_relative() {
Playground::setup("overlay_use_do_cd_file_relative", |dirs, sandbox| {
sandbox
.mkdir("test1/test2")
.with_files(vec![FileWithContentToBeTrimmed(
"test1/test2/spam.nu",
r#"
export-env { cd ($env.FILE_PWD | path join '..') }
"#,
)]);
let inp = &[
r#"overlay use test1/test2/spam.nu"#,
r#"$env.PWD | path basename"#,
];
let actual = nu!(cwd: dirs.test(), pipeline(&inp.join("; ")));
assert_eq!(actual.out, "test1");
})
}
#[test]
fn overlay_use_dont_cd_overlay() {
Playground::setup("overlay_use_dont_cd_overlay", |dirs, sandbox| {
sandbox
.mkdir("test1/test2")
.with_files(vec![FileWithContentToBeTrimmed(
"test1/test2/spam.nu",
r#"
export-env {
overlay new spam
cd test1/test2
overlay hide spam
}
"#,
)]);
let inp = &[
r#"source-env test1/test2/spam.nu"#,
r#"$env.PWD | path basename"#,
];
let actual = nu!(cwd: dirs.test(), pipeline(&inp.join("; ")));
assert_eq!(actual.out, "overlay_use_dont_cd_overlay");
})
}
#[ignore]
#[test]
fn overlay_use_find_module_scoped() {
Playground::setup("overlay_use_find_module_scoped", |dirs, _| {
let inp = &[r#"
do {
module spam { export def foo [] { 'foo' } }
overlay use spam
foo
}
"#];
let actual = nu!(cwd: dirs.test(), pipeline(&inp.join("; ")));
let actual_repl = nu!(cwd: "tests/overlays", nu_repl_code(inp));
assert_eq!(actual.out, "foo");
assert_eq!(actual_repl.out, "foo");
})
}

View File

@ -47,6 +47,40 @@ fn run_nu_script_multiline_end_pipe_win() {
assert_eq!(actual.out, "3");
}
#[test]
fn parse_file_relative_to_parsed_file_simple() {
Playground::setup("relative_files_simple", |dirs, sandbox| {
sandbox
.mkdir("lol")
.mkdir("lol/lol")
.with_files(vec![FileWithContentToBeTrimmed(
"lol/lol/lol.nu",
r#"
use ../lol_shell.nu
let-env LOL = (lol_shell ls)
"#,
)])
.with_files(vec![FileWithContentToBeTrimmed(
"lol/lol_shell.nu",
r#"
export def ls [] { "lol" }
"#,
)]);
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
source-env lol/lol/lol.nu;
$env.LOL
"#
));
assert_eq!(actual.out, "lol");
})
}
#[ignore]
#[test]
fn parse_file_relative_to_parsed_file() {
Playground::setup("relative_files", |dirs, sandbox| {
@ -56,11 +90,11 @@ fn parse_file_relative_to_parsed_file() {
.with_files(vec![FileWithContentToBeTrimmed(
"lol/lol/lol.nu",
r#"
source ../../foo.nu
source-env ../../foo.nu
use ../lol_shell.nu
overlay use ../../lol/lol_shell.nu
$'($env.FOO) (lol_shell ls) (ls)'
let-env LOL = $'($env.FOO) (lol_shell ls) (ls)'
"#,
)])
.with_files(vec![FileWithContentToBeTrimmed(
@ -79,7 +113,8 @@ fn parse_file_relative_to_parsed_file() {
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
source lol/lol/lol.nu
source-env lol/lol/lol.nu;
$env.LOL
"#
));
@ -95,7 +130,7 @@ fn parse_file_relative_to_parsed_file_dont_use_cwd_1() {
.with_files(vec![FileWithContentToBeTrimmed(
"lol/lol.nu",
r#"
source foo.nu
source-env foo.nu
"#,
)])
.with_files(vec![FileWithContentToBeTrimmed(
@ -114,7 +149,7 @@ fn parse_file_relative_to_parsed_file_dont_use_cwd_1() {
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
source lol/lol.nu;
source-env lol/lol.nu;
$env.FOO
"#
));
@ -131,7 +166,7 @@ fn parse_file_relative_to_parsed_file_dont_use_cwd_2() {
.with_files(vec![FileWithContentToBeTrimmed(
"lol/lol.nu",
r#"
source foo.nu
source-env foo.nu
"#,
)])
.with_files(vec![FileWithContentToBeTrimmed(
@ -144,7 +179,7 @@ fn parse_file_relative_to_parsed_file_dont_use_cwd_2() {
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
source lol/lol.nu
source-env lol/lol.nu
"#
));

View File

@ -77,6 +77,20 @@ fn env_shorthand_multi() {
assert_eq!(actual.out, "barbaz");
}
#[test]
fn let_env_file_pwd_env_var_fails() {
let actual = nu!(cwd: ".", r#"let-env FILE_PWD = 'foo'"#);
assert!(actual.err.contains("automatic_env_var_set_manually"));
}
#[test]
fn load_env_file_pwd_env_var_fails() {
let actual = nu!(cwd: ".", r#"load-env { FILE_PWD : 'foo' }"#);
assert!(actual.err.contains("automatic_env_var_set_manually"));
}
// FIXME: for some reason Nu is attempting to execute foo in `let-env FOO = foo`
#[ignore]
#[test]

View File

@ -1,4 +1,5 @@
mod env;
mod source_env;
// FIXME: nu_env tests depend on autoenv which hasn't been ported yet
// mod nu_env;

View File

@ -0,0 +1,152 @@
use nu_test_support::fs::Stub::FileWithContentToBeTrimmed;
use nu_test_support::playground::Playground;
use nu_test_support::{nu, pipeline};
#[test]
fn source_env_eval_export_env() {
Playground::setup("source_env_eval_export_env", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContentToBeTrimmed(
"spam.nu",
r#"
export-env { let-env FOO = 'foo' }
"#,
)]);
let inp = &[r#"source-env spam.nu"#, r#"$env.FOO"#];
let actual = nu!(cwd: dirs.test(), pipeline(&inp.join("; ")));
assert_eq!(actual.out, "foo");
})
}
#[test]
fn source_env_eval_export_env_hide() {
Playground::setup("source_env_eval_export_env", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContentToBeTrimmed(
"spam.nu",
r#"
export-env { hide-env FOO }
"#,
)]);
let inp = &[
r#"let-env FOO = 'foo'"#,
r#"source-env spam.nu"#,
r#"$env.FOO"#,
];
let actual = nu!(cwd: dirs.test(), pipeline(&inp.join("; ")));
assert!(actual.err.contains("did you mean"));
})
}
#[test]
fn source_env_do_cd() {
Playground::setup("source_env_do_cd", |dirs, sandbox| {
sandbox
.mkdir("test1/test2")
.with_files(vec![FileWithContentToBeTrimmed(
"test1/test2/spam.nu",
r#"
cd test1/test2
"#,
)]);
let inp = &[
r#"source-env test1/test2/spam.nu"#,
r#"$env.PWD | path basename"#,
];
let actual = nu!(cwd: dirs.test(), pipeline(&inp.join("; ")));
assert_eq!(actual.out, "test2");
})
}
#[test]
fn source_env_do_cd_file_relative() {
Playground::setup("source_env_do_cd_file_relative", |dirs, sandbox| {
sandbox
.mkdir("test1/test2")
.with_files(vec![FileWithContentToBeTrimmed(
"test1/test2/spam.nu",
r#"
cd ($env.FILE_PWD | path join '..')
"#,
)]);
let inp = &[
r#"source-env test1/test2/spam.nu"#,
r#"$env.PWD | path basename"#,
];
let actual = nu!(cwd: dirs.test(), pipeline(&inp.join("; ")));
assert_eq!(actual.out, "test1");
})
}
#[test]
fn source_env_dont_cd_overlay() {
Playground::setup("source_env_dont_cd_overlay", |dirs, sandbox| {
sandbox
.mkdir("test1/test2")
.with_files(vec![FileWithContentToBeTrimmed(
"test1/test2/spam.nu",
r#"
overlay new spam
cd test1/test2
overlay hide spam
"#,
)]);
let inp = &[
r#"source-env test1/test2/spam.nu"#,
r#"$env.PWD | path basename"#,
];
let actual = nu!(cwd: dirs.test(), pipeline(&inp.join("; ")));
assert_eq!(actual.out, "source_env_dont_cd_overlay");
})
}
#[test]
fn source_env_nice_parse_error() {
Playground::setup("source_env_nice_parse_error", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContentToBeTrimmed(
"spam.nu",
r#"
let x
"#,
)]);
let inp = &[r#"source-env spam.nu"#];
let actual = nu!(cwd: dirs.test(), pipeline(&inp.join("; ")));
assert!(actual.err.contains("cannot parse this file"));
assert!(actual.err.contains("───"));
})
}
#[test]
fn source_env_nice_shell_error() {
Playground::setup("source_env_nice_shell_error", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContentToBeTrimmed(
"spam.nu",
r#"
let-env FILE_PWD = 'foo'
"#,
)]);
let inp = &[r#"source-env spam.nu"#];
let actual = nu!(cwd: dirs.test(), pipeline(&inp.join("; ")));
assert!(actual.err.contains("cannot evaluate this file"));
assert!(actual.err.contains("───"));
})
}

View File

@ -73,7 +73,7 @@ fn nu_lib_dirs_repl() {
let inp_lines = &[
r#"let-env NU_LIB_DIRS = [ ('scripts' | path expand) ]"#,
r#"source foo.nu"#,
r#"source-env foo.nu"#,
r#"$env.FOO"#,
];
@ -98,13 +98,13 @@ fn nu_lib_dirs_script() {
.with_files(vec![FileWithContentToBeTrimmed(
"main.nu",
r#"
source foo.nu
source-env foo.nu
"#,
)]);
let inp_lines = &[
r#"let-env NU_LIB_DIRS = [ ('scripts' | path expand) ]"#,
r#"source main.nu"#,
r#"source-env main.nu"#,
r#"$env.FOO"#,
];
@ -129,7 +129,7 @@ fn nu_lib_dirs_relative_repl() {
let inp_lines = &[
r#"let-env NU_LIB_DIRS = [ 'scripts' ]"#,
r#"source foo.nu"#,
r#"source-env foo.nu"#,
r#"$env.FOO"#,
];
@ -148,7 +148,7 @@ fn nu_lib_dirs_relative_script() {
.with_files(vec![FileWithContentToBeTrimmed(
"scripts/main.nu",
r#"
source ../foo.nu
source-env ../foo.nu
"#,
)])
.with_files(vec![FileWithContentToBeTrimmed(
@ -160,7 +160,7 @@ fn nu_lib_dirs_relative_script() {
let inp_lines = &[
r#"let-env NU_LIB_DIRS = [ 'scripts' ]"#,
r#"source scripts/main.nu"#,
r#"source-env scripts/main.nu"#,
r#"$env.FOO"#,
];