diff --git a/packages/cli-kit/src/public/node/fs.test.ts b/packages/cli-kit/src/public/node/fs.test.ts index d30612b5b9..2289789278 100644 --- a/packages/cli-kit/src/public/node/fs.test.ts +++ b/packages/cli-kit/src/public/node/fs.test.ts @@ -254,12 +254,17 @@ describe('createFileReadStream', () => { await touchFile(filePath) await appendFile(filePath, content) const stream = createFileReadStream(filePath) - let data = '' - stream.on('data', (chunk) => { - data += chunk as string - }) - stream.on('end', () => { - expect(data).toBe(content) + + await new Promise((resolve, reject) => { + let data = '' + stream.on('data', (chunk) => { + data += chunk as string + }) + stream.on('end', () => { + expect(data).toBe(content) + resolve() + }) + stream.on('error', reject) }) }) }) @@ -272,12 +277,17 @@ describe('createFileReadStream', () => { await appendFile(filePath, content) const stream = createFileReadStream(filePath, {start: 1, end: 7}) - let data = '' - stream.on('data', (chunk) => { - data += chunk as string - }) - stream.on('end', () => { - expect(data).toBe('est-con') + + await new Promise((resolve, reject) => { + let data = '' + stream.on('data', (chunk) => { + data += chunk as string + }) + stream.on('end', () => { + expect(data).toBe('est-con') + resolve() + }) + stream.on('error', reject) }) }) }) diff --git a/packages/cli/src/cli/commands/docs/generate.test.ts b/packages/cli/src/cli/commands/docs/generate.test.ts index 7dab0a8047..1a6316f564 100644 --- a/packages/cli/src/cli/commands/docs/generate.test.ts +++ b/packages/cli/src/cli/commands/docs/generate.test.ts @@ -1,8 +1,15 @@ import {CommandData, CommandWithMarkdown, extractCommandData, writeCommandFlagInterface} from './generate.js' -import {writeFile} from '@shopify/cli-kit/node/fs' +import {inTemporaryDirectory, readFile} from '@shopify/cli-kit/node/fs' +import {cwd, joinPath} from '@shopify/cli-kit/node/path' import {describe, test, vi, expect} from 'vitest' -vi.mock('@shopify/cli-kit/node/fs') +vi.mock('@shopify/cli-kit/node/path', async (importOriginal) => { + const actual = await importOriginal() + return { + ...actual, + cwd: vi.fn(), + } +}) const testCommand: CommandWithMarkdown = { aliases: [], @@ -45,12 +52,18 @@ describe('extractCommandData', () => { }) describe('writeCommandFlagInterface', () => { - test('calls writeFile with the correct content', async () => { - await writeCommandFlagInterface(testCommand, commandData) + test('writes the correct content to the file', async () => { + await inTemporaryDirectory(async (tmpDir) => { + // Given + vi.mocked(cwd).mockReturnValue(tmpDir) + const filePath = joinPath(tmpDir, 'docs-shopify.dev/commands/interfaces/topic-test-command.interface.ts') + + // When + await writeCommandFlagInterface(testCommand, commandData) - expect(writeFile).toHaveBeenCalledWith( - expect.stringContaining('docs-shopify.dev/commands/interfaces/topic-test-command.interface.ts'), - `// This is an autogenerated file. Don't edit this file manually. + // Then + const content = await readFile(filePath) + expect(content).toBe(`// This is an autogenerated file. Don't edit this file manually. /** * The following flags are available for the \`topic test-command\` command: * @publicDocs @@ -68,16 +81,22 @@ export interface topictestcommand { */ '-a, --flag2'?: '' } -`, - ) +`) + }) }) - test('calls writeFile with the correct content for a command with no flags', async () => { - await writeCommandFlagInterface({...testCommand, flags: {}}, commandData) + test('writes the correct content for a command with no flags', async () => { + await inTemporaryDirectory(async (tmpDir) => { + // Given + vi.mocked(cwd).mockReturnValue(tmpDir) + const filePath = joinPath(tmpDir, 'docs-shopify.dev/commands/interfaces/topic-test-command.interface.ts') + + // When + await writeCommandFlagInterface({...testCommand, flags: {}}, commandData) - expect(writeFile).toHaveBeenCalledWith( - expect.stringContaining('docs-shopify.dev/commands/interfaces/topic-test-command.interface.ts'), - `// This is an autogenerated file. Don't edit this file manually. + // Then + const content = await readFile(filePath) + expect(content).toBe(`// This is an autogenerated file. Don't edit this file manually. /** * The following flags are available for the \`topic test-command\` command: * @publicDocs @@ -85,7 +104,7 @@ export interface topictestcommand { export interface topictestcommand { } -`, - ) +`) + }) }) }) diff --git a/packages/cli/src/cli/commands/docs/generate.ts b/packages/cli/src/cli/commands/docs/generate.ts index b90da1a97d..653d9cb586 100644 --- a/packages/cli/src/cli/commands/docs/generate.ts +++ b/packages/cli/src/cli/commands/docs/generate.ts @@ -3,7 +3,7 @@ import {Command as oclifCommand} from '@oclif/core' import {mkdir, rmdir, writeFile} from '@shopify/cli-kit/node/fs' import {cwd, joinPath} from '@shopify/cli-kit/node/path' -const docsPath = joinPath(cwd(), '/docs-shopify.dev/commands') +const docsPath = () => joinPath(cwd(), '/docs-shopify.dev/commands') export type CommandWithMarkdown = oclifCommand.Loadable & {descriptionWithMarkdown?: string} @@ -21,8 +21,8 @@ export default class DocsGenerate extends Command { const commands = this.config.commands as CommandWithMarkdown[] // Remove all files and recreate the folder. To make sure we don't leave any orphaned files. - await rmdir(docsPath) - await mkdir(docsPath) + await rmdir(docsPath()) + await mkdir(docsPath()) // Short by length to ensure that we first generate the interfaces for the parent topics to detect hidden ones. const sortedCommands = commands @@ -96,6 +96,6 @@ export interface ${interfaceName} { ${flagsDetails} } ` - await mkdir(`${docsPath}/interfaces`) - await writeFile(`${docsPath}/interfaces/${fileName}.interface.ts`, commandContent) + await mkdir(joinPath(docsPath(), 'interfaces')) + await writeFile(joinPath(docsPath(), 'interfaces', `${fileName}.interface.ts`), commandContent) }