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

refactor(cli): global options #23

Merged
merged 1 commit into from
Dec 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
12 changes: 6 additions & 6 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@
"plugins": ["jest"],
"extends": [
"plugin:jest/recommended"
],
"globals": {
"_COMMIT_CHANGELOG_CONTEXT_ROOT": "readonly",
"generateFixture": "readonly",
"setMockResourceFunctions": "readonly"
}
]
}
],
"parserOptions": {
"ecmaVersion": 2022
},
"globals": {
"generateFixture": "readonly",
"mockObjectProperty": "readonly",
"setMockResourceFunctions": "readonly"
},
"rules": {
"arrow-parens": ["error", "as-needed"],
"comma-dangle": 0,
Expand Down
52 changes: 27 additions & 25 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const yargs = require('yargs');
const packageJson = require('../package');
const { commitChangelog } = require('../src');
const { commitChangelog, OPTIONS } = require('../src');

/**
* Setup yargs
Expand Down Expand Up @@ -77,30 +77,32 @@ const {
type: 'string'
}).argv;

/**
* Set global OPTIONS
*
* @type {{comparePath: string, date: string, commitPath: string, isOverrideVersion: boolean,
* isAllowNonConventionalCommits: boolean, isDryRun: boolean,
* remoteUrl: string, prPath: string, isCommit: boolean, overrideVersion: string|*}}
* @private
*/
OPTIONS._set = {
commitPath,
comparePath,
date,
isAllowNonConventionalCommits,
isDryRun,
isCommit,
isOverrideVersion: overrideVersion !== undefined,
overrideVersion,
prPath,
remoteUrl
};

/**
* If testing stop here, otherwise continue.
*/
if (process.env.NODE_ENV === 'test') {
process.stdout.write(
JSON.stringify({
commitPath,
comparePath,
date,
isAllowNonConventionalCommits,
isDryRun,
isCommit,
overrideVersion,
prPath,
remoteUrl
})
);
process.stdout.write(JSON.stringify(OPTIONS));
} else {
commitChangelog({
commitPath,
comparePath,
date,
isAllowNonConventionalCommits,
isDryRun,
isCommit,
overrideVersion,
prPath,
remoteUrl
});
commitChangelog();
}
28 changes: 26 additions & 2 deletions jest.setupTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ const { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } = require('
const crypto = require('crypto');
const { extname, join, resolve } = require('path');

global._COMMIT_CHANGELOG_CONTEXT_PATH = join('..', 'src', '__fixtures__');

jest.mock('child_process', () => ({
...jest.requireActual('child_process'),
execSync: (...args) => `<execSync>${JSON.stringify(args)}</execSync>`
Expand Down Expand Up @@ -84,3 +82,29 @@ const setMockResourceFunctions = mockFunctions => {
};

global.setMockResourceFunctions = setMockResourceFunctions;

/**
* Shallow mock specific properties, restore with callback, mockClear.
* A simple object property mock for scenarios where the property is not a function/Jest fails.
*
* @param {object} object
* @param {object} propertiesValues
* @returns {{mockClear: Function}}
*/
const mockObjectProperty = (object = {}, propertiesValues) => {
const updatedObject = object;
const originalPropertiesValues = {};

Object.entries(propertiesValues).forEach(([key, value]) => {
originalPropertiesValues[key] = updatedObject[key];
updatedObject[key] = value;
});

return {
mockClear: () => {
Object.assign(updatedObject, originalPropertiesValues);
}
};
};

global.mockObjectProperty = mockObjectProperty;
15 changes: 15 additions & 0 deletions src/__tests__/__snapshots__/global.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

exports[`Global should return specific properties: specific properties 1`] = `
{
"OPTIONS": {
"_set": undefined,
"contextPath": "../src/__fixtures__",
},
"color": {
"BLACK": "",
"BLUE": "",
Expand Down Expand Up @@ -81,3 +85,14 @@ exports[`Global should return specific properties: specific properties 1`] = `
},
}
`;

exports[`Global should set a one-time mutable OPTIONS object: immutable 1`] = `
{
"OPTIONS": {
"contextPath": "../src/__fixtures__",
"dolor": "magna",
"lorem": "ipsum",
},
"isFrozen": true,
}
`;
36 changes: 24 additions & 12 deletions src/__tests__/files.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('Files', () => {
12345dd312345d421231231312312345dca11235 Initial commit
`;

const commitObj = parseCommits(undefined, { getGit: () => commitLog });
const commitObj = parseCommits({ getGit: () => commitLog });
expect(updateChangelog(commitObj, undefined, { date: '2022-10-01' })).toMatchSnapshot('changelog');
});

Expand All @@ -29,18 +29,24 @@ describe('Files', () => {
compareUrl: 'https://localhost/lorem/ipsum/comparmock/'
};

const commitObj = parseCommits(undefined, { getGit: () => commitLog });
const commitObj = parseCommits({ getGit: () => commitLog });
const comparisonObjNoReleaseCommit = getComparisonCommitHashes({
getGit: () => commitLog,
getReleaseCommit: () => ''
});

expect(
updateChangelog(commitObj, '1.0.0', {
date: '2022-10-01',
getComparisonCommitHashes: () => comparisonObjNoReleaseCommit,
getRemoteUrls: () => urlObj
})
updateChangelog(
commitObj,
'1.0.0',
{
date: '2022-10-01'
},
{
getComparisonCommitHashes: () => comparisonObjNoReleaseCommit,
getRemoteUrls: () => urlObj
}
)
).toMatchSnapshot('urls and paths, no release commit');

const comparisonObjReleaseCommit = getComparisonCommitHashes({
Expand All @@ -49,11 +55,17 @@ describe('Files', () => {
});

expect(
updateChangelog(commitObj, '1.0.0', {
date: '2022-10-01',
getComparisonCommitHashes: () => comparisonObjReleaseCommit,
getRemoteUrls: () => urlObj
})
updateChangelog(
commitObj,
'1.0.0',
{
date: '2022-10-01'
},
{
getComparisonCommitHashes: () => comparisonObjReleaseCommit,
getRemoteUrls: () => urlObj
}
)
).toMatchSnapshot('urls and paths, release commit');
});

Expand Down
11 changes: 11 additions & 0 deletions src/__tests__/global.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,15 @@ describe('Global', () => {
it('should return specific properties', () => {
expect(global).toMatchSnapshot('specific properties');
});

it('should set a one-time mutable OPTIONS object', () => {
const { OPTIONS } = global;
OPTIONS.lorem = 'et all';
OPTIONS.dolor = 'magna';
OPTIONS._set = { lorem: 'ipsum' };
cdcabrera marked this conversation as resolved.
Show resolved Hide resolved
OPTIONS.lorem = 'hello world';
OPTIONS.dolor = 'sit';

expect({ isFrozen: Object.isFrozen(OPTIONS), OPTIONS }).toMatchSnapshot('immutable');
});
});
19 changes: 15 additions & 4 deletions src/__tests__/index.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
const { commitChangelog } = require('../index');
const { OPTIONS } = require('../global');

describe('Commit Changelog', () => {
it('should parse commits to produce a CHANGELOG.md', () => {
expect(commitChangelog({ date: new Date('2022-10-01').toISOString() })).toMatchSnapshot('Commit Changelog');
const { mockClear } = mockObjectProperty(OPTIONS, { date: new Date('2022-10-01').toISOString() });

expect(commitChangelog()).toMatchSnapshot('Commit Changelog');

mockClear();
});

it('should parse commits to produce a CHANGELOG.md with an override version', () => {
expect(commitChangelog({ overrideVersion: '15.0.0', date: new Date('2022-10-01').toISOString() })).toMatchSnapshot(
'override'
);
const { mockClear } = mockObjectProperty(OPTIONS, {
date: new Date('2022-10-01').toISOString(),
isOverrideVersion: true,
overrideVersion: '15.0.0'
});

expect(commitChangelog()).toMatchSnapshot('override');

mockClear();
});
});
26 changes: 17 additions & 9 deletions src/__tests__/parse.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const {
semverBump,
getComparisonCommitHashes
} = require('../parse');
const { OPTIONS } = require('../global');

describe('Parse', () => {
it('should return commit types', () => {
Expand Down Expand Up @@ -81,15 +82,22 @@ describe('Parse', () => {
12345dd312345d421231231312312345dca11235 Initial commit
`;

const commitObj = parseCommits(undefined, { getGit: () => commitLog });
const urlPathObj = parseCommits(
{ commitPath: 'sit', prPath: 'dolor', remoteUrl: 'https://localhost/lorem/ipsum' },
{ getGit: () => commitLog }
);
const generalCommitsObj = parseCommits(
{ remoteUrl: 'https://localhost/lorem/ipsum' },
{ getGit: () => commitLog, isAllowNonConventionalCommits: true }
);
const commitObj = parseCommits({ getGit: () => commitLog });

const { mockClear: urlPathObjClear } = mockObjectProperty(OPTIONS, {
commitPath: 'sit',
prPath: 'dolor',
remoteUrl: 'https://localhost/lorem/ipsum'
});
const urlPathObj = parseCommits({ getGit: () => commitLog });
urlPathObjClear();

const { mockClear: generalCommitsObjClear } = mockObjectProperty(OPTIONS, {
isAllowNonConventionalCommits: true,
remoteUrl: 'https://localhost/lorem/ipsum'
});
const generalCommitsObj = parseCommits({ getGit: () => commitLog });
generalCommitsObjClear();

expect({
default: parseCommits(),
Expand Down
Loading