-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathoutput.test.ts
More file actions
390 lines (359 loc) · 14.5 KB
/
Copy pathoutput.test.ts
File metadata and controls
390 lines (359 loc) · 14.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/*
* Copyright 2026, Salesforce, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import path from 'node:path';
import { stripVTControlCharacters } from 'node:util';
import { assert, expect, config } from 'chai';
import sinon from 'sinon';
import {
DeployMessage,
DeployResult,
Failures,
FileResponse,
ComponentStatus,
} from '@salesforce/source-deploy-retrieve';
import { Ux } from '@salesforce/sf-plugins-core';
import { getCoverageFormattersOptions } from '../../src/utils/coverage.js';
import { getZipFileSize } from '../../src/utils/output.js';
import { DeployResultFormatter } from '../../src/formatters/deployResultFormatter.js';
import { TestLevel } from '../../src/utils/types.js';
import { getDeployResult } from './deployResponses.js';
config.truncateThreshold = 0;
describe('deployResultFormatter', () => {
const sandbox = sinon.createSandbox();
afterEach(() => {
sandbox.restore();
});
describe('displayFailures', () => {
const deployResultFailure = getDeployResult('failed');
let tableStub: sinon.SinonStub;
let uxLogStub: sinon.SinonStub;
beforeEach(() => {
tableStub = sandbox.stub(Ux.prototype, 'table');
uxLogStub = sandbox.stub(Ux.prototype, 'log');
});
it('prints file responses, and messages from server', () => {
const formatter = new DeployResultFormatter(deployResultFailure, { verbose: true });
formatter.display();
expect(tableStub.callCount).to.equal(1);
expect(tableStub.firstCall.args[0]).to.deep.equal({
data: [
{
type: 'ApexClass',
fullName: 'ProductController',
error: 'This component has some problems',
loc: '27:18',
},
],
columns: [
{ key: 'type', name: 'Type' },
{ key: 'fullName', name: 'Name' },
{ key: 'error', name: 'Problem' },
{ key: 'loc', name: 'Line:Column' },
],
title: '\x1B[1m\x1B[31mComponent Failures [1]\x1B[39m\x1B[22m',
overflow: 'wrap',
});
});
it('displays errors from the server not in file responses', () => {
const deployFailure = getDeployResult('failed');
const error1 = {
changed: false,
componentType: 'ApexClass',
created: false,
createdDate: '2021-04-27T22:18:07.000Z',
deleted: false,
fileName: 'classes/ProductController.cls',
fullName: 'ProductController',
success: false,
problemType: 'Error',
problem: 'This component has some problems',
lineNumber: '27',
columnNumber: '18',
} as DeployMessage;
// add package.xml error, which is different from a FileResponse error
const error2 = {
changed: false,
componentType: '',
created: false,
createdDate: '2023-11-17T21:18:36.000Z',
deleted: false,
fileName: 'package.xml',
fullName: 'Create_property',
problem:
"An object 'Create_property' of type Flow was named in package.xml, but was not found in zipped directory",
problemType: 'Error',
success: false,
} as DeployMessage;
const testFailure1 = {
id: '01pDS00001AQcuGYAT',
message: 'System.AssertException: Assertion Failed: Expected: 0, Actual: 1',
methodName: 'successResponse',
name: 'GeocodingServiceTest',
namespace: null,
packageName: 'GeocodingServiceTest',
stackTrace: 'Class.GeocodingServiceTest.successResponse: line 32, column 1',
time: '70',
type: 'Class',
} as Failures;
deployFailure.response.details.componentFailures = [error1, error2];
deployFailure.response.numberTestErrors = 1;
deployFailure.response.runTestsEnabled = true;
deployFailure.response.details.runTestResult = {
numTestsRun: '1',
numFailures: '1',
totalTime: '3511',
failures: [testFailure1],
};
sandbox.stub(deployFailure, 'getFileResponses').returns([
{
fullName: error1.fullName,
filePath: error1.fileName,
type: error1.componentType,
state: 'Failed',
lineNumber: error1.lineNumber,
columnNumber: error1.columnNumber,
error: error1.problem,
problemType: error1.problemType,
},
] as FileResponse[]);
const formatter = new DeployResultFormatter(deployFailure, {
verbose: true,
'test-level': TestLevel.RunAllTestsInOrg,
});
formatter.display();
expect(tableStub.callCount).to.equal(1);
expect(tableStub.firstCall.args[0]).to.deep.equal({
data: [
{
type: '',
fullName: 'Create_property',
error:
"An object 'Create_property' of type Flow was named in package.xml, but was not found in zipped directory",
loc: '',
},
{
type: 'ApexClass',
fullName: 'ProductController',
error: 'This component has some problems',
loc: '27:18',
},
],
columns: [
{ key: 'type', name: 'Type' },
{ key: 'fullName', name: 'Name' },
{ key: 'error', name: 'Problem' },
{ key: 'loc', name: 'Line:Column' },
],
title: '\x1B[1m\x1B[31mComponent Failures [2]\x1B[39m\x1B[22m',
overflow: 'wrap',
});
// @ts-expect-error we expect args to be strings
const uxLogArgs: Array<[string]> = uxLogStub.args;
expect(stripVTControlCharacters(uxLogArgs[2][0])).to.equal('Test Failures [1]');
expect(stripVTControlCharacters(uxLogArgs[3][0])).to.equal(`• ${testFailure1.name}.${testFailure1.methodName}`);
expect(stripVTControlCharacters(uxLogArgs[4][0])).to.equal(` message: ${testFailure1.message}`);
});
});
describe('displayDeletes', () => {
let tableStub: sinon.SinonStub;
beforeEach(() => {
tableStub = sandbox.stub(Ux.prototype, 'table');
});
it('should display pre-destructive delete files in Deleted Source table', () => {
const deployResult = getDeployResult('successSync');
// Create pre-destructive file responses with proper typing
const preDestructiveFiles: FileResponse[] = [
{
fullName: 'CustomObject__c',
type: 'CustomObject',
state: ComponentStatus.Deleted,
filePath: 'force-app/main/default/objects/CustomObject__c/CustomObject__c.object-meta.xml',
},
{
fullName: 'CustomField__c.TestField__c',
type: 'CustomField',
state: ComponentStatus.Deleted,
filePath: 'force-app/main/default/objects/CustomObject__c/fields/TestField__c.field-meta.xml',
},
];
// Pass pre-destructive files as extraDeletes to formatter
// eslint-disable-next-line @typescript-eslint/no-explicit-any,@typescript-eslint/no-unsafe-argument
const formatter = new DeployResultFormatter(deployResult, { verbose: true }, preDestructiveFiles as any);
formatter.display();
// The formatter should call table() to display the Deleted Source
const deletesTableCall = tableStub.getCalls().find((call) => {
const callArg = call.args[0] as { title?: string };
return callArg?.title && callArg.title.includes('Deleted Source');
});
expect(deletesTableCall).to.exist;
if (deletesTableCall) {
const tableArg = deletesTableCall.args[0] as {
data: Array<{ fullName: string; type: string; filePath: string; state: string }>;
};
expect(tableArg.data).to.deep.equal([
{
fullName: 'CustomObject__c',
type: 'CustomObject',
state: 'Deleted',
filePath: 'force-app/main/default/objects/CustomObject__c/CustomObject__c.object-meta.xml',
},
{
fullName: 'CustomField__c.TestField__c',
type: 'CustomField',
state: 'Deleted',
filePath: 'force-app/main/default/objects/CustomObject__c/fields/TestField__c.field-meta.xml',
},
]);
}
});
it('should not display Deleted Source table when there are no deletes', () => {
const deployResult = getDeployResult('successSync');
// Pass empty pre-destructive files
const formatter = new DeployResultFormatter(deployResult, { verbose: true }, []);
formatter.display();
// Verify no Deleted Source table is displayed
const deletesTableCall = tableStub.getCalls().find((call) => {
const callArg = call.args[0] as { title?: string };
return callArg?.title && callArg.title.includes('Deleted Source');
});
expect(deletesTableCall).to.not.exist;
});
});
describe('coverage functions', () => {
describe('getCoverageFormattersOptions', () => {
it('clover, json', () => {
const result = getCoverageFormattersOptions(['clover', 'json']);
expect(result).to.deep.equal({
reportFormats: ['clover', 'json'],
reportOptions: {
clover: { file: path.join('coverage', 'clover.xml'), projectRoot: '.' },
json: { file: path.join('coverage', 'coverage.json') },
},
});
});
it('will warn when code coverage warning present from server', () => {
const deployResult = getDeployResult('codeCoverageWarning');
const formatter = new DeployResultFormatter(deployResult, {});
const warnStub = sandbox.stub(Ux.prototype, 'warn');
formatter.display();
expect(warnStub.callCount).to.equal(1);
expect(warnStub.firstCall.args[0]).to.equal(
'Average test coverage across all Apex Classes and Triggers is 25%, at least 75% test coverage is required.'
);
});
it('will write test output when in json mode', async () => {
const deployResult = getDeployResult('passedTest');
const formatter = new DeployResultFormatter(deployResult, {
junit: true,
'coverage-formatters': ['text', 'cobertura'],
});
// private method stub
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const coverageReportStub = sandbox.stub(formatter, 'createCoverageReport');
// private method stub
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const junitStub = sandbox.stub(formatter, 'createJunitResults');
await formatter.getJson();
expect(coverageReportStub.calledOnce).to.equal(true);
expect(junitStub.calledOnce).to.equal(true);
});
it('teamcity', () => {
const result = getCoverageFormattersOptions(['teamcity']);
expect(result).to.deep.equal({
reportFormats: ['teamcity'],
reportOptions: {
teamcity: { file: path.join('coverage', 'teamcity.txt'), blockName: 'coverage' },
},
});
});
});
});
describe('replacements', () => {
const deployResultSuccess = getDeployResult('successSync');
const deployResultSuccessWithReplacements = {
...getDeployResult('successSync'),
replacements: new Map<string, string[]>([['foo', ['bar', 'baz']]]),
} as DeployResult;
describe('json', () => {
it('shows replacements when not concise', async () => {
const formatter = new DeployResultFormatter(deployResultSuccessWithReplacements, { verbose: true });
const json = await formatter.getJson();
assert('replacements' in json && json.replacements);
expect(json.replacements).to.deep.equal({ foo: ['bar', 'baz'] });
});
it('no replacements when concise', () => {
const formatter = new DeployResultFormatter(deployResultSuccessWithReplacements, { concise: true });
const json = formatter.getJson();
expect(json).to.not.have.property('replacements');
});
});
describe('human', () => {
let uxStub: sinon.SinonStub;
beforeEach(() => {
uxStub = sandbox.stub(process.stdout, 'write');
});
const getStdout = () =>
uxStub
.getCalls()
// args are typed as any[]
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
.flatMap((call) => call.args)
.join('\n');
it('shows replacements when verbose and replacements exist', () => {
const formatter = new DeployResultFormatter(deployResultSuccessWithReplacements, { verbose: true });
formatter.display();
expect(getStdout()).to.include('Metadata Replacements');
expect(getStdout()).to.include('TEXT REPLACED');
});
it('no replacements when verbose but there are none', () => {
const formatter = new DeployResultFormatter(deployResultSuccess, { verbose: true });
formatter.display();
expect(getStdout()).to.not.include('Metadata Replacements');
});
it('no replacements when not verbose', () => {
const formatter = new DeployResultFormatter(deployResultSuccessWithReplacements, { verbose: false });
formatter.display();
expect(getStdout()).to.not.include('Metadata Replacements');
});
it('no replacements when concise', () => {
const formatter = new DeployResultFormatter(deployResultSuccessWithReplacements, { concise: true });
formatter.display();
expect(getStdout()).to.not.include('Metadata Replacements');
});
});
});
});
describe('output util functions', () => {
describe('getZipFileSize', () => {
it('should return correct number of Bytes if 0', () => {
expect(getZipFileSize(0)).to.equal('0 B');
});
it('should return correct number of Bytes', () => {
expect(getZipFileSize(724)).to.equal('724 B');
});
it('should return correct number of KiloBytes', () => {
expect(getZipFileSize(46_694)).to.equal('45.6 KB');
});
it('should return correct number of MegaBytes', () => {
expect(getZipFileSize(724_992_234)).to.equal('691.41 MB');
});
it('should return correct number of GigaBytes', () => {
expect(getZipFileSize(724_844_993_378)).to.equal('675.06 GB');
});
});
});