Skip to content

Commit

Permalink
Merge pull request #835 from Green-Software-Foundation/fix-piping-issue
Browse files Browse the repository at this point in the history
Fix piping issue
  • Loading branch information
narekhovhannisyan authored Jun 13, 2024
2 parents 3e3ec07 + 640b0ca commit 9aa1302
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 15 deletions.
6 changes: 5 additions & 1 deletion src/__tests__/unit/lib/load.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ import {PARAMETERS} from '../../../config';
import {PluginParams} from '../../../types/interface';

import {STRINGS} from '../../../config';
import {parseManifestFromStdin} from '../../../util/helpers';
import {LoadDiffParams} from '../../../types/util/args';

const {INVALID_SOURCE} = STRINGS;

Expand Down Expand Up @@ -109,8 +111,10 @@ describe('lib/load: ', () => {

it('successfully loads target, and source from stdin.', async () => {
process.env.readline = 'valid-source';
const params = {
const piped = await parseManifestFromStdin();
const params: LoadDiffParams = {
targetPath: 'target-path.yml',
pipedSourceManifest: piped,
};

const response = await loadIfDiffFiles(params);
Expand Down
11 changes: 3 additions & 8 deletions src/__tests__/unit/util/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,16 +364,11 @@ describe('util/helpers: ', () => {

it('throws error if there is no manifest in stdin.', async () => {
process.env.readline = 'no_manifest';
const expectedMessage = 'Manifest not found in STDIN.';
expect.assertions(1);

try {
await parseManifestFromStdin();
} catch (error) {
if (error instanceof Error) {
expect(error.message).toEqual(expectedMessage);
}
}
const response = await parseManifestFromStdin();

expect(response).toEqual('');
});

it('returns empty string if there is no data in stdin.', async () => {
Expand Down
4 changes: 3 additions & 1 deletion src/diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {loadIfDiffFiles} from './lib/load';
import {compare} from './lib/compare';

import {parseIfDiffArgs} from './util/args';
import {formatNotMatchingLog} from './util/helpers';
import {formatNotMatchingLog, parseManifestFromStdin} from './util/helpers';
import {validateManifest} from './util/validations';

import {CONFIG} from './config';
Expand All @@ -15,6 +15,7 @@ const {IF_DIFF} = CONFIG;
const {SUCCESS_MESSAGE, FAILURE_MESSAGE} = IF_DIFF;

const IfDiff = async () => {
const pipedSourceManifest = await parseManifestFromStdin();
const {sourcePath, targetPath} = parseIfDiffArgs();

// Call this function with false parameter to prevent log debug messages.
Expand All @@ -23,6 +24,7 @@ const IfDiff = async () => {
const {rawSourceManifest, rawTargetManifest} = await loadIfDiffFiles({
targetPath,
sourcePath,
pipedSourceManifest,
});
const [sourceManifest, targetManifest] = [
rawSourceManifest,
Expand Down
4 changes: 1 addition & 3 deletions src/lib/load.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import * as YAML from 'js-yaml';
import {ERRORS} from '../util/errors';
import {openYamlFileAsObject} from '../util/yaml';
import {readAndParseJson} from '../util/json';
import {parseManifestFromStdin} from '../util/helpers';

import {PARAMETERS} from '../config';
import {STRINGS} from '../config';
Expand Down Expand Up @@ -41,8 +40,7 @@ export const load = async (inputPath: string, paramPath?: string) => {
* Loads files to compare. As a source file checks if data is piped and then decides which one to take.
*/
export const loadIfDiffFiles = async (params: LoadDiffParams) => {
const {sourcePath, targetPath} = params;
const pipedSourceManifest = await parseManifestFromStdin();
const {sourcePath, targetPath, pipedSourceManifest} = params;

if (!sourcePath && !pipedSourceManifest) {
throw new CliInputError(INVALID_SOURCE);
Expand Down
1 change: 1 addition & 0 deletions src/types/util/args.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export type LoadDiffParams = {
sourcePath?: string;
targetPath: string;
pipedSourceManifest?: string;
};
4 changes: 2 additions & 2 deletions src/util/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ const collectPipedData = async () => {

/**
* Checks if there is piped data, tries to parse yaml from it.
* Throws error if there is piped info, but there is no valid manifest.
* Returns empty string if haven't found anything.
*/
export const parseManifestFromStdin = async () => {
const pipedSourceManifest = await collectPipedData();
Expand All @@ -177,7 +177,7 @@ export const parseManifestFromStdin = async () => {
const match = regex.exec(pipedSourceManifest);

if (!match) {
throw new Error('Manifest not found in STDIN.');
return '';
}

return match![1];
Expand Down
2 changes: 2 additions & 0 deletions src/util/validations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {STRINGS} from '../config/strings';

const {ManifestValidationError, InputValidationError} = ERRORS;
const {VALIDATING_MANIFEST} = STRINGS;

/**
* At least one property defined handler.
*/
Expand Down Expand Up @@ -148,5 +149,6 @@ const flattenPath = (path: (string | number)[]): string => {
const flattenPath = path.map(part =>
typeof part === 'number' ? `[${part}]` : part
);

return flattenPath.join('.');
};

0 comments on commit 9aa1302

Please sign in to comment.