From 5064e4636d631eb67b3aa3d34375d3d1dafc666a Mon Sep 17 00:00:00 2001 From: Matt Travi Date: Wed, 30 Jan 2019 00:44:30 -0600 Subject: [PATCH] refactor(settings): extracted the settings scaffolding to another module for #1 --- src/scaffolder.js | 39 +------- src/settings-scaffolder.js | 40 ++++++++ test/unit/scaffolder-test.js | 111 ++-------------------- test/unit/settings-scaffolder-test.js | 127 ++++++++++++++++++++++++++ 4 files changed, 177 insertions(+), 140 deletions(-) create mode 100644 src/settings-scaffolder.js create mode 100644 test/unit/settings-scaffolder-test.js diff --git a/src/scaffolder.js b/src/scaffolder.js index b5af4d70..d20c7a43 100644 --- a/src/scaffolder.js +++ b/src/scaffolder.js @@ -1,43 +1,8 @@ import chalk from 'chalk'; -import writeYaml from '../third-party-wrappers/write-yaml'; +import scaffoldSettings from './settings-scaffolder'; export function scaffold({vcs, projectRoot, projectType, description, homepage, visibility}) { console.log(chalk.blue('Generating GitHub')); // eslint-disable-line no-console - return writeYaml(`${projectRoot}/.github/settings.yml`, { - repository: { - name: vcs.name, - description, - homepage, - private: 'Public' !== visibility, - has_wiki: false, - has_projects: false, - has_downloads: false, - allow_squash_merge: false, - allow_merge_commit: true, - allow_rebase_merge: true - }, - labels: [ - {name: 'bug', color: 'ee0701'}, - {name: 'duplicate', color: 'cccccc'}, - {name: 'enhancement', color: '84b6eb'}, - {name: 'help wanted', color: '128A0C'}, - {name: 'invalid', color: 'e6e6e6'}, - {name: 'question', color: 'cc317c'}, - {name: 'wontfix', color: 'ffffff'}, - {name: 'breaking change', color: 'e0fc28'}, - ('JavaScript' === projectType) ? {name: 'greenkeeper', color: '00c775'} : undefined - ].filter(Boolean), - branches: [ - { - name: 'master', - protection: { - required_pull_request_reviews: null, - required_status_checks: null, - restrictions: null, - enforce_admins: true - } - } - ] - }); + return scaffoldSettings(projectRoot, vcs, description, homepage, visibility, projectType); } diff --git a/src/settings-scaffolder.js b/src/settings-scaffolder.js new file mode 100644 index 00000000..5e943a2a --- /dev/null +++ b/src/settings-scaffolder.js @@ -0,0 +1,40 @@ +import writeYaml from '../third-party-wrappers/write-yaml'; + +export default function scaffoldSettings(projectRoot, vcs, description, homepage, visibility, projectType) { + return writeYaml(`${projectRoot}/.github/settings.yml`, { + repository: { + name: vcs.name, + description, + homepage, + private: 'Public' !== visibility, + has_wiki: false, + has_projects: false, + has_downloads: false, + allow_squash_merge: false, + allow_merge_commit: true, + allow_rebase_merge: true + }, + labels: [ + {name: 'bug', color: 'ee0701'}, + {name: 'duplicate', color: 'cccccc'}, + {name: 'enhancement', color: '84b6eb'}, + {name: 'help wanted', color: '128A0C'}, + {name: 'invalid', color: 'e6e6e6'}, + {name: 'question', color: 'cc317c'}, + {name: 'wontfix', color: 'ffffff'}, + {name: 'breaking change', color: 'e0fc28'}, + ('JavaScript' === projectType) ? {name: 'greenkeeper', color: '00c775'} : undefined + ].filter(Boolean), + branches: [ + { + name: 'master', + protection: { + required_pull_request_reviews: null, + required_status_checks: null, + restrictions: null, + enforce_admins: true + } + } + ] + }); +} diff --git a/test/unit/scaffolder-test.js b/test/unit/scaffolder-test.js index a2723179..ab59a49b 100644 --- a/test/unit/scaffolder-test.js +++ b/test/unit/scaffolder-test.js @@ -1,7 +1,7 @@ import {assert} from 'chai'; import sinon from 'sinon'; import any from '@travi/any'; -import * as yamlWriter from '../../third-party-wrappers/write-yaml'; +import * as settingsSecaffolder from '../../src/settings-scaffolder'; import {scaffold} from '../../src/scaffolder'; suite('github', () => { @@ -12,7 +12,7 @@ suite('github', () => { setup(() => { sandbox = sinon.createSandbox(); - sandbox.stub(yamlWriter, 'default'); + sandbox.stub(settingsSecaffolder, 'default'); }); teardown(() => sandbox.restore()); @@ -20,108 +20,13 @@ suite('github', () => { test('that the settings file is produced', async () => { const description = any.sentence(); const homepage = any.url(); - yamlWriter.default.resolves(); + const vcs = {name: projectName}; + const projectType = any.word(); + const visibility = any.word(); + settingsSecaffolder.default.resolves(); - await scaffold({projectRoot, vcs: {name: projectName}, description, homepage}); + await scaffold({projectRoot, vcs, description, homepage, projectType, visibility}); - assert.calledWith( - yamlWriter.default, - `${projectRoot}/.github/settings.yml`, - { - repository: { - name: projectName, - description, - homepage, - private: true, - has_wiki: false, - has_projects: false, - has_downloads: false, - allow_squash_merge: false, - allow_merge_commit: true, - allow_rebase_merge: true - }, - labels: [ - {name: 'bug', color: 'ee0701'}, - {name: 'duplicate', color: 'cccccc'}, - {name: 'enhancement', color: '84b6eb'}, - {name: 'help wanted', color: '128A0C'}, - {name: 'invalid', color: 'e6e6e6'}, - {name: 'question', color: 'cc317c'}, - {name: 'wontfix', color: 'ffffff'}, - {name: 'breaking change', color: 'e0fc28'} - ], - branches: [ - { - name: 'master', - protection: { - required_pull_request_reviews: null, - required_status_checks: null, - restrictions: null, - enforce_admins: true - } - } - ] - } - ); - }); - - test('that the greenkeeper label is defined for javascript projects', async () => { - yamlWriter.default.resolves(); - - await scaffold({vcs: {}, projectRoot, projectType: 'JavaScript'}); - - assert.calledWith( - yamlWriter.default, - `${projectRoot}/.github/settings.yml`, - sinon.match({ - labels: [ - {name: 'bug', color: 'ee0701'}, - {name: 'duplicate', color: 'cccccc'}, - {name: 'enhancement', color: '84b6eb'}, - {name: 'help wanted', color: '128A0C'}, - {name: 'invalid', color: 'e6e6e6'}, - {name: 'question', color: 'cc317c'}, - {name: 'wontfix', color: 'ffffff'}, - {name: 'breaking change', color: 'e0fc28'}, - {name: 'greenkeeper', color: '00c775'} - ] - }) - ); - }); - - test('that the repository is marked as private when the visibility is `Private`', async () => { - yamlWriter.default.resolves(); - - await scaffold({vcs: {}, projectRoot, projectType: any.word(), visibility: 'Private'}); - - assert.calledWith( - yamlWriter.default, - `${projectRoot}/.github/settings.yml`, - sinon.match({repository: {private: true}}) - ); - }); - - test('that the repository is marked as not private when the visibility is `Public`', async () => { - yamlWriter.default.resolves(); - - await scaffold({vcs: {}, projectRoot, projectType: any.word(), visibility: 'Public'}); - - assert.calledWith( - yamlWriter.default, - `${projectRoot}/.github/settings.yml`, - sinon.match({repository: {private: false}}) - ); - }); - - test('that the repository is marked as private when the visibility is not specified', async () => { - yamlWriter.default.resolves(); - - await scaffold({vcs: {}, projectRoot, projectType: any.word()}); - - assert.calledWith( - yamlWriter.default, - `${projectRoot}/.github/settings.yml`, - sinon.match({repository: {private: true}}) - ); + assert.calledWith(settingsSecaffolder.default, projectRoot, vcs, description, homepage, visibility, projectType); }); }); diff --git a/test/unit/settings-scaffolder-test.js b/test/unit/settings-scaffolder-test.js new file mode 100644 index 00000000..7da0cdb9 --- /dev/null +++ b/test/unit/settings-scaffolder-test.js @@ -0,0 +1,127 @@ +import {assert} from 'chai'; +import sinon from 'sinon'; +import any from '@travi/any'; +import * as yamlWriter from '../../third-party-wrappers/write-yaml'; +import scaffoldSettings from '../../src/settings-scaffolder'; + +suite('settings', () => { + let sandbox; + const projectRoot = any.string(); + const projectName = any.string(); + + setup(() => { + sandbox = sinon.createSandbox(); + + sandbox.stub(yamlWriter, 'default'); + }); + + teardown(() => sandbox.restore()); + + test('that the settings file is produced', async () => { + const description = any.sentence(); + const homepage = any.url(); + yamlWriter.default.resolves(); + + await scaffoldSettings(projectRoot, {name: projectName}, description, homepage); + + assert.calledWith( + yamlWriter.default, + `${projectRoot}/.github/settings.yml`, + { + repository: { + name: projectName, + description, + homepage, + private: true, + has_wiki: false, + has_projects: false, + has_downloads: false, + allow_squash_merge: false, + allow_merge_commit: true, + allow_rebase_merge: true + }, + labels: [ + {name: 'bug', color: 'ee0701'}, + {name: 'duplicate', color: 'cccccc'}, + {name: 'enhancement', color: '84b6eb'}, + {name: 'help wanted', color: '128A0C'}, + {name: 'invalid', color: 'e6e6e6'}, + {name: 'question', color: 'cc317c'}, + {name: 'wontfix', color: 'ffffff'}, + {name: 'breaking change', color: 'e0fc28'} + ], + branches: [ + { + name: 'master', + protection: { + required_pull_request_reviews: null, + required_status_checks: null, + restrictions: null, + enforce_admins: true + } + } + ] + } + ); + }); + + test('that the greenkeeper label is defined for javascript projects', async () => { + yamlWriter.default.resolves(); + + await scaffoldSettings(projectRoot, {}, null, null, null, 'JavaScript'); + + assert.calledWith( + yamlWriter.default, + `${projectRoot}/.github/settings.yml`, + sinon.match({ + labels: [ + {name: 'bug', color: 'ee0701'}, + {name: 'duplicate', color: 'cccccc'}, + {name: 'enhancement', color: '84b6eb'}, + {name: 'help wanted', color: '128A0C'}, + {name: 'invalid', color: 'e6e6e6'}, + {name: 'question', color: 'cc317c'}, + {name: 'wontfix', color: 'ffffff'}, + {name: 'breaking change', color: 'e0fc28'}, + {name: 'greenkeeper', color: '00c775'} + ] + }) + ); + }); + + test('that the repository is marked as private when the visibility is `Private`', async () => { + yamlWriter.default.resolves(); + + await scaffoldSettings(projectRoot, {}, null, null, 'Private', any.word()); + + assert.calledWith( + yamlWriter.default, + `${projectRoot}/.github/settings.yml`, + sinon.match({repository: {private: true}}) + ); + }); + + test('that the repository is marked as not private when the visibility is `Public`', async () => { + yamlWriter.default.resolves(); + + await scaffoldSettings(projectRoot, {}, null, null, 'Public', any.word()); + + assert.calledWith( + yamlWriter.default, + `${projectRoot}/.github/settings.yml`, + sinon.match({repository: {private: false}}) + ); + }); + + test('that the repository is marked as private when the visibility is not specified', async () => { + yamlWriter.default.resolves(); + + await scaffoldSettings(projectRoot, {}, null, null, null, any.word()); + + assert.calledWith( + yamlWriter.default, + `${projectRoot}/.github/settings.yml`, + sinon.match({repository: {private: true}}) + ); + }); +});