1- import { getAssets , publishAssets } from '../../src/common' ;
1+ import { cleanupAssets , constructAssets , getAssets , publishAssets } from '../../src/common' ;
22import { Stats } from 'node:fs' ;
33import { assetsFixture } from '../fixtures/assetsFixture' ;
4- import { ActionContext , CdkAssets } from '../../src/types' ;
4+ import { ActionContext } from '../../src/types' ;
5+ import { defaultContext } from '../fixtures/deployFixture' ;
56
67const mockedBucketPut = jest . fn ( ) ;
8+ const mockedDeleteBucket = jest . fn ( ) ;
9+ const mockedDelete = jest . fn ( ) ;
710const mockedReaddirSync = jest . fn ( ) ;
811const mockedLstatSync = jest . fn ( ) ;
912const mockedExistsSync = jest . fn ( ) ;
1013const mockedGenerateAsync = jest . fn ( ) ;
1114const mockedInfoLogger = jest . fn ( ) ;
15+ const mockedDebugLogger = jest . fn ( ) ;
1216
1317jest . mock ( 'ali-oss' , ( ) =>
1418 jest . fn ( ) . mockImplementation ( ( ) => ( {
1519 getBucketInfo : jest . fn ( ) . mockResolvedValue ( { } ) ,
1620 put : ( ...args : unknown [ ] ) => mockedBucketPut ( ...args ) ,
21+ delete : ( ...args : unknown [ ] ) => mockedDelete ( ...args ) ,
22+ deleteBucket : ( ...args : unknown [ ] ) => mockedDeleteBucket ( ...args ) ,
1723 } ) ) ,
1824) ;
1925
@@ -43,14 +49,15 @@ jest.mock('jszip', () =>
4349jest . mock ( '../../src/common/logger' , ( ) => ( {
4450 logger : {
4551 info : ( ...args : unknown [ ] ) => mockedInfoLogger ( ...args ) ,
52+ debug : ( ...args : unknown [ ] ) => mockedDebugLogger ( ...args ) ,
4653 } ,
4754} ) ) ;
4855
4956describe ( 'Unit test for rosAssets' , ( ) => {
5057 beforeEach ( ( ) => {
5158 jest . clearAllMocks ( ) ;
5259 } ) ;
53- describe ( 'Unite test for getAssets' , ( ) => {
60+ describe ( 'Unit test for getAssets' , ( ) => {
5461 it ( 'should return assets from the specified location' , ( ) => {
5562 const mockLocation = 'mock/location' ;
5663 mockedExistsSync . mockReturnValue ( true ) ;
@@ -78,7 +85,7 @@ describe('Unit test for rosAssets', () => {
7885 } ) ;
7986 } ) ;
8087
81- describe ( 'publishAssets' , ( ) => {
88+ describe ( 'Unit test for publishAssets' , ( ) => {
8289 const mockContext = {
8390 region : 'mock-region' ,
8491 accessKeyId : 'mock-access-key-id' ,
@@ -93,7 +100,10 @@ describe('Unit test for rosAssets', () => {
93100 . mockReturnValueOnce ( { isFile : ( ) => true } as Stats ) ;
94101 mockedGenerateAsync . mockResolvedValue ( Buffer . from ( 'mock-zip-content' ) ) ;
95102
96- const bucketName = await publishAssets ( assetsFixture , mockContext ) ;
103+ const bucketName = await publishAssets (
104+ await constructAssets ( assetsFixture , 'mock-region' ) ,
105+ mockContext ,
106+ ) ;
97107
98108 expect ( bucketName ) . toBe ( 'cdk-ajmywduza-assets-mock-region' ) ;
99109 expect ( mockedBucketPut . mock . calls ) . toEqual ( [
@@ -112,21 +122,40 @@ describe('Unit test for rosAssets', () => {
112122 [
113123 'Folder compressed to: path/to/asset.55d1d2dd5d6c1b083a04c15431f70da1f2840b9de06383411cbf7eda2a512efe.zip' ,
114124 ] ,
115- [
116- 'Upload file: path/to/asset.55d1d2dd5d6c1b083a04c15431f70da1f2840b9de06383411cbf7eda2a512efe.zip) to bucket: cdk-ajmywduza-assets-mock-region successfully!' ,
117- ] ,
118- [
119- 'Upload file: path/to/asset.c6a72ed7e7e83f01a000b75885758088fa050298a31a1e95d37ac88f08e42315.zip) to bucket: cdk-ajmywduza-assets-mock-region successfully!' ,
120- ] ,
121125 ] ) ;
122126 } ) ;
123127
124128 it ( 'should log and skip if no assets to publish' , async ( ) => {
125- const emptyAssets = { files : { } } as unknown as CdkAssets ;
126-
127- await publishAssets ( emptyAssets , mockContext ) ;
129+ await publishAssets ( [ ] , mockContext ) ;
128130
129131 expect ( mockedInfoLogger ) . toHaveBeenCalledWith ( 'No assets to publish, skipped!' ) ;
130132 } ) ;
131133 } ) ;
134+
135+ describe ( 'Unit test for cleanupAssets' , ( ) => {
136+ it ( 'should cleanup and delete the bucket when given assets is valid' , async ( ) => {
137+ mockedExistsSync . mockReturnValue ( true ) ;
138+ mockedReaddirSync . mockReturnValueOnce ( [ 'file1' , 'file2' ] ) ;
139+ mockedLstatSync
140+ . mockReturnValueOnce ( { isFile : ( ) => true } as Stats )
141+ . mockReturnValueOnce ( { isFile : ( ) => true } as Stats ) ;
142+ mockedGenerateAsync . mockResolvedValue ( Buffer . from ( 'mock-zip-content' ) ) ;
143+
144+ await cleanupAssets ( await constructAssets ( assetsFixture , 'mock-region' ) , defaultContext ) ;
145+
146+ expect ( mockedDelete ) . toHaveBeenCalledTimes ( 2 ) ;
147+ expect ( mockedDelete . mock . calls ) . toEqual ( [
148+ [ '55d1d2dd5d6c1b083a04c15431f70da1f2840b9de06383411cbf7eda2a512efe.zip' ] ,
149+ [ 'c6a72ed7e7e83f01a000b75885758088fa050298a31a1e95d37ac88f08e42315.zip' ] ,
150+ ] ) ;
151+ expect ( mockedDeleteBucket ) . toHaveBeenCalledTimes ( 1 ) ;
152+ expect ( mockedDeleteBucket ) . toHaveBeenCalledWith ( 'cdk-ajmywduza-assets-mock-region' ) ;
153+ } ) ;
154+
155+ it ( 'should skip the cleanupAssets when there is no assets' , async ( ) => {
156+ await cleanupAssets ( [ ] , defaultContext ) ;
157+
158+ expect ( mockedInfoLogger ) . toHaveBeenCalledWith ( 'No assets to cleanup, skipped!' ) ;
159+ } ) ;
160+ } ) ;
132161} ) ;
0 commit comments