Skip to content
Draft
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
66 changes: 25 additions & 41 deletions packages/theme/src/cli/utilities/asset-ignore.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
import {applyIgnoreFilters, getPatternsFromShopifyIgnore} from './asset-ignore.js'
import {ReadOptions, fileExists, readFile} from '@shopify/cli-kit/node/fs'
import {test, describe, beforeEach, vi, expect} from 'vitest'
import {inTemporaryDirectory, writeFile} from '@shopify/cli-kit/node/fs'
import {test, describe, vi, expect} from 'vitest'
import {renderWarning} from '@shopify/cli-kit/node/ui'

vi.mock('@shopify/cli-kit/node/fs', async () => {
const originalFs: any = await vi.importActual('@shopify/cli-kit/node/fs')
return {
...originalFs,
matchGlob: originalFs.matchGlob,
readFile: vi.fn(),
fileExists: vi.fn(),
}
})

vi.mock('@shopify/cli-kit/node/ui', () => ({
renderWarning: vi.fn(),
}))
Expand All @@ -31,47 +21,41 @@ describe('asset-ignore', () => {
{key: 'templates/customers/account.json', checksum: '7777777777777777777777777777777'},
]

/**
* Explicitly defining the type of 'readFile' as it returns either
* 'Promise<string>' or 'Promise<Buffer>', which are ambiguous to the
* TypeScript language server.
*/
const readFileFn = readFile as (path: string, options?: ReadOptions) => Promise<string>

beforeEach(() => {
vi.mocked(fileExists).mockResolvedValue(true)
vi.mocked(readFileFn).mockResolvedValue('')
})

describe('getPatternsFromShopifyIgnore', () => {
test('returns an empty array when the .shopifyignore file does not exist', async () => {
// Given
vi.mocked(fileExists).mockResolvedValue(false)
await expect(getPatternsFromShopifyIgnore('tmp')).resolves.toEqual([])
await inTemporaryDirectory(async (tmpDir) => {
// Given
// No file created

// When/Then
await expect(getPatternsFromShopifyIgnore(tmpDir)).resolves.toEqual([])
})
})

test('returns an empty array when the .shopifyignore file does not exist', async () => {
vi.mocked(fileExists).mockResolvedValue(true)
vi.mocked(readFileFn).mockResolvedValue(`
test('returns the patterns when the .shopifyignore file exists', async () => {
await inTemporaryDirectory(async (tmpDir) => {
// Given
const content = `
# assets/basic.css
assets/complex.css
assets/*.png
sections/*
templates/*
config/*_data.json
.*settings_schema.json
`)

await expect(getPatternsFromShopifyIgnore('tmp')).resolves.toEqual([
'assets/complex.css',
'assets/*.png',
'sections/*',
'templates/*',
'config/*_data.json',
'.*settings_schema.json',
])
`
await writeFile(`${tmpDir}/.shopifyignore`, content)

expect(readFileFn).toHaveBeenLastCalledWith('tmp/.shopifyignore', {encoding: 'utf8'})
// When/Then
await expect(getPatternsFromShopifyIgnore(tmpDir)).resolves.toEqual([
'assets/complex.css',
'assets/*.png',
'sections/*',
'templates/*',
'config/*_data.json',
'.*settings_schema.json',
])
})
})
})

Expand Down
Loading