Skip to content

Commit

Permalink
[Opc] Fix Prinziple.getDirectories for ZIPs without explicit director…
Browse files Browse the repository at this point in the history
…y entries
  • Loading branch information
hyazinthh committed Aug 27, 2024
1 parent 4074fa8 commit bb48267
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 32 deletions.
56 changes: 39 additions & 17 deletions src/Aardvark.Data.Opc/Prinziple.fs
Original file line number Diff line number Diff line change
Expand Up @@ -105,39 +105,61 @@ module Prinziple =

check 0

member inline private x.GetEntries (predicate: string -> bool) =
member x.GetFiles(path: string, recursive: bool) =
let path = path.ToLowerInvariant()

let isFileInPath (n: string) =
n |> String.endsWith "/" |> not &&
n |> String.startsWith path &&
(recursive || n.LastIndexOf('/') < path.Length)

let result = ResizeArray<string>()

for i = 0 to int zip.Count - 1 do
let n = (zip.EntryByIndex i).Name

if predicate <| n.ToLowerInvariant() then
if isFileInPath <| n.ToLowerInvariant() then
result.Add (Path.Combine(rootPath, n.Replace('/', dirSeparator)))

for KeyValue(n, p) in additionalEntries do
if not <| result.Contains p && predicate n then
if not <| result.Contains p && isFileInPath n then
result.Add p

result.ToArray()

member x.GetFiles(path: string, recursive: bool) =
member x.GetDirectories(path: string) =
let path = path.ToLowerInvariant()

x.GetEntries (fun n ->
n |> String.endsWith "/" |> not &&
n |> String.startsWith path &&
(recursive || n.LastIndexOf('/') < path.Length)
)
let tryGetFolder (n: string) =
if n.ToLowerInvariant().StartsWith(path) && n.Length > path.Length then
let name = n.Substring path.Length
let s = name.IndexOf '/'

member x.GetDirectories(path: string) =
let path = path.ToLowerInvariant()
if s > -1 then
ValueSome <| n.Substring(0, path.Length + s + 1)
else
ValueNone
else
ValueNone

x.GetEntries (fun n ->
n |> String.endsWith "/" &&
n |> String.startsWith path &&
n.Length > path.Length &&
n.Substring(0, n.Length - 1).LastIndexOf('/') < path.Length
)
let result = HashSet<string>()

let addEntry (n: string) =
match tryGetFolder n with
| ValueSome n ->
let p = Path.Combine(rootPath, n.Replace('/', dirSeparator))
result.Add p |> ignore

| _ -> ()

for i = 0 to int zip.Count - 1 do
let n = (zip.EntryByIndex i).Name
addEntry n

for KeyValue(n, _) in additionalEntries do
addEntry n

result.ToArray(result.Count)

member x.Commit(compress: bool) =
if not additionalEntries.IsEmpty then
Expand Down
2 changes: 1 addition & 1 deletion src/Tests/FSharpTests/FSharpTests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
</PropertyGroup>
<ItemGroup>
<None Include="data\opc\test.zip" />
<None Include="data\opc\test_implicit_dirs.zip" />
<EmbeddedResource Include="data\Avocado.zip" />
<EmbeddedResource Include="data\Avocado.glb" />
<EmbeddedResource Include="data\2CylinderEngine.gltf" />
<Compile Include="GLTFTests.fs" />
<Compile Include="OpcTests.fs" />
<Content Include="paket.references" />
</ItemGroup>
<ItemGroup />
<ItemGroup>
<ProjectReference Include="..\..\Aardvark.Data.GLTF\Aardvark.Data.GLTF.fsproj" />
<ProjectReference Include="..\..\Aardvark.Data.Opc\Aardvark.Data.Opc.fsproj" />
Expand Down
50 changes: 36 additions & 14 deletions src/Tests/FSharpTests/OpcTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,42 @@ open System.Threading.Tasks
open NUnit.Framework
open FsUnit

[<Struct>]
type ZipKind =
| ImplicitDirs
| ExplicitDirs

[<Struct>]
type GetFilesMode =
| Recursive
| TopLevel

module Prinziple =

let private rootPath = Path.Combine(__SOURCE_DIRECTORY__, "data", "opc", "test")
let private getRootPath (zipKind: ZipKind) =
let file = if zipKind = ImplicitDirs then "test_implicit_dirs" else "test"
Path.Combine(__SOURCE_DIRECTORY__, "data", "opc", file)

let private ZipKindSource = [| ImplicitDirs; ExplicitDirs |]
let private GetFilesModeSource = [| Recursive; TopLevel |]

[<Test>]
let fileExists() =
let fileExists ([<ValueSource(nameof ZipKindSource)>] zipKind: ZipKind) =
let rootPath = getRootPath zipKind
Prinziple.tryRegister rootPath |> should be True
[rootPath; "files"; "nested"; "g.txt"] |> Path.combine |> Prinziple.fileExists |> should be True
[rootPath; "files"; "nested"] |> Path.combine |> Prinziple.fileExists |> should be False

[<Test>]
let directoryExists() =
let directoryExists([<ValueSource(nameof ZipKindSource)>] zipKind: ZipKind) =
let rootPath = getRootPath zipKind
Prinziple.tryRegister rootPath |> should be True
[rootPath; "files"; "nested"; "g.txt"] |> Path.combine |> Prinziple.directoryExists |> should be False
[rootPath; "files"; "nested"] |> Path.combine |> Prinziple.directoryExists |> should be True

[<TestCase(true)>]
[<TestCase(false)>]
let getFiles (recursive: bool) =
[<Test>]
let getFiles ([<ValueSource(nameof GetFilesModeSource)>] mode: GetFilesMode) ([<ValueSource(nameof ZipKindSource)>] zipKind: ZipKind) =
let rootPath = getRootPath zipKind
Prinziple.tryRegister rootPath |> should be True

let expected =
Expand All @@ -37,21 +54,22 @@ module Prinziple =
"files/c.txt"
"files/d.txt"
"files/e.txt"
if recursive then "files/more/f.txt"
if recursive then "files/nested/g.txt"
if mode = Recursive then "files/more/f.txt"
if mode = Recursive then "files/nested/g.txt"
|]
|> Array.map (fun f -> Path.Combine(rootPath, f) |> Path.GetFullPath)
|> Array.sort

let files =
Path.combine [rootPath; "files"]
|> Prinziple.getFiles recursive
|> Prinziple.getFiles (mode = Recursive)
|> Array.sort

files |> should equal expected

[<Test>]
let getDirectories() =
let getDirectories ([<ValueSource(nameof ZipKindSource)>] zipKind: ZipKind) =
let rootPath = getRootPath zipKind
Prinziple.tryRegister rootPath |> should be True

let expected =
Expand All @@ -70,14 +88,16 @@ module Prinziple =
dirs |> should equal expected

[<Test>]
let reading() =
let reading ([<ValueSource(nameof ZipKindSource)>] zipKind: ZipKind) =
let rootPath = getRootPath zipKind
Prinziple.tryRegister rootPath |> should be True

let path = Path.combine [rootPath; "files"; "a.txt"]
Prinziple.readAllText path |> should equal "TEST"

[<Test>]
let ``concurrent I/O``() =
let ``concurrent I/O`` ([<ValueSource(nameof ZipKindSource)>] zipKind: ZipKind) =
let rootPath = getRootPath zipKind
Prinziple.tryRegister rootPath |> should be True

try
Expand Down Expand Up @@ -127,7 +147,8 @@ module Prinziple =
Directory.Delete(rootPath, true)

[<Test>]
let writing() =
let writing ([<ValueSource(nameof ZipKindSource)>] zipKind: ZipKind) =
let rootPath = getRootPath zipKind
Prinziple.tryRegister rootPath |> should be True

let path = Path.combine [rootPath; "files"; "new.txt"]
Expand All @@ -145,7 +166,8 @@ module Prinziple =
Directory.Delete(rootPath, true)

[<Test>]
let commit() =
let commit ([<ValueSource(nameof ZipKindSource)>] zipKind: ZipKind) =
let rootPath = getRootPath zipKind
let tempRootPath = rootPath + "_temp"
let tempRootZip = Path.ChangeExtension(tempRootPath, ".zip")

Expand Down
Binary file not shown.

0 comments on commit bb48267

Please sign in to comment.