diff --git a/.all-contributorsrc b/.all-contributorsrc index 53d241e1..7459677b 100644 --- a/.all-contributorsrc +++ b/.all-contributorsrc @@ -180,7 +180,9 @@ "profile": "https://in.linkedin.com/in/mzubairahmed", "contributions": [ "doc", - "bug" + "bug", + "code", + "test" ] }, { diff --git a/src/util/__tests__/config-file.js b/src/util/__tests__/config-file.js index 78a79b31..4314059c 100644 --- a/src/util/__tests__/config-file.js +++ b/src/util/__tests__/config-file.js @@ -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}`) }) diff --git a/src/util/config-file.js b/src/util/config-file.js index 9e02b2e7..28b03f55 100644 --- a/src/util/config-file.js +++ b/src/util/config-file.js @@ -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`) }