Skip to content

Commit 9ddcede

Browse files
Merge pull request #927 from Green-Software-Foundation/if-merge
Build If-merge
2 parents b0933d7 + dd24689 commit 9ddcede

File tree

20 files changed

+611
-19
lines changed

20 files changed

+611
-19
lines changed

package-lock.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"if-run": "./build/if-run/index.js",
1212
"if-env": "./build/if-env/index.js",
1313
"if-check": "./build/if-check/index.js",
14-
"if-csv": "./build/if-csv/index.js"
14+
"if-csv": "./build/if-csv/index.js",
15+
"if-merge": "./build/if-merge/index.js"
1516
},
1617
"bugs": {
1718
"url": "https://github.com/Green-Software-Foundation/if/issues/new?assignees=&labels=feedback&projects=&template=feedback.md&title=Feedback+-+"
@@ -80,6 +81,7 @@
8081
"if-csv": "cross-env CURRENT_DIR=$(node -p \"process.env.INIT_CWD\") npx ts-node src/if-csv/index.ts",
8182
"if-diff": "npx ts-node src/if-diff/index.ts",
8283
"if-env": "cross-env CURRENT_DIR=$(node -p \"process.env.INIT_CWD\") npx ts-node src/if-env/index.ts",
84+
"if-merge": "cross-env CURRENT_DIR=$(node -p \"process.env.INIT_CWD\") npx ts-node src/if-merge/index.ts",
8385
"if-run": "npx ts-node src/if-run/index.ts",
8486
"lint": "gts lint",
8587
"pre-commit": "lint-staged",

src/__mocks__/fs/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,8 @@ export const readdir = (directoryPath: string) => {
155155
export const lstat = (filePath: string) => {
156156
if (
157157
filePath.includes('mock-directory') ||
158-
filePath.includes('mock-sub-directory/subdir')
158+
filePath.includes('mock-sub-directory/subdir') ||
159+
filePath === 'true'
159160
) {
160161
return {
161162
isDirectory: () => true,

src/__tests__/if-check/util/args.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,9 @@ const {
7272
CliSourceFileError,
7373
ParseCliParamsError,
7474
} = ERRORS;
75-
const {DIRECTORY_NOT_FOUND, IF_CHECK_FLAGS_MISSING} = STRINGS;
76-
const {SOURCE_IS_NOT_YAML, MANIFEST_NOT_FOUND} = COMMON_STRINGS;
75+
const {IF_CHECK_FLAGS_MISSING} = STRINGS;
76+
const {SOURCE_IS_NOT_YAML, MANIFEST_NOT_FOUND, DIRECTORY_NOT_FOUND} =
77+
COMMON_STRINGS;
7778

7879
describe('if-check/util: ', () => {
7980
const originalEnv = process.env;
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
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

Comments
 (0)