Skip to content

Commit ebb5e21

Browse files
committed
fixup
1 parent d10f2e0 commit ebb5e21

File tree

6 files changed

+100
-14
lines changed

6 files changed

+100
-14
lines changed

src/m365/spfx/commands/project/project-azuredevops-pipeline-add.spec.ts

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ describe(commands.PROJECT_AZUREDEVOPS_PIPELINE_ADD, () => {
188188
sinon.stub(fs, 'writeFileSync').throws(new Error('writeFileSync failed'));
189189

190190
await assert.rejects(command.action(logger, { options: {} } as any),
191-
new CommandError(`Unable to determine the version of the current SharePoint Framework project`));
191+
new CommandError('Unable to determine the version of the current SharePoint Framework project. Could not find the correct version based on @microsoft/generator-sharepoint property in the .yo-rc.json file.'));
192192
});
193193

194194
it('handles error with not found node version', async () => {
@@ -218,7 +218,7 @@ describe(commands.PROJECT_AZUREDEVOPS_PIPELINE_ADD, () => {
218218
sinon.stub(fs, 'writeFileSync').throws(new Error('writeFileSync failed'));
219219

220220
await assert.rejects(command.action(logger, { options: {} } as any),
221-
new CommandError(`Could not find Node version for 99.99.99 of SharePoint Framework`));
221+
new CommandError(`Could not find Node version for version '99.99.99' of SharePoint Framework.`));
222222
});
223223

224224
it('handles unexpected error', async () => {
@@ -245,9 +245,41 @@ describe(commands.PROJECT_AZUREDEVOPS_PIPELINE_ADD, () => {
245245

246246
sinon.stub(command as any, 'getProjectVersion').returns('1.21.1');
247247

248-
sinon.stub(fs, 'writeFileSync').callsFake(() => { throw 'error'; });
248+
sinon.stub(fs, 'writeFileSync').throws(new Error('writeFileSync failed'));
249+
250+
await assert.rejects(command.action(logger, { options: {} } as any),
251+
new CommandError('writeFileSync failed'));
252+
});
253+
254+
it('handles unexpected non-error value', async () => {
255+
sinon.stub(command as any, 'getProjectRoot').returns(path.join(process.cwd(), projectPath));
256+
257+
sinon.stub(fs, 'readFileSync').callsFake((path, options) => {
258+
if (path.toString().endsWith('package.json') && options === 'utf-8') {
259+
return '{"name": "test"}';
260+
}
261+
262+
return '';
263+
});
264+
265+
sinon.stub(fs, 'existsSync').callsFake((fakePath) => {
266+
if (fakePath.toString().endsWith('.azuredevops')) {
267+
return true;
268+
}
269+
else if (fakePath.toString().endsWith('pipelines')) {
270+
return true;
271+
}
272+
273+
return false;
274+
});
275+
276+
sinon.stub(command as any, 'getProjectVersion').returns('1.21.1');
277+
278+
sinon.stub(fs, 'writeFileSync').callsFake(() => {
279+
throw 'string failure';
280+
});
249281

250282
await assert.rejects(command.action(logger, { options: {} } as any),
251-
new CommandError('error'));
283+
new CommandError('string failure'));
252284
});
253285
});

src/m365/spfx/commands/project/project-azuredevops-pipeline-add.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,12 @@ class SpfxProjectAzureDevOpsPipelineAddCommand extends BaseProjectCommand {
130130
this.savePipeline(pipeline);
131131
}
132132
catch (error: any) {
133-
throw new CommandError(error);
133+
if (error instanceof CommandError) {
134+
throw error;
135+
}
136+
137+
const message = error instanceof Error ? error.message : String(error);
138+
throw new CommandError(message);
134139
}
135140
}
136141

@@ -160,13 +165,13 @@ class SpfxProjectAzureDevOpsPipelineAddCommand extends BaseProjectCommand {
160165
const version = this.getProjectVersion();
161166

162167
if (!version) {
163-
throw 'Unable to determine the version of the current SharePoint Framework project. Could not find the correct version based on @microsoft/generator-sharepoint property in the .yo-rc.json file.';
168+
throw new CommandError('Unable to determine the version of the current SharePoint Framework project. Could not find the correct version based on @microsoft/generator-sharepoint property in the .yo-rc.json file.');
164169
}
165170

166171
const versionRequirements = versions[version];
167172

168173
if (!versionRequirements) {
169-
throw `Could not find Node version for version '${version}' of SharePoint Framework.`;
174+
throw new CommandError(`Could not find Node version for version '${version}' of SharePoint Framework.`);
170175
}
171176

172177
const nodeVersion: string = spfx.getHighestNodeVersion(versionRequirements.node.range);
@@ -231,4 +236,4 @@ class SpfxProjectAzureDevOpsPipelineAddCommand extends BaseProjectCommand {
231236
}
232237
}
233238

234-
export default new SpfxProjectAzureDevOpsPipelineAddCommand();
239+
export default new SpfxProjectAzureDevOpsPipelineAddCommand();

src/m365/spfx/commands/project/project-github-workflow-add.spec.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ describe(commands.PROJECT_GITHUB_WORKFLOW_ADD, () => {
189189
sinon.stub(fs, 'writeFileSync').throws(new Error('writeFileSync failed'));
190190

191191
await assert.rejects(command.action(logger, { options: {} }),
192-
new CommandError(`Unable to determine the version of the current SharePoint Framework project`));
192+
new CommandError('Unable to determine the version of the current SharePoint Framework project. Could not find the correct version based on @microsoft/generator-sharepoint property in the .yo-rc.json file.'));
193193

194194
});
195195

@@ -254,4 +254,36 @@ describe(commands.PROJECT_GITHUB_WORKFLOW_ADD, () => {
254254
await assert.rejects(command.action(logger, { options: {} } as any),
255255
new CommandError('writeFileSync failed'));
256256
});
257+
258+
it('handles unexpected non-error value', async () => {
259+
sinon.stub(command as any, 'getProjectRoot').returns(path.join(process.cwd(), projectPath));
260+
261+
sinon.stub(fs, 'readFileSync').callsFake((path, options) => {
262+
if (path.toString().endsWith('package.json') && options === 'utf-8') {
263+
return '{"name": "test"}';
264+
}
265+
266+
return '';
267+
});
268+
269+
sinon.stub(fs, 'existsSync').callsFake((fakePath) => {
270+
if (fakePath.toString().endsWith('.github')) {
271+
return true;
272+
}
273+
else if (fakePath.toString().endsWith('workflows')) {
274+
return true;
275+
}
276+
277+
return false;
278+
});
279+
280+
sinon.stub(command as any, 'getProjectVersion').returns('1.21.1');
281+
282+
sinon.stub(fs, 'writeFileSync').callsFake(() => {
283+
throw 'string failure';
284+
});
285+
286+
await assert.rejects(command.action(logger, { options: {} } as any),
287+
new CommandError('string failure'));
288+
});
257289
});

src/m365/spfx/commands/project/project-github-workflow-add.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,12 @@ class SpfxProjectGithubWorkflowAddCommand extends BaseProjectCommand {
135135
this.saveWorkflow(workflow);
136136
}
137137
catch (error: any) {
138-
throw new CommandError(error);
138+
if (error instanceof CommandError) {
139+
throw error;
140+
}
141+
142+
const message = error instanceof Error ? error.message : String(error);
143+
throw new CommandError(message);
139144
}
140145
}
141146

@@ -160,13 +165,13 @@ class SpfxProjectGithubWorkflowAddCommand extends BaseProjectCommand {
160165
const version = this.getProjectVersion();
161166

162167
if (!version) {
163-
throw 'Unable to determine the version of the current SharePoint Framework project. Could not find the correct version based on @microsoft/generator-sharepoint property in the .yo-rc.json file.';
168+
throw new CommandError('Unable to determine the version of the current SharePoint Framework project. Could not find the correct version based on @microsoft/generator-sharepoint property in the .yo-rc.json file.');
164169
}
165170

166171
const versionRequirements = versions[version];
167172

168173
if (!versionRequirements) {
169-
throw `Could not find Node version for ${version} of SharePoint Framework`;
174+
throw new CommandError(`Could not find Node version for ${version} of SharePoint Framework`);
170175
}
171176

172177
const nodeVersion: string = spfx.getHighestNodeVersion(versionRequirements.node.range);
@@ -221,4 +226,4 @@ class SpfxProjectGithubWorkflowAddCommand extends BaseProjectCommand {
221226
}
222227
}
223228

224-
export default new SpfxProjectGithubWorkflowAddCommand();
229+
export default new SpfxProjectGithubWorkflowAddCommand();

src/utils/spfx.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,16 @@ describe('utils/spfx', () => {
5252
const version = spfx.getHighestNodeVersion('>=16.13.0 <17.0.0 || >=18.17.1 <19.0.0');
5353
assert.strictEqual(version, '18.17.1');
5454
});
55+
56+
it('throws when range is empty', () => {
57+
assert.throws(() => spfx.getHighestNodeVersion(''), new Error('Node version range was not provided.'));
58+
});
59+
60+
it('throws when range cannot be normalized', () => {
61+
assert.throws(() => spfx.getHighestNodeVersion('invalid-range'), new Error("Unable to resolve the highest Node version for range 'invalid-range'."));
62+
});
63+
64+
it('throws when min version cannot be determined', () => {
65+
assert.throws(() => spfx.getHighestNodeVersion('invalid || >=1.0.0 <1.0.0'), new Error("Unable to resolve the highest Node version for range 'invalid || >=1.0.0 <1.0.0'."));
66+
});
5567
});

src/utils/spfx.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export const spfx = {
6060
if (isCaretOrTilde || isSimpleVersion) {
6161
const numeric = isCaretOrTilde ? compactSource.substring(1) : compactSource;
6262
const parts = numeric.split('.').filter(part => part.length > 0);
63-
const { major, minor } = highest.version;
63+
const { major } = highest.version;
6464

6565
if (parts.length >= 2) {
6666
return `${major}.${parts[1]}.x`;

0 commit comments

Comments
 (0)