Skip to content

Commit

Permalink
fix(service): Extend copyFileToRemotePath to take remote file name pa…
Browse files Browse the repository at this point in the history
…rameter
  • Loading branch information
jezhiggins committed Jun 10, 2020
1 parent caeaa01 commit cfbf9da
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
5 changes: 3 additions & 2 deletions lib/components/services/localfilestorage/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,12 @@ class LocalFilestorage {
}
} // listFolderContentsFromPath

async copyFileToRemotePath (localFilePath, remoteFolderPath) {
async copyFileToRemotePath (localFilePath, remoteFolderPath, remoteFileName = null) {
const localFile = path.resolve(localFilePath)
const rootedPath = this.resolvePath(remoteFolderPath)

const rootedFileName = path.join(rootedPath, path.basename(localFile))
const fileName = remoteFileName || path.basename(localFile)
const rootedFileName = path.join(rootedPath, fileName)

await fsp.copyFile(localFile, rootedFileName)

Expand Down
10 changes: 6 additions & 4 deletions test/method-copyFileToRemotePath.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ describe('copyFileToRemotePath', () => {
}

const fileName = 'hello.txt'
const renamedFileName = 'world.txt'
const localPath = path.join(__dirname, 'fixture', 'local', fileName)
const remotePath = path.join('to-write')

Expand Down Expand Up @@ -60,14 +61,15 @@ describe('copyFileToRemotePath', () => {
})

it('copy file to remote folder, giving filename', async () => {
const remoteFile = await localstorage.copyFileToRemotePath(localPath, path.join(remotePath, 'renamed-file'))
expect(remoteFile).to.equal(path.join('/', 'to-write', 'renamed-file'))
const remoteFile = await localstorage.copyFileToRemotePath(localPath, remotePath, renamedFileName)
expect(remoteFile).to.equal(path.join('/', 'to-write', renamedFileName))
})

it('target folder lists the file', async () => {
const contents = await localstorage.listFolderContentsFromPath(remotePath)
expect(contents).to.have.length(1)
expect(contents).to.have.length(2)
expect(contents).to.deep.include({ Name: fileName })
expect(contents).to.deep.include({ Name: renamedFileName })
})
})

Expand All @@ -80,7 +82,7 @@ describe('copyFileToRemotePath', () => {

it('remote path does not exist', () => {
return expect(
localstorage.copyFileToRemotePath(localPath, 'bad-path')
localstorage.copyFileToRemotePath(localPath, 'badpath')
).to.eventually.be.rejectedWith(Error)
})

Expand Down

0 comments on commit cfbf9da

Please sign in to comment.