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

Render warning when using folder/ directory pattern #5251

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
109 changes: 109 additions & 0 deletions packages/theme/src/cli/utilities/asset-ignore.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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 {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')
Expand All @@ -12,6 +13,10 @@ vi.mock('@shopify/cli-kit/node/fs', async () => {
}
})

vi.mock('@shopify/cli-kit/node/ui', () => ({
renderWarning: vi.fn(),
}))

vi.mock('@shopify/cli-kit/node/output')

describe('asset-ignore', () => {
Expand Down Expand Up @@ -284,4 +289,108 @@ describe('asset-ignore', () => {
])
})
})
describe('applyIgnoreFilters with only options', () => {
test(`should return single file when only option is a single file`, () => {
const options = {
only: ['assets/basic.css'],
}

const actualChecksums = applyIgnoreFilters(checksums, options)

expect(actualChecksums).toEqual([{key: 'assets/basic.css', checksum: '00000000000000000000000000000000'}])
})

test(`should return all files in a directory matching the pattern`, () => {
const options = {
only: ['assets/*.css'],
}

const actualChecksums = applyIgnoreFilters(checksums, options)

expect(actualChecksums).toEqual([
{key: 'assets/basic.css', checksum: '00000000000000000000000000000000'},
{key: 'assets/complex.css', checksum: '11111111111111111111111111111111'},
])
})

test(`should return all files in a directory when using proper glob pattern`, () => {
const options = {
only: ['templates/*'],
}

const actualChecksums = applyIgnoreFilters(checksums, options)

expect(actualChecksums).toEqual([
{key: 'templates/404.json', checksum: '6666666666666666666666666666666'},
{key: 'templates/customers/account.json', checksum: '7777777777777777777777777777777'},
])
})
})

describe('applyIgnoreFilters with ignore options', () => {
test(`should ignore single file when ignore option is a single file`, () => {
const options = {
ignore: ['assets/basic.css'],
}

const actualChecksums = applyIgnoreFilters(checksums, options)

expect(actualChecksums).toEqual([
{key: 'assets/complex.css', checksum: '11111111111111111111111111111111'},
{key: 'assets/image.png', checksum: '22222222222222222222222222222222'},
{key: 'config/settings_data.json', checksum: '33333333333333333333333333333333'},
{key: 'config/settings_schema.json', checksum: '44444444444444444444444444444444'},
{key: 'sections/announcement-bar.liquid', checksum: '55555555555555555555555555555555'},
{key: 'templates/404.json', checksum: '6666666666666666666666666666666'},
{key: 'templates/customers/account.json', checksum: '7777777777777777777777777777777'},
])
})

test(`should ignore all files in a directory matching the pattern`, () => {
const options = {
ignore: ['assets/*.css'],
}

const actualChecksums = applyIgnoreFilters(checksums, options)

expect(actualChecksums).toEqual([
{key: 'assets/image.png', checksum: '22222222222222222222222222222222'},
{key: 'config/settings_data.json', checksum: '33333333333333333333333333333333'},
{key: 'config/settings_schema.json', checksum: '44444444444444444444444444444444'},
{key: 'sections/announcement-bar.liquid', checksum: '55555555555555555555555555555555'},
{key: 'templates/404.json', checksum: '6666666666666666666666666666666'},
{key: 'templates/customers/account.json', checksum: '7777777777777777777777777777777'},
])
})

test(`should ignore all files in a directory when using proper glob pattern`, () => {
const options = {
ignore: ['assets/*'],
}

const actualChecksums = applyIgnoreFilters(checksums, options)

expect(actualChecksums).toEqual([
{key: 'config/settings_data.json', checksum: '33333333333333333333333333333333'},
{key: 'config/settings_schema.json', checksum: '44444444444444444444444444444444'},
{key: 'sections/announcement-bar.liquid', checksum: '55555555555555555555555555555555'},
{key: 'templates/404.json', checksum: '6666666666666666666666666666666'},
{key: 'templates/customers/account.json', checksum: '7777777777777777777777777777777'},
])
})
})
describe('pattern warnings', () => {
test('should warn when a pattern ends with a slash', () => {
const options = {
ignore: ['assets/'],
}

applyIgnoreFilters(checksums, options)

expect(renderWarning).toHaveBeenCalledWith({
headline: 'Directory pattern may be misleading.',
body: 'For more reliable matching, consider using assets/* or assets/*.filename instead.',
})
})
})
})
15 changes: 13 additions & 2 deletions packages/theme/src/cli/utilities/asset-ignore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import {uniqBy} from '@shopify/cli-kit/common/array'
import {fileExists, readFile, matchGlob as originalMatchGlob} from '@shopify/cli-kit/node/fs'
import {outputDebug} from '@shopify/cli-kit/node/output'
import {joinPath} from '@shopify/cli-kit/node/path'
import {renderWarning} from '@shopify/cli-kit/node/ui'

const SHOPIFY_IGNORE = '.shopifyignore'
const templatesRegex = /templates\/\*(\.(json|liquid))?$/
const warnedPatterns = new Set<string>()

export function applyIgnoreFilters<T extends {key: string}>(
files: T[],
Expand Down Expand Up @@ -83,9 +85,18 @@ function matchGlob(key: string, pattern: string) {
noglobstar: true,
}

const result = originalMatchGlob(key, pattern, matchOpts)
if (originalMatchGlob(key, pattern, matchOpts)) return true

if (result) return true
if (isRegex(pattern)) return regexMatch(key, asRegex(pattern))

if (!pattern.includes('*') && pattern.endsWith('/') && !warnedPatterns.has(pattern)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we want to present this warning only when the flag has a noop behavior. When I try the following command, it works (via the regex matcher), but I still get this warning.

shopify theme pull -d -f --verbose --only /templates/

warnedPatterns.add(pattern)
renderWarning({
headline: 'Directory pattern may be misleading.',
body: `For more reliable matching, consider using ${pattern}* or ${pattern}*.filename instead.`,
})
return false
}

// When the the standard match fails and the pattern includes '/*.', we
// replace '/*.' with '/**/*.' to emulate Shopify CLI 2.x behavior, as it was
Expand Down
Loading