|
| 1 | +import * as ros from '@alicloud/ros-cdk-core'; |
1 | 2 | import * as ossDeployment from '@alicloud/ros-cdk-ossdeployment';
|
2 |
| -import { getFileSource } from '../../src/common'; |
| 3 | +import { getFileSource, replaceReference } from '../../src/common'; |
3 | 4 | import fs from 'node:fs';
|
| 5 | +import { context } from '../fixtures/contextFixture'; |
4 | 6 |
|
5 | 7 | jest.mock('@alicloud/ros-cdk-ossdeployment');
|
6 |
| - |
| 8 | +jest.mock('@alicloud/ros-cdk-core'); |
7 | 9 | const fcName = 'testFunction';
|
8 | 10 | const location = 'tests/fixtures/artifacts/artifact.zip';
|
9 | 11 |
|
10 |
| -describe('getFileSource', () => { |
11 |
| - it('should return the correct ossDeployment source', () => { |
12 |
| - getFileSource(fcName, location); |
13 |
| - expect(ossDeployment.Source.asset).toHaveBeenCalledWith( |
14 |
| - `${process.cwd()}/${location}`, |
15 |
| - {}, |
16 |
| - `${fcName}/50861cd99a3a678356030f5f189300af.zip`, |
17 |
| - ); |
| 12 | +describe('Unit test for iacHelper', () => { |
| 13 | + beforeEach(() => { |
| 14 | + jest.restoreAllMocks(); |
| 15 | + }); |
| 16 | + describe('Unit test for getFileSource', () => { |
| 17 | + it('should return the correct ossDeployment source', () => { |
| 18 | + getFileSource(fcName, location); |
| 19 | + expect(ossDeployment.Source.asset).toHaveBeenCalledWith( |
| 20 | + `${process.cwd()}/${location}`, |
| 21 | + {}, |
| 22 | + `${fcName}/50861cd99a3a678356030f5f189300af.zip`, |
| 23 | + ); |
| 24 | + }); |
| 25 | + |
| 26 | + it('should throw an error if the path is a directory', () => { |
| 27 | + jest.spyOn(fs, 'lstatSync').mockReturnValue({ isDirectory: () => true } as fs.Stats); |
| 28 | + |
| 29 | + expect(() => getFileSource(fcName, location)).toThrow( |
| 30 | + 'The provided path is a directory, not a file.', |
| 31 | + ); |
| 32 | + }); |
18 | 33 | });
|
19 | 34 |
|
20 |
| - it('should throw an error if the path is a directory', () => { |
21 |
| - jest.spyOn(fs, 'lstatSync').mockReturnValue({ isDirectory: () => true } as fs.Stats); |
| 35 | + describe('Unit test for replaceReference', () => { |
| 36 | + it('should return the value if it does not contain any reference', () => { |
| 37 | + const value = 'testValue'; |
| 38 | + expect(replaceReference(value, context)).toEqual(value); |
| 39 | + expect(ros.Fn.ref).not.toHaveBeenCalled(); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should return the value if it contains a stage reference', () => { |
| 43 | + const value = 'testValue-${ctx.stage}'; |
| 44 | + expect(replaceReference(value, context)).toEqual('testValue-test'); |
| 45 | + |
| 46 | + expect(ros.Fn.ref).not.toHaveBeenCalled(); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should return the value if it match a vars reference', () => { |
| 50 | + replaceReference('${vars.testVar}', context); |
| 51 | + |
| 52 | + expect(ros.Fn.ref).toHaveBeenCalledWith('testVar'); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should return the value if it match a stages reference', () => { |
| 56 | + replaceReference('${stages.testStage}', context); |
| 57 | + expect(ros.Fn.findInMap).toHaveBeenCalledWith('stages', 'test', 'testStage'); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should return the value if it contains a stages reference', () => { |
| 61 | + replaceReference('abcd-${stages.testVar}-efg', context); |
| 62 | + |
| 63 | + expect(ros.Fn.sub).toHaveBeenCalledWith('abcd-${testVar}-efg'); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should return the value if it contains both stages reference and vars references', () => { |
| 67 | + replaceReference('abcd-${stages.testVar}-efg-${vars.newTestVar}-hij', context); |
| 68 | + |
| 69 | + expect(ros.Fn.sub).toHaveBeenCalledWith('abcd-${testVar}-efg-${newTestVar}-hij'); |
| 70 | + }); |
| 71 | + |
| 72 | + it('should return the value if it contains a functions reference', () => { |
| 73 | + replaceReference('${functions.testFunction}', context); |
22 | 74 |
|
23 |
| - expect(() => getFileSource(fcName, location)).toThrow( |
24 |
| - 'The provided path is a directory, not a file.', |
25 |
| - ); |
| 75 | + expect(ros.Fn.getAtt).toHaveBeenCalledWith('testFunction', 'FunctionName'); |
| 76 | + }); |
26 | 77 | });
|
27 | 78 | });
|
0 commit comments