Commit 2326b5f4 by xzyfer

Fix watching of entry points

Currently changes to non-partials are being picked up. This PR Fixes
that and adds addition test coverage.

Fixes #2139
parent 6c5f1101
...@@ -250,8 +250,6 @@ function watch(options, emitter) { ...@@ -250,8 +250,6 @@ function watch(options, emitter) {
}); });
}; };
watcher.reset(options);
var gaze = new Gaze(); var gaze = new Gaze();
gaze.add(watcher.reset(options)); gaze.add(watcher.reset(options));
gaze.on('error', emitter.emit.bind(emitter, 'error')); gaze.on('error', emitter.emit.bind(emitter, 'error'));
......
var grapher = require('sass-graph'), var grapher = require('sass-graph'),
clonedeep = require('lodash.clonedeep'), clonedeep = require('lodash.clonedeep'),
path = require('path'),
config = {}, config = {},
watcher = {}, watcher = {},
graph = null; graph = null;
...@@ -30,8 +31,14 @@ watcher.changed = function(absolutePath) { ...@@ -30,8 +31,14 @@ watcher.changed = function(absolutePath) {
this.reset(); this.reset();
if (absolutePath && path.basename(absolutePath)[0] !== '_') {
files.changed.push(absolutePath);
}
graph.visitAncestors(absolutePath, function(parent) { graph.visitAncestors(absolutePath, function(parent) {
files.changed.push(parent); if (path.basename(parent)[0] !== '_') {
files.changed.push(parent);
}
}); });
graph.visitDescendents(absolutePath, function(child) { graph.visitDescendents(absolutePath, function(child) {
...@@ -50,7 +57,7 @@ watcher.added = function(absolutePath) { ...@@ -50,7 +57,7 @@ watcher.added = function(absolutePath) {
this.reset(); this.reset();
if (Object.keys(graph.index).indexOf(absolutePath) !== -1) { if (Object.keys(graph.index).indexOf(absolutePath) === -1) {
files.added.push(absolutePath); files.added.push(absolutePath);
} }
...@@ -69,7 +76,9 @@ watcher.removed = function(absolutePath) { ...@@ -69,7 +76,9 @@ watcher.removed = function(absolutePath) {
}; };
graph.visitAncestors(absolutePath, function(parent) { graph.visitAncestors(absolutePath, function(parent) {
files.changed.push(parent); if (path.basename(parent)[0] !== '_') {
files.changed.push(parent);
}
}); });
if (Object.keys(graph.index).indexOf(absolutePath) !== -1) { if (Object.keys(graph.index).indexOf(absolutePath) !== -1) {
......
@import "partials/three";
.one { .one {
color: darkred; color: darkred;
} }
@import "partials/three";
.two { .two {
color: darkblue; color: darkblue;
} }
@import "partials/two";
.two { .two {
color: blue; color: blue;
} }
...@@ -24,99 +24,214 @@ describe('watcher', function() { ...@@ -24,99 +24,214 @@ describe('watcher', function() {
}); });
}); });
describe('when a file is changed in the graph', function() { describe('when a file is changed', function() {
it('should return the files to compile', function() { describe('and it is in the graph', function() {
var files = watcher.changed(path.join(main, 'partials', '_one.scss')); describe('if it is a partial', function() {
assert.deepEqual(files, { it('should record its ancestors as changed', function() {
added: [], var file = path.join(main, 'partials', '_one.scss');
changed: [path.join(main, 'one.scss')], var files = watcher.changed(file);
removed: [], assert.deepEqual(files.changed, [
path.join(main, 'one.scss'),
]);
});
it('should record its decendants as added', function() {
var file = path.join(main, 'partials', '_one.scss');
var files = watcher.changed(file);
assert.deepEqual(files.added, [
path.join(main, 'partials', '_three.scss'),
]);
});
it('should record nothing as removed', function() {
var file = path.join(main, 'partials', '_one.scss');
var files = watcher.changed(file);
assert.deepEqual(files.removed, []);
});
}); });
});
describe('and @imports previously not imported files', function() { describe('if it is not a partial', function() {
it('should also return the new imported files', function() { it('should record itself as changed', function() {
var file = path.join(main, 'partials', '_one.scss'); var file = path.join(main, 'one.scss');
fs.writeFileSync(file, '@import "partials/three.scss";', { flag: 'a' }); var files = watcher.changed(file);
var files = watcher.changed(file); assert.deepEqual(files.changed, [
assert.deepEqual(files, { file,
added: [path.join(main, 'partials', '_three.scss')], ]);
changed: [path.join(main, 'one.scss')], });
removed: [],
it('should record its decendants as added', function() {
var file = path.join(main, 'one.scss');
var files = watcher.changed(file);
assert.deepEqual(files.added, [
path.join(main, 'partials', '_one.scss'),
path.join(main, 'partials', '_three.scss'),
]);
});
it('should record nothing as removed', function() {
var file = path.join(main, 'one.scss');
var files = watcher.changed(file);
assert.deepEqual(files.removed, []);
}); });
}); });
}); });
});
describe('when a file is changed outside the graph', function() { describe('and is not in the graph', function() {
it('should return an empty array', function() { describe('if it is a partial', function() {
var files = watcher.changed(path.join(sibling, 'partials', '_three.scss')); it('should not record anything', function() {
assert.deepEqual(files, { var file = path.join(sibling, 'partials', '_three.scss');
added: [], var files = watcher.changed(file);
changed: [], assert.deepEqual(files, {
removed: [], added: [],
changed: [],
removed: [],
});
});
}); });
});
});
describe('when a file is added in the graph', function() { describe('if it is not a partial', function() {
it('should return only the added file', function() { it('should record itself as changed', function() {
var file = path.join(main, 'three.scss'); var file = path.join(sibling, 'three.scss');
fs.writeFileSync(file, '@import "paritals/two.scss";'); var files = watcher.changed(file);
var files = watcher.added(file); assert.deepEqual(files, {
assert.deepEqual(files, { added: [],
added: [file], changed: [file],
changed: [], removed: [],
removed: [], });
});
}); });
}); });
});
describe('and @imports previously not imported files', function() { describe('when a file is added', function() {
it('should also return the new imported files', function() { describe('and it is in the graph', function() {
var file = path.join(main, 'three.scss'); describe('if it is a partial', function() {
fs.writeFileSync(file, '@import "partials/three.scss";'); it('should record nothing as added', function() {
var files = watcher.added(file); var file = path.join(main, 'partials', '_three.scss');
assert.deepEqual(files, { var files = watcher.added(file);
added: [ assert.deepEqual(files.added, []);
file, });
it('should record its decendants as added', function() {
var file = path.join(main, 'partials', '_one.scss');
var files = watcher.added(file);
assert.deepEqual(files.added, [
path.join(main, 'partials', '_three.scss') path.join(main, 'partials', '_three.scss')
], ]);
changed: [], });
removed: [],
it('should record nothing as changed', function() {
var file = path.join(main, 'partials', '_three.scss');
var files = watcher.added(file);
assert.deepEqual(files.changed, []);
});
it('should record nothing as removed', function() {
var file = path.join(main, 'partials', '_three.scss');
var files = watcher.added(file);
assert.deepEqual(files.removed, []);
}); });
}); });
});
});
describe('when a file is added outside the graph', function() { describe('if it is not a partial', function() {
it('should return an empty array', function() { it('should record nothing as added', function() {
var files = watcher.added(path.join(sibling, 'three.scss')); var file = path.join(main, 'three.scss');
assert.deepEqual(files, { var files = watcher.added(file);
added: [], assert.deepEqual(files.added, []);
changed: [], });
removed: [],
it('should record its decendants as added', function() {
var file = path.join(main, 'one.scss');
var files = watcher.added(file);
assert.deepEqual(files.added, [
path.join(main, 'partials', '_one.scss'),
path.join(main, 'partials', '_three.scss'),
]);
});
it('should record nothing as changed', function() {
var file = path.join(main, 'one.scss');
var files = watcher.added(file);
assert.deepEqual(files.changed, []);
});
it('should record nothing as removed', function() {
var file = path.join(main, 'one.scss');
var files = watcher.added(file);
assert.deepEqual(files.removed, []);
});
}); });
}); });
}); });
describe('when a file is removed from the graph', function() { describe('when a file is removed', function() {
it('should return the files to compile', function() { describe('and it is in the graph', function() {
var files = watcher.removed(path.join(main, 'partials', '_one.scss')); describe('if it is a partial', function() {
assert.deepEqual(files, { it('should record nothing as added', function() {
added: [], var file = path.join(main, 'partials', '_one.scss');
changed: [path.join(main, 'one.scss')], var files = watcher.removed(file);
removed: [path.join(main, 'partials', '_one.scss')], assert.deepEqual(files.added, []);
});
it('should record its ancestors as changed', function() {
var file = path.join(main, 'partials', '_one.scss');
var files = watcher.removed(file);
assert.deepEqual(files.changed, [
path.join(main, 'one.scss'),
]);
});
it('should record itself as removed', function() {
var file = path.join(main, 'partials', '_one.scss');
var files = watcher.removed(file);
assert.deepEqual(files.removed, [file]);
});
});
describe('if it is not a partial', function() {
it('should record nothing as added', function() {
var file = path.join(main, 'one.scss');
var files = watcher.removed(file);
assert.deepEqual(files.added, []);
});
it('should record nothing as changed', function() {
var file = path.join(main, 'one.scss');
var files = watcher.removed(file);
assert.deepEqual(files.changed, []);
});
it('should record itself as removed', function() {
var file = path.join(main, 'one.scss');
var files = watcher.removed(file);
assert.deepEqual(files.removed, [file]);
});
}); });
}); });
});
describe('when a file is removed from outside the graph', function() { describe('and is not in the graph', function() {
it('should return an empty array', function() { describe('if it is a partial', function() {
var files = watcher.removed(path.join(sibling, 'partials', '_three.scss')); it('should record nothing', function() {
assert.deepEqual(files, { var file = path.join(sibling, 'partials', '_three.scss');
added: [], var files = watcher.removed(file);
changed: [], assert.deepEqual(files, {
removed: [], added: [],
changed: [],
removed: [],
});
});
});
describe('if it is not a partial', function() {
it('should record nothing', function() {
var file = path.join(sibling, 'three.scss');
var files = watcher.removed(file);
assert.deepEqual(files, {
added: [],
changed: [],
removed: [],
});
});
}); });
}); });
}); });
...@@ -130,59 +245,257 @@ describe('watcher', function() { ...@@ -130,59 +245,257 @@ describe('watcher', function() {
}); });
}); });
describe('when a file is changed in the graph', function() { describe('when a file is changed', function() {
it('should return the files to compile', function() { describe('and it is in the graph', function() {
var files = watcher.changed(path.join(main, 'partials', '_one.scss')); describe('if it is a partial', function() {
assert.deepEqual(files, { it('should record its decendents as added', function() {
added: [], var file = path.join(main, 'partials', '_one.scss');
changed: [path.join(main, 'one.scss')], var files = watcher.changed(file);
removed: [], assert.deepEqual(files.added, [
path.join(main, 'partials', '_three.scss'),
]);
});
it('should record its ancenstors as changed', function() {
var file = path.join(main, 'partials', '_one.scss');
var files = watcher.changed(file);
assert.deepEqual(files.changed, [
path.join(main, 'one.scss'),
]);
});
it('should record nothing as removed', function() {
var file = path.join(main, 'partials', '_one.scss');
var files = watcher.changed(file);
assert.deepEqual(files.removed, []);
});
});
describe('if it is not a partial', function() {
it('should record its decendents as added', function() {
var file = path.join(main, 'one.scss');
var files = watcher.changed(file);
assert.deepEqual(files.added, [
path.join(main, 'partials', '_one.scss'),
path.join(main, 'partials', '_three.scss'),
]);
});
it('should record itself as changed', function() {
var file = path.join(main, 'one.scss');
var files = watcher.changed(file);
assert.deepEqual(files.changed, [file]);
});
it('should record nothing as removed', function() {
var file = path.join(main, 'one.scss');
var files = watcher.changed(file);
assert.deepEqual(files.removed, []);
});
}); });
}); });
});
describe('when a file is changed outside the graph', function() { describe('and it is not in the graph', function() {
it('should return an empty array', function() { describe('if it is a partial', function() {
var files = watcher.changed(path.join(sibling, 'partials', '_three.scss')); it('should record nothing', function() {
assert.deepEqual(files, { var file = path.join(sibling, 'partials', '_three.scss');
added: [], var files = watcher.changed(file);
changed: [], assert.deepEqual(files, {
removed: [], added: [],
changed: [],
removed: [],
});
});
});
describe('if it is not a partial', function() {
it('should record nothing as added', function() {
var file = path.join(sibling, 'three.scss');
var files = watcher.changed(file);
assert.deepEqual(files.added, []);
});
it('should record itself as changed', function() {
var file = path.join(sibling, 'three.scss');
var files = watcher.changed(file);
assert.deepEqual(files.changed, [file]);
});
it('should record nothing as removed', function() {
var file = path.join(sibling, 'three.scss');
var files = watcher.changed(file);
assert.deepEqual(files.removed, []);
});
}); });
}); });
}); });
describe('when a file is added', function() { describe('when a file is added', function() {
it('should return an empty array', function() { describe('and it is in the graph', function() {
var file = path.join(main, 'three.scss'); it('should record nothing as added', function() {
fs.writeFileSync(file, '@import "paritals/one.scss";'); var file = path.join(main, 'partials', '_three.scss');
var files = watcher.added(file); var files = watcher.added(file);
assert.deepEqual(files, { assert.deepEqual(files.added, []);
added: [], });
changed: [],
removed: [], it('should record its decendants as added', function() {
var file = path.join(main, 'partials', '_one.scss');
var files = watcher.added(file);
assert.deepEqual(files.added, [
path.join(main, 'partials', '_three.scss'),
]);
});
it('should record nothing as changed', function() {
var file = path.join(main, 'partials', '_three.scss');
var files = watcher.added(file);
assert.deepEqual(files.changed, []);
});
it('should record nothing as removed', function() {
var file = path.join(main, 'partials', '_three.scss');
var files = watcher.added(file);
assert.deepEqual(files.removed, []);
}); });
}); });
});
describe('when a file is removed from the graph', function() { describe('and it is not in the graph', function() {
it('should return the files to compile', function() { beforeEach(function() {
var files = watcher.removed(path.join(main, 'partials', '_one.scss')); watcher.reset({
assert.deepEqual(files, { src: path.join(main, 'two.scss'),
added: [], includePath: [main]
changed: [path.join(main, 'one.scss')], });
removed: [path.join(main, 'partials', '_one.scss')], });
describe('if it is a partial', function() {
it('should record nothing as added', function() {
var file = path.join(main, 'partials', '_three.scss');
var files = watcher.added(file);
assert.deepEqual(files.added, [
file,
]);
});
it('should not record its decendants as added', function() {
var file = path.join(main, 'partials', '_one.scss');
var files = watcher.added(file);
assert.deepEqual(files.added, [
file,
]);
});
it('should record nothing as changed', function() {
var file = path.join(main, 'partials', '_three.scss');
var files = watcher.added(file);
assert.deepEqual(files.changed, []);
});
it('should record nothing as removed', function() {
var file = path.join(main, 'partials', '_three.scss');
var files = watcher.added(file);
assert.deepEqual(files.removed, []);
});
});
describe('if it is not a partial', function() {
it('should record itself as added', function() {
var file = path.join(main, 'three.scss');
var files = watcher.added(file);
assert.deepEqual(files.added, [
file,
]);
});
it('should record nothing as changed', function() {
var file = path.join(main, 'one.scss');
var files = watcher.added(file);
assert.deepEqual(files.changed, []);
});
it('should record nothing as removed', function() {
var file = path.join(main, 'one.scss');
var files = watcher.added(file);
assert.deepEqual(files.removed, []);
});
}); });
}); });
}); });
describe('when a file is removed from outside the graph', function() { describe('when a file is removed', function() {
it('should return an empty array', function() { describe('and it is in the graph', function() {
var files = watcher.removed(path.join(sibling, 'partials', '_three.scss')); describe('if it is a partial', function() {
assert.deepEqual(files, { it('should record nothing as added', function() {
added: [], var file = path.join(main, 'partials', '_one.scss');
changed: [], var files = watcher.removed(file);
removed: [], assert.deepEqual(files.added, []);
});
it('should record its ancestors as changed', function() {
var file = path.join(main, 'partials', '_one.scss');
var files = watcher.removed(file);
assert.deepEqual(files.changed, [
path.join(main, 'one.scss'),
]);
});
it('should record itself as removed', function() {
var file = path.join(main, 'partials', '_one.scss');
var files = watcher.removed(file);
assert.deepEqual(files.removed, [file]);
});
});
describe('if it is not a partial', function() {
it('should record nothing as added', function() {
var file = path.join(main, 'one.scss');
var files = watcher.removed(file);
assert.deepEqual(files.added, []);
});
it('should record nothing as changed', function() {
var file = path.join(main, 'one.scss');
var files = watcher.removed(file);
assert.deepEqual(files.changed, []);
});
it('should record itself as removed', function() {
var file = path.join(main, 'one.scss');
var files = watcher.removed(file);
assert.deepEqual(files.removed, [file]);
});
});
});
describe('and is not in the graph', function() {
beforeEach(function() {
watcher.reset({
src: path.join(main, 'two.scss'),
includePath: [main]
});
});
describe('if it is a partial', function() {
it('should record nothing as added', function() {
var file = path.join(main, 'partials', '_one.scss');
var files = watcher.removed(file);
assert.deepEqual(files, {
added: [],
changed: [],
removed: [],
});
});
});
describe('if it is not a partial', function() {
it('should record nothing', function() {
var file = path.join(main, 'one.scss');
var files = watcher.removed(file);
assert.deepEqual(files, {
added: [],
changed: [],
removed: [],
});
});
}); });
}); });
}); });
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment