Repository navigation
Add typed JSON output to auth logout #8790
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
Merged
Merged
Changes from all commits
Commits
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,5 @@ | ||
| --- | ||
| '@shopify/cli': minor | ||
| --- | ||
|
|
||
| Add typed JSON output to `auth logout`. |
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
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,121 @@ | ||
| import Logout from './logout.js' | ||
| import {logout} from '@shopify/cli-kit/node/session' | ||
| import {launchCLI} from '@shopify/cli-kit/node/cli-launcher' | ||
| import {ShopifyConfig} from '@shopify/cli-kit/node/custom-oclif-loader' | ||
| import {AbortError} from '@shopify/cli-kit/node/error' | ||
| import {outputInfo, unstyled} from '@shopify/cli-kit/node/output' | ||
| import {mockAndCaptureOutput, withCapturedStandardStreams} from '@shopify/cli-kit/node/testing/output' | ||
| import {beforeEach, afterEach, expect, test, vi} from 'vitest' | ||
|
|
||
| vi.mock('@shopify/cli-kit/node/session') | ||
|
|
||
| beforeEach(() => { | ||
| vi.stubEnv('CI', '1') | ||
| vi.stubEnv('SHOPIFY_CLI_NO_ANALYTICS', '1') | ||
| vi.spyOn(ShopifyConfig.prototype, 'runHook').mockResolvedValue({successes: [], failures: []}) | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| vi.unstubAllEnvs() | ||
| mockAndCaptureOutput().clear() | ||
| }) | ||
|
|
||
| test.each(['--json', '-j'])('writes one completed result through the launcher with %s', async (flag) => { | ||
| await withCapturedStandardStreams(async ({stdout, stderr}) => { | ||
| await launchCLI({ | ||
| moduleURL: import.meta.url, | ||
| argv: ['auth', 'logout', flag], | ||
| lazyCommandLoader: async () => Logout, | ||
| }) | ||
|
|
||
| expect(logout).toHaveBeenCalledExactlyOnceWith() | ||
| expect(stdout()).toBe(`${JSON.stringify({status: 'success'}, null, 2)}\n`) | ||
| expect(stderr()).toBe('') | ||
| }) | ||
| }) | ||
|
|
||
| test('supports the JSON environment flag', async () => { | ||
| vi.stubEnv('SHOPIFY_FLAG_JSON', '1') | ||
|
|
||
| await withCapturedStandardStreams(async ({stdout, stderr}) => { | ||
| await Logout.run([], import.meta.url) | ||
|
|
||
| expect(JSON.parse(stdout())).toEqual({status: 'success'}) | ||
| expect(stderr()).toBe('') | ||
| }) | ||
| }) | ||
|
|
||
| test.each([false, true])('keeps no-input independent from JSON output: %s', async (json) => { | ||
| await withCapturedStandardStreams(async ({stdout, stderr}) => { | ||
| await Logout.run(['--no-input', ...(json ? ['--json'] : [])], import.meta.url) | ||
|
|
||
| if (json) { | ||
| expect(JSON.parse(stdout())).toEqual({status: 'success'}) | ||
| expect(stderr()).toBe('') | ||
| } else { | ||
| expect(stdout()).toBe('') | ||
| expect(unstyled(stderr())).toBe('✅ Success! Logged out from all the accounts.\n') | ||
| } | ||
| }) | ||
| }) | ||
|
|
||
| test('exposes the schema in help', () => { | ||
| expect(Logout.description).toContain('Output from `--json` conforms to the `AuthLogoutResult` schema.') | ||
| }) | ||
|
|
||
| test.each([{}, {status: 'failed'}, {status: true}, {status: null}, {status: 'success', accessToken: 'secret'}])( | ||
| 'rejects an invalid result %j', | ||
| (value) => { | ||
| expect(() => Logout.jsonOutputSchema.validate(value)).toThrow() | ||
| }, | ||
| ) | ||
|
|
||
| test('discovers the schema without logging out', async () => { | ||
| await withCapturedStandardStreams(async ({stdout, stderr}) => { | ||
| await launchCLI({ | ||
| moduleURL: import.meta.url, | ||
| argv: ['auth', 'logout', '--json-schema'], | ||
| lazyCommandLoader: async () => Logout, | ||
| }) | ||
|
|
||
| expect(JSON.parse(stdout()).definitions.Result).toMatchObject({ | ||
| type: 'object', | ||
| properties: {status: {type: 'string', const: 'success'}}, | ||
| required: ['status'], | ||
| additionalProperties: false, | ||
| }) | ||
| expect(logout).not.toHaveBeenCalled() | ||
| expect(stderr()).toBe('') | ||
| }) | ||
| }) | ||
|
|
||
| test('writes diagnostics to stderr separately from the result', async () => { | ||
| vi.mocked(logout).mockImplementation(async () => { | ||
| outputInfo('Session cleanup diagnostic') | ||
| }) | ||
|
|
||
| await withCapturedStandardStreams(async ({stdout, stderr}) => { | ||
| await Logout.run(['--json'], import.meta.url) | ||
|
|
||
| expect(JSON.parse(stdout())).toEqual({status: 'success'}) | ||
| expect(JSON.parse(stderr())).toMatchObject({ | ||
| type: 'diagnostic', | ||
| level: 'info', | ||
| message: 'Session cleanup diagnostic', | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| test('writes only the fatal error and preserves the failure exit code', async () => { | ||
| vi.stubEnv('SHOPIFY_FLAG_JSON', '1') | ||
| const exit = vi.spyOn(process, 'exit').mockReturnValue(undefined as never) | ||
| vi.mocked(logout).mockRejectedValue(new AbortError('Cannot clear sessions.')) | ||
|
|
||
| await withCapturedStandardStreams(async ({stdout, stderr}) => { | ||
| await Logout.run(['--json'], import.meta.url) | ||
|
|
||
| expect(JSON.parse(stdout())).toEqual({error: {type: 'abort', message: 'Cannot clear sessions.'}}) | ||
| expect(stderr()).toBe('') | ||
| expect(exit).toHaveBeenCalledExactlyOnceWith(1) | ||
| }) | ||
| }) | ||
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 |
|---|---|---|
| @@ -1,12 +1,33 @@ | ||
| import Command from '@shopify/cli-kit/node/base-command' | ||
| import {outputSuccess} from '@shopify/cli-kit/node/output' | ||
| import {globalFlags, jsonFlag} from '@shopify/cli-kit/node/cli' | ||
| import {outputResult, outputSuccess} from '@shopify/cli-kit/node/output' | ||
| import {logout} from '@shopify/cli-kit/node/session' | ||
| import {defineJsonOutputSchema} from '@shopify/cli-kit/node/json-output-schema' | ||
| import {zod} from '@shopify/cli-kit/node/schema' | ||
|
|
||
| const authLogoutJsonOutputSchema = defineJsonOutputSchema({ | ||
| name: 'AuthLogoutResult', | ||
| schema: zod.object({status: zod.literal('success')}).strict(), | ||
| }) | ||
|
|
||
| export default class Logout extends Command { | ||
| static description = 'Logs you out of the Shopify account or Partner account and store.' | ||
| static descriptionWithMarkdown = 'Logs you out of the Shopify account or Partner account and store.' | ||
|
|
||
| static description = this.descriptionForHelp() | ||
|
|
||
| static flags = {...globalFlags, ...jsonFlag} | ||
|
|
||
| static get jsonOutputSchema() { | ||
| return authLogoutJsonOutputSchema | ||
| } | ||
|
|
||
| async run(): Promise<void> { | ||
| const {flags} = await this.parse(Logout) | ||
| await logout() | ||
| outputSuccess('Logged out from all the accounts') | ||
| if (flags.json) { | ||
| outputResult(authLogoutJsonOutputSchema.encode({status: 'success'})) | ||
| } else { | ||
| outputSuccess('Logged out from all the accounts') | ||
| } | ||
| } | ||
| } |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some of these tests seem like they could be shared somewhere - like have an
isValidJsonCommand(command, ...workingFlagSet)function somewhere which runs the basic tests in a generalizable way rather than rewriting all the tests for each command.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, i'll take a look at that and probably update it in a future PR