Commit 2378537a 2378537a716a492533a279b8e3bc560ae3deca5a by Nicolas Perriault

fixes 624 - ability to require node modules using their package.json info

1 parent c4049f65
...@@ -185,10 +185,33 @@ CasperError.prototype = Object.getPrototypeOf(new Error()); ...@@ -185,10 +185,33 @@ CasperError.prototype = Object.getPrototypeOf(new Error());
185 if (require.patched) { 185 if (require.patched) {
186 return require; 186 return require;
187 } 187 }
188 function fromPackageJson(module, dir) {
189 var pkgPath, pkgContents, pkg;
190 pkgPath = fs.pathJoin(dir, module, 'package.json');
191 if (!fs.exists(pkgPath)) {
192 return;
193 }
194 pkgContents = fs.read(pkgPath);
195 if (!pkgContents) {
196 return;
197 }
198 try {
199 pkg = JSON.parse(pkgContents);
200 } catch (e) {
201 return;
202 }
203 if (typeof pkg === "object" && pkg.main) {
204 return fs.absolute(fs.pathJoin(dir, module, pkg.main));
205 }
206 }
188 function resolveFile(path, dir) { 207 function resolveFile(path, dir) {
189 var extensions = ['js', 'coffee', 'json']; 208 var extensions = ['js', 'coffee', 'json'];
190 var basenames = [path, path + '/index']; 209 var basenames = [path, path + '/index'];
191 var paths = []; 210 var paths = [];
211 var nodejsScript = fromPackageJson(path, dir);
212 if (nodejsScript) {
213 return nodejsScript;
214 }
192 basenames.forEach(function(basename) { 215 basenames.forEach(function(basename) {
193 paths.push(fs.absolute(fs.pathJoin(dir, basename))); 216 paths.push(fs.absolute(fs.pathJoin(dir, basename)));
194 extensions.forEach(function(extension) { 217 extensions.forEach(function(extension) {
......
...@@ -13,7 +13,8 @@ PHANTOMJS_EXEC = os.environ['PHANTOMJS_EXECUTABLE'] ...@@ -13,7 +13,8 @@ PHANTOMJS_EXEC = os.environ['PHANTOMJS_EXECUTABLE']
13 # make it to an absolute path, because some test change the working directory 13 # make it to an absolute path, because some test change the working directory
14 # and relative path to phantomjs would be invalid 14 # and relative path to phantomjs would be invalid
15 if not os.path.isabs(PHANTOMJS_EXEC): 15 if not os.path.isabs(PHANTOMJS_EXEC):
16 os.environ['PHANTOMJS_EXECUTABLE'] = os.path.join(CASPERJS_ROOT, PHANTOMJS_EXEC) 16 os.environ['PHANTOMJS_EXECUTABLE'] = os.path.join(CASPERJS_ROOT,
17 PHANTOMJS_EXEC)
17 18
18 class TimeoutException(Exception): 19 class TimeoutException(Exception):
19 pass 20 pass
...@@ -53,7 +54,8 @@ class CasperExecTestBase(unittest.TestCase): ...@@ -53,7 +54,8 @@ class CasperExecTestBase(unittest.TestCase):
53 except subprocess.CalledProcessError as err: 54 except subprocess.CalledProcessError as err:
54 if failing: 55 if failing:
55 return err.output.decode('utf-8') 56 return err.output.decode('utf-8')
56 raise IOError('Command %s exited: %s \n %s' % (cmd, err, err.output.decode('utf-8'))) 57 raise IOError('Command %s exited: %s \n %s'
58 % (cmd, err, err.output.decode('utf-8')))
57 59
58 def assertCommandOutputEquals(self, cmd, result, **kwargs): 60 def assertCommandOutputEquals(self, cmd, result, **kwargs):
59 self.assertEqual(self.runCommand(cmd), result) 61 self.assertEqual(self.runCommand(cmd), result)
...@@ -96,7 +98,14 @@ class RequireScriptFullPathTest(CasperExecTestBase): ...@@ -96,7 +98,14 @@ class RequireScriptFullPathTest(CasperExecTestBase):
96 98
97 @timeout(20) 99 @timeout(20)
98 def test_node_module_require_index(self): 100 def test_node_module_require_index(self):
99 script_path = os.path.join(TEST_ROOT, 'modules', 'test_node_mod_index.js') 101 script_path = os.path.join(
102 TEST_ROOT, 'modules', 'test_node_mod_index.js')
103 self.assertCommandOutputEquals(script_path, '42')
104
105 @timeout(20)
106 def test_node_module_require_json_package(self):
107 script_path = os.path.join(
108 TEST_ROOT, 'modules', 'test_node_mod_json_package.js')
100 self.assertCommandOutputEquals(script_path, '42') 109 self.assertCommandOutputEquals(script_path, '42')
101 110
102 @timeout(20) 111 @timeout(20)
...@@ -105,7 +114,6 @@ class RequireScriptFullPathTest(CasperExecTestBase): ...@@ -105,7 +114,6 @@ class RequireScriptFullPathTest(CasperExecTestBase):
105 self.assertCommandOutputEquals(script_path, '42') 114 self.assertCommandOutputEquals(script_path, '42')
106 115
107 116
108
109 class RequireWithOnlyScriptNameTest(CasperExecTestBase): 117 class RequireWithOnlyScriptNameTest(CasperExecTestBase):
110 118
111 def setUp(self): 119 def setUp(self):
...@@ -123,7 +131,8 @@ class RequireWithOnlyScriptNameTest(CasperExecTestBase): ...@@ -123,7 +131,8 @@ class RequireWithOnlyScriptNameTest(CasperExecTestBase):
123 131
124 @timeout(20) 132 @timeout(20)
125 def test_simple_patched_require(self): 133 def test_simple_patched_require(self):
126 self.assertCommandOutputEquals('test_patched_require.js', 'hello, world') 134 self.assertCommandOutputEquals(
135 'test_patched_require.js', 'hello, world')
127 136
128 @timeout(20) 137 @timeout(20)
129 def test_require_coffee(self): 138 def test_require_coffee(self):
...@@ -138,11 +147,15 @@ class RequireWithOnlyScriptNameTest(CasperExecTestBase): ...@@ -138,11 +147,15 @@ class RequireWithOnlyScriptNameTest(CasperExecTestBase):
138 self.assertCommandOutputEquals('test_node_mod_index.js', '42') 147 self.assertCommandOutputEquals('test_node_mod_index.js', '42')
139 148
140 @timeout(20) 149 @timeout(20)
150 def test_node_module_require_json_package(self):
151 self.assertCommandOutputEquals('test_node_mod_json_package.js', '42')
152
153 @timeout(20)
141 def test_node_module_require_json(self): 154 def test_node_module_require_json(self):
142 self.assertCommandOutputEquals('test_node_json.js', '42') 155 self.assertCommandOutputEquals('test_node_json.js', '42')
143 156
144 class RequireWithRelativeScriptPathTest(CasperExecTestBase):
145 157
158 class RequireWithRelativeScriptPathTest(CasperExecTestBase):
146 def setUp(self): 159 def setUp(self):
147 self.currentPath = os.getcwd() 160 self.currentPath = os.getcwd()
148 os.chdir(os.path.join(TEST_ROOT, 'modules')) 161 os.chdir(os.path.join(TEST_ROOT, 'modules'))
...@@ -158,7 +171,8 @@ class RequireWithRelativeScriptPathTest(CasperExecTestBase): ...@@ -158,7 +171,8 @@ class RequireWithRelativeScriptPathTest(CasperExecTestBase):
158 171
159 @timeout(20) 172 @timeout(20)
160 def test_simple_patched_require(self): 173 def test_simple_patched_require(self):
161 self.assertCommandOutputEquals('test_patched_require.js', 'hello, world') 174 self.assertCommandOutputEquals(
175 'test_patched_require.js', 'hello, world')
162 176
163 @timeout(20) 177 @timeout(20)
164 def test_require_coffee(self): 178 def test_require_coffee(self):
...@@ -173,9 +187,14 @@ class RequireWithRelativeScriptPathTest(CasperExecTestBase): ...@@ -173,9 +187,14 @@ class RequireWithRelativeScriptPathTest(CasperExecTestBase):
173 self.assertCommandOutputEquals('./test_node_mod_index.js', '42') 187 self.assertCommandOutputEquals('./test_node_mod_index.js', '42')
174 188
175 @timeout(20) 189 @timeout(20)
190 def test_node_module_require_json_package(self):
191 self.assertCommandOutputEquals('./test_node_mod_json_package.js', '42')
192
193 @timeout(20)
176 def test_node_module_require_json(self): 194 def test_node_module_require_json(self):
177 self.assertCommandOutputEquals('./test_node_json.js', '42') 195 self.assertCommandOutputEquals('./test_node_json.js', '42')
178 196
197
179 class ScriptOutputTest(CasperExecTestBase): 198 class ScriptOutputTest(CasperExecTestBase):
180 @timeout(20) 199 @timeout(20)
181 def test_simple_script(self): 200 def test_simple_script(self):
......