Skip to content

Commit 619c66e

Browse files
Refactored the "paths" helper to clearly distinguish between filesystem paths and URL paths
1 parent 4e992d8 commit 619c66e

File tree

1 file changed

+108
-87
lines changed

1 file changed

+108
-87
lines changed

test/fixtures/path.js

Lines changed: 108 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,111 +1,132 @@
11
(function () {
22
'use strict';
33

4-
var path = global.path = {};
5-
var _path = userAgent.isNode ? require('path') : null;
6-
var _url = userAgent.isNode ? require('url') : null;
7-
var _testsDir = getTestsDir();
8-
4+
var path;
95
if (userAgent.isNode) {
10-
// Run all tests from the "test" directory
11-
process.chdir(_path.join(__dirname, '..'));
6+
path = filesystemPathHelpers();
7+
}
8+
else {
9+
path = urlPathHelpers();
1210
}
1311

12+
global.path = path;
13+
1414
/**
15-
* Returns the relative path of a file in the "test" directory
16-
*
17-
* NOTE: When running in a test-runner (such as Karma) the absolute path is returned instead
15+
* Helper functions for getting local filesystem paths in various formats
1816
*/
19-
path.rel = function (file) {
20-
if (userAgent.isNode) {
21-
// Return the relative path from the project root
22-
return _path.normalize(file);
23-
}
17+
function filesystemPathHelpers () {
18+
var _path = userAgent.isNode ? require('path') : null;
19+
var _url = userAgent.isNode ? require('url') : null;
20+
var _testsDir = _path.resolve(__dirname, '..');
21+
var _isWindows = /^win/.test(process.platform);
2422

25-
// Encode special characters in paths when running in a browser
26-
file = encodeFile(file);
23+
// Run all tests from the "test" directory
24+
process.chdir(_path.join(__dirname, '..'));
2725

28-
if (window.location.href.indexOf(_testsDir) === 0) {
29-
// Return the relative path from "/test/index.html"
30-
return file;
31-
}
26+
return {
27+
/**
28+
* Returns the relative path of a file in the "test" directory
29+
*/
30+
rel: function (file) {
31+
return _path.normalize(file);
32+
},
3233

33-
// We're running in a test-runner (such as Karma), so return an absolute path,
34-
// since we don't know the relative path of the "test" directory.
35-
return _testsDir.replace(/^https?:\/\/[^\/]+(\/.*)/, '$1' + file);
36-
};
34+
/**
35+
* Returns the absolute path of a file in the "test" directory
36+
*/
37+
abs: function (file) {
38+
file = _path.join(_testsDir, file || _path.sep);
39+
return file;
40+
},
3741

38-
/**
39-
* Returns the absolute path of a file in the "test" directory
40-
*/
41-
path.abs = function (file) {
42-
if (userAgent.isNode) {
43-
file = _path.join(_testsDir, file || '/');
44-
}
45-
else {
46-
file = _testsDir + encodeFile(file);
47-
}
48-
if (/^[A-Z]\:[\\\/]/.test(file)) {
49-
// lowercase the drive letter on Windows, for string comparison purposes
50-
file = file[0].toLowerCase() + file.substr(1);
51-
}
52-
return file;
53-
};
42+
/**
43+
* Returns the path of a file in the "test" directory as a URL.
44+
* (e.g. "file://path/to/json-schema-ref-parser/test/files...")
45+
*/
46+
url: function (file) {
47+
var pathname = path.abs(file);
5448

55-
/**
56-
* Returns the path of a file in the "test" directory as a URL.
57-
*/
58-
path.url = function (file) {
59-
if (userAgent.isBrowser) {
60-
// In browsers, just return the absolute URL (e.g. "http://localhost/test/files/...")
61-
return path.abs(file);
62-
}
49+
if (_isWindows) {
50+
pathname = pathname.replace(/\\/g, '/'); // Convert Windows separators to URL separators
51+
}
6352

64-
// In Node, return the absolute path as a URL (e.g. "file://path/to/json-schema-ref-parser/test/files...")
65-
var pathname = path.abs(file);
66-
if (/^win/.test(process.platform)) {
67-
pathname = pathname.replace(/\\/g, '/'); // Convert Windows separators to URL separators
68-
}
69-
var url = _url.format({
70-
protocol: 'file:',
71-
slashes: true,
72-
pathname: pathname
73-
});
53+
var url = _url.format({
54+
protocol: 'file:',
55+
slashes: true,
56+
pathname: pathname
57+
});
7458

75-
return url;
76-
};
59+
return url;
60+
},
7761

78-
/**
79-
* Returns the path of the current working directory.
80-
* In Node, this is the "test" directory. In the browser, it is the directory of the current page.
81-
*/
82-
path.cwd = function () {
83-
if (userAgent.isNode) {
84-
return process.cwd() + '/';
85-
}
86-
else {
87-
return location.href;
88-
}
89-
};
62+
/**
63+
* Returns the absolute path of the current working directory.
64+
*/
65+
cwd: function () {
66+
return _path.join(process.cwd(), _path.sep);
67+
}
68+
};
69+
}
9070

9171
/**
92-
* Returns the path of the "test" directory
72+
* Helper functions for getting URLs in various formats
9373
*/
94-
function getTestsDir () {
95-
if (userAgent.isNode) {
96-
return _path.resolve(__dirname, '..');
97-
}
98-
else {
99-
var filename = document.querySelector('script[src*="fixtures/path.js"]').src;
100-
return filename.substr(0, filename.indexOf('fixtures/path.js'));
74+
function urlPathHelpers () {
75+
// Get the URL of the "test" directory
76+
var filename = document.querySelector('script[src*="fixtures/path.js"]').src;
77+
var _testsDir = filename.substr(0, filename.indexOf('fixtures/path.js'));
78+
79+
/**
80+
* URI-encodes the given file name
81+
*/
82+
function encodePath (file) {
83+
return encodeURIComponent(file).split('%2F').join('/');
10184
}
102-
}
10385

104-
/**
105-
* URI-encodes the given file name
106-
*/
107-
function encodeFile (file) {
108-
return encodeURIComponent(file).split('%2F').join('/');
86+
return {
87+
/**
88+
* Returns the relative path of a file in the "test" directory
89+
*
90+
* NOTE: When running in Karma the absolute path is returned instead
91+
*/
92+
rel: function (file) {
93+
// Encode special characters in paths
94+
file = encodePath(file);
95+
96+
if (window.location.href.indexOf(_testsDir) === 0) {
97+
// We're running from the "/test/index.html" page, directly in a browser.
98+
// So return the relative path from the "test" directory.
99+
return file;
100+
}
101+
else {
102+
// We're running in Karma, so return an absolute path,
103+
// since we don't know the relative path of the "test" directory.
104+
return _testsDir.replace(/^https?:\/\/[^\/]+(\/.*)/, '$1' + file);
105+
}
106+
},
107+
108+
/**
109+
* Returns the absolute path of a file in the "test" directory
110+
*/
111+
abs: function (file) {
112+
return _testsDir + encodePath(file);
113+
},
114+
115+
/**
116+
* Returns the path of a file in the "test" directory as an absolute URL.
117+
* (e.g. "http://localhost/test/files/...")
118+
*/
119+
url: function (file) {
120+
return path.abs(file);
121+
},
122+
123+
/**
124+
* Returns the path of the current page.
125+
*/
126+
cwd: function () {
127+
return location.href;
128+
}
129+
};
109130
}
110131

111132
}());

0 commit comments

Comments
 (0)