Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
helje5 committed Jul 5, 2021
2 parents 616c32f + 0c6bb1a commit 1a32fb5
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
41 changes: 41 additions & 0 deletions Sources/DocCArchive/DocCArchive.swift
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,47 @@ public struct DocCArchive {
let data = try Data(contentsOf: url)
return try JSONDecoder().decode(DocCArchive.Document.self, from: data)
}

public func fetchDataFolderPathes() -> Set<String> {
// In the tests this seems to take about 10secs, no idea why? Doesn't matter
// whether I replace FM w/ straight Posix.
// Almost like someone forgot a debug `sleep(10)`.
var pathes = Set<String>()
pathes.reserveCapacity(100)

guard let de = fm.enumerator(at: dataURL,
includingPropertiesForKeys: [.isDirectoryKey],
options: [ .skipsHiddenFiles ])
else {
return []
}

let basePath = dataURL.path
for item in de {
guard let url = item as? URL else {
assert(item is URL, "Got non-URL item from directory enumeration")
continue
}

let isDir = (try? url.resourceValues(forKeys: [.isDirectoryKey]))?
.isDirectory ?? false
guard isDir else { continue }

guard url.path.hasPrefix(basePath) else {
assert(url.path.hasPrefix(basePath))
continue
}

let relPath = url.path.dropFirst(basePath.count)
assert(!relPath.isEmpty)
assert(!pathes.contains(String(relPath)))
assert(relPath.hasPrefix("/"))
pathes.insert(String(relPath))
}

return pathes
}

}

public extension DocCArchive {
Expand Down
12 changes: 12 additions & 0 deletions Tests/DocCArchiveTests/OpenArchiveTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,16 @@ final class OpenArchiveTests: XCTestCase {
try DocCArchive(contentsOf: URL(fileURLWithPath: "/tmp/missing.txt"))
)
}

func testSubfoldersInSlothCreator() throws {
let fm = FileManager.default
try XCTSkipUnless(fm.fileExists(atPath: Fixtures.slothCreatorArchive.path),
"This test needs the SlothCreator.doccarchive in the " +
"~/Downloads directory")

let archive = try DocCArchive(contentsOf: Fixtures.slothCreatorArchive)

let pathes = archive.fetchDataFolderPathes().sorted()
XCTAssertEqual(pathes.first, "/documentation")
}
}

0 comments on commit 1a32fb5

Please sign in to comment.