From 32a1887b5a0fed215216fd77753f7e338affaa3e Mon Sep 17 00:00:00 2001 From: Tanner Nelson Date: Tue, 28 Jun 2016 13:58:48 -0400 Subject: [PATCH 1/5] vapor 12 --- Sources/MustacheRenderer.swift | 8 ++++++-- Sources/Provider.swift | 8 ++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/Sources/MustacheRenderer.swift b/Sources/MustacheRenderer.swift index 13b28f6..6cd739c 100644 --- a/Sources/MustacheRenderer.swift +++ b/Sources/MustacheRenderer.swift @@ -4,9 +4,13 @@ import Mustache public class MustacheRenderer: RenderDriver { static let currentName = "__current" + public enum Error: ErrorProtocol { + case file(String, ErrorProtocol) + } + public var includes: [String: String] - public init(files: [String: String] = [:]) { + public init(files: [String: String] = [:]) throws { includes = [:] for (name, file) in files { @@ -16,7 +20,7 @@ public class MustacheRenderer: RenderDriver { includes[name] = String(validatingUTF8: bytes) } catch { - Log.warning("Could not open file \(file). Error: \(error)") + throw Error.file(file, error) } } } diff --git a/Sources/Provider.swift b/Sources/Provider.swift index 1eaef14..b5b581c 100644 --- a/Sources/Provider.swift +++ b/Sources/Provider.swift @@ -12,8 +12,12 @@ public class Provider: Vapor.Provider { includeFiles.forEach { (name, file) in files[name] = application.workDir + "Resources/Views/" + file } - - View.renderers[".mustache"] = MustacheRenderer(files: files) + + do { + View.renderers[".mustache"] = try MustacheRenderer(files: files) + } catch { + application.log.error("Could not configure Mustache rendered: \(error)") + } } } From a890116d9c5f10faca0dde7c862df70b97d365ef Mon Sep 17 00:00:00 2001 From: Tanner Nelson Date: Tue, 28 Jun 2016 14:11:10 -0400 Subject: [PATCH 2/5] update travis + package.swift --- .travis.yml | 4 +++- Package.swift | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 2e1485c..63e3d0e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,5 +8,7 @@ osx_image: xcode7.3 install: - eval "$(curl -sL https://gist.githubusercontent.com/kylef/5c0475ff02b7c7671d2a/raw/02090c7ede5a637b76e6df1710e83cd0bbe7dcdf/swiftenv-install.sh)" script: + # Build VaporMustache - swift build - # - swift build -c release + # Build VaporMustache release + - swift build -c release diff --git a/Package.swift b/Package.swift index c6cb4a1..46bad17 100644 --- a/Package.swift +++ b/Package.swift @@ -4,6 +4,6 @@ let package = Package( name: "VaporMustache", dependencies: [ .Package(url: "https://github.com/Zewo/Mustache.git", majorVersion: 0, minor: 6), - .Package(url: "https://github.com/qutheory/vapor.git", majorVersion: 0, minor: 11) + .Package(url: "https://github.com/qutheory/vapor.git", majorVersion: 0, minor: 12) ] ) From a5cd43f04164a6a7e94370448d7e3b82d209ceb2 Mon Sep 17 00:00:00 2001 From: Tanner Nelson Date: Tue, 28 Jun 2016 14:50:29 -0400 Subject: [PATCH 3/5] add tests --- Sources/Provider.swift | 2 +- Tests/LinuxMain.swift | 10 +++++++ Tests/VaporMustache/ProviderTests.swift | 39 +++++++++++++++++++++++++ Tests/VaporMustache/Utilities.swift | 0 4 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 Tests/LinuxMain.swift create mode 100644 Tests/VaporMustache/ProviderTests.swift create mode 100644 Tests/VaporMustache/Utilities.swift diff --git a/Sources/Provider.swift b/Sources/Provider.swift index b5b581c..d254c34 100644 --- a/Sources/Provider.swift +++ b/Sources/Provider.swift @@ -16,7 +16,7 @@ public class Provider: Vapor.Provider { do { View.renderers[".mustache"] = try MustacheRenderer(files: files) } catch { - application.log.error("Could not configure Mustache rendered: \(error)") + application.log.error("Could not configure Mustache: \(error)") } } diff --git a/Tests/LinuxMain.swift b/Tests/LinuxMain.swift new file mode 100644 index 0000000..dbc2822 --- /dev/null +++ b/Tests/LinuxMain.swift @@ -0,0 +1,10 @@ +#if os(Linux) + +import XCTest +@testable import VaporMustacheTestSuite + +XCTMain([ + testCase(ProviderTests.allTests), +]) + +#endif \ No newline at end of file diff --git a/Tests/VaporMustache/ProviderTests.swift b/Tests/VaporMustache/ProviderTests.swift new file mode 100644 index 0000000..c10a193 --- /dev/null +++ b/Tests/VaporMustache/ProviderTests.swift @@ -0,0 +1,39 @@ +import XCTest +@testable import VaporMustache +import Vapor + +class ProviderTests: XCTestCase { + static let allTests = [ + ("testSaveAndFind", testSaveAndFind) + ] + + func testSaveAndFind() { + let provider = Provider(withIncludes: [ + "a": "Includes/test-include-a.mustache", + "b": "Includes/test-include-b.mustache", + ]) + + let app = Application(providers: [provider]) + + let name = "abcdefghijklmnopqrstuvwxyz1234567890!@#$%^*()" + + do { + let view = try app.view("test-view.mustache", context: ["name": name]) + + let response = view.makeResponse() + + switch response.body { + case .data(let bytes): + let encoded = String(name.characters.split(separator: "&").joined(separator: "&".characters)) + let expected = "a\n\nHello, \(encoded)\n\nb" + + let string = try bytes.toString() + XCTAssertEqual(string, expected, "Template did not render properly") + case .chunked(_): + XCTFail("Incorrect body type") + } + } catch { + XCTFail("Could not make view: \(error)") + } + } +} \ No newline at end of file diff --git a/Tests/VaporMustache/Utilities.swift b/Tests/VaporMustache/Utilities.swift new file mode 100644 index 0000000..e69de29 From 6799fd0dccec786d2c33d9408ef6466e8306f90f Mon Sep 17 00:00:00 2001 From: Tanner Nelson Date: Tue, 28 Jun 2016 14:59:31 -0400 Subject: [PATCH 4/5] add testing to yml --- .travis.yml | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 63e3d0e..83fef33 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,7 +8,9 @@ osx_image: xcode7.3 install: - eval "$(curl -sL https://gist.githubusercontent.com/kylef/5c0475ff02b7c7671d2a/raw/02090c7ede5a637b76e6df1710e83cd0bbe7dcdf/swiftenv-install.sh)" script: - # Build VaporMustache + # Build Vapor Mustache - swift build - # Build VaporMustache release - swift build -c release + + # Test Vapor Mustache + - swift test From 274c8838b863f7e9a05d3a8f96e7a00e213b9460 Mon Sep 17 00:00:00 2001 From: Tanner Nelson Date: Tue, 28 Jun 2016 15:17:05 -0400 Subject: [PATCH 5/5] include test resources --- .gitignore | 2 -- Resources/Views/Includes/test-include-a.mustache | 1 + Resources/Views/Includes/test-include-b.mustache | 1 + Resources/Views/test-view.mustache | 5 +++++ 4 files changed, 7 insertions(+), 2 deletions(-) create mode 100644 Resources/Views/Includes/test-include-a.mustache create mode 100644 Resources/Views/Includes/test-include-b.mustache create mode 100644 Resources/Views/test-view.mustache diff --git a/.gitignore b/.gitignore index 3ab37a7..1c41145 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,3 @@ -Sources/main.swift Packages .build -Resources *.xcodeproj diff --git a/Resources/Views/Includes/test-include-a.mustache b/Resources/Views/Includes/test-include-a.mustache new file mode 100644 index 0000000..2e65efe --- /dev/null +++ b/Resources/Views/Includes/test-include-a.mustache @@ -0,0 +1 @@ +a \ No newline at end of file diff --git a/Resources/Views/Includes/test-include-b.mustache b/Resources/Views/Includes/test-include-b.mustache new file mode 100644 index 0000000..63d8dbd --- /dev/null +++ b/Resources/Views/Includes/test-include-b.mustache @@ -0,0 +1 @@ +b \ No newline at end of file diff --git a/Resources/Views/test-view.mustache b/Resources/Views/test-view.mustache new file mode 100644 index 0000000..8e1c87f --- /dev/null +++ b/Resources/Views/test-view.mustache @@ -0,0 +1,5 @@ +{{>a}} + +Hello, {{name}} + +{{>b}} \ No newline at end of file