Skip to content

Commit

Permalink
lint fix and client test
Browse files Browse the repository at this point in the history
  • Loading branch information
prklm10 committed Oct 19, 2023
1 parent f23dbd4 commit 0aee458
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 35 deletions.
5 changes: 0 additions & 5 deletions a.js

This file was deleted.

38 changes: 14 additions & 24 deletions packages/cli-exec/src/exec.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,12 @@ async function* spawn(cmd, args, percy) {
let { default: crossSpawn } = await import('cross-spawn');
let proc, closed, error;
let stderrData = '';
let stdoutData = '';

try {
proc = crossSpawn(cmd, args, { stdio: 'pipe' });
// Writing stdout of proc to process
// Writing stdout of proc to process
if (proc.stdout) {
proc.stdout.on('data', (data) => {
stdoutData += data;
process.stdout.write(`${data}`);
});
}
Expand All @@ -122,33 +120,25 @@ async function* spawn(cmd, args, percy) {

proc.on('close', (code, signal) => {
closed = code;
if (code === 0) {
console.log('Process successfully completed');
} else {
console.error(`Process exited with code ${code}`);
}
console.log(`percyToken: ${percy.client.getToken(false)}`);
if (percy.client.getToken(false)) {
const myObject = {
errorKind: 'cli',
client: percy.clientInfo,
clientVersion: null,
cliVersion: null,
errorMessage: stderrData // You can set any initial value or leave it as undefined
};
console.log(`percy: ${JSON.stringify(myObject)}`)
percy.client.sendFailedEvents(percy.build.id, myObject);
if (code !== 0) {
// Only send event when there is a global error code
if (percy.client.getToken(false)) {
const myObject = {
errorKind: 'cli',
client: percy.clientInfo,
clientVersion: null,
cliVersion: null,
errorMessage: stderrData // You can set any initial value or leave it as undefined
};
percy.client.sendFailedEvents(percy.build.id, myObject);
}
}

// We can send stderr `stderrData` or stdout `stdoutData` to the api from here
});


// run until an event is triggered
/* eslint-disable-next-line no-unmodified-loop-condition */
// console.error('Stderr:', stderrData);
// console.error('Stderr:', stderrData.length);

while (closed == null && error == null) {
yield new Promise(r => setImmediate(r));
}
Expand Down
9 changes: 4 additions & 5 deletions packages/client/src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -516,18 +516,17 @@ export class PercyClient {
return comparison;
}

async sendFailedEvents(buildId, { errorKind = 'cli', client = null, clientVersion = null, cliVersion = null, errorMessage } = {}) {
//validateId(buildId);
console.log(`data: ${buildId}, ${errorKind}, ${client}, ${clientVersion}, ${cliVersion}, ${errorMessage}`);
this.log.debug(`Sending FailedEvents`);
async sendFailedEvents(buildId, { errorKind = 'cli', client = null, clientVersion = null, cliVersion = null, errorMessage = null } = {}) {
validateId(buildId);
this.log.debug('Sending FailedEvents');
return this.post(`builds/${buildId}/failed-events`, {
data: {
buildId: buildId,
errorKind: errorKind,
client: client,
clientVersion: clientVersion,
cliVersion: cliVersion,
errorMessage: errorMessage
message: errorMessage
}
});
}
Expand Down
42 changes: 42 additions & 0 deletions packages/client/test/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1333,6 +1333,48 @@ describe('PercyClient', () => {
});
});

describe('sendFailedEvents', () => {
it('should send failed event with default values', async () => {
await client.sendFailedEvents(123, {});

expect(api.requests['/builds/123/failed-events']).toBeDefined();
expect(api.requests['/builds/123/failed-events'][0].method).toBe('POST');
expect(api.requests['/builds/123/failed-events'][0].body).toEqual({
data: {
buildId: 123,
errorKind: 'cli',
client: null,
clientVersion: null,
cliVersion: null,
message: null
}
});
});

it('should send failed event with default values', async () => {
await client.sendFailedEvents(123, {
errorKind: 'sdk',
client: 'percy-appium-dotnet',
clientVersion: '3.0.1',
cliVersion: '1.27.3',
errorMessage: 'some error'
});

expect(api.requests['/builds/123/failed-events']).toBeDefined();
expect(api.requests['/builds/123/failed-events'][0].method).toBe('POST');
expect(api.requests['/builds/123/failed-events'][0].body).toEqual({
data: {
buildId: 123,
errorKind: 'sdk',
client: 'percy-appium-dotnet',
clientVersion: '3.0.1',
cliVersion: '1.27.3',
message: 'some error'
}
});
});
});

describe('#getToken', () => {
afterEach(() => {
delete process.env.PERCY_TOKEN;
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export function createPercyServer(percy, port) {
percy.upload(await WebdriverUtils.automateScreenshot(req.body));
res.json(200, { success: true });
})
// Recieves events from sdk's.
// Recieves events from sdk's.
.route('post', '/percy/events', async (req, res) => {
percyFailedRequestHandler(req);
console.log(`percy: ${JSON.stringify(percy.build)}`);
Expand Down

0 comments on commit 0aee458

Please sign in to comment.