Skip to content

Commit

Permalink
fix: handle malformed manifest JSON and throw ConfigurationError (#1469)
Browse files Browse the repository at this point in the history
* test: add test for malformed manifest config and versions files

* fix: handle malformed manifest JSON and throw ConfigurationError
  • Loading branch information
chingor13 authored Jun 8, 2022
1 parent dc2bced commit e3af138
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1247,6 +1247,12 @@ async function fetchManifestConfig(
'base',
`${github.repository.owner}/${github.repository.repo}`
);
} else if (e instanceof SyntaxError) {
throw new ConfigurationError(
`Failed to parse manifest config JSON: ${configFile}\n${e.message}`,
'base',
`${github.repository.owner}/${github.repository.repo}`
);
}
throw e;
}
Expand Down Expand Up @@ -1302,6 +1308,12 @@ async function fetchReleasedVersions(
'base',
`${github.repository.owner}/${github.repository.repo}`
);
} else if (e instanceof SyntaxError) {
throw new ConfigurationError(
`Failed to parse manifest versions JSON: ${manifestFile}\n${e.message}`,
'base',
`${github.repository.owner}/${github.repository.repo}`
);
}
throw e;
}
Expand Down
49 changes: 49 additions & 0 deletions test/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,55 @@ describe('Manifest', () => {
await Manifest.fromManifest(github, github.repository.defaultBranch);
}, ConfigurationError);
});

it('should throw a configuration error for a malformed manifest config', async () => {
const getFileContentsStub = sandbox.stub(
github,
'getFileContentsOnBranch'
);
getFileContentsStub
.withArgs('release-please-config.json', 'main')
.resolves(buildGitHubFileRaw('{"malformed json"'))
.withArgs('.release-please-manifest.json', 'main')
.resolves(
buildGitHubFileContent(
fixturesPath,
'manifest/versions/versions.json'
)
);
await assert.rejects(
async () => {
await Manifest.fromManifest(github, github.repository.defaultBranch);
},
e => {
console.log(e);
return e instanceof ConfigurationError && e.message.includes('parse');
}
);
});

it('should throw a configuration error for a malformed manifest config', async () => {
const getFileContentsStub = sandbox.stub(
github,
'getFileContentsOnBranch'
);
getFileContentsStub
.withArgs('release-please-config.json', 'main')
.resolves(
buildGitHubFileContent(fixturesPath, 'manifest/config/config.json')
)
.withArgs('.release-please-manifest.json', 'main')
.resolves(buildGitHubFileRaw('{"malformed json"'));
await assert.rejects(
async () => {
await Manifest.fromManifest(github, github.repository.defaultBranch);
},
e => {
console.log(e);
return e instanceof ConfigurationError && e.message.includes('parse');
}
);
});
});

describe('fromConfig', () => {
Expand Down

0 comments on commit e3af138

Please sign in to comment.