1
1
import * as childProcess from '@lerna/child-process' ;
2
2
import { MockBuilderContext } from '@nrwl/workspace/testing' ;
3
+ import * as fs from 'fs' ;
3
4
import * as standardVersion from 'standard-version' ;
4
-
5
5
import { getMockContext } from '../../utils/testing' ;
6
6
import { runBuilder } from './builder' ;
7
7
import { VersionBuilderSchema } from './schema' ;
@@ -21,89 +21,159 @@ const options: VersionBuilderSchema = {
21
21
describe ( '@jscutlery/semver:version' , ( ) => {
22
22
let context : MockBuilderContext ;
23
23
24
- beforeEach ( async ( ) => {
25
- context = await getMockContext ( ) ;
26
- context . logger . error = jest . fn ( ) ;
27
- context . getProjectMetadata = jest
28
- . fn ( )
29
- . mockResolvedValue ( { root : '/root/lib' } ) ;
24
+ beforeEach ( ( ) => {
25
+ jest
26
+ . spyOn ( fs , 'readFile' )
27
+ . mockImplementation ( ( ...args : Parameters < typeof fs . readFile > ) => {
28
+ const callback = args [ args . length - 1 ] as Function ;
29
+ callback (
30
+ null ,
31
+ JSON . stringify ( {
32
+ version : 1 ,
33
+ projects : {
34
+ a : {
35
+ root : 'packages/a' ,
36
+ } ,
37
+ b : {
38
+ root : 'packages/b' ,
39
+ } ,
40
+ } ,
41
+ } )
42
+ ) ;
43
+ } ) ;
30
44
} ) ;
31
45
32
- it ( 'runs standard-version with project options' , async ( ) => {
33
- const output = await runBuilder ( options , context ) . toPromise ( ) ;
34
-
35
- expect ( output ) . toEqual ( expect . objectContaining ( { success : true } ) ) ;
36
- expect ( standardVersion ) . toBeCalledWith (
37
- expect . objectContaining ( {
38
- silent : false ,
39
- preset : expect . stringContaining ( 'conventional-changelog-angular' ) ,
40
- dryRun : false ,
41
- noVerify : false ,
42
- firstRelease : false ,
43
- path : '/root/lib' ,
44
- infile : '/root/lib/CHANGELOG.md' ,
45
- bumpFiles : [ '/root/lib/package.json' ] ,
46
- packageFiles : [ '/root/lib/package.json' ] ,
47
- } )
48
- ) ;
46
+ describe ( 'Independent version' , ( ) => {
47
+ beforeEach ( async ( ) => {
48
+ context = await getMockContext ( ) ;
49
+ context . logger . error = jest . fn ( ) ;
50
+ context . getProjectMetadata = jest
51
+ . fn ( )
52
+ . mockResolvedValue ( { root : '/root/packages/lib' } ) ;
53
+ } ) ;
54
+ it ( 'runs standard-version with project options' , async ( ) => {
55
+ const output = await runBuilder ( options , context ) . toPromise ( ) ;
56
+
57
+ expect ( output ) . toEqual ( expect . objectContaining ( { success : true } ) ) ;
58
+ expect ( standardVersion ) . toBeCalledWith (
59
+ expect . objectContaining ( {
60
+ silent : false ,
61
+ preset : expect . stringContaining ( 'conventional-changelog-angular' ) ,
62
+ dryRun : false ,
63
+ noVerify : false ,
64
+ firstRelease : false ,
65
+ path : '/root/packages/lib' ,
66
+ infile : '/root/packages/lib/CHANGELOG.md' ,
67
+ bumpFiles : [ '/root/packages/lib/package.json' ] ,
68
+ packageFiles : [ '/root/packages/lib/package.json' ] ,
69
+ } )
70
+ ) ;
71
+ } ) ;
72
+
73
+ it ( 'should not push to Git by default' , async ( ) => {
74
+ await runBuilder ( options , context ) . toPromise ( ) ;
75
+
76
+ expect ( childProcess . exec ) . not . toHaveBeenCalled ( ) ;
77
+ } ) ;
78
+
79
+ it ( 'should push to Git with right options' , async ( ) => {
80
+ await runBuilder (
81
+ { ...options , push : true , remote : 'origin' , baseBranch : 'main' } ,
82
+ context
83
+ ) . toPromise ( ) ;
84
+
85
+ expect ( childProcess . exec ) . toHaveBeenCalledWith (
86
+ 'git' ,
87
+ expect . arrayContaining ( [
88
+ 'push' ,
89
+ '--follow-tags' ,
90
+ '--atomic' ,
91
+ 'origin' ,
92
+ 'main' ,
93
+ ] )
94
+ ) ;
95
+ } ) ;
96
+
97
+ it ( `should push to Git and add '--no-verify' option when asked for` , async ( ) => {
98
+ await runBuilder (
99
+ {
100
+ ...options ,
101
+ push : true ,
102
+ remote : 'origin' ,
103
+ baseBranch : 'main' ,
104
+ noVerify : true ,
105
+ } ,
106
+ context
107
+ ) . toPromise ( ) ;
108
+
109
+ expect ( childProcess . exec ) . toHaveBeenCalledWith (
110
+ 'git' ,
111
+ expect . arrayContaining ( [
112
+ 'push' ,
113
+ '--follow-tags' ,
114
+ '--no-verify' ,
115
+ '--atomic' ,
116
+ 'origin' ,
117
+ 'main' ,
118
+ ] )
119
+ ) ;
120
+ } ) ;
121
+
122
+ it ( 'should fail if Git config is missing' , async ( ) => {
123
+ const output = await runBuilder (
124
+ { ...options , push : true , remote : undefined , baseBranch : undefined } ,
125
+ context
126
+ ) . toPromise ( ) ;
127
+
128
+ expect ( context . logger . error ) . toBeCalled ( ) ;
129
+ expect ( output ) . toEqual ( expect . objectContaining ( { success : false } ) ) ;
130
+ } ) ;
49
131
} ) ;
50
132
51
- it ( 'should not push to Git by default' , async ( ) => {
52
- await runBuilder ( options , context ) . toPromise ( ) ;
53
-
54
- expect ( childProcess . exec ) . not . toHaveBeenCalled ( ) ;
55
- } ) ;
56
-
57
- it ( 'should push to Git with right options' , async ( ) => {
58
- await runBuilder (
59
- { ...options , push : true , remote : 'origin' , baseBranch : 'main' } ,
60
- context
61
- ) . toPromise ( ) ;
62
-
63
- expect ( childProcess . exec ) . toHaveBeenCalledWith (
64
- 'git' ,
65
- expect . arrayContaining ( [
66
- 'push' ,
67
- '--follow-tags' ,
68
- '--atomic' ,
69
- 'origin' ,
70
- 'main' ,
71
- ] )
72
- ) ;
73
- } ) ;
74
-
75
- it ( `should push to Git and add '--no-verify' option when asked for` , async ( ) => {
76
- await runBuilder (
77
- {
78
- ...options ,
79
- push : true ,
80
- remote : 'origin' ,
81
- baseBranch : 'main' ,
82
- noVerify : true ,
83
- } ,
84
- context
85
- ) . toPromise ( ) ;
86
-
87
- expect ( childProcess . exec ) . toHaveBeenCalledWith (
88
- 'git' ,
89
- expect . arrayContaining ( [
90
- 'push' ,
91
- '--follow-tags' ,
92
- '--no-verify' ,
93
- '--atomic' ,
94
- 'origin' ,
95
- 'main' ,
96
- ] )
97
- ) ;
98
- } ) ;
99
-
100
- it ( 'should fail if Git config is missing' , async ( ) => {
101
- const output = await runBuilder (
102
- { ...options , push : true , remote : undefined , baseBranch : undefined } ,
103
- context
104
- ) . toPromise ( ) ;
105
-
106
- expect ( context . logger . error ) . toBeCalled ( )
107
- expect ( output ) . toEqual ( expect . objectContaining ( { success : false } ) ) ;
133
+ describe ( 'Sync version' , ( ) => {
134
+ beforeEach ( async ( ) => {
135
+ context = await getMockContext ( ) ;
136
+
137
+ /* With the sync version, the builder runs on the workspace. */
138
+ context . getProjectMetadata = jest
139
+ . fn ( )
140
+ . mockResolvedValue ( { root : '/root' } ) ;
141
+ } ) ;
142
+
143
+ it ( 'should sync projects versions' , async ( ) => {
144
+ const output = await runBuilder (
145
+ {
146
+ ...options ,
147
+ push : true ,
148
+ remote : undefined ,
149
+ baseBranch : undefined ,
150
+ /* Enable sync versions. */
151
+ syncVersions : true ,
152
+ } ,
153
+ context
154
+ ) . toPromise ( ) ;
155
+
156
+ expect ( fs . readFile ) . toHaveBeenLastCalledWith (
157
+ '/root/workspace.json' ,
158
+ 'utf-8' ,
159
+ expect . any ( Function )
160
+ ) ;
161
+ expect ( standardVersion ) . toBeCalledWith (
162
+ expect . objectContaining ( {
163
+ silent : false ,
164
+ preset : expect . stringContaining ( 'conventional-changelog-angular' ) ,
165
+ dryRun : false ,
166
+ noVerify : false ,
167
+ firstRelease : false ,
168
+ path : '/root' ,
169
+ infile : '/root/CHANGELOG.md' ,
170
+ bumpFiles : [
171
+ '/root/packages/a/package.json' ,
172
+ '/root/packages/b/package.json' ,
173
+ ] ,
174
+ packageFiles : [ '/root/package.json' ] ,
175
+ } )
176
+ ) ;
177
+ } ) ;
108
178
} ) ;
109
179
} ) ;
0 commit comments