Skip to content

Commit

Permalink
fix(node): rethrow missing file error for package.json as a configura…
Browse files Browse the repository at this point in the history
…tion error (#1652)

* fix(node): rethrow missing file error for package.json as a configuration error

* test: add test for throwing MissingRequiredFileError
  • Loading branch information
chingor13 committed Sep 22, 2022
1 parent 4cea3dd commit 65ee57b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/strategies/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {SamplesPackageJson} from '../updaters/node/samples-package-json';
import {Changelog} from '../updaters/changelog';
import {PackageJson} from '../updaters/node/package-json';
import {GitHubFileContents} from '@google-automations/git-file-utils';
import {FileNotFoundError, MissingRequiredFileError} from '../errors';

export class Node extends BaseStrategy {
private pkgJsonContents?: GitHubFileContents;
Expand Down Expand Up @@ -85,10 +86,21 @@ export class Node extends BaseStrategy {

private async getPkgJsonContents(): Promise<GitHubFileContents> {
if (!this.pkgJsonContents) {
this.pkgJsonContents = await this.github.getFileContentsOnBranch(
this.addPath('package.json'),
this.targetBranch
);
try {
this.pkgJsonContents = await this.github.getFileContentsOnBranch(
this.addPath('package.json'),
this.targetBranch
);
} catch (e) {
if (e instanceof FileNotFoundError) {
throw new MissingRequiredFileError(
this.addPath('package.json'),
'node',
`${this.repository.owner}/${this.repository.repo}`
);
}
throw e;
}
}
return this.pkgJsonContents;
}
Expand Down
19 changes: 19 additions & 0 deletions test/strategies/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import {PackageLockJson} from '../../src/updaters/node/package-lock-json';
import {SamplesPackageJson} from '../../src/updaters/node/samples-package-json';
import {Changelog} from '../../src/updaters/changelog';
import {PackageJson} from '../../src/updaters/node/package-json';
import * as assert from 'assert';
import {MissingRequiredFileError, FileNotFoundError} from '../../src/errors';

nock.disableNetConnect();
const sandbox = sinon.createSandbox();
Expand Down Expand Up @@ -145,6 +147,23 @@ describe('Node', () => {
);
expect(pullRequest!.version?.toString()).to.eql(expectedVersion);
});
it('handles missing package.json', async () => {
sandbox
.stub(github, 'getFileContentsOnBranch')
.rejects(new FileNotFoundError('stub/path'));
const strategy = new Node({
targetBranch: 'main',
github,
});
const latestRelease = {
tag: new TagName(Version.parse('0.123.4'), 'some-node-package'),
sha: 'abc123',
notes: 'some notes',
};
assert.rejects(async () => {
await strategy.buildReleasePullRequest(commits, latestRelease);
}, MissingRequiredFileError);
});
});
describe('buildUpdates', () => {
it('builds common files', async () => {
Expand Down

0 comments on commit 65ee57b

Please sign in to comment.