@@ -8,6 +8,7 @@ import { replaceVersions } from '../lib/utils/replace-versions.js';
8
8
import { success } from '../lib/success.js' ;
9
9
import SemanticReleaseError from '@semantic-release/error' ;
10
10
import { publish } from '../lib/publish.js' ;
11
+ import AdmZip , { IZipEntry } from 'adm-zip' ;
11
12
12
13
const pluginConfig : PluginConfig = {
13
14
type : 'plugin' ,
@@ -21,12 +22,37 @@ const pluginConfig: PluginConfig = {
21
22
workDir : 'publish' ,
22
23
} ;
23
24
24
- let releasePath : string ;
25
+ let wDir : string ;
25
26
const env = process . env ;
26
27
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
+
27
53
beforeAll ( async ( ) => {
28
- releasePath = fs . mkdtempSync ( '/tmp/wp-release-' ) ;
29
- pluginConfig . releasePath = releasePath ;
54
+ wDir = fs . mkdtempSync ( '/tmp/wp-release-' ) ;
55
+ pluginConfig . releasePath = wDir ;
30
56
} ) ;
31
57
32
58
beforeEach ( ( ) => {
@@ -51,28 +77,30 @@ afterEach(async () => {
51
77
} ) ;
52
78
53
79
afterAll ( async ( ) => {
54
- fs . removeSync ( releasePath ) ;
80
+ fs . removeSync ( wDir ) ;
55
81
} ) ;
56
82
57
83
describe ( 'Publish step' , ( ) => {
58
- it ( 'Should package a complete plugin' , async ( ) => {
84
+ it ( 'Should zip a complete plugin properly ' , async ( ) => {
59
85
await prepare ( pluginConfig , contexts . publishContext ) ;
60
86
await publish ( pluginConfig , contexts . publishContext ) ;
61
87
62
- const distFolder = fs
63
- . readdirSync ( path . join ( releasePath , 'dist-test' ) )
64
- . join ( ' ' ) ;
88
+ expect ( readFile ( path . join ( wDir , 'dist-test' ) , 'readme.txt' ) ) . toMatch (
89
+ / ^ S t a b l e t a g : 1 .0 .0 $ / gm,
90
+ ) ;
91
+ expect ( readZip ( wDir , 'package.zip' , / ^ d i s t - t e s t \/ / ) ) . toEqual (
92
+ readDir ( wDir , 'dist-test' ) ,
93
+ ) ;
94
+ expect ( readZip ( wDir , 'assets.zip' ) ) . toEqual ( readDir ( wDir , 'assets' , true ) ) ;
95
+ expect ( readFile ( wDir , 'VERSION' ) ) . toEqual ( '1.0.0' ) ;
65
96
66
- expect ( distFolder ) . not . toContain ( 'node_modules' ) ;
67
- expect ( distFolder ) . toContain ( 'vendor' ) ;
68
- expect ( distFolder ) . toContain ( 'dist-test.php' ) ;
69
- expect ( distFolder ) . toContain ( 'test1.php' ) ;
97
+ // expect readZip(releasePath, 'assets.zip');
70
98
} ) ;
71
99
72
100
it ( 'Should should remove folders on success' , async ( ) => {
73
101
await success ( pluginConfig , contexts . publishContext ) ;
74
102
75
- const files = fs . readdirSync ( releasePath ) . join ( ' ' ) ;
103
+ const files = fs . readdirSync ( wDir ) . join ( ' ' ) ;
76
104
77
105
expect ( files ) . toContain ( 'package.zip' ) ;
78
106
expect ( files ) . toContain ( 'assets.zip' ) ;
@@ -85,7 +113,7 @@ describe('Publish step', () => {
85
113
try {
86
114
await prepare ( pluginConfig , contexts . publishContext ) ;
87
115
88
- await fs . remove ( path . join ( releasePath , 'assets' ) ) ;
116
+ await fs . remove ( path . join ( wDir , 'assets' ) ) ;
89
117
await publish ( pluginConfig , contexts . publishContext ) ;
90
118
} catch ( err ) {
91
119
expect ( ( err as SemanticReleaseError ) . code ) . toMatch ( / ( E N O E N T | E Z I P ) / ) ;
0 commit comments