Skip to content
This repository was archived by the owner on Dec 10, 2019. It is now read-only.

Commit 5217108

Browse files
committed
isPatternFile consolidation and tests
1 parent 9d0eff3 commit 5217108

File tree

2 files changed

+46
-20
lines changed

2 files changed

+46
-20
lines changed

builder/pattern_assembler.js

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
/*
2-
* patternlab-node - v0.14.0 - 2015
3-
*
1+
/*
2+
* patternlab-node - v0.14.0 - 2015
3+
*
44
* Brian Muenzenmeyer, and the web community.
55
* Licensed under the MIT license.
66
*
@@ -92,33 +92,31 @@
9292
}
9393
}
9494

95-
function isPatternFile(filename, patternlab) {
95+
// ignore _underscored patterns, dotfiles, and anything not recognized by
96+
// a loaded pattern engine
97+
function isPatternFile(filename) {
98+
// skip hidden patterns/files without a second thought
99+
if (filename.charAt(0) === '_' || filename.charAt(0) === '.') {
100+
return false;
101+
}
102+
103+
// not a hidden pattern, let's dig deeper
96104
var engineNames = Object.keys(patternEngines);
97105
var supportedPatternFileExtensions = engineNames.map(function (engineName) {
98106
return patternEngines[engineName].fileExtension;
99107
});
100108
var extension = path.extname(filename);
101-
return (supportedPatternFileExtensions.lastIndexOf(extension) != -1);
109+
return (supportedPatternFileExtensions.lastIndexOf(extension) !== -1);
102110
}
103111

104112
function processPatternIterative(file, patternlab){
105-
var fs = require('fs-extra'),
106-
of = require('./object_factory'),
107-
path = require('path');
108-
109113
//extract some information
110114
var subdir = path.dirname(path.relative(patternlab.config.patterns.source, file)).replace('\\', '/');
111115
var filename = path.basename(file);
112116
var ext = path.extname(filename);
113117

114-
// ignore _underscored patterns, dotfiles, and anything not recognized by
115-
// a loaded pattern engine
116-
if (filename.charAt(0) === '_' ||
117-
filename.charAt(0) === '.' ||
118-
(ext === '.json' && filename.indexOf('~') === -1) ||
119-
!isPatternFile(filename, patternlab)) {
120-
return;
121-
}
118+
// skip non-pattern files
119+
if (!isPatternFile(filename, patternlab)) { return; }
122120
console.log('found pattern', file);
123121

124122
//make a new Pattern Object
@@ -183,8 +181,7 @@
183181
ph = require('./parameter_hunter'),
184182
pph = require('./pseudopattern_hunter'),
185183
lih = require('./list_item_hunter'),
186-
smh = require('./style_modifier_hunter'),
187-
path = require('path');
184+
smh = require('./style_modifier_hunter');
188185

189186
var parameter_hunter = new ph(),
190187
lineage_hunter = new lh(),
@@ -385,7 +382,8 @@
385382
},
386383
is_object_empty: function(obj){
387384
return isObjectEmpty(obj);
388-
}
385+
},
386+
is_pattern_file: isPatternFile
389387
};
390388

391389
};

test/pattern_assembler_tests.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,34 @@
283283
//test that 00-foo.mustache included partial 01-bar.mustache
284284
test.equals(fooExtended, 'bar');
285285

286+
test.done();
287+
},
288+
289+
'isPatternFile correctly identifies pattern files and rejects non-pattern files': function(test){
290+
var pattern_assembler = new pa();
291+
292+
// each test case
293+
var filenames = {
294+
'00-comment-thread.mustache': true,
295+
'00-comment-thread.fakeextthatdoesntexist': false,
296+
'00-comment-thread': false,
297+
'_00-comment-thread.mustache': false,
298+
'.00-comment-thread.mustache': false,
299+
'00-comment-thread.json': false,
300+
'00-homepage~emergency.json': false
301+
};
302+
// expect one test per test case
303+
test.expect(Object.keys(filenames).length);
304+
305+
// loop over each test case and test it
306+
Object.keys(filenames).forEach(function (filename) {
307+
var expectedResult = filenames[filename],
308+
actualResult = pattern_assembler.is_pattern_file(filename),
309+
testMessage = 'isPatternFile should return ' + expectedResult + ' for ' + filename;
310+
test.strictEqual(actualResult, expectedResult, testMessage);
311+
});
312+
313+
// done
286314
test.done();
287315
}
288316
};

0 commit comments

Comments
 (0)