Skip to content

Commit c9df71d

Browse files
committed
add forceOverwrite flag to write contract handling
1 parent 49692b8 commit c9df71d

3 files changed

Lines changed: 57 additions & 22 deletions

File tree

src/ARCtrl/ARC.fs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -151,13 +151,13 @@ type ARC(identifier : string, ?title : string, ?description : string, ?submissio
151151
static member fromArcInvestigation (isa : ArcInvestigation, ?fs : FileSystem, ?license: License) =
152152
ARC(identifier = isa.Identifier, ?title = isa.Title, ?description = isa.Description, ?submissionDate = isa.SubmissionDate, ?publicReleaseDate = isa.PublicReleaseDate, ontologySourceReferences = isa.OntologySourceReferences, publications = isa.Publications, contacts = isa.Contacts, assays = isa.Assays, studies = isa.Studies, workflows = isa.Workflows, runs = isa.Runs, registeredStudyIdentifiers = isa.RegisteredStudyIdentifiers, comments = isa.Comments, remarks = isa.Remarks, ?fs = fs, ?license = license)
153153

154-
member this.TryWriteAsync(arcPath) =
154+
member this.TryWriteAsync(arcPath, ?forceOverwrite) =
155155
this.GetWriteContracts()
156-
|> fullFillContractBatchAsync arcPath
156+
|> fullFillContractBatchAsync (defaultArg forceOverwrite false) arcPath
157157

158-
member this.TryUpdateAsync(arcPath) =
158+
member this.TryUpdateAsync(arcPath, ?forceOverwrite) =
159159
this.GetUpdateContracts()
160-
|> fullFillContractBatchAsync arcPath
160+
|> fullFillContractBatchAsync (defaultArg forceOverwrite false) arcPath
161161

162162
member this.SetLicenseFulltext (fulltext : string, ?path : string) : unit =
163163
match this.License with
@@ -180,7 +180,7 @@ type ARC(identifier : string, ?title : string, ?description : string, ?submissio
180180

181181
let! fulFilledContracts =
182182
contracts
183-
|> fullFillContractBatchAsync arcPath
183+
|> fullFillContractBatchAsync false arcPath
184184

185185
match fulFilledContracts with
186186
| Ok c ->
@@ -236,15 +236,15 @@ type ARC(identifier : string, ?title : string, ?description : string, ?submissio
236236

237237
member this.TryRemoveAssayAsync(arcPath : string, assayIdentifier: string) =
238238
this.GetAssayRemoveContracts(assayIdentifier)
239-
|> fullFillContractBatchAsync arcPath
239+
|> fullFillContractBatchAsync false arcPath
240240

241241
member this.TryRemoveRunAsync(arcPath : string, runIdentifier: string) =
242242
this.GetRunRemoveContracts(runIdentifier)
243-
|> fullFillContractBatchAsync arcPath
243+
|> fullFillContractBatchAsync false arcPath
244244

245245
member this.TryRemoveWorkflowAsync(arcPath : string, workflowIdentifier: string) =
246246
this.GetWorkflowRemoveContracts(workflowIdentifier)
247-
|> fullFillContractBatchAsync arcPath
247+
|> fullFillContractBatchAsync false arcPath
248248

249249
member this.RemoveRunAsync(arcPath, runIdentifier) =
250250
crossAsync {
@@ -268,11 +268,11 @@ type ARC(identifier : string, ?title : string, ?description : string, ?submissio
268268

269269
member this.TryRenameRunAsync(arcPath : string, oldRunIdentifier: string, newRunIdentifier: string) =
270270
this.GetRunRenameContracts(oldRunIdentifier,newRunIdentifier)
271-
|> fullFillContractBatchAsync arcPath
271+
|> fullFillContractBatchAsync false arcPath
272272

273273
member this.TryRenameWorkflowAsync(arcPath : string, oldWorkflowIdentifier: string, newWorkflowIdentifier: string) =
274274
this.GetWorkflowRenameContracts(oldWorkflowIdentifier,newWorkflowIdentifier)
275-
|> fullFillContractBatchAsync arcPath
275+
|> fullFillContractBatchAsync false arcPath
276276

277277
member this.RenameRunAsync(arcPath, oldRunIdentifier, newRunIdentifier) =
278278
crossAsync {
@@ -341,7 +341,7 @@ type ARC(identifier : string, ?title : string, ?description : string, ?submissio
341341

342342
member this.TryRenameAssayAsync(arcPath : string, oldAssayIdentifier: string, newAssayIdentifier: string) =
343343
this.GetAssayRenameContracts(oldAssayIdentifier,newAssayIdentifier)
344-
|> fullFillContractBatchAsync arcPath
344+
|> fullFillContractBatchAsync false arcPath
345345

346346
member this.GetStudyRemoveContracts(studyIdentifier: string) =
347347
base.RemoveStudy(studyIdentifier)
@@ -356,7 +356,7 @@ type ARC(identifier : string, ?title : string, ?description : string, ?submissio
356356

357357
member this.TryRemoveStudyAsync(arcPath : string, studyIdentifier: string) =
358358
this.GetStudyRemoveContracts(studyIdentifier)
359-
|> fullFillContractBatchAsync arcPath
359+
|> fullFillContractBatchAsync false arcPath
360360

361361
member this.GetStudyRenameContracts(oldStudyIdentifier: string, newStudyIdentifier: string) =
362362
if this.StudyIdentifiers |> Seq.contains oldStudyIdentifier |> not then
@@ -375,7 +375,7 @@ type ARC(identifier : string, ?title : string, ?description : string, ?submissio
375375

376376
member this.TryRenameStudyAsync(arcPath : string, oldStudyIdentifier: string, newStudyIdentifier: string) =
377377
this.GetStudyRenameContracts(oldStudyIdentifier,newStudyIdentifier)
378-
|> fullFillContractBatchAsync arcPath
378+
|> fullFillContractBatchAsync false arcPath
379379

380380

381381
member this.WriteAsync(arcPath) =

src/ARCtrl/ContractIO/ContractIO.fs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,13 @@ let fullfillContractBatchAsyncBy
5454
return res
5555
}
5656

57-
let fulfillWriteContractAsync basePath (c : Contract) =
57+
let fulfillWriteContractAsync (forceOverwrite : bool) basePath (c : Contract) =
5858
crossAsync {
59-
try
59+
try
60+
let! exists = FileSystemHelper.fileExistsAsync (ArcPathHelper.combine basePath c.Path)
61+
if exists && not forceOverwrite then
62+
return Error (sprintf "Contract %s already exists and overwrite is not allowed" c.Path)
63+
else
6064
match c.DTO with
6165
| Some (DTO.Text t) ->
6266
let path = ArcPathHelper.combine basePath c.Path
@@ -134,17 +138,17 @@ let fullfillDeleteContractAsync basePath (c : Contract) =
134138
}
135139
|> catchWith (fun e -> Error (sprintf "Error deleting contract %s: %s" c.Path e.Message))
136140

137-
let fullFillContract basePath (c : Contract) =
141+
let fullFillContract (forceOverwrite : bool) basePath (c : Contract) =
138142
crossAsync {
139143
match c.Operation with
140144
| Operation.READ -> return! fulfillReadContractAsync basePath c
141-
| Operation.CREATE -> return! fulfillWriteContractAsync basePath c
145+
| Operation.CREATE -> return! fulfillWriteContractAsync forceOverwrite basePath c
142146
| Operation.UPDATE -> return! fulfillUpdateContractAsync basePath c
143147
| Operation.DELETE -> return! fullfillDeleteContractAsync basePath c
144148
| Operation.RENAME -> return! fullfillRenameContractAsync basePath c
145149
| _ -> return Error (sprintf "Operation %A not supported" c.Operation)
146150
}
147151
|> catchWith (fun e -> Error (sprintf "Error fulfilling contract %s: %s" c.Path e.Message))
148152

149-
let fullFillContractBatchAsync basePath (cs : Contract []) =
150-
fullfillContractBatchAsyncBy fullFillContract basePath cs
153+
let fullFillContractBatchAsync (forceOverwrite : bool) basePath (cs : Contract []) =
154+
fullfillContractBatchAsyncBy (fullFillContract forceOverwrite) basePath cs

tests/ARCtrl/ContractIO.Tests.fs

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ let testWrite =
4949

5050
do! FileSystemHelper.ensureDirectoryAsync TestObjects.IO.testResultsFolder
5151

52-
let! resultContract = fulfillWriteContractAsync TestObjects.IO.testResultsFolder contract
52+
let! resultContract = fulfillWriteContractAsync false TestObjects.IO.testResultsFolder contract
5353

5454
Expect.isOk resultContract "Contract was not fulfilled correctly"
5555

@@ -67,7 +67,7 @@ let testWrite =
6767

6868
do! FileSystemHelper.ensureDirectoryAsync TestObjects.IO.testResultsFolder
6969

70-
let! resultContract = fulfillWriteContractAsync TestObjects.IO.testResultsFolder contract
70+
let! resultContract = fulfillWriteContractAsync false TestObjects.IO.testResultsFolder contract
7171

7272
Expect.isOk resultContract "Contract was not fulfilled correctly"
7373

@@ -78,6 +78,37 @@ let testWrite =
7878
let! resultText = FileSystemHelper.readFileTextAsync filePath
7979
Expect.equal resultText testText $"File {filePath} was not empty"
8080
})
81+
testCaseCrossAsync "ForceOverwrite" (crossAsync {
82+
let fileName = "TestReadMe.txt"
83+
let initialText = "This is the initial text"
84+
let newText = "This is the new text"
85+
let filePath = ArcPathHelper.combine TestObjects.IO.testResultsFolder fileName
86+
do! FileSystemHelper.writeFileTextAsync filePath initialText
87+
let dto = DTO.Text newText
88+
let contract = Contract.createCreate(fileName,DTOType.PlainText, dto)
89+
do! FileSystemHelper.ensureDirectoryAsync TestObjects.IO.testResultsFolder
90+
let! resultContract = fulfillWriteContractAsync true TestObjects.IO.testResultsFolder contract
91+
Expect.isOk resultContract "Contract was not fulfilled correctly"
92+
let! resultText = FileSystemHelper.readFileTextAsync filePath
93+
Expect.equal resultText newText $"File {filePath} was not overwritten"
94+
})
95+
testCaseCrossAsync "NoOverwrite" (crossAsync {
96+
let fileName = "TestReadMe.txt"
97+
let initialText = "This is the initial text"
98+
let newText = "This is the new text"
99+
let filePath = ArcPathHelper.combine TestObjects.IO.testResultsFolder fileName
100+
do! FileSystemHelper.writeFileTextAsync filePath initialText
101+
let dto = DTO.Text newText
102+
let contract = Contract.createCreate(fileName,DTOType.PlainText, dto)
103+
do! FileSystemHelper.ensureDirectoryAsync TestObjects.IO.testResultsFolder
104+
try
105+
let! r = fulfillWriteContractAsync false TestObjects.IO.testResultsFolder contract
106+
failwith $"Contract was fulfilled when it should not have been: {r}"
107+
with
108+
| _ -> ()
109+
let! resultText = FileSystemHelper.readFileTextAsync filePath
110+
Expect.equal resultText initialText $"File {filePath} was overwritten when it should not have been"
111+
})
81112
testCaseCrossAsync "XLSXFile" (crossAsync {
82113

83114
let worksheetName = "TestSheet"
@@ -92,7 +123,7 @@ let testWrite =
92123

93124
do! FileSystemHelper.ensureDirectoryAsync TestObjects.IO.testResultsFolder
94125

95-
let! resultContract = fulfillWriteContractAsync TestObjects.IO.testResultsFolder contract
126+
let! resultContract = fulfillWriteContractAsync false TestObjects.IO.testResultsFolder contract
96127

97128
Expect.isOk resultContract "Contract was not fulfilled correctly"
98129

0 commit comments

Comments
 (0)