Commit a7de99b4 a7de99b4ebcc353feeadf10d14cefb8ef73af497 by Nicolas Perriault

last refactor, it works

1 parent 3ca19aeb
...@@ -12,6 +12,22 @@ This version is yet to be released, and will possibly be tagged as 2.0 as not-so ...@@ -12,6 +12,22 @@ This version is yet to be released, and will possibly be tagged as 2.0 as not-so
12 12
13 **PhantomJS 1.8.1 or later is required for 1.1.** 13 **PhantomJS 1.8.1 or later is required for 1.1.**
14 14
15 #### `require()` in custom modules
16
17 CasperJS 1.1 now internally uses PhantomJS' native `require()` function, but it has side effect if you write your own casperjs modules; in any casperjs module, you now have to use the new global `patchRequire()` function first:
18
19 ```js
20 // casperjs module code
21 var require = patchRequire(require);
22 // now you can require casperjs builtins
23 var utils = require('utils');
24 exports = {
25 // ...
26 };
27 ```
28
29 **Note:** you don't have to use `patchRequire()` in a standard casperjs script.
30
15 #### Testing framework refactoring 31 #### Testing framework refactoring
16 32
17 A new `Tester.begin()` method has been introduced to help organizing tests better: 33 A new `Tester.begin()` method has been introduced to help organizing tests better:
......
...@@ -145,35 +145,56 @@ CasperError.prototype = Object.getPrototypeOf(new Error()); ...@@ -145,35 +145,56 @@ CasperError.prototype = Object.getPrototypeOf(new Error());
145 } 145 }
146 146
147 /** 147 /**
148 * Prints CasperJS help.
149 */
150 function printHelp() {
151 var phantomVersion = [phantom.version.major, phantom.version.minor, phantom.version.patch].join('.');
152 return __terminate([
153 'CasperJS version ' + phantom.casperVersion.toString() +
154 ' at ' + phantom.casperPath + ', using PhantomJS version ' + phantomVersion,
155 fs.read(fs.pathJoin(phantom.casperPath, 'bin', 'usage.txt'))
156 ].join('\n'))
157 }
158
159 /**
148 * Patched require to allow loading of native casperjs modules. 160 * Patched require to allow loading of native casperjs modules.
149 * Every casperjs native module have to first call this function in order to 161 * Every casperjs native module have to first call this function in order to
150 * load a native casperjs module: 162 * load a native casperjs module:
151 * 163 *
152 * var require = patchRequire(require); 164 * var require = patchRequire(require);
153 * var utils = require('utils'); 165 * var utils = require('utils');
154 *
155 */ 166 */
156 function patchRequire(require) { 167 function patchRequire(require) {
157 if (require.patched) { 168 if (require.patched) {
158 return require; 169 return require;
159 } 170 }
160 var patchedRequire = function _require(path) { 171 function casperBuiltinPath(path) {
161 var moduleFilePath = fs.pathJoin(phantom.casperPath, 'modules', path + '.js'); 172 var absPath = fs.pathJoin(phantom.casperPath, 'modules', path + '.js');
162 if (!fs.exists(moduleFilePath)) { 173 return fs.isFile(absPath) ? absPath : undefined;
163 return require(path); // native phantomjs' require() behavior 174 }
175 function localModulePath(path) {
176 var baseDir = phantom.casperScriptBaseDir || fs.workingDirectory;
177 var paths = [
178 fs.absolute(fs.pathJoin(baseDir, path)),
179 fs.absolute(fs.pathJoin(baseDir, path + '.js'))
180 ];
181 return paths.filter(function(path) {
182 return fs.isFile(path);
183 }).pop();
184 }
185 var patchedRequire = function patchedRequire(path) {
186 var moduleFilePath = casperBuiltinPath(path);
187 if (moduleFilePath) {
188 return require(moduleFilePath);
164 } 189 }
165 try { 190 moduleFilePath = localModulePath(path);
191 if (moduleFilePath) {
166 return require(moduleFilePath); 192 return require(moduleFilePath);
193 }
194 try {
195 return require(path);
167 } catch (e) { 196 } catch (e) {
168 var error = new CasperError('__mod_error(' + path + ':' + e.line + '):: ' + e); 197 throw new CasperError("Can't find module " + path);
169 error.file = moduleFilePath;
170 error.line = e.line;
171 error.stack = e.stack;
172 error.stackArray = JSON.parse(JSON.stringify(e.stackArray));
173 if (error.stackArray.length > 0) {
174 error.stackArray[0].sourceURL = moduleFilePath;
175 }
176 throw error;
177 } 198 }
178 }; 199 };
179 patchedRequire.cache = require.cache; 200 patchedRequire.cache = require.cache;
...@@ -182,22 +203,20 @@ CasperError.prototype = Object.getPrototypeOf(new Error()); ...@@ -182,22 +203,20 @@ CasperError.prototype = Object.getPrototypeOf(new Error());
182 patchedRequire.patched = true; 203 patchedRequire.patched = true;
183 return patchedRequire; 204 return patchedRequire;
184 } 205 }
185 global.patchRequire = patchRequire;
186 206
187 /** 207 /**
188 * Initializes the CasperJS Command Line Interface. 208 * Initializes the CasperJS Command Line Interface.
189 *
190 */ 209 */
191 function initCasperCli() { 210 function initCasperCli() {
192 var baseTestsPath = fs.pathJoin(phantom.casperPath, 'tests'); 211 var baseTestsPath = fs.pathJoin(phantom.casperPath, 'tests');
193 212
194 if (!!phantom.casperArgs.options.version) { 213 if (!!phantom.casperArgs.options.version) {
195 console.log(phantom.casperVersion.toString()); 214 return __terminate(phantom.casperVersion.toString())
196 return phantom.exit();
197 } else if (phantom.casperArgs.get(0) === "test") { 215 } else if (phantom.casperArgs.get(0) === "test") {
198 phantom.casperScript = fs.absolute(fs.pathJoin(baseTestsPath, 'run.js')); 216 phantom.casperScript = fs.absolute(fs.pathJoin(baseTestsPath, 'run.js'));
199 phantom.casperTest = true; 217 phantom.casperTest = true;
200 phantom.casperArgs.drop("test"); 218 phantom.casperArgs.drop("test");
219 phantom.casperScriptBaseDir = fs.dirname(phantom.casperArgs.get(0));
201 } else if (phantom.casperArgs.get(0) === "selftest") { 220 } else if (phantom.casperArgs.get(0) === "selftest") {
202 phantom.casperScript = fs.absolute(fs.pathJoin(baseTestsPath, 'run.js')); 221 phantom.casperScript = fs.absolute(fs.pathJoin(baseTestsPath, 'run.js'));
203 phantom.casperSelfTest = phantom.casperTest = true; 222 phantom.casperSelfTest = phantom.casperTest = true;
...@@ -206,16 +225,9 @@ CasperError.prototype = Object.getPrototypeOf(new Error()); ...@@ -206,16 +225,9 @@ CasperError.prototype = Object.getPrototypeOf(new Error());
206 phantom.casperArgs.args.push(fs.pathJoin(baseTestsPath, 'suites')); 225 phantom.casperArgs.args.push(fs.pathJoin(baseTestsPath, 'suites'));
207 } 226 }
208 phantom.casperArgs.drop("selftest"); 227 phantom.casperArgs.drop("selftest");
228 phantom.casperScriptBaseDir = fs.dirname(phantom.casperArgs.get(0) || fs.dirname(phantom.casperScript));
209 } else if (phantom.casperArgs.args.length === 0 || !!phantom.casperArgs.options.help) { 229 } else if (phantom.casperArgs.args.length === 0 || !!phantom.casperArgs.options.help) {
210 var phantomVersion = [phantom.version.major, phantom.version.minor, phantom.version.patch].join('.'); 230 return printHelp();
211 var f = require("utils").format;
212 return __terminate([
213 f('CasperJS version %s at %s, using PhantomJS version %s',
214 phantom.casperVersion.toString(),
215 phantom.casperPath,
216 phantomVersion)
217 , fs.read(fs.pathJoin(phantom.casperPath, 'bin', 'usage.txt'))
218 ].join('\n'));
219 } 231 }
220 232
221 if (!phantom.casperScript) { 233 if (!phantom.casperScript) {
...@@ -226,15 +238,16 @@ CasperError.prototype = Object.getPrototypeOf(new Error()); ...@@ -226,15 +238,16 @@ CasperError.prototype = Object.getPrototypeOf(new Error());
226 return __die('Unable to open file: ' + phantom.casperScript); 238 return __die('Unable to open file: ' + phantom.casperScript);
227 } 239 }
228 240
241 if (!phantom.casperScriptBaseDir) {
242 phantom.casperScriptBaseDir = fs.absolute(fs.dirname(phantom.casperScript));
243 }
244
229 // filter out the called script name from casper args 245 // filter out the called script name from casper args
230 phantom.casperArgs.drop(phantom.casperScript); 246 phantom.casperArgs.drop(phantom.casperScript);
231 } 247 }
232 248
233 // Embedded, up-to-date, validatable & controlable CoffeeScript
234 phantom.injectJs(fs.pathJoin(phantom.casperPath, 'modules', 'vendors', 'coffee-script.js'));
235
236 // CasperJS version, extracted from package.json - see http://semver.org/ 249 // CasperJS version, extracted from package.json - see http://semver.org/
237 phantom.casperVersion = (function getVersion(path) { 250 phantom.casperVersion = (function getCasperVersion(path) {
238 var parts, patchPart, pkg, pkgFile; 251 var parts, patchPart, pkg, pkgFile;
239 pkgFile = fs.absolute(fs.pathJoin(path, 'package.json')); 252 pkgFile = fs.absolute(fs.pathJoin(path, 'package.json'));
240 if (!fs.exists(pkgFile)) { 253 if (!fs.exists(pkgFile)) {
...@@ -265,7 +278,9 @@ CasperError.prototype = Object.getPrototypeOf(new Error()); ...@@ -265,7 +278,9 @@ CasperError.prototype = Object.getPrototypeOf(new Error());
265 }; 278 };
266 })(phantom.casperPath); 279 })(phantom.casperPath);
267 280
268 // patch require (must be called in every casperjs module as of 1.1) 281 // patch require
282 global.__require = require;
283 global.patchRequire = patchRequire; // must be called in every casperjs module as of 1.1
269 global.require = patchRequire(global.require); 284 global.require = patchRequire(global.require);
270 285
271 // casper cli args 286 // casper cli args
......
This diff could not be displayed because it is too large.