|
| 1 | +import * as path from 'path'; |
| 2 | + |
| 3 | +jest.mock('../../../common/util/fs', () => ({ |
| 4 | + isFileExists: () => { |
| 5 | + if (process.env.fileExists === 'true') { |
| 6 | + return true; |
| 7 | + } |
| 8 | + return false; |
| 9 | + }, |
| 10 | + isDirectoryExists: () => { |
| 11 | + if (process.env.directoryExists === 'true') { |
| 12 | + return true; |
| 13 | + } |
| 14 | + return false; |
| 15 | + }, |
| 16 | +})); |
| 17 | + |
| 18 | +jest.mock('ts-command-line-args', () => ({ |
| 19 | + __esModule: true, |
| 20 | + parse: () => { |
| 21 | + switch (process.env.result) { |
| 22 | + case 'manifests-are-provided': |
| 23 | + return {manifests: ['mock-manifest1.yaml', 'mock-manifest2.yaml']}; |
| 24 | + case 'directory-is-provided': |
| 25 | + return {manifests: ['/mock-directory']}; |
| 26 | + case 'flags-are-not-provided': |
| 27 | + return {manifests: undefined, name: undefined, description: undefined}; |
| 28 | + case 'manifest-is-not-yaml': |
| 29 | + return {manifests: ['mock-manifest1.yaml', './mock-manifest2']}; |
| 30 | + case 'env-throw-error': |
| 31 | + throw new Error('mock-error'); |
| 32 | + case 'env-throw': |
| 33 | + throw 'mock-error'; |
| 34 | + default: |
| 35 | + return { |
| 36 | + manifests: ['mock-manifest1.yaml', 'mock-manifest2.yaml'], |
| 37 | + output: 'mock-output', |
| 38 | + }; |
| 39 | + } |
| 40 | + }, |
| 41 | +})); |
| 42 | + |
| 43 | +import {ERRORS} from '@grnsft/if-core/utils'; |
| 44 | + |
| 45 | +import {parseIfMergeArgs} from '../../../if-merge/util/args'; |
| 46 | + |
| 47 | +import {STRINGS as COMMON_STRINGS} from '../../../common/config'; |
| 48 | + |
| 49 | +import {STRINGS} from '../../../if-merge/config'; |
| 50 | + |
| 51 | +const {InvalidDirectoryError, CliSourceFileError, ParseCliParamsError} = ERRORS; |
| 52 | + |
| 53 | +const {DIRECTORY_NOT_FOUND, MANIFEST_NOT_FOUND} = COMMON_STRINGS; |
| 54 | +const {MANIFEST_IS_NOT_YAML} = STRINGS; |
| 55 | + |
| 56 | +describe('if-merge/util/args: ', () => { |
| 57 | + const originalEnv = process.env; |
| 58 | + |
| 59 | + describe('parseIfMergeArgs(): ', () => { |
| 60 | + const manifests = [ |
| 61 | + path.join(process.cwd(), 'mock-manifest1.yaml'), |
| 62 | + path.join(process.cwd(), 'mock-manifest2.yaml'), |
| 63 | + ]; |
| 64 | + it('executes when `manifest` is provided.', async () => { |
| 65 | + process.env.fileExists = 'true'; |
| 66 | + process.env.result = 'manifests-are-provided'; |
| 67 | + const response = await parseIfMergeArgs(); |
| 68 | + |
| 69 | + expect.assertions(1); |
| 70 | + |
| 71 | + expect(response).toEqual({ |
| 72 | + name: undefined, |
| 73 | + description: undefined, |
| 74 | + manifests, |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + it('executes when the directory is provided.', async () => { |
| 79 | + process.env.directoryExists = 'true'; |
| 80 | + process.env.result = 'directory-is-provided'; |
| 81 | + |
| 82 | + const response = await parseIfMergeArgs(); |
| 83 | + |
| 84 | + expect.assertions(1); |
| 85 | + |
| 86 | + expect(response).toEqual({ |
| 87 | + name: undefined, |
| 88 | + description: undefined, |
| 89 | + manifests: ['/mock-directory'], |
| 90 | + }); |
| 91 | + }); |
| 92 | + |
| 93 | + it('throws an error when the directory does not exist.', async () => { |
| 94 | + process.env.directoryExists = 'false'; |
| 95 | + process.env.result = 'directory-is-provided'; |
| 96 | + expect.assertions(1); |
| 97 | + |
| 98 | + try { |
| 99 | + await parseIfMergeArgs(); |
| 100 | + } catch (error) { |
| 101 | + expect(error).toEqual(new InvalidDirectoryError(DIRECTORY_NOT_FOUND)); |
| 102 | + } |
| 103 | + }); |
| 104 | + |
| 105 | + it('throws an error if one of manifests is not a yaml.', async () => { |
| 106 | + process.env.fileExists = 'true'; |
| 107 | + process.env.result = 'manifest-is-not-yaml'; |
| 108 | + expect.assertions(1); |
| 109 | + |
| 110 | + try { |
| 111 | + await parseIfMergeArgs(); |
| 112 | + } catch (error) { |
| 113 | + if (error instanceof Error) { |
| 114 | + expect(error).toEqual( |
| 115 | + new CliSourceFileError(MANIFEST_IS_NOT_YAML('./mock-manifest2')) |
| 116 | + ); |
| 117 | + } |
| 118 | + } |
| 119 | + }); |
| 120 | + |
| 121 | + it('throws an error if `manifest` path is invalid.', async () => { |
| 122 | + process.env.fileExists = 'false'; |
| 123 | + process.env.result = 'manifest-is-not-yaml'; |
| 124 | + expect.assertions(1); |
| 125 | + |
| 126 | + try { |
| 127 | + await parseIfMergeArgs(); |
| 128 | + } catch (error) { |
| 129 | + if (error instanceof Error) { |
| 130 | + expect(error).toEqual( |
| 131 | + new ParseCliParamsError(`mock-manifest1.yaml ${MANIFEST_NOT_FOUND}`) |
| 132 | + ); |
| 133 | + } |
| 134 | + } |
| 135 | + }); |
| 136 | + |
| 137 | + it('throws an error if parsing failed.', async () => { |
| 138 | + process.env.result = 'env-throw-error'; |
| 139 | + expect.assertions(1); |
| 140 | + |
| 141 | + try { |
| 142 | + await parseIfMergeArgs(); |
| 143 | + } catch (error) { |
| 144 | + if (error instanceof Error) { |
| 145 | + expect(error).toEqual(new ParseCliParamsError('mock-error')); |
| 146 | + } |
| 147 | + } |
| 148 | + }); |
| 149 | + |
| 150 | + it('throws error if parsing failed (not instance of error).', async () => { |
| 151 | + process.env.result = 'env-throw'; |
| 152 | + expect.assertions(1); |
| 153 | + |
| 154 | + try { |
| 155 | + await parseIfMergeArgs(); |
| 156 | + } catch (error) { |
| 157 | + expect(error).toEqual('mock-error'); |
| 158 | + } |
| 159 | + }); |
| 160 | + }); |
| 161 | + |
| 162 | + process.env = originalEnv; |
| 163 | +}); |
0 commit comments