Skip to content

Commit 2d5381d

Browse files
committed
fix: ensure that target file paths exists before writing to file
1 parent 7bc08f5 commit 2d5381d

3 files changed

Lines changed: 9 additions & 3 deletions

File tree

shared/packages/worker/src/worker/accessorHandlers/fileShare.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ import { FileShareAccessorHandleType, GenericFileAccessorHandle } from './lib/Fi
2929
import { MonitorInProgress } from '../lib/monitorInProgress'
3030
import { MAX_EXEC_BUFFER } from '../lib/lib'
3131
import { defaultCheckHandleRead, defaultCheckHandleWrite } from './lib/lib'
32+
import * as path from 'path'
33+
import { mkdirp } from 'mkdirp'
3234

3335
const fsStat = promisify(fs.stat)
3436
const fsAccess = promisify(fs.access)
@@ -250,6 +252,8 @@ export class FileShareAccessorHandle<Metadata> extends GenericFileAccessorHandle
250252

251253
const fullPath = this.workOptions.useTemporaryFilePath ? this.temporaryFilePath : this.fullPath
252254

255+
await mkdirp(path.dirname(fullPath)) // Create folder if it doesn't exist
256+
253257
// Remove the file if it exists:
254258
let exists = false
255259
try {

shared/packages/worker/src/worker/accessorHandlers/localFolder.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { GenericFileAccessorHandle, LocalFolderAccessorHandleType } from './lib/
2626
import { MonitorInProgress } from '../lib/monitorInProgress'
2727
import { compareResourceIds } from '../workers/windowsWorker/lib/lib'
2828
import { defaultCheckHandleRead, defaultCheckHandleWrite } from './lib/lib'
29+
import { mkdirp } from 'mkdirp'
2930

3031
const fsStat = promisify(fs.stat)
3132
const fsAccess = promisify(fs.access)
@@ -199,6 +200,8 @@ export class LocalFolderAccessorHandle<Metadata> extends GenericFileAccessorHand
199200

200201
const fullPath = this.workOptions.useTemporaryFilePath ? this.temporaryFilePath : this.fullPath
201202

203+
await mkdirp(path.dirname(fullPath)) // Create folder if it doesn't exist
204+
202205
// Remove the file if it exists:
203206
let exists = false
204207
try {

tests/internal-tests/src/__tests__/basic.spec.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ describe('Basic', () => {
6969
sources: [getLocalSource('source0', 'file0Source.mp4')],
7070
},
7171
endRequirement: {
72-
targets: [getLocalTarget('target0', 'file0Target.mp4')],
72+
targets: [getLocalTarget('target0', 'myFolder/file0Target.mp4')],
7373
content: {
7474
filePath: 'file0Target.mp4',
7575
},
@@ -90,14 +90,13 @@ describe('Basic', () => {
9090

9191
expect(env.expectationStatuses['copy0'].statusInfo.status).toEqual('fulfilled')
9292

93-
expect(await fsStat('/targets/target0/file0Target.mp4')).toMatchObject({
93+
expect(await fsStat('/targets/target0/myFolder/file0Target.mp4')).toMatchObject({
9494
size: 1234,
9595
})
9696
})
9797
test('Be able to copy Networked file to local', async () => {
9898
fs.__mockSetFile('\\\\networkShare/sources/source1/file0Source.mp4', 1234)
9999
fs.__mockSetDirectory('/targets/target1')
100-
// console.log(fs.__printAllFiles())
101100

102101
env.expectationManager.updateExpectations({
103102
copy0: literal<Expectation.FileCopy>({

0 commit comments

Comments
 (0)