Skip to content

Commit

Permalink
Merge pull request #125 from ericmorand/issue_124
Browse files Browse the repository at this point in the history
Fix issue #124
  • Loading branch information
ericmorand authored Mar 14, 2018
2 parents ef18dcb + 76a63b2 commit e1f3f5d
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 15 deletions.
14 changes: 8 additions & 6 deletions src/twing/loader/filesystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {TwingErrorLoader} from "../error/loader";

const nodePath = require('path');
const fs = require('fs');
const rtrim = require('locutus/php/strings/rtrim');

/**
* Loads template from the filesystem.
Expand Down Expand Up @@ -89,18 +90,17 @@ export class TwingLoaderFilesystem implements TwingLoaderInterface {

let checkPath = this.isAbsolutePath(path) ? path : nodePath.join(this.rootPath, path);

let stat = fs.statSync(checkPath);
let stat = fs.statSync(this.normalizeName(checkPath));

if (!stat.isDirectory()) {
throw new TwingErrorLoader(`The "${path}" directory does not exist ("${checkPath}").`);
}

if (!this.paths.has(namespace)) {
this.paths.set(namespace, [path]);
}
else {
this.paths.get(namespace).push(path);
this.paths.set(namespace, []);
}

this.paths.get(namespace).push(rtrim(path, '\/\\\\'));
}

/**
Expand All @@ -117,12 +117,14 @@ export class TwingLoaderFilesystem implements TwingLoaderInterface {

let checkPath = this.isAbsolutePath(path) ? path : nodePath.join(this.rootPath, path);

let stat = fs.statSync(checkPath);
let stat = fs.statSync(this.normalizeName(checkPath));

if (!stat.isDirectory()) {
throw new TwingErrorLoader(`The "${path}" directory does not exist ("${checkPath}").`);
}

path = rtrim(path, '\/\\\\');

if (!this.paths.has(namespace)) {
this.paths.set(namespace, [path]);
}
Expand Down
52 changes: 43 additions & 9 deletions test/tests/unit/twing/loader/filesystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,21 +208,21 @@ tap.test('loader filesystem', function (test) {
});

test.test('array-inheritance', async function (test) {
for (let [testMessage, arrayInheritanceTest] of arrayInheritanceTests) {
let templateName = arrayInheritanceTest[0];
let loader = new TwingLoaderFilesystem([]);
loader.addPath(nodePath.join(fixturesPath, 'inheritance'));
for (let [testMessage, arrayInheritanceTest] of arrayInheritanceTests) {
let templateName = arrayInheritanceTest[0];
let loader = new TwingLoaderFilesystem([]);
loader.addPath(nodePath.join(fixturesPath, 'inheritance'));

let twing = new TwingEnvironment(loader, {cache: 'tmp'});
let template = twing.loadTemplate(templateName);
let twing = new TwingEnvironment(loader, {cache: 'tmp'});
let template = twing.loadTemplate(templateName);

test.same(await template.renderBlock('body', {}), 'VALID Child', testMessage);
}
test.same(await template.renderBlock('body', {}), 'VALID Child', testMessage);
}

test.end();
});

test.test('should normalize template name', function(test) {
test.test('should normalize template name', function (test) {
let loader = new TwingLoaderFilesystem(fixturesPath);

let names = [
Expand All @@ -247,5 +247,39 @@ tap.test('loader filesystem', function (test) {
test.end();
});

test.test('addPath and prependPath should trim trailing slashes', function (test) {
// ensure that trailing slashes are removed by addPath
let loader = new TwingLoaderFilesystem(fixturesPath);
loader.addPath(nodePath.join(fixturesPath, 'normal/'));
loader.addPath(nodePath.join(fixturesPath, 'normal//'));
loader.addPath(nodePath.join(fixturesPath, 'normal\\'));
loader.addPath(nodePath.join(fixturesPath, 'normal\\\\'));

test.same(loader.getPaths(), [
fixturesPath,
nodePath.join(fixturesPath, 'normal'),
nodePath.join(fixturesPath, 'normal'),
nodePath.join(fixturesPath, 'normal'),
nodePath.join(fixturesPath, 'normal')
]);

// ensure that trailing slashes are removed by prependPath
loader = new TwingLoaderFilesystem(fixturesPath);
loader.prependPath(nodePath.join(fixturesPath, 'normal/'));
loader.prependPath(nodePath.join(fixturesPath, 'normal//'));
loader.prependPath(nodePath.join(fixturesPath, 'normal\\'));
loader.prependPath(nodePath.join(fixturesPath, 'normal\\\\'));

test.same(loader.getPaths(), [
nodePath.join(fixturesPath, 'normal'),
nodePath.join(fixturesPath, 'normal'),
nodePath.join(fixturesPath, 'normal'),
nodePath.join(fixturesPath, 'normal'),
fixturesPath
]);

test.end();
});

test.end();
});

0 comments on commit e1f3f5d

Please sign in to comment.