Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(shell): add unset command #6430

Merged
merged 4 commits into from
Aug 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions .yarn/versions/2f8bb76f.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
releases:
"@yarnpkg/cli": patch
"@yarnpkg/core": patch
"@yarnpkg/shell": patch

declined:
- "@yarnpkg/plugin-compat"
- "@yarnpkg/plugin-constraints"
- "@yarnpkg/plugin-dlx"
- "@yarnpkg/plugin-essentials"
- "@yarnpkg/plugin-exec"
- "@yarnpkg/plugin-file"
- "@yarnpkg/plugin-git"
- "@yarnpkg/plugin-github"
- "@yarnpkg/plugin-http"
- "@yarnpkg/plugin-init"
- "@yarnpkg/plugin-interactive-tools"
- "@yarnpkg/plugin-link"
- "@yarnpkg/plugin-nm"
- "@yarnpkg/plugin-npm"
- "@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/doctor"
- "@yarnpkg/extensions"
- "@yarnpkg/nm"
- "@yarnpkg/pnpify"
- "@yarnpkg/sdks"
13 changes: 13 additions & 0 deletions packages/yarnpkg-shell/sources/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,19 @@ const BUILTINS = new Map<string, ShellBuiltin>([
return await setTimeout(1000 * seconds, 0);
}],

[`unset`, async ([name, ...rest]: Array<string>, opts: ShellOptions, state: ShellState) => {
if (typeof name === `undefined`)
throw new ShellError(`unset: missing name`);

if (!Object.hasOwn(state.environment, name) && !Object.hasOwn(state.variables, name))
throw new ShellError(`unset: unbound variable "${name}"`);

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
29 changes: 29 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,34 @@ describe(`Shell`, () => {
});
});
});

describe(`unset`, () => {
it(`should throw a recoverable error when the variable name is missing`, async () => {
await expectResult(bufferResult(
`unset`,
), {
exitCode: 1,
stderr: `unset: missing name\n`,
});
});

it(`should throw a recoverable error when the variable is unbound`, async () => {
await expectResult(bufferResult(
`unset FOO`,
), {
exitCode: 1,
stderr: `unset: unbound variable "FOO"\n`,
});
});

it(`should unset the variable`, async () => {
await expectResult(bufferResult(
`FOO=bar; unset FOO; echo $FOO`,
), {
exitCode: 1,
stderr: `Unbound variable "FOO"\n`,
});
});
});
});
});
Loading