Skip to content

Commit

Permalink
fix: error out when projectOwner & projectName is not set in .all-con…
Browse files Browse the repository at this point in the history
…tributorsrc file. (#80)

* error message when no project name and project owner is added in the init command

* moved error message away from init prompt

* 1. placed json parse in try catch
2. added check for owner and name in getcontributionsfromgithub before constructing url

* added check before config file is written

* changes as per #80 (review)

* uncommented code

* tests added

* Added myself to contributors table

* test inside utils were moved to github in master branch

* changes to validation check, instead of checking for empty string to null obj value

* typo fixed

* unnecisary check.js file removed
  • Loading branch information
M-ZubairAhmed authored and Kent C. Dodds committed May 29, 2018
1 parent 1562a37 commit c4a0484
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
4 changes: 3 additions & 1 deletion .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,9 @@
"profile": "https://in.linkedin.com/in/mzubairahmed",
"contributions": [
"doc",
"bug"
"bug",
"code",
"test"
]
},
{
Expand Down
34 changes: 31 additions & 3 deletions src/util/__tests__/config-file.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,43 @@
import configFile from '../config-file'

const absentFile = './abc'
const expected = `Configuration file not found: ${absentFile}`
const absentConfileFileExpected = `Configuration file not found: ${absentFile}`
const incompleteConfigFilePath = './.all-contributorsrc'
const NoOwnerConfigFile = {
projectOwner: '',
projectName: 'all-contributors-cli',
imageSize: 100,
commit: false,
contributorsPerLine: 6,
contributors: [],
}
const NoNameConfigFile = {
projectOwner: 'jfmengels',
projectName: '',
imageSize: 100,
commit: false,
contributorsPerLine: 6,
contributors: [],
}

test('Reading an absent configuration file throws a helpful error', () => {
expect(() => configFile.readConfig(absentFile)).toThrowError(expected)
expect(() => configFile.readConfig(absentFile)).toThrowError(
absentConfileFileExpected,
)
})

test('Writing contributors in an absent configuration file throws a helpful error', async () => {
const resolvedError = await configFile
.writeContributors(absentFile, [])
.catch(e => e)
expect(resolvedError.message).toBe(expected)
expect(resolvedError.message).toBe(absentConfileFileExpected)
})

test('Should throw error and not allow editing config file if project name or owner is not set', () => {
expect(() =>
configFile.writeConfig(incompleteConfigFilePath, NoOwnerConfigFile),
).toThrow(`Error! Project owner is not set in ${incompleteConfigFilePath}`)
expect(() =>
configFile.writeConfig(incompleteConfigFilePath, NoNameConfigFile),
).toThrow(`Error! Project name is not set in ${incompleteConfigFilePath}`)
})
6 changes: 6 additions & 0 deletions src/util/config-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ function readConfig(configPath) {
}

function writeConfig(configPath, content) {
if (!content.projectOwner) {
throw new Error(`Error! Project owner is not set in ${configPath}`)
}
if (!content.projectName) {
throw new Error(`Error! Project name is not set in ${configPath}`)
}
return pify(fs.writeFile)(configPath, `${JSON.stringify(content, null, 2)}\n`)
}

Expand Down

0 comments on commit c4a0484

Please sign in to comment.