Skip to content

Commit

Permalink
chore(src): fetch changes from if-check-error
Browse files Browse the repository at this point in the history
  • Loading branch information
narekhovhannisyan committed Jul 2, 2024
2 parents e828a11 + b5ace20 commit 23a8b0b
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 48 deletions.
2 changes: 1 addition & 1 deletion src/__tests__/unit/util/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@ description: mock-description
expect.assertions(2);

expect(logSpy).toHaveBeenCalledWith(
`if-check could not verify ${mockFilename}. The re-executed file does not match the original.\n`
`if-check could not verify ${mockFilename}. The re-executed file does not match the original.\n`
);

expect(logSpy).toHaveBeenCalledWith('mock error message');
Expand Down
70 changes: 32 additions & 38 deletions src/__tests__/unit/util/npm.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as fs from 'fs/promises';
import * as fsSync from 'fs';
import * as path from 'path';

jest.mock('fs/promises', () => require('../../../__mocks__/fs'));
Expand All @@ -15,6 +14,29 @@ jest.mock('../../../util/logger', () => ({
},
}));

jest.mock('../../../util/helpers', () => {
const originalModule = jest.requireActual('../../../util/helpers');
return {
...originalModule,
execPromise: async (param: any) => {
switch (process.env.NPM_INSTALL) {
case 'true':
expect(param).toEqual('npm install @grnsft/if@0.3.3-beta.0');
break;
case 'npm init -y':
expect(param).toEqual('npm init -y');
break;
case 'if-check':
expect(param).toEqual(
"npm run if-env -- -m ./src/__mocks__/mock-manifest.yaml && npm run if-run -- -m ./src/__mocks__/mock-manifest.yaml -o src/__mocks__/re-mock-manifest && node -p 'Boolean(process.stdout.isTTY)' | npm run if-diff -- -s src/__mocks__/re-mock-manifest.yaml -t ./src/__mocks__/mock-manifest.yaml"
);
break;
}
return;
},
};
});

import {
installDependencies,
initPackageJsonIfNotExists,
Expand All @@ -31,33 +53,17 @@ import {ManifestPlugin} from '../../../types/npm';
const {INITIALIZING_PACKAGE_JSON, INSTALLING_NPM_PACKAGES} = STRINGS;

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

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

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

describe('initPackageJsonIfNotExists(): ', () => {
it('initializes package.json if it does not exist.', async () => {
const spyExecPromise = jest.spyOn(helpers, 'execPromise');
process.env.NPM_INSTALL = 'npm init -y';
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 () => {
Expand All @@ -66,7 +72,7 @@ describe('util/npm: ', () => {

const result = await initPackageJsonIfNotExists(folderPath);

expect.assertions(1);
expect.assertions(2);
expect(result).toBe(packageJsonPath);
});
});
Expand All @@ -77,17 +83,11 @@ describe('util/npm: ', () => {
};

it('calls execPromise with the correct arguments.', async () => {
const spyExecPromise = jest.spyOn(helpers, 'execPromise');
const formattedDependencies = ['@grnsft/if@0.3.3-beta.0'];
process.env.NPM_INSTALL = 'true';
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 = {
Expand All @@ -96,7 +96,7 @@ describe('util/npm: ', () => {

await installDependencies(folderPath, dependencies);

expect.assertions(1);
expect.assertions(2);
expect(mockInfo).toHaveBeenCalledWith(INSTALLING_NPM_PACKAGES);
});
});
Expand Down Expand Up @@ -250,22 +250,16 @@ describe('util/npm: ', () => {

describe('executeCommands(): ', () => {
it('successfully executes with correct commands.', async () => {
process.env.NPM_INSTALL = 'if-check';
const manifest = './src/__mocks__/mock-manifest.yaml';
const reManifest = 'src/__mocks__/re-mock-manifest.yaml';
const logSpy = jest.spyOn(global.console, 'log');

jest.spyOn(fs, 'unlink').mockResolvedValue();

await executeCommands(manifest, false);

expect.assertions(1);
expect.assertions(2);
expect(logSpy).toHaveBeenCalledWith(
'if-check successfully verified mock-manifest.yaml\n'
'if-check successfully verified mock-manifest.yaml\n'
);

const packageJsonPath = 'src/__mocks__/package.json';
fsSync.unlink(path.resolve(process.cwd(), reManifest), () => {});
fsSync.unlink(path.resolve(process.cwd(), packageJsonPath), () => {});
}, 70000);
});
});
});
14 changes: 10 additions & 4 deletions src/check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@ import {getYamlFiles, removeFileIfExists} from './util/fs';
import {STRINGS} from './config';
import {executeCommands} from './util/npm';

const {CHECKING, DIRECTORY_YAML_FILES_NOT_FOUND, IF_CHECK_SUMMARY_LOG} =
STRINGS;
const {
CHECKING,
DIRECTORY_YAML_FILES_NOT_FOUND,
IF_CHECK_VERIFICATION_FAILURES,
IF_CHECK_SUMMARY_LOG,
IF_CHECK_EXECUTING,
} = STRINGS;

const IfCheck = async () => {
const commandArgs = await parseIfCheckArgs();
Expand Down Expand Up @@ -47,7 +52,7 @@ const IfCheck = async () => {

for await (const file of files) {
const fileName = path.basename(file);
console.log(fileName);
console.log(IF_CHECK_EXECUTING(fileName));

try {
await executeCommands(file, true);
Expand All @@ -68,8 +73,9 @@ const IfCheck = async () => {
if (failedLogs.count > 0) {
const passedFilesCount = files.length - failedLogs.count;

console.log(IF_CHECK_VERIFICATION_FAILURES);
console.log(failedLogs.message);
console.log(IF_CHECK_SUMMARY_LOG(passedFilesCount, files.length));
console.error(failedLogs.message);
process.exit(1);
}
}
Expand Down
11 changes: 8 additions & 3 deletions src/config/strings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,17 @@ ${error}`,
DIRECTORY_NOT_FOUND: 'Directory not found.',
DIRECTORY_YAML_FILES_NOT_FOUND:
'The directory does not contain any YAML/YML files.\n',
IF_CHECK_EXECUTING: (filename: string) => `Executing \`${filename}\``,
IF_CHECK_VERIFICATION_FAILURES:
'---------\nif-check verification failures:\n',
IF_CHECK_FAILED: (filename: string) =>
`if-check could not verify ${filename}. The re-executed file does not match the original.\n`,
`if-check could not verify ${filename}. The re-executed file does not match the original.\n`,
IF_CHECK_VERIFIED: (filename: string) =>
`if-check successfully verified ${filename}\n`,
`✔ if-check successfully verified ${filename}\n`,
IF_CHECK_SUMMARY_ERROR_MESSAGE: (filename: string, message: string) =>
`Executing \`${filename}\`\n✖ ${message}`,
IF_CHECK_SUMMARY_LOG: (passedCount: number, totalCount: number) =>
`\nSummary of all failing files:\n${passedCount} of ${totalCount} files are passed.\n`,
`---------\nCheck summary:\n${passedCount} of ${totalCount} files are passed.\n`,
ZERO_DIVISION: (moduleName: string, index: number) =>
`-- SKIPPING -- DivisionByZero: you are attempting to divide by zero in ${moduleName} plugin : inputs[${index}]\n`,
};
5 changes: 3 additions & 2 deletions src/util/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ const {
FAILURE_MESSAGE_DEPENDENCIES,
} = IF_ENV;

const {UNSUPPORTED_ERROR, IF_CHECK_FAILED} = STRINGS;
const {UNSUPPORTED_ERROR, IF_CHECK_FAILED, IF_CHECK_SUMMARY_ERROR_MESSAGE} =
STRINGS;
const {MissingPluginDependenciesError} = ERRORS;

/**
Expand Down Expand Up @@ -280,5 +281,5 @@ export const logStdoutFailMessage = (error: any, fileName: string) => {
const failMessage = logs[logs.length - 1];

console.log(failMessage);
return `${fileName}\n${failMessage}`;
return IF_CHECK_SUMMARY_ERROR_MESSAGE(fileName, failMessage);
};

0 comments on commit 23a8b0b

Please sign in to comment.