Skip to content

Commit

Permalink
Merge pull request #790 from Green-Software-Foundation/if-env
Browse files Browse the repository at this point in the history
Build `If-env`
  • Loading branch information
MariamKhalatova authored Jun 26, 2024
2 parents fb19f2f + ed31d52 commit 62928e7
Show file tree
Hide file tree
Showing 21 changed files with 1,157 additions and 28 deletions.
2 changes: 1 addition & 1 deletion manifests/plugins/csv-lookup/region-metadata/success.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ tree:
- timestamp: 2023-08-06T00:00
duration: 3600
cloud/provider: Google Cloud
cloud/region: asia-east-1
cloud/region: asia-east1
2 changes: 1 addition & 1 deletion manifests/plugins/divide/success.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: divide
description: success path
tags:
initialize:
# outputs: ['yaml']
outputs: ['yaml']
plugins:
cloud-metadata:
path: builtin
Expand Down
2 changes: 1 addition & 1 deletion manifests/plugins/sum/success.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: sum
description: successful path
tags:
initialize:
outputs: ['yaml']
# outputs: ['yaml']
plugins:
sum:
method: Sum
Expand Down
28 changes: 24 additions & 4 deletions package-lock.json

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

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@
"email": "[email protected]"
},
"bin": {
"if-diff": "./build/diff.js",
"if-run": "./build/index.js",
"if-diff": "./build/diff.js"
"if-env": "./build/env.js"
},
"bugs": {
"url": "https://github.com/Green-Software-Foundation/if/issues/new?assignees=&labels=feedback&projects=&template=feedback.md&title=Feedback+-+"
},
"dependencies": {
"@commitlint/cli": "^18.6.0",
"@commitlint/config-conventional": "^18.6.0",
"@grnsft/if-core": "^0.0.7",
"@grnsft/if-core": "^0.0.9",
"axios": "^1.7.2",
"csv-parse": "^5.5.6",
"csv-stringify": "^6.4.6",
Expand All @@ -37,6 +38,7 @@
"@types/luxon": "^3.4.2",
"@types/node": "^20.8.9",
"axios-mock-adapter": "^1.22.0",
"cross-env": "7.0.3",
"fixpack": "^4.0.0",
"gts": "^5.0.0",
"husky": "^8.0.0",
Expand Down Expand Up @@ -74,6 +76,7 @@
"fix": "gts fix",
"fix:package": "fixpack",
"if-diff": "npx ts-node src/diff.ts",
"if-env": "cross-env CURRENT_DIR=$(node -p \"process.env.INIT_CWD\") npx ts-node src/env.ts",
"if-run": "npx ts-node src/index.ts",
"lint": "gts lint",
"pre-commit": "lint-staged",
Expand Down
62 changes: 52 additions & 10 deletions src/__mocks__/fs/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
import * as YAML from 'js-yaml';
import * as fs from 'fs';
import * as fsAsync from 'fs/promises';
import * as path from 'path';

export const readFile = async (filePath: string) => {
/** mock for util/npm */
if (filePath.includes('package.json-npm')) {
const updatedPath = filePath.replace('-npm', '');
return fs.readFileSync(updatedPath, 'utf8');
}

/** mock for util/json */
if (filePath.includes('json-reject')) {
return Promise.reject(new Error('rejected'));
}

if (filePath.includes('json')) {
if (filePath.includes('param')) {
return JSON.stringify({
Expand Down Expand Up @@ -80,16 +90,48 @@ cpu-cores-available,cpu-cores-utilized,cpu-manufacturer,cpu-model-name,cpu-tdp,g
export const mkdir = (dirPath: string) => dirPath;

export const writeFile = async (pathToFile: string, content: string) => {
if (pathToFile === 'reject') {
throw new Error('Wrong file path');
}
if (pathToFile.includes('package.json-npm1')) {
const updatedPath = pathToFile.replace('-npm1', '');
const fileContent = await fsAsync.readFile(updatedPath, 'utf8');
const fileContentObject = JSON.parse(fileContent);
const parsedContent = JSON.parse(content);

const mockPathToFile = 'mock-pathToFile';
const mockContent = {
name: 'mock-name',
};
const mockObject = YAML.dump(mockContent, {noRefs: true});
for (const property in fileContentObject) {
expect(parsedContent).toHaveProperty(property);
}
} else if (pathToFile.includes('package.json-npm')) {
const updatedPath = pathToFile.replace('-npm', '');
const fileContent = await fsAsync.readFile(updatedPath, 'utf8');

expect(content).toBe(fileContent);
} else if (pathToFile.includes('/manifest.yml')) {
const templateManifest = path.resolve(
__dirname,
'../../config/env-template.yml'
);
const fileContent = await fsAsync.readFile(templateManifest, 'utf8');

expect(pathToFile).toBe(mockPathToFile);
expect(content).toBe(mockObject);
expect(content).toBe(fileContent);
} else {
if (pathToFile === 'reject') {
throw new Error('Wrong file path');
}

const mockPathToFile = 'mock-pathToFile';
const mockContent = {
name: 'mock-name',
};
const mockObject = YAML.dump(mockContent, {noRefs: true});

expect(pathToFile).toBe(mockPathToFile);
expect(content).toBe(mockObject);
}
};

export const stat = async (filePath: string) => {
if (filePath === 'true') {
return true;
} else {
throw new Error('File not found.');
}
};
106 changes: 105 additions & 1 deletion src/__tests__/unit/util/args.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
const processRunningPath = process.cwd();

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

jest.mock('ts-command-line-args', () => ({
__esModule: true,
parse: () => {
Expand Down Expand Up @@ -60,6 +69,22 @@ jest.mock('ts-command-line-args', () => ({
throw new Error('mock-error');
case 'diff-throw':
throw 'mock-error';
/** If-env mocks */
// case 'env-manifest-is-missing':
// return;
case 'manifest-install-provided':
return {
install: true,
manifest: 'mock-manifest.yaml',
};
case 'manifest-is-not-yaml':
return {manifest: 'manifest'};
case 'manifest-path-invalid':
throw new Error(MANIFEST_NOT_FOUND);
case 'env-throw-error':
throw new Error('mock-error');
case 'env-throw':
throw 'mock-error';
default:
return {
manifest: 'mock-manifest.yaml',
Expand All @@ -72,7 +97,11 @@ jest.mock('ts-command-line-args', () => ({
import * as path from 'node:path';
import {ERRORS} from '@grnsft/if-core/utils';

import {parseIEProcessArgs, parseIfDiffArgs} from '../../../util/args';
import {
parseIEProcessArgs,
parseIfDiffArgs,
parseIfEnvArgs,
} from '../../../util/args';

import {STRINGS} from '../../../config';

Expand All @@ -83,6 +112,7 @@ const {
TARGET_IS_NOT_YAML,
INVALID_TARGET,
SOURCE_IS_NOT_YAML,
MANIFEST_NOT_FOUND,
} = STRINGS;

describe('util/args: ', () => {
Expand Down Expand Up @@ -298,5 +328,79 @@ describe('util/args: ', () => {
});
});

describe('parseIfEnvArgs(): ', () => {
it('executes if `manifest` is missing.', async () => {
process.env.fileExists = 'true';
process.env.result = 'manifest-is-missing';
const response = await parseIfEnvArgs();

expect.assertions(1);

expect(response).toEqual({install: undefined});
});

it('executes if `manifest` and `install` are provided.', async () => {
process.env.fileExists = 'true';
process.env.result = 'manifest-install-provided';

const response = await parseIfEnvArgs();

expect.assertions(2);
expect(response).toHaveProperty('install');
expect(response).toHaveProperty('manifest');
});

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

try {
await parseIfEnvArgs();
} catch (error) {
if (error instanceof Error) {
expect(error).toEqual(new CliSourceFileError(SOURCE_IS_NOT_YAML));
}
}
});

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

try {
await parseIfEnvArgs();
} catch (error) {
if (error instanceof Error) {
expect(error).toEqual(new ParseCliParamsError(MANIFEST_NOT_FOUND));
}
}
});

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

try {
await parseIfEnvArgs();
} 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 parseIfEnvArgs();
} catch (error) {
expect(error).toEqual('mock-error');
}
});
});

process.env = originalEnv;
});
21 changes: 21 additions & 0 deletions src/__tests__/unit/util/fs.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {isFileExists} from '../../../util/fs';

jest.mock('fs/promises', () => require('../../../__mocks__/fs'));

describe('util/fs: ', () => {
describe('isFileExists(): ', () => {
it('returns true if the file exists.', async () => {
const result = await isFileExists('true');

expect.assertions(1);
expect(result).toEqual(true);
});

it('returns fale if the file does not exist.', async () => {
const result = await isFileExists('false');

expect.assertions(1);
expect(result).toEqual(false);
});
});
});
Loading

0 comments on commit 62928e7

Please sign in to comment.