Skip to content

Commit

Permalink
Merge pull request #247 from semantic-release/fix/asset-publish
Browse files Browse the repository at this point in the history
Fix/asset-publish
  • Loading branch information
seebeen authored Sep 21, 2024
2 parents c56dcaf + 59825fb commit af008dc
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 20 deletions.
2 changes: 1 addition & 1 deletion lib/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export async function publish(
const zipResult = config.withAssets
? await execa(
zipCommand,
['-qjr', path.join(releaseDir, `assets.zip`), assetDir],
['-qr', path.join(releaseDir, `assets.zip`), '.'],
{
cwd: assetDir,
timeout: 30 * 1000,
Expand Down
20 changes: 20 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"devDependencies": {
"@babel/core": "7.24.7",
"@babel/preset-env": "7.24.7",
"@types/adm-zip": "^0.5.5",
"@types/fs-extra": "11.0.4",
"@types/jest": "29.5.12",
"@types/node": "20.14.7",
Expand All @@ -54,6 +55,7 @@
"@types/sinon": "17.0.3",
"@typescript-eslint/eslint-plugin": "6.21.0",
"@typescript-eslint/parser": "6.21.0",
"adm-zip": "^0.5.16",
"babel-jest": "29.7.0",
"eslint": "8.56.0",
"eslint-config-prettier": "9.1.0",
Expand Down
15 changes: 10 additions & 5 deletions test/2-prepare-plugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,17 +212,22 @@ describe('Package preparation - default work directory', () => {
contexts.prepareContext,
);

const assets = fs.readdirSync(path.join(releasePath, 'assets'));
const assets = new Set(
fs.readdirSync(path.join(releasePath, 'assets'), {
recursive: true,
}) as string[],
);

expect(assets).toHaveLength(5);
expect(assets.sort()).toStrictEqual(
[
expect([...assets]).toHaveLength(6);
expect(assets).toEqual(
new Set([
'blueprints',
'blueprints/blueprint.json',
'banner-low.jpg',
'banner-high.jpg',
'screenshot-1.jpg',
'screenshot-2.jpg',
].sort(),
]),
);
});
});
56 changes: 42 additions & 14 deletions test/3-publish-plugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { replaceVersions } from '../lib/utils/replace-versions.js';
import { success } from '../lib/success.js';
import SemanticReleaseError from '@semantic-release/error';
import { publish } from '../lib/publish.js';
import AdmZip, { IZipEntry } from 'adm-zip';

const pluginConfig: PluginConfig = {
type: 'plugin',
Expand All @@ -21,12 +22,37 @@ const pluginConfig: PluginConfig = {
workDir: 'publish',
};

let releasePath: string;
let wDir: string;
const env = process.env;

function readZip(dir: string, file: string, pfx: RegExp = /.^/): Set<string> {
return new Set(
new AdmZip(path.join(dir, file))
.getEntries()
.map(({ entryName }) => entryName.replace(pfx, '').replace(/\/$/, ''))
.filter((e) => e !== '' && (pfx.source == '.^' || !e.match(/\//))),
);
}

function readDir(
root: string,
dir: string,
recursive: boolean = false,
): Set<string> {
return new Set(
fs.readdirSync(path.join(root, dir), {
recursive,
}) as string[],
);
}

function readFile(root: string, file: string): string {
return fs.readFileSync(path.join(root, file), 'utf8');
}

beforeAll(async () => {
releasePath = fs.mkdtempSync('/tmp/wp-release-');
pluginConfig.releasePath = releasePath;
wDir = fs.mkdtempSync('/tmp/wp-release-');
pluginConfig.releasePath = wDir;
});

beforeEach(() => {
Expand All @@ -51,28 +77,30 @@ afterEach(async () => {
});

afterAll(async () => {
fs.removeSync(releasePath);
fs.removeSync(wDir);
});

describe('Publish step', () => {
it('Should package a complete plugin', async () => {
it('Should zip a complete plugin properly', async () => {
await prepare(pluginConfig, contexts.publishContext);
await publish(pluginConfig, contexts.publishContext);

const distFolder = fs
.readdirSync(path.join(releasePath, 'dist-test'))
.join(' ');
expect(readFile(path.join(wDir, 'dist-test'), 'readme.txt')).toMatch(
/^Stable tag: 1.0.0$/gm,
);
expect(readZip(wDir, 'package.zip', /^dist-test\//)).toEqual(
readDir(wDir, 'dist-test'),
);
expect(readZip(wDir, 'assets.zip')).toEqual(readDir(wDir, 'assets', true));
expect(readFile(wDir, 'VERSION')).toEqual('1.0.0');

expect(distFolder).not.toContain('node_modules');
expect(distFolder).toContain('vendor');
expect(distFolder).toContain('dist-test.php');
expect(distFolder).toContain('test1.php');
// expect readZip(releasePath, 'assets.zip');
});

it('Should should remove folders on success', async () => {
await success(pluginConfig, contexts.publishContext);

const files = fs.readdirSync(releasePath).join(' ');
const files = fs.readdirSync(wDir).join(' ');

expect(files).toContain('package.zip');
expect(files).toContain('assets.zip');
Expand All @@ -85,7 +113,7 @@ describe('Publish step', () => {
try {
await prepare(pluginConfig, contexts.publishContext);

await fs.remove(path.join(releasePath, 'assets'));
await fs.remove(path.join(wDir, 'assets'));
await publish(pluginConfig, contexts.publishContext);
} catch (err) {
expect((err as SemanticReleaseError).code).toMatch(/(ENOENT|EZIP)/);
Expand Down

0 comments on commit af008dc

Please sign in to comment.