Skip to content
This repository has been archived by the owner on Mar 14, 2023. It is now read-only.

Commit

Permalink
fix: fix scpCopyFromRemote & scpCopyToRemote
Browse files Browse the repository at this point in the history
Fixes #178
  • Loading branch information
gregberge committed Mar 25, 2018
1 parent be1d621 commit 01bc213
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 28 deletions.
8 changes: 4 additions & 4 deletions packages/ssh-pool/src/Connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -387,13 +387,13 @@ class Connection {

return results.reduce(
(aggregate, result) => ({
stdout: Buffer.concat([aggregate.stdout, result.stdout]),
stderr: Buffer.concat([aggregate.stderr, result.stderr]),
stdout: String(aggregate.stdout) + String(result.stdout),
stderr: String(aggregate.stderr) + String(result.stderr),
children: [...aggregate.children, result.child],
}),
{
stdout: Buffer.from([]),
stderr: Buffer.from([]),
stdout: '',
stderr: '',
children: [],
},
)
Expand Down
31 changes: 7 additions & 24 deletions packages/ssh-pool/src/Connection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ jest.mock('tmp')

describe('Connection', () => {
beforeEach(() => {
exec.mockClear()
util.deprecateV3 = jest.fn()
__setPaths__({ rsync: '/bin/rsync' })
})
Expand Down Expand Up @@ -48,22 +49,20 @@ describe('Connection', () => {
await connection.run('my-command -x', { cwd: '/root' })

expect(exec).toHaveBeenCalledWith(
'ssh user@host "my-command -x"',
'ssh user@host "cd /root > /dev/null; my-command -x; cd - > /dev/null"',
{
cwd: '/root',
maxBuffer: 1024000,
},
expect.any(Function),
)
})

it('should escape double quotes', async () => {
await connection.run('echo "ok"', { cwd: '/root' })
await connection.run('echo "ok"')

expect(exec).toHaveBeenCalledWith(
'ssh user@host "echo \\"ok\\""',
{
cwd: '/root',
maxBuffer: 1024000,
},
expect.any(Function),
Expand All @@ -76,26 +75,12 @@ describe('Connection', () => {
expect(result.stderr.toString()).toBe('stderr')
})

it('should handle sudo', async () => {
await connection.run('sudo my-command -x', { cwd: '/root' })

expect(exec).toHaveBeenCalledWith(
'ssh -tt user@host "sudo my-command -x"',
{
cwd: '/root',
maxBuffer: 1024000,
},
expect.any(Function),
)
})

it('should handle tty', async () => {
await connection.run('sudo my-command -x', { cwd: '/root', tty: true })
await connection.run('sudo my-command -x', { tty: true })

expect(exec).toHaveBeenCalledWith(
'ssh -tt user@host "sudo my-command -x"',
{
cwd: '/root',
maxBuffer: 1024000,
},
expect.any(Function),
Expand Down Expand Up @@ -168,7 +153,7 @@ describe('Connection', () => {
)
})

it.only('should log output', async () => {
it('should log output', async () => {
const log = jest.fn()
connection = new Connection({
remote: 'user@host',
Expand Down Expand Up @@ -209,25 +194,23 @@ describe('Connection', () => {
})

it('should handle sudo as user correctly', async () => {
await connection.run('my-command -x', { cwd: '/root', tty: true })
await connection.run('my-command -x', { tty: true })

expect(exec).toHaveBeenCalledWith(
'ssh -tt user@host "sudo -u test my-command -x"',
{
cwd: '/root',
maxBuffer: 1000 * 1024,
},
expect.any(Function),
)
})

it('should handle sudo as user without double sudo', () => {
connection.run('sudo my-command -x', { cwd: '/root' })
connection.run('sudo my-command -x', { tty: true })

expect(exec).toHaveBeenCalledWith(
'ssh -tt user@host "sudo -u test my-command -x"',
{
cwd: '/root',
maxBuffer: 1000 * 1024,
},
expect.any(Function),
Expand Down
1 change: 1 addition & 0 deletions packages/ssh-pool/tests/__fixtures__/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello
13 changes: 13 additions & 0 deletions packages/ssh-pool/tests/integration.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import path from 'path'

const sshPool = require('../src')

describe('ssh-pool', () => {
Expand All @@ -21,4 +23,15 @@ describe('ssh-pool', () => {
const [{ stdout: second }] = await pool.run("echo '$USER'")
expect(second).toBe('$USER\n')
})

it(
'should copy to remote',
async () => {
await pool.scpCopyToRemote(
path.resolve(__dirname, '__fixtures__/test.txt'),
'./',
)
},
20000,
)
})

0 comments on commit 01bc213

Please sign in to comment.