Skip to content

Commit eece385

Browse files
chainhelendougwilson
authored andcommitted
tests: use path.join instead of concatenation
closes #3236
1 parent 8eb95ae commit eece385

File tree

4 files changed

+64
-43
lines changed

4 files changed

+64
-43
lines changed

test/app.engine.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
var express = require('../')
33
, fs = require('fs');
4+
var path = require('path')
45

56
function render(path, options, fn) {
67
fs.readFile(path, 'utf8', function(err, str){
@@ -15,7 +16,7 @@ describe('app', function(){
1516
it('should map a template engine', function(done){
1617
var app = express();
1718

18-
app.set('views', __dirname + '/fixtures');
19+
app.set('views', path.join(__dirname, 'fixtures'))
1920
app.engine('.html', render);
2021
app.locals.user = { name: 'tobi' };
2122

@@ -36,7 +37,7 @@ describe('app', function(){
3637
it('should work without leading "."', function(done){
3738
var app = express();
3839

39-
app.set('views', __dirname + '/fixtures');
40+
app.set('views', path.join(__dirname, 'fixtures'))
4041
app.engine('html', render);
4142
app.locals.user = { name: 'tobi' };
4243

@@ -50,7 +51,7 @@ describe('app', function(){
5051
it('should work "view engine" setting', function(done){
5152
var app = express();
5253

53-
app.set('views', __dirname + '/fixtures');
54+
app.set('views', path.join(__dirname, 'fixtures'))
5455
app.engine('html', render);
5556
app.set('view engine', 'html');
5657
app.locals.user = { name: 'tobi' };
@@ -65,7 +66,7 @@ describe('app', function(){
6566
it('should work "view engine" with leading "."', function(done){
6667
var app = express();
6768

68-
app.set('views', __dirname + '/fixtures');
69+
app.set('views', path.join(__dirname, 'fixtures'))
6970
app.engine('.html', render);
7071
app.set('view engine', '.html');
7172
app.locals.user = { name: 'tobi' };

test/app.render.js

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11

2+
var assert = require('assert')
23
var express = require('..');
4+
var path = require('path')
35
var tmpl = require('./support/tmpl');
46

57
describe('app', function(){
@@ -9,7 +11,7 @@ describe('app', function(){
911

1012
app.locals.user = { name: 'tobi' };
1113

12-
app.render(__dirname + '/fixtures/user.tmpl', function (err, str) {
14+
app.render(path.join(__dirname, 'fixtures', 'user.tmpl'), function (err, str) {
1315
if (err) return done(err);
1416
str.should.equal('<p>tobi</p>');
1517
done();
@@ -22,7 +24,7 @@ describe('app', function(){
2224
app.set('view engine', 'tmpl');
2325
app.locals.user = { name: 'tobi' };
2426

25-
app.render(__dirname + '/fixtures/user', function(err, str){
27+
app.render(path.join(__dirname, 'fixtures', 'user'), function (err, str) {
2628
if (err) return done(err);
2729
str.should.equal('<p>tobi</p>');
2830
done();
@@ -32,7 +34,7 @@ describe('app', function(){
3234
it('should expose app.locals', function(done){
3335
var app = createApp();
3436

35-
app.set('views', __dirname + '/fixtures');
37+
app.set('views', path.join(__dirname, 'fixtures'))
3638
app.locals.user = { name: 'tobi' };
3739

3840
app.render('user.tmpl', function (err, str) {
@@ -45,7 +47,7 @@ describe('app', function(){
4547
it('should support index.<engine>', function(done){
4648
var app = createApp();
4749

48-
app.set('views', __dirname + '/fixtures');
50+
app.set('views', path.join(__dirname, 'fixtures'))
4951
app.set('view engine', 'tmpl');
5052

5153
app.render('blog/post', function (err, str) {
@@ -80,9 +82,10 @@ describe('app', function(){
8082
it('should provide a helpful error', function(done){
8183
var app = createApp();
8284

83-
app.set('views', __dirname + '/fixtures');
85+
app.set('views', path.join(__dirname, 'fixtures'))
8486
app.render('rawr.tmpl', function (err) {
85-
err.message.should.equal('Failed to lookup view "rawr.tmpl" in views directory "' + __dirname + '/fixtures"');
87+
assert.ok(err)
88+
assert.equal(err.message, 'Failed to lookup view "rawr.tmpl" in views directory "' + path.join(__dirname, 'fixtures') + '"')
8689
done();
8790
});
8891
})
@@ -92,7 +95,7 @@ describe('app', function(){
9295
it('should invoke the callback', function(done){
9396
var app = createApp();
9497

95-
app.set('views', __dirname + '/fixtures');
98+
app.set('views', path.join(__dirname, 'fixtures'))
9699

97100
app.render('user.tmpl', function (err, str) {
98101
// nextTick to prevent cyclic
@@ -108,7 +111,7 @@ describe('app', function(){
108111
it('should render the template', function(done){
109112
var app = createApp();
110113

111-
app.set('views', __dirname + '/fixtures');
114+
app.set('views', path.join(__dirname, 'fixtures'))
112115

113116
app.render('email.tmpl', function (err, str) {
114117
if (err) return done(err);
@@ -123,7 +126,7 @@ describe('app', function(){
123126
var app = createApp();
124127

125128
app.set('view engine', 'tmpl');
126-
app.set('views', __dirname + '/fixtures');
129+
app.set('views', path.join(__dirname, 'fixtures'))
127130

128131
app.render('email', function(err, str){
129132
if (err) return done(err);
@@ -137,7 +140,7 @@ describe('app', function(){
137140
it('should lookup the file in the path', function(done){
138141
var app = createApp();
139142

140-
app.set('views', __dirname + '/fixtures/default_layout');
143+
app.set('views', path.join(__dirname, 'fixtures', 'default_layout'))
141144
app.locals.user = { name: 'tobi' };
142145

143146
app.render('user.tmpl', function (err, str) {
@@ -150,7 +153,10 @@ describe('app', function(){
150153
describe('when array of paths', function(){
151154
it('should lookup the file in the path', function(done){
152155
var app = createApp();
153-
var views = [__dirname + '/fixtures/local_layout', __dirname + '/fixtures/default_layout'];
156+
var views = [
157+
path.join(__dirname, 'fixtures', 'local_layout'),
158+
path.join(__dirname, 'fixtures', 'default_layout')
159+
]
154160

155161
app.set('views', views);
156162
app.locals.user = { name: 'tobi' };
@@ -164,7 +170,10 @@ describe('app', function(){
164170

165171
it('should lookup in later paths until found', function(done){
166172
var app = createApp();
167-
var views = [__dirname + '/fixtures/local_layout', __dirname + '/fixtures/default_layout'];
173+
var views = [
174+
path.join(__dirname, 'fixtures', 'local_layout'),
175+
path.join(__dirname, 'fixtures', 'default_layout')
176+
]
168177

169178
app.set('views', views);
170179
app.locals.name = 'tobi';
@@ -178,13 +187,17 @@ describe('app', function(){
178187

179188
it('should error if file does not exist', function(done){
180189
var app = createApp();
181-
var views = [__dirname + '/fixtures/local_layout', __dirname + '/fixtures/default_layout'];
190+
var views = [
191+
path.join(__dirname, 'fixtures', 'local_layout'),
192+
path.join(__dirname, 'fixtures', 'default_layout')
193+
]
182194

183195
app.set('views', views);
184196
app.locals.name = 'tobi';
185197

186198
app.render('pet.tmpl', function (err, str) {
187-
err.message.should.equal('Failed to lookup view "pet.tmpl" in views directories "' + __dirname + '/fixtures/local_layout" or "' + __dirname + '/fixtures/default_layout"');
199+
assert.ok(err)
200+
assert.equal(err.message, 'Failed to lookup view "pet.tmpl" in views directories "' + views[0] + '" or "' + views[1] + '"')
188201
done();
189202
})
190203
})
@@ -281,7 +294,7 @@ describe('app', function(){
281294
it('should render the template', function(done){
282295
var app = createApp();
283296

284-
app.set('views', __dirname + '/fixtures');
297+
app.set('views', path.join(__dirname, 'fixtures'))
285298

286299
var user = { name: 'tobi' };
287300

@@ -295,7 +308,7 @@ describe('app', function(){
295308
it('should expose app.locals', function(done){
296309
var app = createApp();
297310

298-
app.set('views', __dirname + '/fixtures');
311+
app.set('views', path.join(__dirname, 'fixtures'))
299312
app.locals.user = { name: 'tobi' };
300313

301314
app.render('user.tmpl', {}, function (err, str) {
@@ -308,7 +321,7 @@ describe('app', function(){
308321
it('should give precedence to app.render() locals', function(done){
309322
var app = createApp();
310323

311-
app.set('views', __dirname + '/fixtures');
324+
app.set('views', path.join(__dirname, 'fixtures'))
312325
app.locals.user = { name: 'tobi' };
313326
var jane = { name: 'jane' };
314327

0 commit comments

Comments
 (0)