Skip to content

Commit

Permalink
Only allow unaliasing in current scope, add tests (#3936)
Browse files Browse the repository at this point in the history
* unalias only removes aliases in the current scope

* Add a test and fix previous ones which did not function as expected
  • Loading branch information
filaretov committed Aug 19, 2021
1 parent ead4029 commit 6db5692
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
2 changes: 1 addition & 1 deletion crates/nu-engine/src/evaluate/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ impl ParserScope for Scope {
}

fn remove_alias(&self, name: &str) {
for frame in self.frames.lock().iter_mut().rev() {
if let Some(frame) = self.frames.lock().last_mut() {
frame.aliases.remove(name);
}
}
Expand Down
39 changes: 30 additions & 9 deletions tests/shell/pipeline/commands/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1150,23 +1150,44 @@ fn unalias_shadowing() {
let actual = nu!(
cwd: ".", pipeline(
r#"
alias ll = ls -l
let xyz = { ll -a }
unalias ll
do $xyz
def test-shadowing [] {
alias greet = echo hello;
let xyz = { greet };
unalias greet;
do $xyz
};
test-shadowing
"#)
);
assert_eq!(actual.out, "hello");
}

assert_eq!(actual.out, "");
#[test]
fn unalias_does_not_escape_scope() {
let actual = nu!(
cwd: ".", pipeline(
r#"
def test-alias [] {
alias greet = echo hello;
(unalias greet);
greet
};
test-alias
"#)
);
assert_eq!(actual.out, "hello");
}

#[test]
fn unalias_hides_alias() {
let actual = nu!(cwd: ".", pipeline(
r#"alias ll = ls -l
ll
unalias ll
ll
r#"
def test-alias [] {
alias ll = ls -l;
unalias ll;
ll
};
test-alias
"#)
);

Expand Down

0 comments on commit 6db5692

Please sign in to comment.