From 214c96c1e67fd9bf57b09d6916ff685fc6456fcc Mon Sep 17 00:00:00 2001 From: Adam Fowler Date: Wed, 31 Jan 2024 15:07:27 +0000 Subject: [PATCH] Make HBMustacheLibrary.loadTemplates async --- .../Library+FileSystem.swift | 2 +- Sources/HummingbirdMustache/Library.swift | 4 ++-- .../LibraryTests.swift | 18 ++++++++---------- 3 files changed, 11 insertions(+), 13 deletions(-) diff --git a/Sources/HummingbirdMustache/Library+FileSystem.swift b/Sources/HummingbirdMustache/Library+FileSystem.swift index ead2e38..9503eed 100644 --- a/Sources/HummingbirdMustache/Library+FileSystem.swift +++ b/Sources/HummingbirdMustache/Library+FileSystem.swift @@ -16,7 +16,7 @@ import Foundation extension HBMustacheLibrary { /// Load templates from a folder - static func loadTemplates(from directory: String, withExtension extension: String = "mustache") throws -> [String: HBMustacheTemplate] { + static func loadTemplates(from directory: String, withExtension extension: String = "mustache") async throws -> [String: HBMustacheTemplate] { var directory = directory if !directory.hasSuffix("/") { directory += "/" diff --git a/Sources/HummingbirdMustache/Library.swift b/Sources/HummingbirdMustache/Library.swift index 0d88e8c..515292e 100644 --- a/Sources/HummingbirdMustache/Library.swift +++ b/Sources/HummingbirdMustache/Library.swift @@ -40,8 +40,8 @@ public struct HBMustacheLibrary: Sendable { /// the folder is recursive and templates in subfolders will be registered with the name `subfolder/template`. /// - Parameter directory: Directory to look for mustache templates /// - Parameter extension: Extension of files to look for - public init(directory: String, withExtension extension: String = "mustache") throws { - self.templates = try Self.loadTemplates(from: directory, withExtension: `extension`) + public init(directory: String, withExtension extension: String = "mustache") async throws { + self.templates = try await Self.loadTemplates(from: directory, withExtension: `extension`) } /// Register template under name diff --git a/Tests/HummingbirdMustacheTests/LibraryTests.swift b/Tests/HummingbirdMustacheTests/LibraryTests.swift index 8f4cee5..5a92bcf 100644 --- a/Tests/HummingbirdMustacheTests/LibraryTests.swift +++ b/Tests/HummingbirdMustacheTests/LibraryTests.swift @@ -16,7 +16,7 @@ import XCTest final class LibraryTests: XCTestCase { - func testDirectoryLoad() throws { + func testDirectoryLoad() async throws { let fs = FileManager() try? fs.createDirectory(atPath: "templates", withIntermediateDirectories: false) defer { XCTAssertNoThrow(try fs.removeItem(atPath: "templates")) } @@ -24,12 +24,12 @@ final class LibraryTests: XCTestCase { try mustache.write(to: URL(fileURLWithPath: "templates/test.mustache")) defer { XCTAssertNoThrow(try fs.removeItem(atPath: "templates/test.mustache")) } - let library = try HBMustacheLibrary(directory: "./templates") + let library = try await HBMustacheLibrary(directory: "./templates") let object = ["value": ["value1", "value2"]] XCTAssertEqual(library.render(object, withTemplate: "test"), "value1value2") } - func testPartial() throws { + func testPartial() async throws { let fs = FileManager() try? fs.createDirectory(atPath: "templates", withIntermediateDirectories: false) let mustache = Data("{{#value}}{{.}}{{/value}}".utf8) @@ -42,12 +42,12 @@ final class LibraryTests: XCTestCase { XCTAssertNoThrow(try fs.removeItem(atPath: "templates")) } - let library = try HBMustacheLibrary(directory: "./templates") + let library = try await HBMustacheLibrary(directory: "./templates") let object = ["value": ["value1", "value2"]] XCTAssertEqual(library.render(object, withTemplate: "test"), "value1value2") } - func testLibraryParserError() throws { + func testLibraryParserError() async throws { let fs = FileManager() try? fs.createDirectory(atPath: "templates", withIntermediateDirectories: false) defer { XCTAssertNoThrow(try fs.removeItem(atPath: "templates")) } @@ -62,11 +62,9 @@ final class LibraryTests: XCTestCase { try mustache2.write(to: URL(fileURLWithPath: "templates/error.mustache")) defer { XCTAssertNoThrow(try fs.removeItem(atPath: "templates/error.mustache")) } - XCTAssertThrowsError(try HBMustacheLibrary(directory: "./templates")) { error in - guard let parserError = error as? HBMustacheLibrary.ParserError else { - XCTFail("\(error)") - return - } + do { + _ = try await HBMustacheLibrary(directory: "./templates") + } catch let parserError as HBMustacheLibrary.ParserError { XCTAssertEqual(parserError.filename, "error.mustache") XCTAssertEqual(parserError.context.line, "{{{name}}") XCTAssertEqual(parserError.context.lineNumber, 2)