Skip to content

Commit 10d16c3

Browse files
tthijmmerceyz
andauthored
feat(shell): add unset command (#6430)
## 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 <[email protected]>
1 parent 3b156c9 commit 10d16c3

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

.yarn/versions/2f8bb76f.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
releases:
2+
"@yarnpkg/cli": minor
3+
"@yarnpkg/shell": minor
4+
5+
declined:
6+
- "@yarnpkg/plugin-compat"
7+
- "@yarnpkg/plugin-constraints"
8+
- "@yarnpkg/plugin-dlx"
9+
- "@yarnpkg/plugin-essentials"
10+
- "@yarnpkg/plugin-init"
11+
- "@yarnpkg/plugin-interactive-tools"
12+
- "@yarnpkg/plugin-nm"
13+
- "@yarnpkg/plugin-npm-cli"
14+
- "@yarnpkg/plugin-pack"
15+
- "@yarnpkg/plugin-patch"
16+
- "@yarnpkg/plugin-pnp"
17+
- "@yarnpkg/plugin-pnpm"
18+
- "@yarnpkg/plugin-stage"
19+
- "@yarnpkg/plugin-typescript"
20+
- "@yarnpkg/plugin-version"
21+
- "@yarnpkg/plugin-workspace-tools"
22+
- "@yarnpkg/builder"
23+
- "@yarnpkg/core"
24+
- "@yarnpkg/doctor"

packages/yarnpkg-shell/sources/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,15 @@ const BUILTINS = new Map<string, ShellBuiltin>([
166166
return await setTimeout(1000 * seconds, 0);
167167
}],
168168

169+
[`unset`, async (args: Array<string>, opts: ShellOptions, state: ShellState) => {
170+
for (const name of args) {
171+
delete state.environment[name];
172+
delete state.variables[name];
173+
}
174+
175+
return 0;
176+
}],
177+
169178
[`__ysh_run_procedure`, async (args: Array<string>, opts: ShellOptions, state: ShellState) => {
170179
const procedure = state.procedures[args[0]];
171180

packages/yarnpkg-shell/tests/shell.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2094,5 +2094,25 @@ describe(`Shell`, () => {
20942094
});
20952095
});
20962096
});
2097+
2098+
describe(`unset`, () => {
2099+
it(`should unset one variable`, async () => {
2100+
await expectResult(bufferResult(
2101+
`FOO=bar; unset FOO; echo $FOO`,
2102+
), {
2103+
exitCode: 1,
2104+
stderr: `Unbound variable "FOO"\n`,
2105+
});
2106+
});
2107+
2108+
it(`should unset multiple variables`, async () => {
2109+
await expectResult(bufferResult(
2110+
`A=1 B=2; unset A B; echo $A; echo $B`,
2111+
), {
2112+
exitCode: 1,
2113+
stderr: `Unbound variable "A"\nUnbound variable "B"\n`,
2114+
});
2115+
});
2116+
});
20972117
});
20982118
});

0 commit comments

Comments
 (0)