|
| 1 | +import Check from './check.js' |
| 2 | +import {checkTheme, formatOffensesJson, sortOffenses} from '../../services/check.js' |
| 3 | +import {themeCheckJsonOutputSchema} from '../../services/check/types.js' |
| 4 | +import {encodeThemeCheckResult} from '../../services/check/result.js' |
| 5 | +import {captureStandardStreams} from '../../utilities/testing/streams.js' |
| 6 | +import {afterEach, expect, test, vi} from 'vitest' |
| 7 | +import {themeCheckRun, Severity, SourceCodeType, path as pathUtils} from '@shopify/theme-check-node' |
| 8 | +import {inTemporaryDirectory} from '@shopify/cli-kit/node/fs' |
| 9 | + |
| 10 | +vi.mock('@shopify/theme-check-node', async (importOriginal) => ({ |
| 11 | + ...(await importOriginal<typeof import('@shopify/theme-check-node')>()), |
| 12 | + themeCheckRun: vi.fn(), |
| 13 | +})) |
| 14 | + |
| 15 | +vi.mock('@shopify/cli-kit/node/environments') |
| 16 | + |
| 17 | +const offense = { |
| 18 | + type: SourceCodeType.LiquidHtml, |
| 19 | + check: 'ExampleCheck', |
| 20 | + severity: Severity.ERROR, |
| 21 | + uri: 'file:///theme/templates/index.liquid', |
| 22 | + start: {index: 0, line: 0, character: 0}, |
| 23 | + end: {index: 1, line: 0, character: 1}, |
| 24 | + message: 'Example offense', |
| 25 | +} |
| 26 | + |
| 27 | +afterEach(() => vi.unstubAllEnvs()) |
| 28 | + |
| 29 | +test('exposes the contract and preserves compact JSON, environment omission and counts', async () => { |
| 30 | + expect(Check.jsonOutputSchema).toBe(themeCheckJsonOutputSchema) |
| 31 | + vi.mocked(themeCheckRun).mockResolvedValue({offenses: [offense], theme: [], config: {} as never}) |
| 32 | + const {result} = await checkTheme('/theme') |
| 33 | + expect(result).toEqual(formatOffensesJson(sortOffenses([offense]))) |
| 34 | + expect(encodeThemeCheckResult(result)).toBe( |
| 35 | + JSON.stringify([ |
| 36 | + { |
| 37 | + path: pathUtils.fsPath(offense.uri), |
| 38 | + offenses: [ |
| 39 | + { |
| 40 | + check: 'ExampleCheck', |
| 41 | + severity: 'error', |
| 42 | + start_row: 0, |
| 43 | + start_column: 0, |
| 44 | + end_row: 0, |
| 45 | + end_column: 1, |
| 46 | + message: 'Example offense', |
| 47 | + }, |
| 48 | + ], |
| 49 | + errorCount: 1, |
| 50 | + warningCount: 0, |
| 51 | + infoCount: 0, |
| 52 | + }, |
| 53 | + ]), |
| 54 | + ) |
| 55 | + expect(encodeThemeCheckResult([])).toBe('[]') |
| 56 | + expect(() => themeCheckJsonOutputSchema.validate([{...result[0], errorCount: '1'}])).toThrow() |
| 57 | + expect(() => |
| 58 | + themeCheckJsonOutputSchema.validate([{...result[0], offenses: [{...result[0]!.offenses[0], severity: 'fatal'}]}]), |
| 59 | + ).toThrow() |
| 60 | +}) |
| 61 | + |
| 62 | +test.each(['--json', '--output=json'])('writes compact JSON and preserves unsuccessful exit with %s', async (flag) => { |
| 63 | + await inTemporaryDirectory(async (directory) => { |
| 64 | + vi.stubEnv('SHOPIFY_UNIT_TEST', 'false') |
| 65 | + vi.resetModules() |
| 66 | + const {default: StreamCheck} = await import('./check.js') |
| 67 | + const {themeCheckRun: check} = await import('@shopify/theme-check-node') |
| 68 | + const {Config} = await import('@oclif/core') |
| 69 | + const config = new Config({root: __dirname}) |
| 70 | + await config.load() |
| 71 | + vi.mocked(check).mockResolvedValue({offenses: [offense], theme: [], config: {} as never}) |
| 72 | + const exit = vi.spyOn(process, 'exit').mockImplementation(() => undefined as never) |
| 73 | + const streams = captureStandardStreams() |
| 74 | + try { |
| 75 | + await new StreamCheck([`--path=${directory}`, flag], config).run() |
| 76 | + } finally { |
| 77 | + streams.restore() |
| 78 | + } |
| 79 | + expect(streams.stdout()).toBe(`${JSON.stringify(formatOffensesJson(sortOffenses([offense])))}\n`) |
| 80 | + expect(streams.stderr()).toBe('') |
| 81 | + expect(exit).toHaveBeenCalledWith(1) |
| 82 | + }) |
| 83 | +}) |
| 84 | + |
| 85 | +test.each(['init', 'version', 'print', 'list'])('rejects --json with --%s before execution', async (mode) => { |
| 86 | + const {Config} = await import('@oclif/core') |
| 87 | + const config = new Config({root: __dirname}) |
| 88 | + await config.load() |
| 89 | + await expect(new Check(['--json', `--${mode}`], config).run()).rejects.toThrow() |
| 90 | + expect(themeCheckRun).not.toHaveBeenCalled() |
| 91 | +}) |
| 92 | + |
| 93 | +test.each(['success', 'partial failure', 'total failure'])( |
| 94 | + 'collects multiple environments into one document on %s', |
| 95 | + async (mode) => { |
| 96 | + await inTemporaryDirectory(async (directory) => { |
| 97 | + vi.stubEnv('SHOPIFY_UNIT_TEST', 'false') |
| 98 | + vi.resetModules() |
| 99 | + const {default: StreamCheck} = await import('./check.js') |
| 100 | + const {themeCheckRun: check} = await import('@shopify/theme-check-node') |
| 101 | + const {Config} = await import('@oclif/core') |
| 102 | + const config = new Config({root: __dirname}) |
| 103 | + await config.load() |
| 104 | + vi.mocked(check).mockResolvedValue({offenses: [], theme: [], config: {} as never}) |
| 105 | + if (mode === 'partial failure') vi.mocked(check).mockRejectedValueOnce(new Error('Check failed')) |
| 106 | + if (mode === 'total failure') vi.mocked(check).mockRejectedValue(new Error('Check failed')) |
| 107 | + const exit = vi.spyOn(process, 'exit').mockImplementation(() => undefined as never) |
| 108 | + const {loadEnvironment} = await import('@shopify/cli-kit/node/environments') |
| 109 | + vi.mocked(loadEnvironment).mockResolvedValue({path: directory}) |
| 110 | + const {runWithCommandEventsForCommand} = await import('@shopify/cli-kit/node/command-events') |
| 111 | + const streams = captureStandardStreams() |
| 112 | + try { |
| 113 | + const argv = ['--environment=first', '--environment=second', '--json'] |
| 114 | + await runWithCommandEventsForCommand(argv, () => new StreamCheck(argv, config).run()) |
| 115 | + } finally { |
| 116 | + streams.restore() |
| 117 | + } |
| 118 | + let names = ['first', 'second'] |
| 119 | + if (mode === 'partial failure') names = ['second'] |
| 120 | + if (mode === 'total failure') names = [] |
| 121 | + expect(JSON.parse(streams.stdout())).toEqual({ |
| 122 | + environments: names.map((environment) => ({environment, result: []})), |
| 123 | + }) |
| 124 | + if (mode.includes('failure')) expect(streams.stderr()).toContain('Check failed') |
| 125 | + expect(exit).not.toHaveBeenCalled() |
| 126 | + }) |
| 127 | + }, |
| 128 | +) |
0 commit comments