Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add complete result info for getLogs #146

Merged
merged 3 commits into from
Mar 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/apex-node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export {
ExecuteAnonymousResponse,
ApexExecuteOptions
} from './execute';
export { LogService, ApexLogGetOptions, LogRecord } from './logs';
export { LogService, ApexLogGetOptions, LogRecord, LogResult } from './logs';
export { JUnitReporter, TapReporter, HumanReporter } from './reporters';
export {
ApexTestResultData,
Expand Down
2 changes: 1 addition & 1 deletion packages/apex-node/src/logs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
*/

export { LogService } from './logService';
export { ApexLogGetOptions, LogRecord } from './types';
export { ApexLogGetOptions, LogRecord, LogResult } from './types';
20 changes: 16 additions & 4 deletions packages/apex-node/src/logs/logService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import { Connection } from '@salesforce/core';
import { ApexLogGetOptions, LogQueryResult, LogRecord } from './types';
import {
ApexLogGetOptions,
LogQueryResult,
LogRecord,
LogResult
} from './types';
import { createFile } from '../utils';
import { nls } from '../i18n';
import * as path from 'path';
Expand Down Expand Up @@ -38,7 +43,7 @@ export class LogService {
}

// TODO: readableStream cannot be used until updates are made in jsforce and sfdx-core
public async getLogs(options: ApexLogGetOptions): Promise<string[]> {
public async getLogs(options: ApexLogGetOptions): Promise<LogResult[]> {
const logIdList = await this.getLogIds(options);
const logPaths: string[] = [];
const connectionRequests = logIdList.map(async id => {
Expand All @@ -54,9 +59,16 @@ export class LogService {

const logs = await Promise.all(connectionRequests);
if (logPaths.length > 0) {
return logPaths;
const logMap: LogResult[] = [];
for (let i = 0; i < logs.length; i++) {
logMap.push({ log: logs[i], logPath: logPaths[i] });
}
return logMap;
}
return logs;

return logs.map(log => {
return { log };
});
}

public async getLogRecords(numberOfLogs?: number): Promise<LogRecord[]> {
Expand Down
5 changes: 5 additions & 0 deletions packages/apex-node/src/logs/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,8 @@ export interface LogRecord {
export type LogQueryResult = {
records: LogRecord[];
};

export type LogResult = {
logPath?: string;
log: string;
};
26 changes: 18 additions & 8 deletions packages/apex-node/test/logs/logService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { createSandbox, SinonSandbox, SinonStub } from 'sinon';
import { LogService } from '../../src/logs/logService';
import * as path from 'path';
import * as stream from 'stream';
import { LogQueryResult, LogRecord } from '../../src/logs/types';
import { LogQueryResult, LogRecord, LogResult } from '../../src/logs/types';

const $$ = testSetup();

Expand Down Expand Up @@ -125,7 +125,9 @@ describe('Apex Log Service Tests', () => {
const toolingQueryStub = sandboxStub.stub(mockConnection.tooling, 'query');
//@ts-ignore
toolingQueryStub.onFirstCall().resolves(queryRecords);
const response = await apexLogGet.getLogs({ numberOfLogs: 2 });
const response = await apexLogGet.getLogs({
numberOfLogs: 2
});
expect(response.length).to.eql(2);
});

Expand All @@ -137,7 +139,9 @@ describe('Apex Log Service Tests', () => {
'getLogRecords'
);
toolingRequestStub.onFirstCall().resolves(log);
const response = await apexLogGet.getLogs({ logId: '07L5w00005PGdTnEAL' });
const response = await apexLogGet.getLogs({
logId: '07L5w00005PGdTnEAL'
});
expect(response.length).to.eql(1);
expect(getLogIdStub.callCount).to.eql(0);
});
Expand Down Expand Up @@ -175,7 +179,9 @@ describe('Apex Log Service Tests', () => {
const toolingQueryStub = sandboxStub.stub(mockConnection.tooling, 'query');
//@ts-ignore
toolingQueryStub.onFirstCall().resolves(queryRecords);
const response = await apexLogGet.getLogs({ numberOfLogs: 27 });
const response = await apexLogGet.getLogs({
numberOfLogs: 27
});
expect(response.length).to.eql(25);
});

Expand Down Expand Up @@ -220,14 +226,18 @@ describe('Apex Log Service Tests', () => {
const logs = ['48jnskd', '57fskjf'];
toolingRequestStub.onFirstCall().resolves(logs[0]);
toolingRequestStub.onSecondCall().resolves(logs[1]);

const logResult: LogResult[] = [
{ log: logs[0], logPath: path.join(filePath, `${logRecords[0].Id}.log`) },
{ log: logs[1], logPath: path.join(filePath, `${logRecords[1].Id}.log`) }
];

const response = await apexLogGet.getLogs({
numberOfLogs: 2,
outputDir: filePath
});
expect(response).to.deep.equal([
path.join(filePath, '07L5tgg0005PGdTnEAL.log'),
path.join(filePath, '07L5tgg0005PGdTnFPL.log')
]);

expect(response).to.deep.equal(logResult);
expect(createStreamStub.callCount).to.eql(2);
});

Expand Down
12 changes: 6 additions & 6 deletions packages/plugin-apex/src/commands/force/apex/log/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,25 +68,25 @@ export default class Get extends SfdxCommand {
if (!this.flags.logid && !this.flags.number) {
this.flags.number = 1;
}
const logs = await logService.getLogs({
const logResults = await logService.getLogs({
logId: this.flags.logid,
numberOfLogs: this.flags.number,
outputDir: this.flags.outputdir
});

if (logs.length === 0) {
if (logResults.length === 0) {
this.ux.log(messages.getMessage('noResultsFound'));
return [];
}

if (this.flags.outputdir) {
this.ux.log(`Log files written to ${this.flags.outputdir}`);
return logs;
return logResults.map(logResult => logResult.log);
}
const parsedLogs = logs.map(log => {
const colored = colorLogs(log);
const parsedLogs = logResults.map(logResult => {
const colored = colorLogs(logResult.log);
this.ux.log(colored);
return { log };
return { log: logResult.log };
});

return parsedLogs;
Expand Down