Skip to content

Commit

Permalink
fix(app-builder): support slashes in archive filename
Browse files Browse the repository at this point in the history
  • Loading branch information
EdieLemoine committed Oct 24, 2024
1 parent 3a0c317 commit 870d00a
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 27 deletions.
21 changes: 21 additions & 0 deletions apps/app-builder/src/__tests__/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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': '<?php return [];',
},
src: {
Pdk: {
'MyParcelNLController.php': '<?php echo "Hello from MyParcelNL";',
},
'index.php': '<?php echo "Version: 1.0.0";',
},
'composer.json': JSON.stringify({
name: 'myparcelnl/app',
version: '1.0.0',
}),
'package.json': JSON.stringify({
name: '@myparcel/app',
version: '1.0.0',
}),
'readme.txt': 'Hello world!',
});
27 changes: 3 additions & 24 deletions apps/app-builder/src/__tests__/mockFileSystem.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,9 @@
import path from 'path';
import fs from 'fs';
import {type TestContext, vi} from 'vitest';
import {type TaskContext, vi} from 'vitest';
import {isObject, merge} from 'lodash-unified';
import {exists} from '../utils/fs/exists';
import {MOCK_ROOT_DIR} from './constants';

const DEFAULT_FILE_SYSTEM = Object.freeze({
config: {
'pdk.php': '<?php return [];',
},
src: {
Pdk: {
'MyParcelNLController.php': '<?php echo "Hello from MyParcelNL";',
},
'index.php': '<?php echo "Version: 1.0.0";',
},
'composer.json': JSON.stringify({
name: 'myparcelnl/app',
version: '1.0.0',
}),
'package.json': JSON.stringify({
name: '@myparcel/app',
version: '1.0.0',
}),
'readme.txt': 'Hello world!',
});
import {DEFAULT_FILE_SYSTEM, MOCK_ROOT_DIR} from './constants';

const recursiveCreate = async (entries: Record<string, unknown>, rootDir: string): Promise<unknown[]> => {
return Promise.all(
Expand All @@ -47,7 +26,7 @@ const recursiveCreate = async (entries: Record<string, unknown>, rootDir: string
);
};

export const mockFileSystem = async (ctx: TestContext, fileSystem?: Record<string, unknown>): Promise<string> => {
export const mockFileSystem = async (ctx: TaskContext, fileSystem?: Record<string, unknown>): Promise<string> => {
const rootDir = path.resolve(MOCK_ROOT_DIR, ctx.task.id);

await recursiveCreate(merge({}, DEFAULT_FILE_SYSTEM, fileSystem), rootDir);
Expand Down
Original file line number Diff line number Diff line change
@@ -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<string, unknown>,
config?: RecursivePartial<PdkBuilderContext>,
): Promise<PdkBuilderContext> => {
Expand Down
87 changes: 87 additions & 0 deletions apps/app-builder/src/commands/zip.spec.ts
Original file line number Diff line number Diff line change
@@ -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<string, unknown> = {};
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);
});
});
3 changes: 2 additions & 1 deletion apps/app-builder/src/commands/zip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 870d00a

Please sign in to comment.