Skip to content

Commit

Permalink
feat(shell): add unset command (#6430)
Browse files Browse the repository at this point in the history
## What's the problem this PR addresses?

<!-- Describe the rationale of your PR. -->
<!-- Link all issues that it closes. (Closes/Resolves #xxxx.) -->

Fixes #4447.

## How did you fix it?

<!-- A detailed description of your implementation. -->

I fixed it by adding an `unset` built-in.

## Checklist

<!--- Don't worry if you miss something, chores are automatically
tested. -->
<!--- This checklist exists to help you remember doing the chores when
you submit a PR. -->
<!--- Put an `x` in all the boxes that apply. -->
- [x] I have read the [Contributing
Guide](https://yarnpkg.com/advanced/contributing).

<!-- See
https://yarnpkg.com/advanced/contributing#preparing-your-pr-to-be-released
for more details. -->
<!-- Check with `yarn version check` and fix with `yarn version check
-i` -->
- [x] I have set the packages that need to be released for my changes to
be effective.

<!-- The "Testing chores" workflow validates that your PR follows our
guidelines. -->
<!-- If it doesn't pass, click on it to see details as to what your PR
might be missing. -->
- [x] I will check that all automated PR checks pass before the PR gets
reviewed.

---------

Co-authored-by: merceyz <merceyz@users.noreply.github.com>
  • Loading branch information
tthijm and merceyz authored Aug 25, 2024
1 parent 3b156c9 commit 10d16c3
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
24 changes: 24 additions & 0 deletions .yarn/versions/2f8bb76f.yml
Original file line number Diff line number Diff line change
@@ -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"
9 changes: 9 additions & 0 deletions packages/yarnpkg-shell/sources/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,15 @@ const BUILTINS = new Map<string, ShellBuiltin>([
return await setTimeout(1000 * seconds, 0);
}],

[`unset`, async (args: Array<string>, 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<string>, opts: ShellOptions, state: ShellState) => {
const procedure = state.procedures[args[0]];

Expand Down
20 changes: 20 additions & 0 deletions packages/yarnpkg-shell/tests/shell.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
});
});
});
});
});

0 comments on commit 10d16c3

Please sign in to comment.