Skip to content

Commit

Permalink
Merge pull request #14 from Maaaartin/fix/allow_exec_with_muliple_params
Browse files Browse the repository at this point in the history
Fix/allow exec with muliple params
  • Loading branch information
Maaaartin authored Oct 10, 2024
2 parents f00a748 + 9c8cef8 commit ff3a3a2
Show file tree
Hide file tree
Showing 12 changed files with 249 additions and 277 deletions.
6 changes: 6 additions & 0 deletions README_DOCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,9 @@ adb.listDevices((devices) => {
- `time` option in `touch` method is converted to UTC time.
- Tracker `change` event emits the same instance of the device instead of creating a new device object every time.
- `install` and `uninstall` commands will fail if any other response than `Success` is received. Until V5 the promise could have resolved even when the operation was not successful.

## Change log

### 6.2

`exec` methods accept `string[]` as an argument. Fix for https://github.com/Maaaartin/adb-ts/issues/13.
14 changes: 11 additions & 3 deletions __tests__/adbClient/connection.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import { Client } from '../../lib';
import { mockExec } from '../../mockery/execMock';
import { execFile } from 'child_process';

jest.mock('child_process', () => ({
execFile: jest.fn()
}));

const mockExecFile = execFile as unknown as jest.Mock;

describe('Client connection tests', () => {
it('Should try to start server on ECONNREFUSED error', async () => {
const mocked = mockExec(null);
mockExecFile.mockImplementation((_cmd, _args, callback_) => {
callback_(null, '', '');
});
const client = new Client({ port: 1 });
await expect(() => client['connection']()).rejects.toThrow();
expect(mocked).toHaveBeenCalledTimes(1);
expect(mockExecFile).toHaveBeenCalledTimes(1);
});
});
12 changes: 10 additions & 2 deletions __tests__/adbClient/constructorAndStartServer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { Client } from '../../lib/client';
import { mockExec } from '../../mockery/execMock';
import { execFile } from 'child_process';

jest.mock('child_process', () => ({
execFile: jest.fn()
}));

const mockExecFile = execFile as unknown as jest.Mock;

describe('Client constructor tests', () => {
it('Create Adb client instance', () => {
Expand All @@ -25,7 +31,9 @@ describe('Client constructor tests', () => {

describe('Start server tests', () => {
it('Start adb server', async () => {
mockExec(null);
mockExecFile.mockImplementation((_cmd, _args, callback_) => {
callback_(null, '', '');
});
const client = new Client();
await expect(client.startServer()).resolves.toBeUndefined();
});
Expand Down
43 changes: 38 additions & 5 deletions __tests__/adbClient/exec.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,56 @@
import { Client } from '../../lib/client';
import { AdbExecError } from '../../lib/util';
import { mockExec } from '../../mockery/execMock';
import { execFile } from 'child_process';

jest.mock('child_process', () => ({
execFile: jest.fn()
}));

const mockExecFile = execFile as unknown as jest.Mock;

describe('Exec tests', () => {
it('Should execute without error', async () => {
mockExec(null, '', '');
let callback;
mockExecFile.mockImplementation((_cmd, _args, callback_) => {
callback = callback_;
callback_(null, '', '');
});
const adb = new Client({ noAutoStart: true });
const result = await adb.exec('cmd');
expect(result).toBe('');
expect(mockExecFile).toHaveBeenCalledWith('adb', ['cmd'], callback);
});

it('Should execute with multiple parameters', async () => {
let callback;
mockExecFile.mockImplementation((_cmd, _args, callback_) => {
callback = callback_;
callback_(null, '', '');
});
const adb = new Client({ noAutoStart: true });
const result = await adb.exec(['cmd', 'param']);
expect(result).toBe('');
expect(mockExecFile).toHaveBeenCalledWith(
'adb',
['cmd', 'param'],
callback
);
});

it('Should execute with error', async () => {
mockExec(new Error('message'), '', '');
mockExecFile.mockImplementation((_cmd, _args, callback_) => {
callback_(new Error('message'), '', '');
});
const adb = new Client({ noAutoStart: true });
await expect(() => adb.exec('cmd')).rejects.toEqual(
new Error('message')
);
});

it('Should execute with std error', async () => {
mockExec(null, '', 'message');
mockExecFile.mockImplementation((_cmd, _args, callback_) => {
callback_(null, '', 'message');
});
const adb = new Client({ noAutoStart: true });
try {
await adb.exec('cmd');
Expand All @@ -31,7 +62,9 @@ describe('Exec tests', () => {
});

it('Should execute with std out matching error reg exp', async () => {
mockExec(null, 'Error: message', '');
mockExecFile.mockImplementation((_cmd, _args, callback_) => {
callback_(null, 'Error: message', '');
});
const adb = new Client({ noAutoStart: true });
try {
await adb.exec('cmd');
Expand Down
47 changes: 42 additions & 5 deletions __tests__/adbClient/execDevice.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,60 @@
import { Client } from '../../lib/client';
import { AdbExecError } from '../../lib/util';
import { mockExec } from '../../mockery/execMock';
import { execFile } from 'child_process';

jest.mock('child_process', () => ({
execFile: jest.fn()
}));

const mockExecFile = execFile as unknown as jest.Mock;

describe('Exec device tests', () => {
it('Should execute without error', async () => {
mockExec(null, '', '');
let callback;
mockExecFile.mockImplementation((_cmd, _args, callback_) => {
callback = callback_;
callback_(null, '', '');
});
const adb = new Client({ noAutoStart: true });
const result = await adb.execDevice('serial', 'cmd');
expect(result).toBe('');
expect(mockExecFile).toHaveBeenCalledWith(
'adb',
['-s', 'serial', 'cmd'],
callback
);
});

it('Should execute with multiple parameters', async () => {
let callback;
mockExecFile.mockImplementation((_cmd, _args, callback_) => {
callback = callback_;
callback_(null, '', '');
});
const adb = new Client({ noAutoStart: true });
const result = await adb.execDevice('serial', ['cmd', 'param']);
expect(result).toBe('');
expect(mockExecFile).toHaveBeenCalledWith(
'adb',
['-s', 'serial', 'cmd', 'param'],
callback
);
});

it('Should execute with error', async () => {
mockExec(new Error('message'), '', '');
mockExecFile.mockImplementation((_cmd, _args, callback_) => {
callback_(new Error('message'), '', '');
});
const adb = new Client({ noAutoStart: true });
await expect(() => adb.execDevice('serial', 'cmd')).rejects.toEqual(
new Error('message')
);
});

it('Should execute with std error', async () => {
mockExec(null, '', 'message');
mockExecFile.mockImplementation((_cmd, _args, callback_) => {
callback_(null, '', 'message');
});
const adb = new Client({ noAutoStart: true });
try {
await adb.execDevice('serial', 'cmd');
Expand All @@ -31,7 +66,9 @@ describe('Exec device tests', () => {
});

it('Should execute with std out matching error reg exp', async () => {
mockExec(null, 'Error: message', '');
mockExecFile.mockImplementation((_cmd, _args, callback_) => {
callback_(null, 'Error: message', '');
});
const adb = new Client({ noAutoStart: true });
try {
await adb.execDevice('serial', 'cmd');
Expand Down
47 changes: 42 additions & 5 deletions __tests__/adbClient/execDeviceShell.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,60 @@
import { Client } from '../../lib/client';
import { AdbExecError } from '../../lib/util';
import { mockExec } from '../../mockery/execMock';
import { execFile } from 'child_process';

jest.mock('child_process', () => ({
execFile: jest.fn()
}));

const mockExecFile = execFile as unknown as jest.Mock;

describe('Exec device shell tests', () => {
it('Should execute without error', async () => {
mockExec(null, '', '');
let callback;
mockExecFile.mockImplementation((_cmd, _args, callback_) => {
callback = callback_;
callback_(null, '', '');
});
const adb = new Client({ noAutoStart: true });
const result = await adb.execDeviceShell('serial', 'cmd');
expect(result).toBe('');
expect(mockExecFile).toHaveBeenCalledWith(
'adb',
['-s', 'serial', 'shell', 'cmd'],
callback
);
});

it('Should execute with multiple params', async () => {
let callback;
mockExecFile.mockImplementation((_cmd, _args, callback_) => {
callback = callback_;
callback_(null, '', '');
});
const adb = new Client({ noAutoStart: true });
const result = await adb.execDeviceShell('serial', ['cmd', 'param']);
expect(result).toBe('');
expect(mockExecFile).toHaveBeenCalledWith(
'adb',
['-s', 'serial', 'shell', 'cmd', 'param'],
callback
);
});

it('Should execute with error', async () => {
mockExec(new Error('message'), '', '');
mockExecFile.mockImplementation((_cmd, _args, callback_) => {
callback_(new Error('message'), '', '');
});
const adb = new Client({ noAutoStart: true });
await expect(() =>
adb.execDeviceShell('serial', 'cmd')
).rejects.toEqual(new Error('message'));
});

it('Should execute with std error', async () => {
mockExec(null, '', 'message');
mockExecFile.mockImplementation((_cmd, _args, callback_) => {
callback_(null, '', 'message');
});
const adb = new Client({ noAutoStart: true });
try {
await adb.execDeviceShell('serial', 'cmd');
Expand All @@ -31,7 +66,9 @@ describe('Exec device shell tests', () => {
});

it('Should execute with std out matching error reg exp', async () => {
mockExec(null, 'Error: message', '');
mockExecFile.mockImplementation((_cmd, _args, callback_) => {
callback_(null, 'Error: message', '');
});
const adb = new Client({ noAutoStart: true });
try {
await adb.execDeviceShell('serial', 'cmd');
Expand Down
46 changes: 41 additions & 5 deletions __tests__/device/exec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { Client } from '../../lib/client';
import { Device } from '../../lib/device';
import { AdbExecError } from '../../lib/util';
import { mockExec } from '../../mockery/execMock';
import { execFile } from 'child_process';

jest.mock('child_process', () => ({
execFile: jest.fn()
}));

const mockExecFile = execFile as unknown as jest.Mock;

const device = new Device(new Client({ noAutoStart: true }), {
id: 'serial',
Expand All @@ -16,20 +22,48 @@ const device = new Device(new Client({ noAutoStart: true }), {

describe('Device exec tests', () => {
it('Should execute without error', async () => {
mockExec(null, '', '');
let callback;
mockExecFile.mockImplementation((_cmd, _args, callback_) => {
callback = callback_;
callback_(null, '', '');
});
const result = await device.exec('cmd');
expect(result).toBe('');
expect(mockExecFile).toHaveBeenCalledWith(
'adb',
['-s', 'serial', 'cmd'],
callback
);
});

it('Should execute with multiple params', async () => {
let callback;
mockExecFile.mockImplementation((_cmd, _args, callback_) => {
callback = callback_;
callback_(null, '', '');
});
const result = await device.exec(['cmd', 'param']);
expect(result).toBe('');
expect(mockExecFile).toHaveBeenCalledWith(
'adb',
['-s', 'serial', 'cmd', 'param'],
callback
);
});

it('Should execute with error', async () => {
mockExec(new Error('message'), '', '');
mockExecFile.mockImplementation((_cmd, _args, callback_) => {
callback_(new Error('message'), '', '');
});
await expect(() => device.exec('cmd')).rejects.toEqual(
new Error('message')
);
});

it('Should execute with std error', async () => {
mockExec(null, '', 'message');
mockExecFile.mockImplementation((_cmd, _args, callback_) => {
callback_(null, '', 'message');
});
try {
await device.exec('cmd');
} catch (e: unknown) {
Expand All @@ -40,7 +74,9 @@ describe('Device exec tests', () => {
});

it('Should execute with std out matching error reg exp', async () => {
mockExec(null, 'Error: message', '');
mockExecFile.mockImplementation((_cmd, _args, callback_) => {
callback_(null, 'Error: message', '');
});
try {
await device.exec('cmd');
} catch (e: unknown) {
Expand Down
Loading

0 comments on commit ff3a3a2

Please sign in to comment.