Skip to content

Commit

Permalink
Merge pull request #927 from Green-Software-Foundation/if-merge
Browse files Browse the repository at this point in the history
Build If-merge
  • Loading branch information
narekhovhannisyan authored Aug 9, 2024
2 parents b0933d7 + dd24689 commit 9ddcede
Show file tree
Hide file tree
Showing 20 changed files with 611 additions and 19 deletions.
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"if-run": "./build/if-run/index.js",
"if-env": "./build/if-env/index.js",
"if-check": "./build/if-check/index.js",
"if-csv": "./build/if-csv/index.js"
"if-csv": "./build/if-csv/index.js",
"if-merge": "./build/if-merge/index.js"
},
"bugs": {
"url": "https://github.com/Green-Software-Foundation/if/issues/new?assignees=&labels=feedback&projects=&template=feedback.md&title=Feedback+-+"
Expand Down Expand Up @@ -80,6 +81,7 @@
"if-csv": "cross-env CURRENT_DIR=$(node -p \"process.env.INIT_CWD\") npx ts-node src/if-csv/index.ts",
"if-diff": "npx ts-node src/if-diff/index.ts",
"if-env": "cross-env CURRENT_DIR=$(node -p \"process.env.INIT_CWD\") npx ts-node src/if-env/index.ts",
"if-merge": "cross-env CURRENT_DIR=$(node -p \"process.env.INIT_CWD\") npx ts-node src/if-merge/index.ts",
"if-run": "npx ts-node src/if-run/index.ts",
"lint": "gts lint",
"pre-commit": "lint-staged",
Expand Down
3 changes: 2 additions & 1 deletion src/__mocks__/fs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ export const readdir = (directoryPath: string) => {
export const lstat = (filePath: string) => {
if (
filePath.includes('mock-directory') ||
filePath.includes('mock-sub-directory/subdir')
filePath.includes('mock-sub-directory/subdir') ||
filePath === 'true'
) {
return {
isDirectory: () => true,
Expand Down
5 changes: 3 additions & 2 deletions src/__tests__/if-check/util/args.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@ const {
CliSourceFileError,
ParseCliParamsError,
} = ERRORS;
const {DIRECTORY_NOT_FOUND, IF_CHECK_FLAGS_MISSING} = STRINGS;
const {SOURCE_IS_NOT_YAML, MANIFEST_NOT_FOUND} = COMMON_STRINGS;
const {IF_CHECK_FLAGS_MISSING} = STRINGS;
const {SOURCE_IS_NOT_YAML, MANIFEST_NOT_FOUND, DIRECTORY_NOT_FOUND} =
COMMON_STRINGS;

describe('if-check/util: ', () => {
const originalEnv = process.env;
Expand Down
163 changes: 163 additions & 0 deletions src/__tests__/if-merge/util/args.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
import * as path from 'path';

jest.mock('../../../common/util/fs', () => ({
isFileExists: () => {
if (process.env.fileExists === 'true') {
return true;
}
return false;
},
isDirectoryExists: () => {
if (process.env.directoryExists === 'true') {
return true;
}
return false;
},
}));

jest.mock('ts-command-line-args', () => ({
__esModule: true,
parse: () => {
switch (process.env.result) {
case 'manifests-are-provided':
return {manifests: ['mock-manifest1.yaml', 'mock-manifest2.yaml']};
case 'directory-is-provided':
return {manifests: ['/mock-directory']};
case 'flags-are-not-provided':
return {manifests: undefined, name: undefined, description: undefined};
case 'manifest-is-not-yaml':
return {manifests: ['mock-manifest1.yaml', './mock-manifest2']};
case 'env-throw-error':
throw new Error('mock-error');
case 'env-throw':
throw 'mock-error';
default:
return {
manifests: ['mock-manifest1.yaml', 'mock-manifest2.yaml'],
output: 'mock-output',
};
}
},
}));

import {ERRORS} from '@grnsft/if-core/utils';

import {parseIfMergeArgs} from '../../../if-merge/util/args';

import {STRINGS as COMMON_STRINGS} from '../../../common/config';

import {STRINGS} from '../../../if-merge/config';

const {InvalidDirectoryError, CliSourceFileError, ParseCliParamsError} = ERRORS;

const {DIRECTORY_NOT_FOUND, MANIFEST_NOT_FOUND} = COMMON_STRINGS;
const {MANIFEST_IS_NOT_YAML} = STRINGS;

describe('if-merge/util/args: ', () => {
const originalEnv = process.env;

describe('parseIfMergeArgs(): ', () => {
const manifests = [
path.join(process.cwd(), 'mock-manifest1.yaml'),
path.join(process.cwd(), 'mock-manifest2.yaml'),
];
it('executes when `manifest` is provided.', async () => {
process.env.fileExists = 'true';
process.env.result = 'manifests-are-provided';
const response = await parseIfMergeArgs();

expect.assertions(1);

expect(response).toEqual({
name: undefined,
description: undefined,
manifests,
});
});

it('executes when the directory is provided.', async () => {
process.env.directoryExists = 'true';
process.env.result = 'directory-is-provided';

const response = await parseIfMergeArgs();

expect.assertions(1);

expect(response).toEqual({
name: undefined,
description: undefined,
manifests: ['/mock-directory'],
});
});

it('throws an error when the directory does not exist.', async () => {
process.env.directoryExists = 'false';
process.env.result = 'directory-is-provided';
expect.assertions(1);

try {
await parseIfMergeArgs();
} catch (error) {
expect(error).toEqual(new InvalidDirectoryError(DIRECTORY_NOT_FOUND));
}
});

it('throws an error if one of manifests is not a yaml.', async () => {
process.env.fileExists = 'true';
process.env.result = 'manifest-is-not-yaml';
expect.assertions(1);

try {
await parseIfMergeArgs();
} catch (error) {
if (error instanceof Error) {
expect(error).toEqual(
new CliSourceFileError(MANIFEST_IS_NOT_YAML('./mock-manifest2'))
);
}
}
});

it('throws an error if `manifest` path is invalid.', async () => {
process.env.fileExists = 'false';
process.env.result = 'manifest-is-not-yaml';
expect.assertions(1);

try {
await parseIfMergeArgs();
} catch (error) {
if (error instanceof Error) {
expect(error).toEqual(
new ParseCliParamsError(`mock-manifest1.yaml ${MANIFEST_NOT_FOUND}`)
);
}
}
});

it('throws an error if parsing failed.', async () => {
process.env.result = 'env-throw-error';
expect.assertions(1);

try {
await parseIfMergeArgs();
} catch (error) {
if (error instanceof Error) {
expect(error).toEqual(new ParseCliParamsError('mock-error'));
}
}
});

it('throws error if parsing failed (not instance of error).', async () => {
process.env.result = 'env-throw';
expect.assertions(1);

try {
await parseIfMergeArgs();
} catch (error) {
expect(error).toEqual('mock-error');
}
});
});

process.env = originalEnv;
});
Loading

0 comments on commit 9ddcede

Please sign in to comment.