From 10d16c36653713dae4c4347394cd80b7c2015412 Mon Sep 17 00:00:00 2001 From: tthijm <59415467+tthijm@users.noreply.github.com> Date: Sun, 25 Aug 2024 16:47:29 +0200 Subject: [PATCH] feat(shell): add unset command (#6430) ## What's the problem this PR addresses? Fixes #4447. ## How did you fix it? I fixed it by adding an `unset` built-in. ## Checklist - [x] I have read the [Contributing Guide](https://yarnpkg.com/advanced/contributing). - [x] I have set the packages that need to be released for my changes to be effective. - [x] I will check that all automated PR checks pass before the PR gets reviewed. --------- Co-authored-by: merceyz --- .yarn/versions/2f8bb76f.yml | 24 ++++++++++++++++++++++ packages/yarnpkg-shell/sources/index.ts | 9 ++++++++ packages/yarnpkg-shell/tests/shell.test.ts | 20 ++++++++++++++++++ 3 files changed, 53 insertions(+) create mode 100644 .yarn/versions/2f8bb76f.yml diff --git a/.yarn/versions/2f8bb76f.yml b/.yarn/versions/2f8bb76f.yml new file mode 100644 index 000000000000..a03b6c200680 --- /dev/null +++ b/.yarn/versions/2f8bb76f.yml @@ -0,0 +1,24 @@ +releases: + "@yarnpkg/cli": minor + "@yarnpkg/shell": minor + +declined: + - "@yarnpkg/plugin-compat" + - "@yarnpkg/plugin-constraints" + - "@yarnpkg/plugin-dlx" + - "@yarnpkg/plugin-essentials" + - "@yarnpkg/plugin-init" + - "@yarnpkg/plugin-interactive-tools" + - "@yarnpkg/plugin-nm" + - "@yarnpkg/plugin-npm-cli" + - "@yarnpkg/plugin-pack" + - "@yarnpkg/plugin-patch" + - "@yarnpkg/plugin-pnp" + - "@yarnpkg/plugin-pnpm" + - "@yarnpkg/plugin-stage" + - "@yarnpkg/plugin-typescript" + - "@yarnpkg/plugin-version" + - "@yarnpkg/plugin-workspace-tools" + - "@yarnpkg/builder" + - "@yarnpkg/core" + - "@yarnpkg/doctor" diff --git a/packages/yarnpkg-shell/sources/index.ts b/packages/yarnpkg-shell/sources/index.ts index b0c505b7cd12..175448ac6cb3 100644 --- a/packages/yarnpkg-shell/sources/index.ts +++ b/packages/yarnpkg-shell/sources/index.ts @@ -166,6 +166,15 @@ const BUILTINS = new Map([ return await setTimeout(1000 * seconds, 0); }], + [`unset`, async (args: Array, opts: ShellOptions, state: ShellState) => { + for (const name of args) { + delete state.environment[name]; + delete state.variables[name]; + } + + return 0; + }], + [`__ysh_run_procedure`, async (args: Array, opts: ShellOptions, state: ShellState) => { const procedure = state.procedures[args[0]]; diff --git a/packages/yarnpkg-shell/tests/shell.test.ts b/packages/yarnpkg-shell/tests/shell.test.ts index dc90d6e6874b..c79ea00405e2 100644 --- a/packages/yarnpkg-shell/tests/shell.test.ts +++ b/packages/yarnpkg-shell/tests/shell.test.ts @@ -2094,5 +2094,25 @@ describe(`Shell`, () => { }); }); }); + + describe(`unset`, () => { + it(`should unset one variable`, async () => { + await expectResult(bufferResult( + `FOO=bar; unset FOO; echo $FOO`, + ), { + exitCode: 1, + stderr: `Unbound variable "FOO"\n`, + }); + }); + + it(`should unset multiple variables`, async () => { + await expectResult(bufferResult( + `A=1 B=2; unset A B; echo $A; echo $B`, + ), { + exitCode: 1, + stderr: `Unbound variable "A"\nUnbound variable "B"\n`, + }); + }); + }); }); });