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

show error message in replay command when no logs available #5292

Merged
merged 1 commit into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions .changeset/lemon-eggs-exist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/app': patch
---

Improve error message in function replay command when log directory doesnt exist
25 changes: 23 additions & 2 deletions packages/app/src/cli/services/function/replay.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import {readFile} from '@shopify/cli-kit/node/fs'
import {describe, expect, beforeAll, test, vi} from 'vitest'
import {AbortError} from '@shopify/cli-kit/node/error'
import {outputInfo} from '@shopify/cli-kit/node/output'
import {readdirSync} from 'fs'
import {getLogsDir} from '@shopify/cli-kit/node/logs'

import {existsSync, readdirSync} from 'fs'

vi.mock('fs')
vi.mock('@shopify/cli-kit/node/fs')
Expand Down Expand Up @@ -111,6 +113,24 @@ describe('replay', () => {

test('throws error if no logs available', async () => {
// Given
mockFileOperations([])

// When/Then
await expect(async () => {
await replay({
app: testAppLinked(),
extension,
stdout: false,
path: 'test-path',
json: true,
watch: false,
})
}).rejects.toThrow(new AbortError(`No logs found in ${getLogsDir()}`))
})

test('throws error if log directory does not exist', async () => {
// Given
vi.mocked(existsSync).mockReturnValue(false)
jacobsteves marked this conversation as resolved.
Show resolved Hide resolved

// When/Then
await expect(async () => {
Expand All @@ -122,7 +142,7 @@ describe('replay', () => {
json: true,
watch: false,
})
}).rejects.toThrow()
}).rejects.toThrow(new AbortError(`No logs found in ${getLogsDir()}`))
})

test('delegates to renderReplay when watch is true', async () => {
Expand Down Expand Up @@ -285,6 +305,7 @@ function expectFunctionRun(functionExtension: ExtensionInstance<FunctionConfigTy
}

function mockFileOperations(data: {run: FunctionRunData; path: string}[]) {
vi.mocked(existsSync).mockReturnValue(true)
vi.mocked(readdirSync).mockReturnValue([...data].reverse().map(({path}) => path) as any)
vi.mocked(readFile).mockImplementation((path) => {
const run = data.find((file) => path.endsWith(file.path))
Expand Down
10 changes: 7 additions & 3 deletions packages/app/src/cli/services/function/replay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {getLogsDir} from '@shopify/cli-kit/node/logs'
import {AbortError} from '@shopify/cli-kit/node/error'
import {AbortController} from '@shopify/cli-kit/node/abort'

import {readdirSync} from 'fs'
import {existsSync, readdirSync} from 'fs'

const LOG_SELECTOR_LIMIT = 100

Expand Down Expand Up @@ -124,7 +124,7 @@ async function findFunctionRun(
functionHandle: string,
identifier: string,
): Promise<string | undefined> {
const fileName = readdirSync(functionRunsDir).find((filename) => {
const fileName = getAllFunctionRunFileNames(functionRunsDir).find((filename) => {
const fileMetadata = parseLogFilename(filename)
return (
fileMetadata?.namespace === 'extensions' &&
Expand All @@ -147,7 +147,7 @@ async function getRunFromSelector(functionRunsDir: string, functionHandle: strin
}

async function getFunctionRunData(functionRunsDir: string, functionHandle: string): Promise<FunctionRunData[]> {
const allFunctionRunFileNames = readdirSync(functionRunsDir)
const allFunctionRunFileNames = getAllFunctionRunFileNames(functionRunsDir)
.filter((filename) => {
// Expected format: 20240522_150641_827Z_extensions_my-function_abcdef.json
const fileMetadata = parseLogFilename(filename)
Expand Down Expand Up @@ -191,3 +191,7 @@ async function getFunctionRunData(functionRunsDir: string, functionHandle: strin
function getIdentifierFromFilename(fileName: string): string {
return fileName.split('_').pop()!.substring(0, 6)
}

function getAllFunctionRunFileNames(functionRunsDir: string): string[] {
return existsSync(functionRunsDir) ? readdirSync(functionRunsDir) : []
}
Loading