@@ -4,39 +4,12 @@ import { Test } from 'nodeunit';
4
4
import os = require( 'os' ) ;
5
5
import path = require( 'path' ) ;
6
6
import cdk = require( '../lib' ) ;
7
- import { InMemorySynthesisSession , SynthesisSession } from '../lib' ;
7
+ import { FileSystemStore , InMemoryStore , SynthesisSession } from '../lib' ;
8
8
9
- const sessionTestMatix : any = { } ;
9
+ const storeTestMatrix : any = { } ;
10
10
11
11
export = {
12
- 'constructs that implement "synthesize" can emit artifacts during synthesis' ( test : Test ) {
13
- // GIVEN
14
- const app = new cdk . App ( ) ;
15
- new Synthesizer1 ( app , 'synthe1' ) ;
16
- const s2 = new Synthesizer2 ( app , 'synthe2' ) ;
17
- new Synthesizer3 ( s2 , 'synthe3' ) ;
18
-
19
- // WHEN
20
- const session = app . run ( ) ;
21
-
22
- // THEN
23
- test . deepEqual ( session . readFile ( 's1.txt' ) , 'hello, s1' ) ;
24
- test . deepEqual ( session . readFile ( 's2.txt' ) , 'hello, s2' ) ;
25
-
26
- test . deepEqual ( session . list ( ) , [
27
- 'cdk.out' ,
28
- 's1.txt' ,
29
- 's2.txt' ,
30
- 'synthe2Group0512C945A.txt' ,
31
- 'synthe2Group181E95665.txt' ,
32
- 'synthe2Group20BD1A3CD.txt' ,
33
- 'synthe2synthe30CE80559.txt'
34
- ] ) ;
35
-
36
- test . done ( ) ;
37
- } ,
38
-
39
- 'cdk.out contains all synthesized stacks' ( test : Test ) {
12
+ 'backwards compatibility: cdk.out contains all synthesized stacks' ( test : Test ) {
40
13
// GIVEN
41
14
const app = new cdk . App ( ) ;
42
15
const stack1 = new cdk . Stack ( app , 'stack1' ) ;
@@ -47,7 +20,7 @@ export = {
47
20
48
21
// WHEN
49
22
const session = app . run ( ) ;
50
- const manifest : cxapi . SynthesizeResponse = JSON . parse ( session . readFile ( cxapi . OUTFILE_NAME ) . toString ( ) ) ;
23
+ const manifest = session . manifest ;
51
24
52
25
// THEN
53
26
const t1 = manifest . stacks . find ( s => s . name === 'stack1' ) ! . template ;
@@ -67,96 +40,184 @@ export = {
67
40
test . done ( ) ;
68
41
} ,
69
42
70
- 'session ' : sessionTestMatix
43
+ 'store ' : storeTestMatrix
71
44
} ;
72
45
73
- const sessionTests = {
74
- 'writeFile()/readFile()' ( test : Test , session : cdk . ISynthesisSession ) {
46
+ //
47
+ // all these tests will be executed for each type of store
48
+ //
49
+ const storeTests = {
50
+ 'writeFile()/readFile()' ( test : Test , store : cdk . ISessionStore ) {
75
51
// WHEN
76
- session . writeFile ( 'bla.txt' , 'hello' ) ;
77
- session . writeFile ( 'hey.txt' , '1234' ) ;
52
+ store . writeFile ( 'bla.txt' , 'hello' ) ;
53
+ store . writeFile ( 'hey.txt' , '1234' ) ;
78
54
79
55
// THEN
80
- test . deepEqual ( session . readFile ( 'bla.txt' ) . toString ( ) , 'hello' ) ;
81
- test . deepEqual ( session . readFile ( 'hey.txt' ) . toString ( ) , '1234' ) ;
82
- test . throws ( ( ) => session . writeFile ( 'bla.txt' , 'override is forbidden' ) ) ;
56
+ test . deepEqual ( store . readFile ( 'bla.txt' ) . toString ( ) , 'hello' ) ;
57
+ test . deepEqual ( store . readFile ( 'hey.txt' ) . toString ( ) , '1234' ) ;
58
+ test . throws ( ( ) => store . writeFile ( 'bla.txt' , 'override is forbidden' ) ) ;
83
59
84
60
// WHEN
85
- session . finalize ( ) ;
61
+ store . finalize ( ) ;
86
62
87
63
// THEN
88
- test . throws ( ( ) => session . writeFile ( 'another.txt' , 'locked!' ) ) ;
64
+ test . throws ( ( ) => store . writeFile ( 'another.txt' , 'locked!' ) ) ;
89
65
test . done ( ) ;
90
66
} ,
91
67
92
- 'exists() for files' ( test : Test , session : cdk . ISynthesisSession ) {
68
+ 'exists() for files' ( test : Test , store : cdk . ISessionStore ) {
93
69
// WHEN
94
- session . writeFile ( 'A.txt' , 'aaa' ) ;
70
+ store . writeFile ( 'A.txt' , 'aaa' ) ;
95
71
96
72
// THEN
97
- test . ok ( session . exists ( 'A.txt' ) ) ;
98
- test . ok ( ! session . exists ( 'B.txt' ) ) ;
73
+ test . ok ( store . exists ( 'A.txt' ) ) ;
74
+ test . ok ( ! store . exists ( 'B.txt' ) ) ;
99
75
test . done ( ) ;
100
76
} ,
101
77
102
- 'mkdir' ( test : Test , session : cdk . ISynthesisSession ) {
78
+ 'mkdir' ( test : Test , store : cdk . ISessionStore ) {
103
79
// WHEN
104
- const dir1 = session . mkdir ( 'dir1' ) ;
105
- const dir2 = session . mkdir ( 'dir2' ) ;
80
+ const dir1 = store . mkdir ( 'dir1' ) ;
81
+ const dir2 = store . mkdir ( 'dir2' ) ;
106
82
107
83
// THEN
108
84
test . ok ( fs . statSync ( dir1 ) . isDirectory ( ) ) ;
109
85
test . ok ( fs . statSync ( dir2 ) . isDirectory ( ) ) ;
110
- test . throws ( ( ) => session . mkdir ( 'dir1' ) ) ;
86
+ test . throws ( ( ) => store . mkdir ( 'dir1' ) ) ;
111
87
112
88
// WHEN
113
- session . finalize ( ) ;
114
- test . throws ( ( ) => session . mkdir ( 'dir3' ) ) ;
89
+ store . finalize ( ) ;
90
+ test . throws ( ( ) => store . mkdir ( 'dir3' ) ) ;
115
91
test . done ( ) ;
116
92
} ,
117
93
118
- 'list' ( test : Test , session : cdk . ISynthesisSession ) {
94
+ 'list' ( test : Test , store : cdk . ISessionStore ) {
119
95
// WHEN
120
- session . mkdir ( 'dir1' ) ;
121
- session . writeFile ( 'file1.txt' , 'boom1' ) ;
96
+ store . mkdir ( 'dir1' ) ;
97
+ store . writeFile ( 'file1.txt' , 'boom1' ) ;
122
98
123
99
// THEN
124
- test . deepEqual ( session . list ( ) , [ 'dir1' , 'file1.txt' ] ) ;
100
+ test . deepEqual ( store . list ( ) , [ 'dir1' , 'file1.txt' ] ) ;
125
101
test . done ( ) ;
126
- }
127
- } ;
102
+ } ,
128
103
129
- for ( const [ name , fn ] of Object . entries ( sessionTests ) ) {
130
- const outdir = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , 'synthesis-tests' ) ) ;
131
- const fsSession = new SynthesisSession ( { outdir } ) ;
132
- const memorySession = new InMemorySynthesisSession ( ) ;
133
- sessionTestMatix [ `SynthesisSession - ${ name } ` ] = ( test : Test ) => fn ( test , fsSession ) ;
134
- sessionTestMatix [ `InMemorySession - ${ name } ` ] = ( test : Test ) => fn ( test , memorySession ) ;
135
- }
104
+ 'SynthesisSession' ( test : Test , store : cdk . ISessionStore ) {
105
+ // GIVEN
106
+ const session = new SynthesisSession ( store ) ;
107
+ const templateFile = 'foo.template.json' ;
136
108
137
- class Synthesizer1 extends cdk . Construct {
138
- public synthesize ( s : cdk . ISynthesisSession ) {
139
- s . writeFile ( 's1.txt' , 'hello, s1' ) ;
140
- }
141
- }
109
+ // WHEN
110
+ session . addArtifact ( 'my-first-artifact' , {
111
+ type : cxapi . ArtifactType . CloudFormationStack ,
112
+ environment : 'aws://1222344/us-east-1' ,
113
+ dependencies : [ 'a' , 'b' ] ,
114
+ metadata : {
115
+ foo : { bar : 123 }
116
+ } ,
117
+ properties : {
118
+ template : templateFile ,
119
+ prop1 : 1234 ,
120
+ prop2 : 555
121
+ } ,
122
+ missing : {
123
+ foo : {
124
+ provider : 'context-provider' ,
125
+ props : {
126
+ a : 'A' ,
127
+ b : 2
128
+ }
129
+ }
130
+ }
131
+ } ) ;
142
132
143
- class Synthesizer2 extends cdk . Construct {
144
- constructor ( scope : cdk . Construct , id : string ) {
145
- super ( scope , id ) ;
133
+ session . addArtifact ( 'minimal-artifact' , {
134
+ type : cxapi . ArtifactType . CloudFormationStack ,
135
+ environment : 'aws://111/helo-world' ,
136
+ properties : {
137
+ template : templateFile
138
+ }
139
+ } ) ;
146
140
147
- const group = new cdk . Construct ( this , 'Group' ) ;
148
- for ( let i = 0 ; i < 3 ; ++ i ) {
149
- new Synthesizer3 ( group , `${ i } ` ) ;
150
- }
151
- }
141
+ session . store . writeJson ( templateFile , {
142
+ Resources : {
143
+ MyTopic : {
144
+ Type : 'AWS::S3::Topic'
145
+ }
146
+ }
147
+ } ) ;
152
148
153
- public synthesize ( s : cdk . ISynthesisSession ) {
154
- s . writeFile ( 's2.txt' , 'hello, s2' ) ;
155
- }
156
- }
149
+ session . finalize ( ) ;
157
150
158
- class Synthesizer3 extends cdk . Construct {
159
- public synthesize ( s : cdk . ISynthesisSession ) {
160
- s . writeFile ( this . node . uniqueId + '.txt' , 'hello, s3' ) ;
151
+ // THEN
152
+ delete session . manifest . stacks ; // remove legacy
153
+ delete session . manifest . runtime ; // deterministic tests
154
+
155
+ // verify the manifest looks right
156
+ test . deepEqual ( session . manifest , {
157
+ version : cxapi . PROTO_RESPONSE_VERSION ,
158
+ artifacts : {
159
+ 'my-first-artifact' : {
160
+ type : 'aws:cloudformation:stack' ,
161
+ environment : 'aws://1222344/us-east-1' ,
162
+ dependencies : [ 'a' , 'b' ] ,
163
+ metadata : { foo : { bar : 123 } } ,
164
+ properties : { template : 'foo.template.json' , prop1 : 1234 , prop2 : 555 } ,
165
+ missing : {
166
+ foo : { provider : 'context-provider' , props : { a : 'A' , b : 2 } }
167
+ }
168
+ } ,
169
+ 'minimal-artifact' : {
170
+ type : 'aws:cloudformation:stack' ,
171
+ environment : 'aws://111/helo-world' ,
172
+ properties : { template : 'foo.template.json' }
173
+ }
174
+ }
175
+ } ) ;
176
+
177
+ // verify we have a template file
178
+ test . deepEqual ( session . store . readJson ( templateFile ) , {
179
+ Resources : {
180
+ MyTopic : {
181
+ Type : 'AWS::S3::Topic'
182
+ }
183
+ }
184
+ } ) ;
185
+
186
+ test . done ( ) ;
161
187
}
188
+ } ;
189
+
190
+ for ( const [ name , fn ] of Object . entries ( storeTests ) ) {
191
+ const outdir = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , 'synthesis-tests' ) ) ;
192
+ const fsStore = new FileSystemStore ( { outdir } ) ;
193
+ const memoryStore = new InMemoryStore ( ) ;
194
+ storeTestMatrix [ `FileSystemStore - ${ name } ` ] = ( test : Test ) => fn ( test , fsStore ) ;
195
+ storeTestMatrix [ `InMemoryStore - ${ name } ` ] = ( test : Test ) => fn ( test , memoryStore ) ;
162
196
}
197
+
198
+ // class Synthesizer1 extends cdk.Construct {
199
+ // public synthesize(s: cdk.ISynthesisSession) {
200
+ // s.writeFile('s1.txt', 'hello, s1');
201
+ // }
202
+ // }
203
+
204
+ // class Synthesizer2 extends cdk.Construct {
205
+ // constructor(scope: cdk.Construct, id: string) {
206
+ // super(scope, id);
207
+
208
+ // const group = new cdk.Construct(this, 'Group');
209
+ // for (let i = 0; i < 3; ++i) {
210
+ // new Synthesizer3(group, `${i}`);
211
+ // }
212
+ // }
213
+
214
+ // public synthesize(s: cdk.ISynthesisSession) {
215
+ // s.writeFile('s2.txt', 'hello, s2');
216
+ // }
217
+ // }
218
+
219
+ // class Synthesizer3 extends cdk.Construct {
220
+ // public synthesize(s: cdk.ISynthesisSession) {
221
+ // s.writeFile(this.node.uniqueId + '.txt', 'hello, s3');
222
+ // }
223
+ // }
0 commit comments