Skip to content

Commit

Permalink
Fix issue #502
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric MORAND committed Apr 26, 2020
1 parent 0878858 commit df68334
Show file tree
Hide file tree
Showing 15 changed files with 81 additions and 48 deletions.
13 changes: 5 additions & 8 deletions src/lib/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,10 +348,7 @@ export abstract class TwingEnvironment extends EventEmitter {
this.getTemplateHash(name, index, from)
];

return Promise.all(hashesPromises).then((hashes) => {
let mainTemplateHash = hashes[0];
let templateHash = hashes[1];

return Promise.all(hashesPromises).then(([mainTemplateHash, templateHash]) => {
if (this.loadedTemplates.has(templateHash)) {
return Promise.resolve(this.loadedTemplates.get(templateHash));
} else {
Expand Down Expand Up @@ -918,13 +915,13 @@ return module.exports;
enterSourceMapBlock(line: number, column: number, nodeType: TwingNodeType, source: TwingSource) {
TwingOutputBuffering.obStart();

let sourceFQN = source.getFQN();
let sourceName = source.getResolvedName();

if (path.isAbsolute(sourceFQN)) {
sourceFQN = path.relative('.', sourceFQN);
if (path.isAbsolute(sourceName)) {
sourceName = path.relative('.', sourceName);
}

source = new TwingSource(source.getCode(), sourceFQN);
source = new TwingSource(source.getCode(), sourceName);

let factory = this.getSourceMapNodeFactory(nodeType);

Expand Down
2 changes: 1 addition & 1 deletion src/lib/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export class TwingError extends Error {
}

if (this.source) {
this.message += ` in "${this.source.getFQN()}"`;
this.message += ` in "${this.source.getName()}"`;
}

if (this.lineno && this.lineno >= 0) {
Expand Down
4 changes: 2 additions & 2 deletions src/lib/loader/relative-filesystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class TwingLoaderRelativeFilesystem implements TwingLoaderInterface {

getSourceContext(name: string, from: TwingSource): Promise<TwingSource> {
return this.findTemplate(name, true, from).then((path) => {
return new Promise((resolve, reject) => {
return new Promise((resolve) => {
readFile(path, 'UTF-8', (err, data) => {
resolve(new TwingSource(data, name, path))
})
Expand Down Expand Up @@ -129,7 +129,7 @@ export class TwingLoaderRelativeFilesystem implements TwingLoaderInterface {

private resolvePath(name: string, from: TwingSource): string {
if (name && from && !isAbsolutePath(name)) {
name = joinPath(dirname(from.getFQN()), name);
name = joinPath(dirname(from.getResolvedName()), name);
}

return name;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ export class TwingNode {
setTemplateName(name: string) {
this.name = name;

for (let [k, node] of this.nodes) {
for (let node of this.nodes.values()) {
node.setTemplateName(name);
}
}
Expand Down
4 changes: 1 addition & 3 deletions src/lib/node/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,7 @@ export class TwingNodeModule extends TwingNode {
.write('this.sourceContext = new this.Source(')
.string(compiler.getEnvironment().isDebug() || compiler.getEnvironment().isSourceMap() ? this.source.getCode() : '')
.raw(', ')
.string(this.source.getName())
.raw(', ')
.string(this.source.getFQN())
.string(this.source.getResolvedName())
.raw(");\n\n")
.write('let aliases = new this.Context();\n')
;
Expand Down
10 changes: 5 additions & 5 deletions src/lib/source.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
export class TwingSource {
private readonly code: string;
private readonly name: string;
private readonly fqn: string;
private readonly resolvedName: string;

constructor(code: string, name: string, fqn?: string) {
constructor(code: string, name: string, resolvedName?: string) {
this.code = code;
this.name = name;
this.fqn = fqn || name;
this.resolvedName = resolvedName || name;
}

getCode() {
Expand All @@ -17,7 +17,7 @@ export class TwingSource {
return this.name;
}

getFQN() {
return this.fqn;
getResolvedName() {
return this.resolvedName;
}
}
2 changes: 1 addition & 1 deletion test/tests/unit/index/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ tape('main and browser indexes', (test) => {
test.true(expected.includes(key), `${key} is legit in browser index`);
}

test.same(browserIndex.TwingLoaderFilesystem.name, 'TwingLoaderNull', 'browser export of TwingLoaderFilesystem is a noop');
test.same(browserIndex.TwingLoaderFilesystem.name, 'TwingLoaderNull', 'browser export of TwingLoaderRelativeFilesystem is a noop');
test.same(browserIndex.TwingLoaderRelativeFilesystem.name, 'TwingLoaderNull', 'browser export of TwingLoaderRelativefilesystem is a noop');
test.same(browserIndex.TwingCacheFilesystem.name, 'TwingCacheNull', 'browser export of TwingCacheFilesystem is a noop');

Expand Down
1 change: 1 addition & 0 deletions test/tests/unit/lib/environment/fixtures/index.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% include partial %}
Empty file.
31 changes: 30 additions & 1 deletion test/tests/unit/lib/environment/test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as tape from 'tape';
import * as sinon from 'sinon';
import {join} from 'path';
import {join, resolve} from 'path';
import {readFileSync} from 'fs';
import {TwingTokenParser} from "../../../../../src/lib/token-parser";
import {Token, TokenType} from "twig-lexer";
Expand Down Expand Up @@ -30,6 +30,7 @@ import {MappingItem, SourceMapConsumer} from "source-map";
import {TwingLoaderFilesystem} from "../../../../../src/lib/loader/filesystem";
import {TwingNodeText} from "../../../../../src/lib/node/text";
import {TwingSandboxSecurityPolicy} from "../../../../../src/lib/sandbox/security-policy";
import {TwingLoaderRelativeFilesystem} from "../../../../../src/lib/loader/relative-filesystem";

const tmp = require('tmp');

Expand Down Expand Up @@ -1363,5 +1364,33 @@ BAROOF</FOO></foo>oof`);
test.end();
});

test.test('with relative filesystem loader', async (test) => {
const names: Array<string> = [];

class CustomCache extends TwingCacheNull {
generateKey(name: string, className: string): Promise<string> {
names.push(name);

return Promise.resolve(name);
}
}

const cache = new CustomCache();
const fixturesPath = 'test/tests/unit/lib/environment/fixtures';
const env = new TwingEnvironmentNode(new TwingLoaderRelativeFilesystem(), {
cache: cache
});

await env.render(resolve(fixturesPath, 'index.html.twig'), {partial: 'partials/foo.html.twig'});
await env.render(resolve(fixturesPath, 'index.html.twig'), {partial: 'partials/../partials/foo.html.twig'});

test.same(names, [
resolve(fixturesPath, 'index.html.twig'),
'partials/foo.html.twig'
]);

test.end();
});

test.end();
});
3 changes: 1 addition & 2 deletions test/tests/unit/lib/error/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,7 @@ tape('TwingError', (test) => {
test.true(e instanceof TwingErrorRuntime);
test.same(e.getMessage(), `Variable \`foo\` does not exist in "${path.resolve('test/tests/integration/fixtures/errors/index.html')}" at line 3.`);
test.same(e.getTemplateLine(), 3);
test.same(e.getSourceContext().getName(), 'index.html');
test.same(e.getSourceContext().getFQN(), path.resolve('test/tests/integration/fixtures/errors/index.html'));
test.same(e.getSourceContext().getName(), path.resolve('test/tests/integration/fixtures/errors/index.html'));
}

test.end();
Expand Down
2 changes: 1 addition & 1 deletion test/tests/unit/lib/loader/chain/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ tape('loader chain', (test) => {
test.test('errors/base.html', (test) => {
loader.getSourceContext('errors/base.html', null).then((source) => {
test.same(source.getName(), 'errors/base.html');
test.same(source.getFQN(), join(fixturesPath, 'errors/base.html'));
test.same(source.getResolvedName(), join(fixturesPath, 'errors/base.html'));
test.notSame(source.getCode(), 'baz');
test.end();
});
Expand Down
2 changes: 1 addition & 1 deletion test/tests/unit/lib/loader/filesystem/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ tape('loader filesystem', (test) => {
let source = await loader.getSourceContext('errors/index.html', null);

test.same(source.getName(), 'errors/index.html');
test.same(source.getFQN(), nodePath.resolve(nodePath.join(path, '/errors/index.html')));
test.same(source.getResolvedName(), nodePath.resolve(nodePath.join(path, '/errors/index.html')));

try {
await loader.getSourceContext('@foo/bar', null);
Expand Down
47 changes: 28 additions & 19 deletions test/tests/unit/lib/loader/relative-filesystem/test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as tape from 'tape';
import {TwingLoaderRelativeFilesystem as TwingLoaderFilesystem} from "../../../../../../src/lib/loader/relative-filesystem";
import {TwingLoaderRelativeFilesystem} from "../../../../../../src/lib/loader/relative-filesystem";
import {TwingErrorLoader} from "../../../../../../src/lib/error/loader";
import {TwingSource} from "../../../../../../src/lib/source";

Expand Down Expand Up @@ -30,20 +30,13 @@ let securityTests = [
['/../AutoloaderTest.php'],
];

let arrayInheritanceTests = new Map([
['valid array inheritance', ['array_inheritance_valid_parent.html.twig']],
['array inheritance with null first template', ['array_inheritance_null_parent.html.twig']],
['array inheritance with empty first template', ['array_inheritance_empty_parent.html.twig']],
['array inheritance with non-existent first template', ['array_inheritance_nonexistent_parent.html.twig']]
]);

tape('loader filesystem', (test) => {
test.test('getSourceContext', async (test) => {
let resolvePath = (path: string) => {
return nodePath.resolve('test/tests/integration/fixtures', path);
};

let loader = new TwingLoaderFilesystem();
let loader = new TwingLoaderRelativeFilesystem();

try {
await loader.getSourceContext('errors/index.html', null);
Expand All @@ -55,12 +48,12 @@ tape('loader filesystem', (test) => {
let source = await loader.getSourceContext('errors/index.html', new TwingSource('', resolvePath('foo.html')));

test.same(source.getName(), 'errors/index.html');
test.same(source.getFQN(), resolvePath('errors/index.html'));
test.same(source.getResolvedName(), resolvePath('errors/index.html'));

source = await loader.getSourceContext('../errors/index.html', new TwingSource('', resolvePath('foo/bar.html')));

test.same(source.getName(), '../errors/index.html');
test.same(source.getFQN(), resolvePath('errors/index.html'));
test.same(source.getResolvedName(), resolvePath('errors/index.html'));

try {
await loader.getSourceContext('foo', new TwingSource('', 'foo/bar/index.html'));
Expand Down Expand Up @@ -95,7 +88,7 @@ tape('loader filesystem', (test) => {
test.test('security', async (test) => {
for (let securityTest of securityTests) {
let template = securityTest[0];
let loader = new TwingLoaderFilesystem();
let loader = new TwingLoaderRelativeFilesystem();

try {
await loader.getCacheKey(template, null);
Expand All @@ -114,7 +107,7 @@ tape('loader filesystem', (test) => {
return nodePath.resolve(fixturesPath, path);
};

let CustomLoader = class extends TwingLoaderFilesystem {
let CustomLoader = class extends TwingLoaderRelativeFilesystem {
findTemplate(name: string, throw_: boolean, from: TwingSource) {
return super.findTemplate(name, throw_, from);
}
Expand Down Expand Up @@ -145,7 +138,7 @@ tape('loader filesystem', (test) => {
test.end();
});

test.test('ddd', async (test) => {
test.test('with from defined', async (test) => {
test.same(await loader.resolve('partial.html.twig', new TwingSource('', 'index.html.twig', 'test/tests/unit/lib/loader/relative-filesystem/fixtures/index.html.twig')), nodePath.resolve('test/tests/unit/lib/loader/relative-filesystem/fixtures/partial.html.twig'));

test.end();
Expand All @@ -159,7 +152,7 @@ tape('loader filesystem', (test) => {
return nodePath.resolve(fixturesPath, path);
};

let loader = new TwingLoaderFilesystem();
let loader = new TwingLoaderRelativeFilesystem();
let namedSource = (await loader.getSourceContext('named/index.html', new TwingSource('', resolvePath('index.html')))).getCode();

test.same(namedSource, "named path\n");
Expand All @@ -172,7 +165,7 @@ tape('loader filesystem', (test) => {
return nodePath.resolve(fixturesPath, path);
};

let loader = new TwingLoaderFilesystem();
let loader = new TwingLoaderRelativeFilesystem();

let names = [
['named/index.html', 'named/index.html'],
Expand Down Expand Up @@ -209,7 +202,7 @@ tape('loader filesystem', (test) => {
return nodePath.resolve('test/tests/unit/lib/loader/filesystem/fixtures', path);
};

let loader = new TwingLoaderFilesystem();
let loader = new TwingLoaderRelativeFilesystem();
let source = new TwingSource('', resolvePath('index.html'));

test.equals(await loader.exists('normal/index.html', source), true);
Expand Down Expand Up @@ -237,7 +230,7 @@ tape('loader filesystem', (test) => {
return nodePath.resolve('test/tests/unit/lib/loader/filesystem/fixtures', path);
};

let loader = new TwingLoaderFilesystem();
let loader = new TwingLoaderRelativeFilesystem();
let source = new TwingSource('', resolvePath('index.html'));

test.true(await loader.isFresh('normal/index.html', new Date().getTime(), source));
Expand All @@ -250,7 +243,7 @@ tape('loader filesystem', (test) => {
return nodePath.resolve('test/tests/unit/lib/loader/filesystem/fixtures', path);
};

let loader = new TwingLoaderFilesystem();
let loader = new TwingLoaderRelativeFilesystem();
let source = new TwingSource('', resolvePath('index.html'));

test.same(await loader.resolve('normal/index.html', source), resolvePath('normal/index.html'));
Expand All @@ -259,5 +252,21 @@ tape('loader filesystem', (test) => {
test.end();
});

test.test('getCacheKey', async (test) => {
let loader = new TwingLoaderRelativeFilesystem();
let resolvePath = (path: string) => {
return nodePath.resolve('test/tests/unit/lib/loader/relative-filesystem/fixtures', path);
};

let source = new TwingSource('', resolvePath('index.html'));

let key1 = loader.getCacheKey('partial.html.twig', source);
let key2 = loader.getCacheKey('../fixtures/partial.html.twig', source);

test.same(key1, key2);

test.end();
});

test.end();
});
6 changes: 3 additions & 3 deletions test/tests/unit/lib/node/module/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ tape('node/module', (test) => {
constructor(env) {
super(env);
this.sourceContext = new this.Source(\`\`, \`foo.twig\`, \`foo.twig\`);
this.sourceContext = new this.Source(\`\`, \`foo.twig\`);
let aliases = new this.Context();
}
Expand Down Expand Up @@ -93,7 +93,7 @@ tape('node/module', (test) => {
constructor(env) {
super(env);
this.sourceContext = new this.Source(\`\`, \`foo.twig\`, \`foo.twig\`);
this.sourceContext = new this.Source(\`\`, \`foo.twig\`);
let aliases = new this.Context();
}
Expand Down Expand Up @@ -164,7 +164,7 @@ tape('node/module', (test) => {
constructor(env) {
super(env);
this.sourceContext = new this.Source(\`{{ foo }}\`, \`foo.twig\`, \`foo.twig\`);
this.sourceContext = new this.Source(\`{{ foo }}\`, \`foo.twig\`);
let aliases = new this.Context();
}
Expand Down

0 comments on commit df68334

Please sign in to comment.