Skip to content

Commit 608ede0

Browse files
Fixed some bugs on Windows
1 parent 9cdc9ef commit 608ede0

9 files changed

Lines changed: 33 additions & 17 deletions

File tree

dist/ref-parser.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1664,13 +1664,15 @@ exports.doCallback = function doCallback(callback, err, params) {
16641664
(function (process){
16651665
'use strict';
16661666

1667-
var protocolPattern = /^[a-z0-9.+-]+:\/\//i;
1667+
var isWindows = /^win/.test(process.platform),
1668+
forwardSlashPattern = /\//g,
1669+
protocolPattern = /^[a-z0-9.+-]+:\/\//i;
16681670

16691671
// RegExp patterns to URL-encode special characters in local filesystem paths
16701672
var urlEncodePatterns = [
16711673
/\?/g, '%3F',
16721674
/\#/g, '%23',
1673-
/^win/.test(process.platform) ? /\\/g : /\//, '/'
1675+
isWindows ? /\\/g : /\//, '/'
16741676
];
16751677

16761678
// RegExp patterns to URL-decode special characters for local filesystem paths
@@ -1730,6 +1732,9 @@ exports.urlToLocalPath = function urlToLocalPath(url) {
17301732
for (var i = 0; i < urlDecodePatterns.length; i += 2) {
17311733
url = url.replace(urlDecodePatterns[i], urlDecodePatterns[i + 1]);
17321734
}
1735+
if (isWindows) {
1736+
url = url.replace(forwardSlashPattern, '\\');
1737+
}
17331738
return url;
17341739
};
17351740

dist/ref-parser.js.map

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/ref-parser.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/ref-parser.min.js.map

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/util/path.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
'use strict';
22

3-
var protocolPattern = /^[a-z0-9.+-]+:\/\//i;
3+
var isWindows = /^win/.test(process.platform),
4+
forwardSlashPattern = /\//g,
5+
protocolPattern = /^[a-z0-9.+-]+:\/\//i;
46

57
// RegExp patterns to URL-encode special characters in local filesystem paths
68
var urlEncodePatterns = [
79
/\?/g, '%3F',
810
/\#/g, '%23',
9-
/^win/.test(process.platform) ? /\\/g : /\//, '/'
11+
isWindows ? /\\/g : /\//, '/'
1012
];
1113

1214
// RegExp patterns to URL-decode special characters for local filesystem paths
@@ -66,6 +68,9 @@ exports.urlToLocalPath = function urlToLocalPath(url) {
6668
for (var i = 0; i < urlDecodePatterns.length; i += 2) {
6769
url = url.replace(urlDecodePatterns[i], urlDecodePatterns[i + 1]);
6870
}
71+
if (isWindows) {
72+
url = url.replace(forwardSlashPattern, '\\');
73+
}
6974
return url;
7075
};
7176

tests/fixtures/helper.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
var values = $refs.values();
7474
expect(values).to.have.keys(expectedFiles);
7575
expectedFiles.forEach(function(file, i) {
76-
var actual = convertNodeBuffersToPOJOs(values[file]);
76+
var actual = helper.convertNodeBuffersToPOJOs(values[file]);
7777
var expected = expectedValues[i];
7878
expect(actual).to.deep.equal(expected, file);
7979
});
@@ -87,7 +87,7 @@
8787
/**
8888
* Converts Buffer objects to POJOs, so they can be compared using Chai
8989
*/
90-
function convertNodeBuffersToPOJOs(value) {
90+
helper.convertNodeBuffersToPOJOs = function convertNodeBuffersToPOJOs(value) {
9191
if (value && value.constructor && value.constructor.name === 'Buffer') {
9292
// Convert Buffers to POJOs for comparison
9393
value = value.toJSON();

tests/fixtures/path.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,16 @@
3434
*/
3535
path.abs = function(file) {
3636
if (userAgent.isNode) {
37-
return _path.join(_testsDir, file || '/');
37+
file = _path.join(_testsDir, file || '/');
3838
}
3939
else {
40-
return _testsDir + encodeFile(file);
40+
file = _testsDir + encodeFile(file);
4141
}
42+
if (/^[A-Z]\:[\\\/]/.test(file)) {
43+
// lowercase the drive letter on Windows, for string comparison purposes
44+
file = file[0].toLowerCase() + file.substr(1);
45+
}
46+
return file;
4247
};
4348

4449
/**

tests/specs/unknown/unknown.dereferenced.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/specs/unknown/unknown.spec.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ describe('Schema with $refs to unknown file types', function() {
4949
.then(function(schema) {
5050
expect(schema).to.equal(parser.schema);
5151

52-
// Serialize the schema, to convert the Buffers into POJOs
53-
schema = JSON.parse(JSON.stringify(schema));
52+
schema.definitions.html = helper.convertNodeBuffersToPOJOs(schema.definitions.html);
53+
schema.definitions.binary = helper.convertNodeBuffersToPOJOs(schema.definitions.binary);
5454
expect(schema).to.deep.equal(helper.dereferenced.unknown);
5555

5656
done();
@@ -66,8 +66,8 @@ describe('Schema with $refs to unknown file types', function() {
6666
.then(function(schema) {
6767
expect(schema).to.equal(parser.schema);
6868

69-
// Serialize the schema, to convert the Buffers into POJOs
70-
schema = JSON.parse(JSON.stringify(schema));
69+
schema.definitions.html = helper.convertNodeBuffersToPOJOs(schema.definitions.html);
70+
schema.definitions.binary = helper.convertNodeBuffersToPOJOs(schema.definitions.binary);
7171
expect(schema).to.deep.equal(helper.dereferenced.unknown);
7272

7373
done();

0 commit comments

Comments
 (0)