From 870d00aa442be73dd331677b09de0fd44f745ca9 Mon Sep 17 00:00:00 2001 From: Edie Lemoine Date: Thu, 24 Oct 2024 14:38:37 +0200 Subject: [PATCH] fix(app-builder): support slashes in archive filename --- apps/app-builder/src/__tests__/constants.ts | 21 +++++ .../src/__tests__/mockFileSystem.ts | 27 +----- .../mockFileSystemAndCreateContext.ts | 4 +- apps/app-builder/src/commands/zip.spec.ts | 87 +++++++++++++++++++ apps/app-builder/src/commands/zip.ts | 3 +- 5 files changed, 115 insertions(+), 27 deletions(-) create mode 100644 apps/app-builder/src/commands/zip.spec.ts diff --git a/apps/app-builder/src/__tests__/constants.ts b/apps/app-builder/src/__tests__/constants.ts index c9a511f4f..04f115d41 100644 --- a/apps/app-builder/src/__tests__/constants.ts +++ b/apps/app-builder/src/__tests__/constants.ts @@ -3,3 +3,24 @@ import path from 'path'; const dirname = new URL('.', import.meta.url).pathname; export const MOCK_ROOT_DIR = path.resolve(dirname, '../..', '.test'); + +export const DEFAULT_FILE_SYSTEM = Object.freeze({ + config: { + 'pdk.php': ', rootDir: string): Promise => { return Promise.all( @@ -47,7 +26,7 @@ const recursiveCreate = async (entries: Record, rootDir: string ); }; -export const mockFileSystem = async (ctx: TestContext, fileSystem?: Record): Promise => { +export const mockFileSystem = async (ctx: TaskContext, fileSystem?: Record): Promise => { const rootDir = path.resolve(MOCK_ROOT_DIR, ctx.task.id); await recursiveCreate(merge({}, DEFAULT_FILE_SYSTEM, fileSystem), rootDir); diff --git a/apps/app-builder/src/__tests__/mockFileSystemAndCreateContext.ts b/apps/app-builder/src/__tests__/mockFileSystemAndCreateContext.ts index f1f62ce06..a9207d1f9 100644 --- a/apps/app-builder/src/__tests__/mockFileSystemAndCreateContext.ts +++ b/apps/app-builder/src/__tests__/mockFileSystemAndCreateContext.ts @@ -1,11 +1,11 @@ -import {type TestContext} from 'vitest'; +import {type TaskContext} from 'vitest'; import {type RecursivePartial} from '@myparcel/ts-utils'; import {type PdkBuilderContext} from '../types/command'; import {mockFileSystem} from './mockFileSystem'; import {createTestContextWithMockedFs} from './createTestContextWithMockedFs'; export const mockFileSystemAndCreateContext = async ( - ctx: TestContext, + ctx: TaskContext, fileSystem?: Record, config?: RecursivePartial, ): Promise => { diff --git a/apps/app-builder/src/commands/zip.spec.ts b/apps/app-builder/src/commands/zip.spec.ts new file mode 100644 index 000000000..7e809efcb --- /dev/null +++ b/apps/app-builder/src/commands/zip.spec.ts @@ -0,0 +1,87 @@ +import path from 'path'; +import fs from 'fs'; +import {describe, expect, it} from 'vitest'; +import {resolveString} from '../utils/resolveString'; +import {type PdkBuilderContext} from '../types/command'; +import {PdkPlatformName} from '../constants'; +import {fsModifyingMethodSpies} from '../__tests__/spies/fs'; +import {mockFileSystemAndCreateContext} from '../__tests__/mockFileSystemAndCreateContext'; +import {mockFileSystem} from '../__tests__/mockFileSystem'; +import {DEFAULT_FILE_SYSTEM} from '../__tests__/constants'; +import zip from './zip'; + +const mockDistDirectoryFileSystem = (context: PdkBuilderContext) => { + const outDir: Record = {}; + const name = resolveString(context.config.name, context); + + context.config.platforms.forEach((platform) => { + outDir[`${platform}-${name}`] = DEFAULT_FILE_SYSTEM; + }); + + return { + [context.config.outDir]: outDir, + }; +}; + +describe('command: zip', () => { + it('does nothing when dry run is passed', async (ctx) => { + expect.assertions(fsModifyingMethodSpies.length); + + const context = await mockFileSystemAndCreateContext(ctx, undefined, {args: {dryRun: true}}); + await mockFileSystem(ctx, mockDistDirectoryFileSystem(context)); + + await zip(context); + + fsModifyingMethodSpies.forEach((spy) => { + expect(spy).not.toHaveBeenCalled(); + }); + }); + + it.for([ + { + version: '1.0.0', + archiveFilename: '{{platform}}-{{name}}-{{version}}.zip', + expectedFilename: '[PLATFORM]-test-1.0.0.zip', + }, + { + version: '3.0.0-beta', + archiveFilename: '{{platform}}-{{name}}-{{version}}.zip', + expectedFilename: '[PLATFORM]-test-3.0.0-beta.zip', + }, + { + version: 'dev-123-fix/fix-some-bug', + archiveFilename: '{{platform}}-{{name}}-{{version}}.zip', + expectedFilename: '[PLATFORM]-test-dev-123-fix-fix-some-bug.zip', + }, + ])('zips files', async ({version, archiveFilename, expectedFilename}, ctx) => { + expect.assertions(1); + + const platforms = [PdkPlatformName.MyParcelBe, PdkPlatformName.MyParcelNl]; + + const context = await mockFileSystemAndCreateContext(ctx, undefined, { + args: {dryRun: false, version}, + config: { + archiveFilename, + platforms, + }, + }); + + await mockFileSystem(ctx, mockDistDirectoryFileSystem(context)); + + await zip(context); + + const outDir = path.resolve(context.env.cwd, 'dist'); + + const platformDirsAndZips = platforms + .map((platform) => { + const zipFilename = expectedFilename.replace('[PLATFORM]', platform); + + return [`${platform}-test`, zipFilename]; + }) + .flat(); + + const outDirContents = await fs.promises.readdir(outDir); + + expect(outDirContents).toEqual(platformDirsAndZips); + }); +}); diff --git a/apps/app-builder/src/commands/zip.ts b/apps/app-builder/src/commands/zip.ts index 4866ce9cf..8a1bb7c14 100644 --- a/apps/app-builder/src/commands/zip.ts +++ b/apps/app-builder/src/commands/zip.ts @@ -27,7 +27,8 @@ const zip: PdkBuilderCommand = async (context) => { return; } - const archivePath = resolvePath([config.outDir, config.archiveFilename], platformContext); + const archiveFilename = resolveString(config.archiveFilename, platformContext).replace(/\//g, '-'); + const archivePath = resolvePath([config.outDir, archiveFilename], platformContext); await rmFile(archivePath, context);