Skip to content

Commit

Permalink
fix(cargo-workspace): validate Cargo.toml version field (#1877)
Browse files Browse the repository at this point in the history
* test: failing test for invalid version field in Cargo.toml

* fix(cargo-workspace): throw ConfigurationError if Cargo.toml has invalid version entry

---------

Co-authored-by: Benjamin E. Coe <bencoe@google.com>
  • Loading branch information
chingor13 and bcoe authored Mar 14, 2023
1 parent 14d2a1e commit 0303e2e
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
"error",
{"argsIgnorePattern": "^_", "varsIgnorePattern": "^_$"}
]
}
},
"root": true
}
13 changes: 11 additions & 2 deletions src/plugins/cargo-workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {PullRequestBody} from '../util/pull-request-body';
import {BranchName} from '../util/branch-name';
import {PatchVersionUpdate} from '../versioning-strategy';
import {CargoLock} from '../updaters/rust/cargo-lock';
import {ConfigurationError} from '../errors';

interface CrateInfo {
/**
Expand Down Expand Up @@ -137,8 +138,16 @@ export class CargoWorkspace extends WorkspacePlugin<CrateInfo> {

const version = manifest.package?.version;
if (!version) {
throw new Error(
`package manifest at ${manifestPath} is missing [package.version]`
throw new ConfigurationError(
`package manifest at ${manifestPath} is missing [package.version]`,
'cargo-workspace',
`${this.github.repository.owner}/${this.github.repository.repo}`
);
} else if (typeof version !== 'string') {
throw new ConfigurationError(
`package manifest at ${manifestPath} has an invalid [package.version]`,
'cargo-workspace',
`${this.github.repository.owner}/${this.github.repository.repo}`
);
}
allCrates.push({
Expand Down
112 changes: 112 additions & 0 deletions test/plugins/cargo-workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ import snapshot = require('snap-shot-it');
import {RawContent} from '../../src/updaters/raw-content';
import {CargoToml} from '../../src/updaters/rust/cargo-toml';
import {parseCargoManifest} from '../../src/updaters/rust/common';
import {ConfigurationError} from '../../src/errors';
import assert = require('assert');

const sandbox = sinon.createSandbox();
const fixturesPath = './test/fixtures/plugins/cargo-workspace';
Expand Down Expand Up @@ -496,5 +498,115 @@ describe('CargoWorkspace plugin', () => {
assertHasUpdate(updates, 'packages/rustB/Cargo.toml', RawContent);
snapshot(dateSafe(rustCandidate!.pullRequest.body.toString()));
});
it('handles packages without version', async () => {
const candidates: CandidateReleasePullRequest[] = [
buildMockCandidatePullRequest('packages/rustA', 'rust', '1.1.2', {
component: '@here/pkgA',
updates: [
buildMockPackageUpdate(
'packages/rustA/Cargo.toml',
'packages/rustA/Cargo.toml'
),
],
}),
];
stubFilesFromFixtures({
sandbox,
github,
fixturePath: fixturesPath,
files: ['packages/rustA/Cargo.toml'],
flatten: false,
targetBranch: 'main',
inlineFiles: [
[
'Cargo.toml',
'[workspace]\nmembers = ["packages/rustA", "packages/rustB"]',
],
[
'packages/rustB/Cargo.toml',
'[package]\nname = "pkgB"\n\n[dependencies]\npkgA = { version = "1.1.1", path = "../pkgA" }',
],
],
});
sandbox
.stub(github, 'findFilesByGlob')
.withArgs('packages/rustA')
.resolves(['packages/rustA'])
.withArgs('packages/rustB')
.resolves(['packages/rustB']);
plugin = new CargoWorkspace(github, 'main', {
'packages/rustA': {
releaseType: 'rust',
},
'packages/rustB': {
releaseType: 'rust',
},
});
await assert.rejects(
async () => {
await plugin.run(candidates);
},
err => {
return (
err instanceof ConfigurationError && err.message.includes('missing')
);
}
);
});
it('handles packages with invalid version', async () => {
const candidates: CandidateReleasePullRequest[] = [
buildMockCandidatePullRequest('packages/rustA', 'rust', '1.1.2', {
component: '@here/pkgA',
updates: [
buildMockPackageUpdate(
'packages/rustA/Cargo.toml',
'packages/rustA/Cargo.toml'
),
],
}),
];
stubFilesFromFixtures({
sandbox,
github,
fixturePath: fixturesPath,
files: ['packages/rustA/Cargo.toml'],
flatten: false,
targetBranch: 'main',
inlineFiles: [
[
'Cargo.toml',
'[workspace]\nmembers = ["packages/rustA", "packages/rustB"]',
],
[
'packages/rustB/Cargo.toml',
'[package]\nname = "pkgB"\nversion = { major = 1, minor = 2, patch = 3 }\n\n[dependencies]\npkgA = { version = "1.1.1", path = "../pkgA" }',
],
],
});
sandbox
.stub(github, 'findFilesByGlob')
.withArgs('packages/rustA')
.resolves(['packages/rustA'])
.withArgs('packages/rustB')
.resolves(['packages/rustB']);
plugin = new CargoWorkspace(github, 'main', {
'packages/rustA': {
releaseType: 'rust',
},
'packages/rustB': {
releaseType: 'rust',
},
});
await assert.rejects(
async () => {
await plugin.run(candidates);
},
err => {
return (
err instanceof ConfigurationError && err.message.includes('invalid')
);
}
);
});
});
});

0 comments on commit 0303e2e

Please sign in to comment.