Skip to content

Commit

Permalink
fix: Requiring local node files is restricted: adding test for relati…
Browse files Browse the repository at this point in the history
…ve path resolvers
  • Loading branch information
paveltiunov committed Sep 9, 2019
1 parent abeb288 commit f328d07
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,9 @@ class DataSchemaCompiler {
} else {
const foundFile = self.resolveModuleFile(file, extensionName, toCompile, errorsReport);
if (!foundFile && this.allowNodeRequire) {
if (extensionName.indexOf('.') === 0) {
extensionName = path.resolve(this.repository.localPath(), extensionName);
}
// eslint-disable-next-line global-require,import/no-dynamic-require
return require(extensionName);
}
Expand Down
49 changes: 49 additions & 0 deletions packages/cubejs-schema-compiler/test/AsyncModuleTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,53 @@ describe('AsyncModule', () => {
});
});
});

it('import local node module', () => {
const { joinGraph, cubeEvaluator, compiler } = prepareCompiler(`
import { foo } from '../test/TestHelperForImport.js';
cube(foo(), {
sql: \`
select * from visitors
\`,
measures: {
visitor_count: {
type: 'count',
sql: 'id'
},
visitor_revenue: {
type: 'sum',
sql: 'amount'
}
},
dimensions: {
source: {
type: 'string',
sql: 'source'
},
created_at: {
type: 'time',
sql: 'created_at'
}
}
})
`, { allowNodeRequire: true });
return compiler.compile().then(() => {
const query = new PostgresQuery({ joinGraph, cubeEvaluator, compiler }, {
measures: ['bar.visitor_count'],
timezone: 'America/Los_Angeles'
});

console.log(query.buildSqlAndParams());
return dbRunner.testQuery(query.buildSqlAndParams()).then(res => {
res.should.be.deepEqual(
[
{ "bar__visitor_count": "6" }
]
);
});
});
});
});
1 change: 1 addition & 0 deletions packages/cubejs-schema-compiler/test/PrepareCompiler.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const PrepareCompiler = require('../compiler/PrepareCompiler');

exports.prepareCompiler = (content, options) => PrepareCompiler.prepareCompiler({
localPath: () => __dirname,
dataSchemaFiles: () => Promise.resolve([
{ fileName: "main.js", content }
])
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exports.foo = () => 'bar';

0 comments on commit f328d07

Please sign in to comment.