1- import { existsSync , statSync , mkdtempSync , copyFileSync } from 'fs' ;
2- import { tmpdir } from 'os' ;
3- import { join , basename , extname } from 'path' ;
1+ import { join } from 'path' ;
2+ import { pathToFileURL } from 'url' ;
43import { describe , it , expect , vi , beforeEach } from 'vitest' ;
54import { ArtifactExporter } from '../../../src/artifactexporter/ArtifactExporter' ;
65import { DocumentType } from '../../../src/document/Document' ;
76import { S3Service } from '../../../src/services/S3Service' ;
87
98vi . mock ( '../../../src/services/S3Service' ) ;
10- vi . mock ( 'fs' ) ;
11- vi . mock ( 'os' ) ;
12- vi . mock ( 'path' ) ;
13- vi . mock ( 'archiver' ) ;
9+
10+ const FIXTURES_DIR = join ( __dirname , '..' , '..' , 'resources' , 'templates' , 'artifact' ) ;
1411
1512describe ( 'ArtifactExporter' , ( ) => {
1613 let mockS3Service : S3Service ;
17- const templatePath = `file:/// ${ join ( __dirname , 'template.yaml' ) } ` ;
14+ const templatePath = pathToFileURL ( join ( FIXTURES_DIR , 'template.yaml' ) ) . href ;
1815
1916 const BASIC_TEMPLATE = 'Resources:\n Bucket:\n Type: AWS::S3::Bucket' ;
2017
@@ -23,7 +20,7 @@ Resources:
2320 MyFunction:
2421 Type: AWS::Lambda::Function
2522 Properties:
26- Code: ./src/lambda
23+ Code: ./code
2724 Runtime: nodejs18.x
2825 Handler: index.handler
2926 FunctionName: MyTestFunction
@@ -62,22 +59,6 @@ Resources:
6259 putObjectContent : vi . fn ( ) ,
6360 putObject : vi . fn ( ) . mockResolvedValue ( { VersionId : 'v123' } ) ,
6461 } as any ;
65-
66- vi . mocked ( existsSync ) . mockReturnValue ( true ) ;
67- vi . mocked ( statSync ) . mockReturnValue ( {
68- isFile : ( ) => true ,
69- isDirectory : ( ) => false ,
70- } as any ) ;
71- vi . mocked ( tmpdir ) . mockReturnValue ( '/tmp' ) ;
72- vi . mocked ( join ) . mockImplementation ( ( ...args ) => args . join ( '/' ) ) ;
73- vi . mocked ( basename ) . mockImplementation ( ( path ) => path ?. split ( '/' ) . pop ( ) ?? '' ) ;
74- vi . mocked ( extname ) . mockImplementation ( ( path ) => {
75- if ( ! path ) return '' ;
76- const parts = path . split ( '.' ) ;
77- return parts . length > 1 ? '.' + parts [ parts . length - 1 ] : '' ;
78- } ) ;
79- vi . mocked ( mkdtempSync ) . mockReturnValue ( '/tmp/cfn-123' ) ;
80- vi . mocked ( copyFileSync ) . mockImplementation ( ( ) => { } ) ;
8162 } ) ;
8263
8364 describe ( 'getTemplateArtifacts' , ( ) => {
@@ -88,7 +69,7 @@ Resources:
8869 expect ( artifacts ) . toEqual ( [
8970 {
9071 resourceType : 'AWS::Lambda::Function' ,
91- filePath : './src/lambda ' ,
72+ filePath : './code ' ,
9273 } ,
9374 ] ) ;
9475 } ) ;
@@ -111,7 +92,7 @@ Resources:
11192 const template = new ArtifactExporter (
11293 mockS3Service ,
11394 DocumentType . YAML ,
114- `file:/// ${ join ( __dirname , 'path/to/template.yaml' ) } ` ,
95+ pathToFileURL ( join ( FIXTURES_DIR , 'path/to/template.yaml' ) ) . href ,
11596 BASIC_TEMPLATE ,
11697 ) ;
11798 expect ( template ) . toBeDefined ( ) ;
@@ -121,7 +102,7 @@ Resources:
121102 const template = new ArtifactExporter (
122103 mockS3Service ,
123104 DocumentType . YAML ,
124- `file:/// ${ join ( __dirname , 'path/to/template.yaml' ) } ` ,
105+ pathToFileURL ( join ( FIXTURES_DIR , 'path/to/template.yaml' ) ) . href ,
125106 BASIC_TEMPLATE ,
126107 ) ;
127108 const result = await template . export ( 'test-bucket' ) ;
@@ -133,10 +114,15 @@ Resources:
133114
134115 const result = await exporter . export ( 'test-bucket' ) ;
135116
117+ expect ( mockS3Service . putObject ) . toHaveBeenCalledWith (
118+ expect . stringMatching ( / \. z i p $ / ) ,
119+ expect . stringMatching ( / ^ s 3 : \/ \/ t e s t - b u c k e t \/ a r t i f a c t \/ .* \. z i p $ / ) ,
120+ ) ;
121+
136122 const resources = ( result as any ) . Resources ;
137123 expect ( resources . MyFunction . Properties . Code ) . toEqual ( {
138124 S3Bucket : 'test-bucket' ,
139- S3Key : expect . stringMatching ( / ^ a r t i f a c t \/ c f n - 1 2 3 - \d + $ / ) ,
125+ S3Key : expect . stringMatching ( / ^ a r t i f a c t \/ . * \. z i p $ / ) ,
140126 S3ObjectVersion : 'v123' ,
141127 } ) ;
142128 expect ( resources . MyFunction . Properties . Runtime ) . toBe ( 'nodejs18.x' ) ;
@@ -147,17 +133,17 @@ Resources:
147133 } ) ;
148134
149135 it ( 'should update Serverless function CodeUri to S3 URL' , async ( ) => {
150- const exporter = new ArtifactExporter (
151- mockS3Service ,
152- DocumentType . YAML ,
153- `file:///${ join ( __dirname , 'template.yaml' ) } ` ,
154- SERVERLESS_TEMPLATE ,
155- ) ;
136+ const exporter = new ArtifactExporter ( mockS3Service , DocumentType . YAML , templatePath , SERVERLESS_TEMPLATE ) ;
156137
157138 const result = await exporter . export ( 'my-bucket' ) ;
158139
140+ expect ( mockS3Service . putObject ) . toHaveBeenCalledWith (
141+ expect . stringMatching ( / \. z i p $ / ) ,
142+ expect . stringMatching ( / ^ s 3 : \/ \/ m y - b u c k e t \/ a r t i f a c t \/ .* \. z i p $ / ) ,
143+ ) ;
144+
159145 const resources = ( result as any ) . Resources ;
160- expect ( resources . MyFunction . Properties . CodeUri ) . toMatch ( / ^ s 3 : \/ \/ m y - b u c k e t \/ a r t i f a c t \/ c f n - 1 2 3 - \d + $ / ) ;
146+ expect ( resources . MyFunction . Properties . CodeUri ) . toMatch ( / ^ s 3 : \/ \/ m y - b u c k e t \/ a r t i f a c t \/ . * \. z i p $ / ) ;
161147 expect ( resources . MyFunction . Properties . Runtime ) . toBe ( 'python3.9' ) ;
162148 expect ( resources . MyFunction . Properties . Handler ) . toBe ( 'app.lambda_handler' ) ;
163149 expect ( resources . MyFunction . Properties . Description ) . toBe ( 'Test serverless function' ) ;
0 commit comments