Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build If-env #790

Merged
merged 73 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from 36 commits
Commits
Show all changes
73 commits
Select commit Hold shift + click to select a range
1adbf1d
feat(util): add function to check if the file exists in the given path
manushak Jun 7, 2024
152cfe3
feat(config): add 'MANIFEST_NOT_FOUND' string
manushak Jun 7, 2024
5fd1e79
feat(types): adds IFEnvArgs interface
manushak Jun 7, 2024
33ef812
feat(util): add if-env releated functions
manushak Jun 7, 2024
6986792
test(util): add args test
manushak Jun 7, 2024
13a1ab2
feat(config): add `IF_ENV` config
manushak Jun 7, 2024
9d0d73c
feat(src): add if-env template manifest file
manushak Jun 7, 2024
f9d4de7
feat(src): add if-env functionality
manushak Jun 7, 2024
4139704
feat(package): add `if-env` into bin and scripts
manushak Jun 7, 2024
1f6b3be
Merge branch 'main' into if-env
manushak Jun 7, 2024
10cf52a
fix(src): move template manifest to the top level of the scr folder
manushak Jun 10, 2024
caf68b1
feat(config): add log strings
manushak Jun 10, 2024
a28f05b
feat(types): add env types
manushak Jun 10, 2024
ec3ce36
feat(util): move some env functions into helpers.ts
manushak Jun 10, 2024
1f0700e
fix(src): move env to the src folder
manushak Jun 10, 2024
4583da3
fix(package): fix env file path
manushak Jun 10, 2024
9dec82b
test(mocks): add fs.stat mock function
manushak Jun 11, 2024
ec43b2d
test(util): add test for npm manipulation functions in helpers.ts
manushak Jun 11, 2024
91c1bed
test(util): fix commit lint error
manushak Jun 11, 2024
76c6e80
fix(src): update return data of parseIfEnvArgs
manushak Jun 13, 2024
2d2ca0e
test(util): update test of parseIfEnvArgs function
manushak Jun 13, 2024
be6412a
fix(src): remove functioanlity that create sepereate folder
manushak Jun 13, 2024
9c4983d
Merge branch 'main' into if-env
manushak Jun 13, 2024
ee94393
Merge branch 'main' into if-env
manushak Jun 14, 2024
1e04a0e
fix(src): get all dependencies from the manifest file
manushak Jun 17, 2024
c3a1d41
Merge branch 'main' into if-env
manushak Jun 17, 2024
22febb1
chore(src): fetch changes from remote
narekhovhannisyan Jun 18, 2024
be0862b
feat(package): init cross env
narekhovhannisyan Jun 18, 2024
4a8a962
fix(src): fix getting command working directory
manushak Jun 18, 2024
ea5e643
Merge branch 'if-env' of https://github.com/Green-Software-Foundation…
manushak Jun 18, 2024
aebd849
feat(types): add `cmd` property to IFEnvArgs type
manushak Jun 19, 2024
d6f4280
feat(config): add `cmd` flag for if-env
manushak Jun 19, 2024
c9fc62b
feat(util): update parseIfEnvArgs function to add cmd flag
manushak Jun 19, 2024
a2dd022
feat(types): add cmd property into EnvironmentOptions type and add Ma…
manushak Jun 19, 2024
ff44391
fix(src): add cmd flag, fix template manifest directory and package.j…
manushak Jun 19, 2024
f1b204a
fix(src): rename `cmd` to `cwd`
manushak Jun 19, 2024
6cb5356
Merge branch 'main' into if-env
jmcook1186 Jun 19, 2024
6819c5c
fix(lib): fix import for error strings
jmcook1186 Jun 19, 2024
c9b6db6
fix(lib): apply linter
jmcook1186 Jun 19, 2024
3c09a50
fix(config): move template manifest into config directory
manushak Jun 19, 2024
14b2b38
fix(src): update template manifest path
manushak Jun 19, 2024
f6c6ce1
feat(util): move the isFileExists funtion to the relevant file
manushak Jun 20, 2024
9ff31c8
feat(util): move the npm related funtions to the relevant file
manushak Jun 20, 2024
5e38270
fix(util): move some functions to the coresponding files
manushak Jun 20, 2024
5483ad7
fix(util): update isFileExists path
manushak Jun 20, 2024
2a6f242
feat(util): move the npm related funtions to the relevant file
manushak Jun 20, 2024
0b403b2
test(util): fix isFileExists path
manushak Jun 20, 2024
ab54440
feat(mocks): add mocks for util/npm functionality
manushak Jun 20, 2024
6a3ab7b
test(util): add test file for util/fs
manushak Jun 20, 2024
83fd072
test(util): add test file for util/npm
manushak Jun 20, 2024
46afe5a
fix(types): move some type to the corresponding file
manushak Jun 20, 2024
07d91a4
test(util): move not related tests
manushak Jun 20, 2024
041098c
feat(src): move some function to the corresponding files
manushak Jun 20, 2024
f3cd231
Merge branch 'main' into if-env
manushak Jun 20, 2024
cf78e06
fix(util): add boolean argument to check if need to update package de…
manushak Jun 21, 2024
1545fcd
test(util): update the test according to changes
manushak Jun 21, 2024
8e78685
fix(src): update package depenedencies depend on cwd flag
manushak Jun 21, 2024
67e1595
fix(util): add some function into helpers.ts from env.ts
manushak Jun 24, 2024
b3e49bb
fix(util): remove unnecessary logs
manushak Jun 24, 2024
3bfa285
test(util): add test for helpers function
manushak Jun 24, 2024
276589f
fix(src): move some funcions into helpers
manushak Jun 24, 2024
fc8e42d
fix(src): fix merge conflicts with main
manushak Jun 25, 2024
13a7019
fix(util): change `CliInputError` to `ParseCliParamsError` error
manushak Jun 25, 2024
66a1c4c
test(util): fix npm test
manushak Jun 25, 2024
0e789b1
test(mocks): add mocks for fs.writeFile
manushak Jun 25, 2024
accf32f
test(util): add missied tests
manushak Jun 25, 2024
d46d71c
fix(util): add `MissingPluginDependenciesError` custom error class
manushak Jun 25, 2024
04e514a
feat(package): update if-core version
manushak Jun 25, 2024
ad2f87d
fix(util): fix dependencies append logic
manushak Jun 25, 2024
621a311
fix(util): typo in npm
narekhovhannisyan Jun 26, 2024
136f062
test(util): add cases for initializeAndInstallLibs
narekhovhannisyan Jun 26, 2024
7a466cc
test(util): put back original process exit
narekhovhannisyan Jun 26, 2024
ed31d52
fix(src): add newline to package.json
narekhovhannisyan Jun 26, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion manifests/plugins/csv-lookup/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:
method: CloudMetadata
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
20 changes: 20 additions & 0 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,8 +7,9 @@
"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+-+"
Expand Down Expand Up @@ -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 All @@ -85,4 +88,4 @@
},
"stability": "stable",
"types": "src/index.d.ts"
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing newline

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@narekhovhannisyan in cases like this, please just add the newline and commit. It's silly for these changes to involve >1 person.

narekhovhannisyan marked this conversation as resolved.
Show resolved Hide resolved
8 changes: 8 additions & 0 deletions src/__mocks__/fs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,11 @@ export const writeFile = async (pathToFile: string, content: string) => {
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/helpers', () => ({
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';

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

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

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

describe('util/args: ', () => {
Expand Down Expand Up @@ -299,5 +329,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 CliInputError(FILE_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 CliInputError(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 CliInputError('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;
});
104 changes: 102 additions & 2 deletions src/__tests__/unit/util/helpers.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import * as fs from 'fs';
import * as path from 'path';

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

const mockInfo = jest.fn();
const mockWarn = jest.fn();
const mockError = jest.fn();

Expand All @@ -6,22 +12,30 @@ jest.mock('node:readline/promises', () =>
);
jest.mock('../../../util/logger', () => ({
logger: {
info: mockInfo,
warn: mockWarn,
error: mockError,
},
}));

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

import {
andHandle,
checkIfEqual,
formatNotMatchingLog,
mergeObjects,
oneIsPrimitive,
parseManifestFromStdin,
isFileExists,
installDependencies,
initPackageJsonIfNotExists,
} from '../../../util/helpers';
import {Difference} from '../../../types/lib/compare';
import {STRINGS} from '../../../config/strings';

const {WriteFileError} = ERRORS;
const {INITIALIZING_PACKAGE_JSON, INSTALLING_NPM_PACKAGES} = STRINGS;

describe('util/helpers: ', () => {
describe('andHandle(): ', () => {
Expand All @@ -47,9 +61,7 @@ describe('util/helpers: ', () => {
expect(mockError).toHaveBeenCalledTimes(1);
});
});
});

describe('util/helpers: ', () => {
describe('mergeObjects(): ', () => {
it('does not override input.', () => {
expect.assertions(1);
Expand Down Expand Up @@ -408,4 +420,92 @@ description: mock-description
expect(response).toBeFalsy();
});
});

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);
});
});

describe('npm manipulation tests.', () => {
const helpers = require('../../../util/helpers');
const folderPath = path.resolve(__dirname, 'npm-test');

beforeAll(() => {
if (!fs.existsSync(folderPath)) {
fs.mkdirSync(folderPath, {recursive: true});
}
});

afterAll(() => {
if (fs.existsSync(folderPath)) {
fs.rmSync(folderPath, {recursive: true, force: true});
}
});

describe('initPackageJsonIfNotExists(): ', () => {
it('initializes package.json if it does not exist.', async () => {
const spyExecPromise = jest.spyOn(helpers, 'execPromise');
isFileExists('true');

await initPackageJsonIfNotExists(folderPath);

expect.assertions(2);
expect(mockInfo).toHaveBeenCalledWith(INITIALIZING_PACKAGE_JSON);
expect(spyExecPromise).toHaveBeenCalledWith('npm init -y', {
cwd: folderPath,
});
});

it('returns the package.json path if it exists.', async () => {
const packageJsonPath = path.resolve(folderPath, 'package.json');
isFileExists('false');

const result = await initPackageJsonIfNotExists(folderPath);

expect.assertions(1);
expect(result).toBe(packageJsonPath);
});
});

describe('installDependencies(): ', () => {
const dependencies = {
'@grnsft/if': '^0.3.3-beta.0',
};

it('calls execPromise with the correct arguments.', async () => {
const spyExecPromise = jest.spyOn(helpers, 'execPromise');
const formattedDependencies = ['@grnsft/[email protected]'];
expect.assertions(1);

await installDependencies(folderPath, dependencies);

expect(spyExecPromise).toHaveBeenCalledWith(
`npm install ${formattedDependencies.join(' ')}`,
{cwd: folderPath}
);
}, 30000);

it('logs the installation message.', async () => {
const dependencies = {
'@grnsft/if': '^0.3.3-beta.0',
};

await installDependencies(folderPath, dependencies);

expect.assertions(1);
expect(mockInfo).toHaveBeenCalledWith(INSTALLING_NPM_PACKAGES);
});
});
});
});
Loading