Skip to content

Commit 7ecec18

Browse files
committed
Find relative root of all test files for displaing directories
Fixes #249.
1 parent 8346d35 commit 7ecec18

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

api.js

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ function Api(files, options) {
3131
this.stats = [];
3232
this.tests = [];
3333
this.files = files || [];
34+
this.base = '';
3435

3536
Object.keys(Api.prototype).forEach(function (key) {
3637
this[key] = this[key].bind(this);
@@ -99,24 +100,34 @@ Api.prototype._handleTest = function (test) {
99100
this.emit('test', test);
100101
};
101102

103+
Api.prototype._findBase = function (files) {
104+
this.base = files.reduce(function (base, file) {
105+
file = path.relative('.', file);
106+
file = file.split(path.sep);
107+
if (base === false) return file;
108+
return base.filter(function (part, i) {
109+
return file[i].toLowerCase() === part.toLowerCase();
110+
});
111+
}, false).join(path.sep);
112+
113+
if (this.base === '' || this.base === '.') {
114+
this.base = this.files[0] || 'test';
115+
}
116+
117+
this.base += path.sep;
118+
};
119+
102120
Api.prototype._prefixTitle = function (file) {
103121
if (this.fileCount === 1) {
104122
return '';
105123
}
106124

107125
var separator = ' ' + chalk.gray.dim(figures.pointerSmall) + ' ';
108126

109-
var base = path.dirname(this.files[0]);
110-
111-
if (base === '.') {
112-
base = this.files[0] || 'test';
113-
}
114-
115-
base += path.sep;
116-
117127
var prefix = path.relative('.', file)
118-
.replace(base, '')
128+
.replace(this.base, '')
119129
.replace(/\.spec/, '')
130+
.replace(/\.test/, '')
120131
.replace(/test\-/g, '')
121132
.replace(/\.js$/, '')
122133
.split(path.sep)
@@ -143,6 +154,8 @@ Api.prototype.run = function () {
143154

144155
self.fileCount = files.length;
145156

157+
self._findBase(files);
158+
146159
var tests = files.map(self._runFile);
147160

148161
// receive test count from all files and then run the tests

0 commit comments

Comments
 (0)