Skip to content

Commit

Permalink
Merge pull request #865 from Green-Software-Foundation/if-check2
Browse files Browse the repository at this point in the history
Build `If-check`
  • Loading branch information
jmcook1186 authored Jun 28, 2024
2 parents 2d8a8d8 + 86da2e6 commit 95ebafb
Show file tree
Hide file tree
Showing 16 changed files with 701 additions and 19 deletions.
9 changes: 5 additions & 4 deletions package-lock.json

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

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@
"bin": {
"if-diff": "./build/diff.js",
"if-run": "./build/index.js",
"if-env": "./build/env.js"
"if-env": "./build/env.js",
"if-check": "./build/check.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.9",
"@grnsft/if-core": "^0.0.10",
"axios": "^1.7.2",
"csv-parse": "^5.5.6",
"csv-stringify": "^6.4.6",
Expand Down Expand Up @@ -75,6 +76,7 @@
"coverage": "jest --verbose --coverage --testPathPattern=src/__tests__/unit",
"fix": "gts fix",
"fix:package": "fixpack",
"if-check": "cross-env CURRENT_DIR=$(node -p \"process.env.INIT_CWD\") npx ts-node src/check.ts",
"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",
Expand Down
50 changes: 50 additions & 0 deletions src/__mocks__/fs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,53 @@ export const stat = async (filePath: string) => {
throw new Error('File not found.');
}
};

export const access = async (directoryPath: string) => {
if (directoryPath === 'true') {
return true;
} else {
throw new Error('Directory not found.');
}
};

export const unlink = async (filePath: string) => {
if (filePath === 'true') {
return;
} else {
throw new Error('File not found.');
}
};

export const readdir = (directoryPath: string) => {
if (directoryPath.includes('mock-empty-directory')) {
return [];
}

if (directoryPath.includes('mock-directory')) {
return ['file1.yaml', 'file2.yml', 'file3.txt'];
}

if (directoryPath.includes('mock-sub-directory')) {
return ['subdir/file2.yml', 'file1.yaml'];
}

return [];
};

export const lstat = (filePath: string) => {
if (
filePath.includes('mock-directory') ||
filePath.includes('mock-sub-directory/subdir')
) {
return {
isDirectory: () => true,
};
}

if (filePath.includes('mock-file')) {
return {
isDirectory: () => false,
};
}
return;
};
72 changes: 72 additions & 0 deletions src/__mocks__/mock-manifest.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
name: template manifest
description: auto-generated template
tags: null
initialize:
plugins:
memory-energy-from-memory-util:
path: builtin
method: Coefficient
global-config:
input-parameter: memory/utilization
coefficient: 0.0001
output-parameter: memory/energy
outputs:
- yaml
execution:
command: >-
/Users/manushak/.npm/_npx/1bf7c3c15bf47d04/node_modules/.bin/ts-node
/Users/manushak/Documents/Projects/Green-Software/if/src/index.ts -m
./src/env-template.yml -o ./manifests/outputs/template
environment:
if-version: 0.4.0
os: macOS
os-version: 13.6.6
node-version: 20.12.2
date-time: 2024-06-18T08:39:55.771Z (UTC)
dependencies:
- "@babel/[email protected]"
- "@babel/[email protected]"
- "@commitlint/[email protected]"
- "@commitlint/[email protected]"
- "@grnsft/[email protected]"
- "@jest/[email protected]"
- "@types/[email protected]"
- "@types/[email protected]"
- "@types/[email protected]"
- "@types/[email protected]"
- [email protected]
- [email protected]
- [email protected]
- [email protected]
- [email protected]
- [email protected]
- [email protected]
- [email protected]
- [email protected]
- [email protected]
- [email protected]
- [email protected]
- [email protected]
- [email protected]
- [email protected]
- [email protected]
- [email protected]
- [email protected]
- [email protected]
- [email protected]
status: success
tree:
children:
child:
pipeline:
- memory-energy-from-memory-util
config: null
inputs:
- timestamp: 2023-12-12T00:00:00.000Z
duration: 3600
memory/utilization: 10
outputs:
- timestamp: 2023-12-12T00:00:00.000Z
duration: 3600
memory/utilization: 10
memory/energy: 0.001
122 changes: 119 additions & 3 deletions src/__tests__/unit/util/args.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ jest.mock('../../../util/fs', () => ({
}
return false;
},
isDirectoryExists: () => {
if (process.env.directoryExists === 'true') {
return true;
}
return false;
},
}));

jest.mock('ts-command-line-args', () => ({
Expand Down Expand Up @@ -70,8 +76,6 @@ jest.mock('ts-command-line-args', () => ({
case 'diff-throw':
throw 'mock-error';
/** If-env mocks */
// case 'env-manifest-is-missing':
// return;
case 'manifest-install-provided':
return {
install: true,
Expand All @@ -85,6 +89,13 @@ jest.mock('ts-command-line-args', () => ({
throw new Error('mock-error');
case 'env-throw':
throw 'mock-error';
/** If-check */
case 'manifest-is-provided':
return {manifest: 'mock-manifest.yaml'};
case 'directory-is-provided':
return {directory: '/mock-directory'};
case 'flags-are-not-provided':
return {manifest: undefined, directory: undefined};
default:
return {
manifest: 'mock-manifest.yaml',
Expand All @@ -99,20 +110,28 @@ import {ERRORS} from '@grnsft/if-core/utils';

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

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

const {CliSourceFileError, ParseCliParamsError} = ERRORS;
const {
CliSourceFileError,
ParseCliParamsError,
InvalidDirectoryError,
MissingCliFlagsError,
} = ERRORS;

const {
MANIFEST_IS_MISSING,
TARGET_IS_NOT_YAML,
INVALID_TARGET,
SOURCE_IS_NOT_YAML,
MANIFEST_NOT_FOUND,
DIRECTORY_NOT_FOUND,
IF_CHECK_FLAGS_MISSING,
} = STRINGS;

describe('util/args: ', () => {
Expand Down Expand Up @@ -402,5 +421,102 @@ describe('util/args: ', () => {
});
});

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

expect.assertions(1);

expect(response).toEqual({manifest: 'mock-manifest.yaml'});
});

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

const response = await parseIfCheckArgs();

expect.assertions(1);

expect(response).toEqual({directory: '/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 parseIfCheckArgs();
} catch (error) {
expect(error).toEqual(new InvalidDirectoryError(DIRECTORY_NOT_FOUND));
}
});

it('throws an error when both `manifest` and `directory` flags are not provided.', async () => {
process.env.result = 'flags-are-not-provided';
expect.assertions(1);

try {
await parseIfCheckArgs();
} catch (error) {
expect(error).toEqual(new MissingCliFlagsError(IF_CHECK_FLAGS_MISSING));
}
});

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

process.env = originalEnv;
});
Loading

0 comments on commit 95ebafb

Please sign in to comment.