From 3e7ebf1634e40b500e91c321542d62f2f9bf03f4 Mon Sep 17 00:00:00 2001 From: Sibin Grasic Date: Wed, 18 Sep 2024 18:40:26 +0200 Subject: [PATCH] fix: Fixed nested globbing --- lib/utils/copy-assets.ts | 7 +- lib/utils/copy-files.ts | 12 ++-- test/0-corner-cases.spec.ts | 43 ++++++++++-- test/2-prepare-plugin.spec.ts | 3 +- test/2-prepare-theme.spec.ts | 9 ++- .../assets/blueprints/blueprint.json | 65 +++++++++++++++++++ test/fixtures/nested-exclude/.distignore | 3 + .../nested-exclude/assets/ignore-this.js | 0 .../nested-exclude/assets/need-this.js | 0 .../nested-exclude/assets/src/file.css | 0 test/fixtures/nested-exclude/example.js | 0 test/fixtures/nested-exclude/file.php | 0 12 files changed, 127 insertions(+), 15 deletions(-) create mode 100644 test/fixtures/dist-test/.wordpress-org/assets/blueprints/blueprint.json create mode 100644 test/fixtures/nested-exclude/.distignore create mode 100644 test/fixtures/nested-exclude/assets/ignore-this.js create mode 100644 test/fixtures/nested-exclude/assets/need-this.js create mode 100644 test/fixtures/nested-exclude/assets/src/file.css create mode 100644 test/fixtures/nested-exclude/example.js create mode 100644 test/fixtures/nested-exclude/file.php diff --git a/lib/utils/copy-assets.ts b/lib/utils/copy-assets.ts index c73192b1..eb8811d7 100644 --- a/lib/utils/copy-assets.ts +++ b/lib/utils/copy-assets.ts @@ -11,7 +11,12 @@ export async function copyAssets( const basePath = path.resolve(path.join(workDir, '.wordpress-org', 'assets')); const assets = await glob( - ['screenshot*', 'banner-*'].map((g) => `${g}.{jpg,png,gif,jpeg}`), + [ + ...['icon-*', 'screenshot*', 'banner-*'].map( + (g) => `${g}.{jpg,png,gif,jpeg}`, + ), + 'blueprints/**.json', + ], { cwd: basePath, }, diff --git a/lib/utils/copy-files.ts b/lib/utils/copy-files.ts index ae428845..76bbb95a 100644 --- a/lib/utils/copy-files.ts +++ b/lib/utils/copy-files.ts @@ -28,7 +28,11 @@ export function getInclude(workDir: string, files?: string[]): string[] { ...new Set(getFileArray(workDir, '.distinclude') ?? files ?? []), ]; - return include.length !== 0 ? include : ['**/*']; + return include.length > 0 + ? include + .filter((p) => p.includes('*') || fs.existsSync(path.join(workDir, p))) + .map((p) => remapGlobs(workDir, p)) + : ['**']; } /** @@ -51,8 +55,7 @@ export function getIgnore(workDir: string, files?: string[]): string[] { .filter( (ignorePath) => ignorePath.includes('*') || - (!ignorePath.includes('*') && - fs.existsSync(path.join(workDir, ignorePath))), + fs.existsSync(path.join(workDir, ignorePath)), ) .map((ignorePath) => remapGlobs(workDir, ignorePath)); } @@ -67,7 +70,8 @@ export async function copyFiles( const files = await glob(getInclude(workDir, config.include), { cwd: workDir, ignore: getIgnore(workDir, config.exclude), - // maxDepth: 1, + nodir: true, + mark: true, }); await fs.mkdir(releasePath, { recursive: true }); diff --git a/test/0-corner-cases.spec.ts b/test/0-corner-cases.spec.ts index 1a2127e2..574dc6a5 100644 --- a/test/0-corner-cases.spec.ts +++ b/test/0-corner-cases.spec.ts @@ -89,16 +89,51 @@ describe('Corner cases affecting releases', () => { const ignore1 = DEFAULT_EXCLUDES; const include2 = getInclude(workDir, ['dist-test.php']); const ignore2 = getIgnore(workDir); - const expected = ['dist-test.php', 'test1.php', 'vendor'].sort(); + const expected = [ + 'dist-test.php', + 'test1.php', + 'vendor/composer.php', + ].sort(); - expect(include1.sort()).toEqual(['dist-test.php', 'test1.php', 'vendor']); + expect(include1.sort()).toEqual([ + 'dist-test.php', + 'test1.php', + 'vendor/**', + ]); expect( ( - await glob(include1, { cwd: workDir, ignore: ignore1, maxDepth: 1 }) + await glob(include1, { + cwd: workDir, + ignore: ignore1, + nodir: true, + mark: true, + }) ).sort(), ).toEqual(expected); expect( - (await glob(include2, { cwd: workDir, ignore: ignore2 })).sort(), + ( + await glob(include2, { + cwd: workDir, + ignore: ignore2, + nodir: true, + mark: true, + }) + ).sort(), ).toEqual(['dist-test.php']); }); + + it('Should glob properly with nesting', async () => { + const workDir = path.resolve('./test/fixtures/nested-exclude'); + + expect( + ( + await glob(getInclude(workDir), { + cwd: workDir, + ignore: getIgnore(workDir), + nodir: true, + mark: true, + }) + ).sort(), + ).toEqual(['assets/need-this.js', 'file.php']); + }); }); diff --git a/test/2-prepare-plugin.spec.ts b/test/2-prepare-plugin.spec.ts index 9505dedd..19900954 100644 --- a/test/2-prepare-plugin.spec.ts +++ b/test/2-prepare-plugin.spec.ts @@ -217,9 +217,10 @@ describe('Package preparation - default work directory', () => { const assets = fs.readdirSync(path.join(releasePath, 'assets')); - expect(assets).toHaveLength(4); + expect(assets).toHaveLength(5); expect(assets.sort()).toStrictEqual( [ + 'blueprints', 'banner-low.jpg', 'banner-high.jpg', 'screenshot-1.jpg', diff --git a/test/2-prepare-theme.spec.ts b/test/2-prepare-theme.spec.ts index dcfd0ab5..abee84d2 100644 --- a/test/2-prepare-theme.spec.ts +++ b/test/2-prepare-theme.spec.ts @@ -41,6 +41,10 @@ beforeEach(() => { afterEach(async () => { fs.rmSync('/tmp/workDir', { recursive: true, force: true }); fs.rmSync('/tmp/wp-release', { recursive: true, force: true }); + fs.rmSync('./test/fixtures/complete-theme-copy', { + recursive: true, + force: true, + }); }); it('Should fail on a badly versioned theme', async () => { @@ -131,9 +135,4 @@ it('Should fully prepare a theme for release', async () => { ); expect(versions).toMatch(/1\.0\.0/); expect(versions).toMatch(/3\.2\.111/); - - await fs.rm('./test/fixtures/complete-theme-copy', { - recursive: true, - force: true, - }); }); diff --git a/test/fixtures/dist-test/.wordpress-org/assets/blueprints/blueprint.json b/test/fixtures/dist-test/.wordpress-org/assets/blueprints/blueprint.json new file mode 100644 index 00000000..a77346ca --- /dev/null +++ b/test/fixtures/dist-test/.wordpress-org/assets/blueprints/blueprint.json @@ -0,0 +1,65 @@ +{ + "landingPage": "/wp-admin/index.php", + "preferredVersions": { + "php": "8.0", + "wp": "6.4" + }, + "phpExtensionBundles": ["kitchen-sink"], + "steps": [ + { + "step": "login", + "username": "admin", + "password": "password" + }, + { + "step": "setSiteOptions", + "options": { + "WPLANG": "sr_RS", + "wcsrb_settings_general": "a:3:{s:22:\"enabled_customer_types\";s:4:\"both\";s:22:\"remove_unneeded_fields\";s:3:\"yes\";s:19:\"fix_currency_symbol\";s:3:\"yes\";}", + "woocommerce_store_name": "Oblak Solutions DOO", + "woocommerce_store_address": "Pozeska", + "woocommerce_store_city": "Beograd", + "woocommerce_store_postcode": "11000", + "woocommerce_default_country": "RS:RS00", + "woocommerce_store_bank_accounts": { + "acct": ["160-428634-06"] + }, + "woocommerce_currency": "RSD", + "woocommerce_currency_pos": "right_space", + "woocommerce_wcsrb_payment_slip_settings": { + "enabled": "yes", + "title": "Op\u0161ta uplatnica", + "description": "Platite narud\u017ebinu op\u0161tom uplatnicom", + "basic": "", + "style": "modern", + "bank_account": "160-428634-06", + "payment_code": "auto", + "payment_model": "mod97", + "payment_reference": "%mod97%-%order_id%-%year%", + "payment_purpose": "Pla\u0107anje narud\u017ebine", + "qrcode": "", + "qrcode_shown": "yes", + "qrcode_color": "#000000", + "qrcode_corner_color": "#0e77c0", + "qrcode_image": "no", + "advanced": "", + "debug": "no" + } + } + }, + { + "step": "installPlugin", + "pluginZipFile": { + "resource": "wordpress.org/plugins", + "slug": "woocommerce" + }, + "options": { + "activate": true + } + }, + { + "step": "runPHP", + "code": "