Skip to content

Commit

Permalink
Merge pull request #8 from vapor/alternate-file-types
Browse files Browse the repository at this point in the history
work with non leaf documents as raw bytes #2
  • Loading branch information
loganwright authored Aug 31, 2016
2 parents ec4c13e + d62c3ac commit 491436e
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
1 change: 1 addition & 0 deletions Resources/random-file.any
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This file #(won't) be #rendered() {}
24 changes: 19 additions & 5 deletions Sources/Leaf/Stem+Spawn.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,28 @@ extension Stem {
}

public func spawnLeaf(named name: String) throws -> Leaf {
if let existing = cache?[name] { return existing }
var name = name
if name.hasPrefix("/") {
name = String(name.characters.dropFirst())
}

var subpath = name.finished(with: SUFFIX)
if subpath.hasPrefix("/") {
subpath = String(subpath.characters.dropFirst())
// non-leaf document. rendered as pure bytes
if name.characters.contains("."), !name.hasSuffix(".leaf") {
if let existing = cache?[name] { return existing }
let path = workingDirectory + name
let bytes = try Bytes.load(path: path)
let component = Leaf.Component.raw(bytes)
let leaf = Leaf(raw: bytes.string, components: [component])
cache(leaf, named: name)
return leaf
}
let path = workingDirectory + subpath

name = name.finished(with: SUFFIX)

// add suffix if necessary
if let existing = cache?[name] { return existing }

let path = workingDirectory + name
let raw = try Bytes.load(path: path)
let leaf = try spawnLeaf(raw: raw)
cache(leaf, named: name)
Expand Down
22 changes: 22 additions & 0 deletions Tests/LeafTests/FileLoadTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import Foundation
import XCTest
@testable import Leaf

class FileLoadTests: XCTestCase {
static let allTests = [
("testLoadRawBytes", testLoadRawBytes),
]

func testLoadRawBytes() throws {
let leaf = try stem.spawnLeaf(named: "random-file.any")
XCTAssert(Array(leaf.components).count == 1)

let rendered = try stem.render(leaf, with: Context([])).string
// tags are not parsed in non-leaf document
let expectation = "This file #(won't) be #rendered() {}\n"
XCTAssertEqual(rendered, expectation)

// test cache
_ = try stem.spawnLeaf(named: "random-file.any")
}
}

0 comments on commit 491436e

Please sign in to comment.