-
Notifications
You must be signed in to change notification settings - Fork 19
feat: remove login dependency on help command [XTNSNS-2080] #1270
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
Open
silvadenisaraujo
wants to merge
3
commits into
main
Choose a base branch
from
feat/help-without-login
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+304
−8
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| #!/usr/bin/env bash | ||
| # Smoke test: help must render without login (specs/help-without-login.md). | ||
| # | ||
| # Runs the CLI in an isolated HOME (no credentials, empty feature-flag cache) | ||
| # and asserts that help invocations exit 0, render help output, and never | ||
| # emit an interactive login prompt. | ||
| set -u | ||
|
|
||
| ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" | ||
| cd "$ROOT_DIR" | ||
|
|
||
| if [ ! -d lib ]; then | ||
| echo "lib/ not found — building..." | ||
| yarn build || exit 1 | ||
| fi | ||
|
|
||
| FAKE_HOME="$(mktemp -d)" | ||
| trap 'rm -rf "$FAKE_HOME"' EXIT | ||
|
|
||
| # On a fresh build the init hook autofixes the node_modules/vtex symlink and | ||
| # exits asking for a re-run. Prime it so it doesn't interfere with the cases. | ||
| HOME="$FAKE_HOME" node bin/run --version < /dev/null > /dev/null 2>&1 || true | ||
|
|
||
| FAILURES=0 | ||
|
|
||
| run_case() { | ||
| local description="$1" | ||
| shift | ||
| local output | ||
| local status | ||
|
|
||
| # Isolated HOME/XDG dirs => no session token, empty feature-flag cache. | ||
| output="$(HOME="$FAKE_HOME" XDG_CONFIG_HOME="$FAKE_HOME/.config" XDG_CACHE_HOME="$FAKE_HOME/.cache" \ | ||
| node bin/run "$@" < /dev/null 2>&1)" | ||
| status=$? | ||
|
|
||
| if [ $status -ne 0 ]; then | ||
| echo "FAIL [$description]: exited with status $status" | ||
| echo "$output" | head -30 | ||
| FAILURES=$((FAILURES + 1)) | ||
| return | ||
| fi | ||
|
|
||
| if ! echo "$output" | grep -qi "usage"; then | ||
| echo "FAIL [$description]: output does not look like help (no 'Usage' section)" | ||
| echo "$output" | head -30 | ||
| FAILURES=$((FAILURES + 1)) | ||
| return | ||
| fi | ||
|
|
||
| if echo "$output" | grep -qiE "Account:|previous login|previous account"; then | ||
| echo "FAIL [$description]: interactive login prompt detected" | ||
| echo "$output" | head -30 | ||
| FAILURES=$((FAILURES + 1)) | ||
| return | ||
| fi | ||
|
|
||
| echo "PASS [$description]" | ||
| } | ||
|
|
||
| run_case "vtex help" help | ||
| run_case "vtex help <command>" help link | ||
| run_case "vtex <command> --help" link --help | ||
| run_case "vtex <command> -h" link -h | ||
| run_case "vtex --help" --help | ||
|
|
||
| if [ $FAILURES -gt 0 ]; then | ||
| echo "$FAILURES smoke test case(s) failed" | ||
| exit 1 | ||
| fi | ||
|
|
||
| echo "All help smoke tests passed" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| # Spec: Remove login dependency for `help` in the toolbelt CLI | ||
|
|
||
| ## Problem | ||
|
|
||
| `src/oclif/hooks/init.ts` → `main()` always calls `checkLogin(options.id)`, which | ||
| forces an interactive login for any command not present in an `allowedList`. | ||
| Running `vtex <command> --help` (e.g. `vtex deploy --help`) sets | ||
| `options.id = 'deploy'` (not in the allowed list) and triggers a login prompt, | ||
| even though only static help text is requested. This blocks agents (and users) | ||
| from discovering commands without credentials. | ||
|
|
||
| ## Goal | ||
|
|
||
| All help invocations render without requiring login, so agents on any machine can | ||
| discover the proper commands to run. | ||
|
|
||
| ## Scope | ||
|
|
||
| **In scope** — skip login for every help form: | ||
|
|
||
| - `vtex help` | ||
| - `vtex help <command>` | ||
| - `vtex <command> --help` / `-h` | ||
| - `vtex --help` / `-h` | ||
| - `vtex` (no args — already allowed via `undefined`) | ||
|
|
||
| **In scope (hardening)** — defensive fallback for the empty-feature-flag-cache | ||
| crash in `showHelp` (see design decision 6). Logged-out help on clean machines | ||
| makes this crash much more likely, so it lands in the same PR. | ||
|
|
||
| **Out of scope** — anything beyond the login gate and the help-rendering | ||
| fallback (e.g. reworking how feature flags are fetched/updated). | ||
|
|
||
| ## Design decisions | ||
|
|
||
| 1. **Generic help detection, not per-command whitelisting.** Whitelisting each | ||
| command cannot cover `vtex deploy --help`, so we detect "this is a help | ||
| invocation" generically. | ||
|
|
||
| 2. **New helper `isHelpInvocation(commandId, argv)` in | ||
| `src/oclif/hooks/utils.ts`** (next to `getHelpSubject`). Returns `true` when: | ||
| - `commandId` is `help`, `--help` or `-h` (oclif passes bare `vtex --help` / | ||
| `vtex -h` as the command id itself, with empty argv), or | ||
| - `argv` contains `--help` or `-h` **before** any `--` separator. | ||
| - `-h` / `--help` are safe to treat as help: `-h` is globally reserved for | ||
| help in `src/api/oclif/CustomCommand.ts`, with no other usage. | ||
|
|
||
| 3. **Respect the `--` separator** when scanning args, consistent with the existing | ||
| `getHelpSubject`, so `vtex cmd -- --help` is not treated as help. | ||
|
|
||
| 4. **Wire into `checkLogin` via an explicit `argv` parameter.** Change the | ||
| signature to `checkLogin(command: string, argv: string[])` and early-return | ||
| when `isHelpInvocation(command, argv)` is `true`. The init hook already | ||
| receives the parsed args (`options.argv` in `src/oclif/hooks/init.ts`), so | ||
| the call site becomes `checkLogin(options.id, options.argv)`. Passing argv | ||
| explicitly (instead of reading `process.argv` inside `checkLogin`) keeps the | ||
| function testable. | ||
|
|
||
| 5. **No hidden dependency in help rendering.** `FeatureFlag.getSingleton()` reads | ||
| a local Configstore file (no network/auth at render time), so skipping the | ||
| login gate fully unblocks help. | ||
|
|
||
| 6. **Defensive feature-flag fallback in `showHelp`.** `showHelp` reads | ||
| `COMMANDS_GROUP` / `COMMANDS_GROUP_ID` from the feature-flag Configstore, | ||
| which is populated by a non-blocking child process | ||
| (`FeatureFlagUpdateChecker` → `spawnUnblockingChildProcess`). On a fresh | ||
| install both can be `undefined`, and `Object.keys(commandsId)` throws. | ||
| Guard with sensible defaults so help always renders: when either value is | ||
| missing, fall back to rendering all commands under a single default | ||
| ("Other") group. This is a small hardening change and ships in the same PR. | ||
|
|
||
| 7. **Document the `--` separator handling.** Add a short comment in | ||
| `src/oclif/hooks/utils.ts` (next to `isHelpInvocation` / `getHelpSubject`) | ||
| explaining why args after `--` are ignored, to avoid future regressions. | ||
|
|
||
| ## Testing | ||
|
|
||
| - Add `src/oclif/hooks/utils.test.ts` with table-driven cases for | ||
| `isHelpInvocation`: | ||
| - `help` → true; `help deploy` → true; `deploy --help` → true; | ||
| `deploy -h` → true; `--help` / `-h` → true | ||
| - `deploy` → false; `deploy -- --help` → false; no args → false | ||
| - Unit test for the `checkLogin` early return (export `checkLogin` from | ||
| `init.ts` to make it testable): with | ||
| `SessionManager.getSingleton().checkValidCredentials()` mocked to `false` and | ||
| a help argv (e.g. `deploy --help`), assert `authLogin` is **not** called; | ||
| with a non-help, non-allowed command, assert it **is** called. | ||
| - Integration smoke test (shell): run `node bin/run help` (and | ||
| `node bin/run deploy --help`) in an environment simulating no credentials | ||
| (isolated `HOME`/config dir with no session and an empty feature-flag cache), | ||
| asserting the process exits 0, renders help output, and emits no interactive | ||
| login prompt. This also exercises the feature-flag fallback (decision 6). | ||
| - Beyond the above, the init hook is not unit-tested directly (heavy side | ||
| effects); coverage is via the pure helper and the smoke test. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| // bin/run has import-time side effects (and uses `node:module`, which the | ||
| // jest resolver can't handle) — mock it before importing the hook. | ||
| jest.mock('../../../bin/run', () => ({ initTimeStartTime: [0, 0] })) | ||
|
|
||
| import { checkLogin } from './init' | ||
| import authLogin from '../../modules/auth/login' | ||
|
|
||
| const mockCheckValidCredentials = jest.fn() | ||
|
|
||
| jest.mock('../../api/session/SessionManager', () => ({ | ||
| SessionManager: { | ||
| getSingleton: () => ({ checkValidCredentials: mockCheckValidCredentials }), | ||
| }, | ||
| })) | ||
|
|
||
| jest.mock('../../modules/auth/login', () => ({ | ||
| __esModule: true, | ||
| default: jest.fn().mockResolvedValue(undefined), | ||
| })) | ||
|
|
||
| const mockedAuthLogin = authLogin as jest.MockedFunction<typeof authLogin> | ||
|
|
||
| beforeEach(() => { | ||
| jest.clearAllMocks() | ||
| mockCheckValidCredentials.mockReturnValue(false) | ||
| }) | ||
|
|
||
| describe('checkLogin', () => { | ||
| it('does not trigger login for `vtex help`', async () => { | ||
| await checkLogin('help', []) | ||
| expect(mockedAuthLogin).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('does not trigger login for `vtex help <command>`', async () => { | ||
| await checkLogin('help', ['deploy']) | ||
| expect(mockedAuthLogin).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('does not trigger login for `vtex <command> --help`', async () => { | ||
| await checkLogin('deploy', ['--help']) | ||
| expect(mockedAuthLogin).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('does not trigger login for `vtex <command> -h`', async () => { | ||
| await checkLogin('deploy', ['-h']) | ||
| expect(mockedAuthLogin).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('does not trigger login for bare `vtex --help` / `vtex -h`', async () => { | ||
| await checkLogin('--help', []) | ||
| await checkLogin('-h', []) | ||
| expect(mockedAuthLogin).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('triggers login for a non-help, non-allowed command without credentials', async () => { | ||
| await checkLogin('deploy', []) | ||
| expect(mockedAuthLogin).toHaveBeenCalledTimes(1) | ||
| }) | ||
|
|
||
| it('does not trigger login for `vtex <command> -- --help` when logged in', async () => { | ||
| mockCheckValidCredentials.mockReturnValue(true) | ||
| await checkLogin('deploy', ['--', '--help']) | ||
| expect(mockedAuthLogin).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('treats `vtex <command> -- --help` as a normal command (login required)', async () => { | ||
| await checkLogin('deploy', ['--', '--help']) | ||
| expect(mockedAuthLogin).toHaveBeenCalledTimes(1) | ||
| }) | ||
|
|
||
| it('does not trigger login for allowed commands', async () => { | ||
| await checkLogin('login', []) | ||
| expect(mockedAuthLogin).not.toHaveBeenCalled() | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,25 @@ | ||||||||||
| import { isHelpInvocation } from './utils' | ||||||||||
|
|
||||||||||
| describe('isHelpInvocation', () => { | ||||||||||
| test.each<[string | undefined, string[], boolean]>([ | ||||||||||
| // help forms → true | ||||||||||
| ['help', [], true], | ||||||||||
| ['help', ['deploy'], true], | ||||||||||
| ['deploy', ['--help'], true], | ||||||||||
| ['deploy', ['-h'], true], | ||||||||||
| // oclif passes bare `vtex --help` / `vtex -h` as the command id itself | ||||||||||
| ['--help', [], true], | ||||||||||
| ['-h', [], true], | ||||||||||
| [undefined, ['--help'], true], | ||||||||||
| [undefined, ['-h'], true], | ||||||||||
| ['workspace', ['use', '--help'], true], | ||||||||||
| // non-help forms → false | ||||||||||
| ['deploy', [], false], | ||||||||||
| ['deploy', ['--', '--help'], false], | ||||||||||
| ['deploy', ['--', '-h'], false], | ||||||||||
| [undefined, [], false], | ||||||||||
| ['link', ['--verbose'], false], | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
| ])('commandId=%p argv=%p → %p', (commandId, argv, expected) => { | ||||||||||
| expect(isHelpInvocation(commandId, argv)).toBe(expected) | ||||||||||
| }) | ||||||||||
| }) | ||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.