Skip to content

Commit 18f1371

Browse files
committed
chore(Tests): Improved tests
1 parent 59825fb commit 18f1371

File tree

3 files changed

+51
-74
lines changed

3 files changed

+51
-74
lines changed

test/2-prepare-plugin.spec.ts

Lines changed: 16 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import fs from 'fs-extra';
44
import { prepare } from '../lib/prepare.js';
55
import * as contexts from './get-context.js';
66
import { replaceVersions } from '../lib/utils/replace-versions.js';
7+
import { readDir, readFile } from './utils.js';
78

89
function getWorkDir(suffix: string): string {
910
const files = fs.readdirSync(os.tmpdir());
@@ -40,11 +41,9 @@ describe('Package preparation - cusom work directory', () => {
4041
},
4142
contexts.prepareContext,
4243
);
43-
expect(
44-
fs
45-
.readFileSync(path.join(releasePath, 'plugin1', 'plugin1.php'), 'utf8')
46-
.toString(),
47-
).not.toMatch(/0\.0\.0/);
44+
expect(readFile(releasePath, 'plugin1', 'plugin1.php')).not.toMatch(
45+
/0\.0\.0/,
46+
);
4847
});
4948

5049
it('Should fail on invalid plugin version', async () => {
@@ -79,9 +78,10 @@ describe('Package preparation - cusom work directory', () => {
7978
contexts.prepareContext,
8079
);
8180

82-
const versions = fs.readFileSync(
83-
path.join(getWorkDir(workDir), 'plugin1/extra-versions.php'),
84-
'utf8',
81+
const versions = readFile(
82+
getWorkDir(workDir),
83+
'plugin1',
84+
'extra-versions.php',
8585
);
8686

8787
expect(versions).toMatch(/1\.0\.0/);
@@ -102,12 +102,9 @@ describe('Package preparation - cusom work directory', () => {
102102
contexts.prepareContext,
103103
);
104104

105-
const readme = fs.readFileSync(
106-
path.join(releasePath, 'plugin-with-readme/readme.txt'),
107-
'utf8',
105+
expect(readFile(releasePath, 'plugin-with-readme', 'readme.txt')).toMatch(
106+
/Stable tag: 1\.0\.0/,
108107
);
109-
110-
expect(readme).toMatch(/Stable tag: 1\.0\.0/);
111108
});
112109

113110
it('Should change the readme.txt version in the rootdir', async () => {
@@ -124,12 +121,9 @@ describe('Package preparation - cusom work directory', () => {
124121
contexts.prepareContext,
125122
);
126123

127-
const readme = fs.readFileSync(
128-
path.join(getWorkDir(workDir), 'root-readme/readme.txt'),
129-
'utf8',
124+
expect(readFile(getWorkDir(workDir), 'root-readme', 'readme.txt')).toMatch(
125+
/Stable tag: 1\.0\.0/,
130126
);
131-
132-
expect(readme).toMatch(/Stable tag: 1\.0\.0/);
133127
});
134128

135129
it('Should work with empty assets', async () => {
@@ -185,17 +179,10 @@ describe('Package preparation - default work directory', () => {
185179
contexts.prepareContext,
186180
);
187181

188-
const distFiles = fs.readdirSync(path.join(releasePath, 'dist-test'));
189-
const vendorFiles = fs.readdirSync(
190-
path.join(releasePath, 'dist-test', 'vendor'),
182+
expect(readDir(true, releasePath, 'dist-test')).toStrictEqual(
183+
new Set(['dist-test.php', 'test1.php', 'vendor', 'vendor/composer.php']),
191184
);
192-
const versionExists = fs.existsSync(path.join(releasePath, 'VERSION'));
193-
194-
expect(distFiles).toHaveLength(3);
195-
expect(distFiles).toStrictEqual(['dist-test.php', 'test1.php', 'vendor']);
196-
expect(vendorFiles).toHaveLength(1);
197-
expect(vendorFiles).toStrictEqual(['composer.php']);
198-
expect(versionExists).toBe(false);
185+
expect(fs.existsSync(path.join(releasePath, 'VERSION'))).toBe(false);
199186
});
200187

201188
it('Should copy the assets files', async () => {
@@ -212,14 +199,7 @@ describe('Package preparation - default work directory', () => {
212199
contexts.prepareContext,
213200
);
214201

215-
const assets = new Set(
216-
fs.readdirSync(path.join(releasePath, 'assets'), {
217-
recursive: true,
218-
}) as string[],
219-
);
220-
221-
expect([...assets]).toHaveLength(6);
222-
expect(assets).toEqual(
202+
expect(readDir(true, releasePath, 'assets')).toEqual(
223203
new Set([
224204
'blueprints',
225205
'blueprints/blueprint.json',

test/3-publish-plugin.spec.ts

Lines changed: 7 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { replaceVersions } from '../lib/utils/replace-versions.js';
88
import { success } from '../lib/success.js';
99
import SemanticReleaseError from '@semantic-release/error';
1010
import { publish } from '../lib/publish.js';
11-
import AdmZip, { IZipEntry } from 'adm-zip';
11+
import { readFile, readDir, readZip } from './utils.js';
1212

1313
const pluginConfig: PluginConfig = {
1414
type: 'plugin',
@@ -25,31 +25,6 @@ const pluginConfig: PluginConfig = {
2525
let wDir: string;
2626
const env = process.env;
2727

28-
function readZip(dir: string, file: string, pfx: RegExp = /.^/): Set<string> {
29-
return new Set(
30-
new AdmZip(path.join(dir, file))
31-
.getEntries()
32-
.map(({ entryName }) => entryName.replace(pfx, '').replace(/\/$/, ''))
33-
.filter((e) => e !== '' && (pfx.source == '.^' || !e.match(/\//))),
34-
);
35-
}
36-
37-
function readDir(
38-
root: string,
39-
dir: string,
40-
recursive: boolean = false,
41-
): Set<string> {
42-
return new Set(
43-
fs.readdirSync(path.join(root, dir), {
44-
recursive,
45-
}) as string[],
46-
);
47-
}
48-
49-
function readFile(root: string, file: string): string {
50-
return fs.readFileSync(path.join(root, file), 'utf8');
51-
}
52-
5328
beforeAll(async () => {
5429
wDir = fs.mkdtempSync('/tmp/wp-release-');
5530
pluginConfig.releasePath = wDir;
@@ -85,28 +60,22 @@ describe('Publish step', () => {
8560
await prepare(pluginConfig, contexts.publishContext);
8661
await publish(pluginConfig, contexts.publishContext);
8762

88-
expect(readFile(path.join(wDir, 'dist-test'), 'readme.txt')).toMatch(
63+
expect(readFile(wDir, 'dist-test', 'readme.txt')).toMatch(
8964
/^Stable tag: 1.0.0$/gm,
9065
);
9166
expect(readZip(wDir, 'package.zip', /^dist-test\//)).toEqual(
92-
readDir(wDir, 'dist-test'),
67+
readDir(false, wDir, 'dist-test'),
9368
);
94-
expect(readZip(wDir, 'assets.zip')).toEqual(readDir(wDir, 'assets', true));
69+
expect(readZip(wDir, 'assets.zip')).toEqual(readDir(true, wDir, 'assets'));
9570
expect(readFile(wDir, 'VERSION')).toEqual('1.0.0');
96-
97-
// expect readZip(releasePath, 'assets.zip');
9871
});
9972

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

103-
const files = fs.readdirSync(wDir).join(' ');
104-
105-
expect(files).toContain('package.zip');
106-
expect(files).toContain('assets.zip');
107-
expect(files).toContain('VERSION');
108-
expect(files).not.toContain('dist-test');
109-
expect(files).not.toContain('assets ');
76+
expect(readDir(false, wDir)).toEqual(
77+
new Set(['package.zip', 'assets.zip', 'VERSION']),
78+
);
11079
});
11180

11281
it('Should fail when no assets exist', async () => {

test/utils.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import AdmZip from 'adm-zip';
2+
import fs from 'fs-extra';
3+
import * as path from 'node:path';
4+
5+
export function readZip(
6+
dir: string,
7+
file: string,
8+
pfx: RegExp = /.^/,
9+
): Set<string> {
10+
return new Set(
11+
new AdmZip(path.join(dir, file))
12+
.getEntries()
13+
.map(({ entryName }) => entryName.replace(pfx, '').replace(/\/$/, ''))
14+
.filter((e) => e !== '' && (pfx.source == '.^' || !e.match(/\//))),
15+
);
16+
}
17+
18+
export function readDir(recursive = false, ...dirs: string[]): Set<string> {
19+
return new Set(
20+
fs.readdirSync(path.join(...dirs), {
21+
recursive,
22+
}) as string[],
23+
);
24+
}
25+
26+
export function readFile(...paths: string[]): string {
27+
return fs.readFileSync(path.join(...paths), 'utf8');
28+
}

0 commit comments

Comments
 (0)