Skip to content

Commit

Permalink
Fix support for non-json return values
Browse files Browse the repository at this point in the history
  • Loading branch information
d4vidi committed Dec 26, 2024
1 parent 68f1a3d commit 863ebe1
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 48 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// @ts-nocheck
const exec = require('../../../../../../utils/childProcess').execWithRetriesAndLogs;

const defaultFlags = '--format compactjson';

class GenyCloudExec {
constructor(binaryPath) {
this.binaryExec = binaryPath;
Expand All @@ -14,7 +12,7 @@ class GenyCloudExec {
}

doctor() {
return this._exec('doctor', { retries: 0 }, '--format text');
return this._exec('doctor', { retries: 0 }, 'text');
}

getRecipe(name) {
Expand Down Expand Up @@ -44,23 +42,25 @@ class GenyCloudExec {
return this._exec(`instances stop ${instanceUUID}`, options);
}

async _exec(args, options, flags = defaultFlags) {
async _exec(args, options, format = 'compactjson') {
try {
const rawResult = await this.__exec(flags, args, options);
return JSON.parse(rawResult);
const rawResult = await this.__exec(args, options, format);
return (
format === 'compactjson' ? JSON.parse(rawResult) : rawResult
);
} catch (error) {
throw new Error(error.stderr);
}
}

async __exec(flags, args, _options) {
const options = {
..._options,
async __exec(args, options, format) {
const _options = {
...options,
statusLogs: {
retrying: true,
},
};
return (await exec(`"${this.binaryExec}" ${flags} ${args}`, options )).stdout;
return (await exec(`"${this.binaryExec}" --format ${format} ${args}`, _options )).stdout;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,18 @@ describe('Genymotion-cloud executable', () => {
const instanceUUID = 'mock-uuid';
const instanceName = 'detox-instance1';

const givenSuccessResult = () => exec.mockResolvedValue({
const givenSuccessJSONResult = () => exec.mockResolvedValue({
stdout: JSON.stringify(successResponse),
});
const givenErrorResult = () => exec.mockRejectedValue({
const givenSuccessTextResult = () => exec.mockResolvedValue({
stdout: successResponse,
});
const givenErrorJSONResult = () => exec.mockRejectedValue({
stderr: JSON.stringify(failResponse),
});
const givenErrorResult = (errorMessage) => exec.mockRejectedValue({
stderr: errorMessage,
});

let exec;
let uut;
Expand All @@ -40,44 +46,82 @@ describe('Genymotion-cloud executable', () => {
delete process.env.GMSAAS_USER_AGENT_EXTRA_DATA;
});

describe.each([
['version', () => uut.getVersion(), `"mock/path/to/gmsaas" --format compactjson --version`],
['Doctor', () => uut.doctor(), `"mock/path/to/gmsaas" --format text doctor`, { retries: 0 }],
['Get Recipe', () => uut.getRecipe(recipeName), `"mock/path/to/gmsaas" --format compactjson recipes list --name "${recipeName}"`],
['Get Instance', () => uut.getInstance(instanceUUID), `"mock/path/to/gmsaas" --format compactjson instances get ${instanceUUID}`],
['Get Instances', () => uut.getInstances(), `"mock/path/to/gmsaas" --format compactjson instances list -q`],
['Start Instance', () => uut.startInstance(recipeUUID, instanceName), `"mock/path/to/gmsaas" --format compactjson instances start --no-wait ${recipeUUID} "${instanceName}"`, { retries: 0 }],
['ADB Connect', () => uut.adbConnect(instanceUUID), `"mock/path/to/gmsaas" --format compactjson instances adbconnect ${instanceUUID}`, { retries: 0 }],
['Stop Instance', () => uut.stopInstance(instanceUUID), `"mock/path/to/gmsaas" --format compactjson instances stop ${instanceUUID}`, { retries: 3 }],
])(`%s command`, (commandName, commandExecFn, expectedExec, expectedExecOptions) => {
it('should execute command by name', async () => {
givenSuccessResult();

const expectedOptions = {
...expectedExecOptions,
statusLogs: {
retrying: true,
}
};

await commandExecFn();
expect(exec).toHaveBeenCalledWith(
expectedExec,
expectedOptions,
);
describe('JSON command', () => {
describe.each([
['version', () => uut.getVersion(), `"mock/path/to/gmsaas" --format compactjson --version`],
['Get Recipe', () => uut.getRecipe(recipeName), `"mock/path/to/gmsaas" --format compactjson recipes list --name "${recipeName}"`],
['Get Instance', () => uut.getInstance(instanceUUID), `"mock/path/to/gmsaas" --format compactjson instances get ${instanceUUID}`],
['Get Instances', () => uut.getInstances(), `"mock/path/to/gmsaas" --format compactjson instances list -q`],
['Start Instance', () => uut.startInstance(recipeUUID, instanceName), `"mock/path/to/gmsaas" --format compactjson instances start --no-wait ${recipeUUID} "${instanceName}"`, { retries: 0 }],
['ADB Connect', () => uut.adbConnect(instanceUUID), `"mock/path/to/gmsaas" --format compactjson instances adbconnect ${instanceUUID}`, { retries: 0 }],
['Stop Instance', () => uut.stopInstance(instanceUUID), `"mock/path/to/gmsaas" --format compactjson instances stop ${instanceUUID}`, { retries: 3 }],
])(`%s`, (commandName, commandExecFn, expectedExec, expectedExecOptions) => {
it('should execute command by name', async () => {
givenSuccessJSONResult();

const expectedOptions = {
...expectedExecOptions,
statusLogs: {
retrying: true,
}
};

await commandExecFn();
expect(exec).toHaveBeenCalledWith(
expectedExec,
expectedOptions,
);
});

it('should return the result', async () => {
givenSuccessJSONResult();

const result = await commandExecFn();
expect(result).toEqual(successResponse);
});

it('should fail upon an error result', async () => {
givenErrorJSONResult();

await expect(commandExecFn()).rejects.toThrowError(JSON.stringify(failResponse));
});
});
});

it('should return the result', async () => {
givenSuccessResult();

const result = await commandExecFn();
expect(result).toEqual(successResponse);
});

it('should fail upon an error result', async () => {
givenErrorResult();

await expect(commandExecFn()).rejects.toThrowError(JSON.stringify(failResponse));
describe('Text command', () => {
describe.each([
['Doctor', () => uut.doctor(), `"mock/path/to/gmsaas" --format text doctor`, { retries: 0 }],
])(`%s`, (commandName, commandExecFn, expectedExec, expectedExecOptions) => {
it('should execute command by name', async () => {
givenSuccessTextResult();

const expectedOptions = {
...expectedExecOptions,
statusLogs: {
retrying: true,
}
};

await commandExecFn();
expect(exec).toHaveBeenCalledWith(
expectedExec,
expectedOptions,
);
});

it('should return the result', async () => {
givenSuccessTextResult();

const result = await commandExecFn();
expect(result).toEqual(successResponse);
});

it('should fail upon an error result', async () => {
const errorMessage = 'Oh no, mocked error has occurred!';
givenErrorResult(errorMessage);

await expect(commandExecFn()).rejects.toThrowError(errorMessage);
});
});
});

Expand Down

0 comments on commit 863ebe1

Please sign in to comment.