Skip to content

Commit

Permalink
Merge pull request #193 from tswaters/fix-path-import
Browse files Browse the repository at this point in the history
Fixes path handling for imports under windows
  • Loading branch information
DevSide committed Oct 6, 2016
2 parents c8f65c0 + b9bd1f8 commit 8feecc4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
5 changes: 4 additions & 1 deletion lib/parser/nools/util.js
Expand Up @@ -59,13 +59,16 @@ exports.getParamList = function (str) {
};

exports.resolve = function (from, to) {
if (process.platform === 'win32') {
to = to.replace(/\//g, '\\');
}
if (path.extname(from) !== '') {
from = path.dirname(from);
}
if (to.split(pathSep).length === 1) {
return to;
}
return path.resolve(from, to);
return path.resolve(from, to).replace(/\\/g, '/');

};

Expand Down
16 changes: 8 additions & 8 deletions test/noolsParser.test.js
Expand Up @@ -83,7 +83,7 @@ it.describe("nools dsl parser", function (it) {
it.should("resolve relative require paths", function () {
var parsed = noolsParser.parse("global util = require('../util');", "./rules/test.nools");
assert.equal(parsed.scope[0].name, 'util');
assert.equal(parsed.scope[0].body, "require('" + path.resolve("./rules", "../util") + "')");
assert.equal(parsed.scope[0].body, "require('" + path.resolve("./rules", "../util").replace(/\\/g, '/') + "')");
});

it.should("parse a member look up", function () {
Expand Down Expand Up @@ -131,7 +131,7 @@ it.describe("nools dsl parser", function (it) {
it.should("parse a relative path and default to process.cwd()", function () {
var called = false;
fs.readFileSync = function (file, encoding) {
assert.equal(file, path.resolve(process.cwd(), './test.nools'));
assert.equal(file, path.resolve(process.cwd(), './test.nools').replace(/\\/g, '/'));
assert.equal(encoding, "utf8");
called = true;
return "";
Expand All @@ -143,7 +143,7 @@ it.describe("nools dsl parser", function (it) {
it.should("parse a relative path and use the file path", function () {
var called = false;
fs.readFileSync = function (file, encoding) {
assert.equal(file, path.resolve("./rules", './test.nools'));
assert.equal(file, path.resolve("./rules", './test.nools').replace(/\\/g, '/'));
assert.equal(encoding, "utf8");
called = true;
return "";
Expand All @@ -155,7 +155,7 @@ it.describe("nools dsl parser", function (it) {
it.should("parse a absolute path and not change the location ", function () {
var called = false;
fs.readFileSync = function (file, encoding) {
assert.equal(file, "/rules/test.nools");
assert.equal(file, path.resolve("/rules/test.nools").replace(/\\/g, '/'));
assert.equal(encoding, "utf8");
called = true;
return "";
Expand All @@ -167,7 +167,7 @@ it.describe("nools dsl parser", function (it) {
it.should("should parse import with optional ';'", function () {
var called = false;
fs.readFileSync = function (file, encoding) {
assert.equal(file, "/rules/test.nools");
assert.equal(file, path.resolve("/rules/test.nools").replace(/\\/g, '/'));
assert.equal(encoding, "utf8");
called = true;
return "";
Expand Down Expand Up @@ -208,9 +208,9 @@ it.describe("nools dsl parser", function (it) {
],
"scope": [],
"loaded": [
require.resolve("./rules/import/import1.nools"),
require.resolve("./rules/import/import2.nools"),
require.resolve("./rules/import/import3.nools")
require.resolve("./rules/import/import1.nools").replace(/\\/g, '/'),
require.resolve("./rules/import/import2.nools").replace(/\\/g, '/'),
require.resolve("./rules/import/import3.nools").replace(/\\/g, '/')
],
"file": source
});
Expand Down

0 comments on commit 8feecc4

Please sign in to comment.