Skip to content

Commit 09805e3

Browse files
madwizard-thomasscniro
authored andcommitted
Rebasing fix and test set (#24)
* Rebasing fix * Rebasing test set
1 parent 06c1706 commit 09805e3

File tree

10 files changed

+272
-3
lines changed

10 files changed

+272
-3
lines changed

gulpfile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ gulp.task('pre-test', function () {
1010
});
1111

1212
gulp.task('test', ['pre-test'], function () {
13-
return gulp.src('test/test.js')
13+
return gulp.src('test/*.js')
1414
.pipe(mocha({reporter: 'list'}))
1515
.pipe(istanbul.writeReports({
1616
includeUntested: true,

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ module.exports = function gulpCleanCSS(options, callback) {
2424

2525
var fileOptions = objectAssign({target: file.path}, options);
2626

27-
if (!fileOptions.relativeTo && (fileOptions.root || file.path))
28-
fileOptions.relativeTo = path.dirname(path.resolve(options.root || file.path));
27+
if (!fileOptions.relativeTo && file.path)
28+
fileOptions.relativeTo = path.dirname(path.resolve(file.path));
2929

3030
if (file.sourceMap)
3131
fileOptions.sourceMap = JSON.parse(JSON.stringify(file.sourceMap));
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
p.fromroot
2+
{
3+
background: url(../sbudir/insubdir.png);
4+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
@import url(nestedsub/nested.css);
2+
3+
p.imported_same
4+
{
5+
background: url(imported.png);
6+
}
7+
p.imported_parent
8+
{
9+
background: url(../parent.png);
10+
}
11+
p.imported_other
12+
{
13+
background: url(../othersub/inother.png);
14+
}
15+
p.imported_absolute
16+
{
17+
background: url(/inroot.png);
18+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
p.imported_nested
2+
{
3+
background: url(nested.png);
4+
}

test/fixtures/rebasing/root.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@import url(otherdir/fromroot.css);
2+
p.root
3+
{
4+
background: url(inroot.png);
5+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
@import url(../otherdir/imported.css);
2+
@import url(insub.css);
3+
4+
p.import
5+
{
6+
background: url(import.png);
7+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
@import url(../otherdir/imported.css);
2+
@import url(/rebasing/subdir/insub.css);
3+
@import url(/rebasing/root.css);
4+
5+
p.import
6+
{
7+
background: url(import.png);
8+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
p.insub_same
3+
{
4+
background: url(insub.png);
5+
}
6+
p.insub_child
7+
{
8+
background url(child/child.png);
9+
}
10+
p.insub_parent
11+
{
12+
background: url(../parent.png);
13+
}
14+
p.insub_other
15+
{
16+
background: url(../othersub/inother.png);
17+
}
18+
p.insub_absolute
19+
{
20+
background: url(/inroot.png);
21+
}

test/rebasing.js

Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
const buffer = require('vinyl-buffer');
2+
const chai = require('chai');
3+
const cleanCSS = require('..');
4+
const concat = require('gulp-concat');
5+
const del = require('del');
6+
const expect = chai.expect;
7+
const File = require('vinyl');
8+
const gulp = require('gulp');
9+
const gulpSass = require('gulp-sass');
10+
const rename = require('gulp-rename');
11+
const sourcemaps = require('gulp-sourcemaps');
12+
const vfsFake = require('vinyl-fs-fake');
13+
14+
chai.should();
15+
16+
describe('gulp-clean-css: rebase', function () {
17+
18+
it('should not rebase files by default', function (done) {
19+
20+
// CLI: cleancss test/fixtures/rebasing/subdir/insub.css
21+
gulp.src(['test/fixtures/rebasing/subdir/insub.css'])
22+
.pipe(cleanCSS())
23+
.on('data', function(file) {
24+
expect(file.contents.toString()).to.equal(
25+
'p.insub_same{background:url(insub.png)}' +
26+
'p.insub_parent{background:url(../parent.png)}' +
27+
'p.insub_other{background:url(../othersub/inother.png)}' +
28+
'p.insub_absolute{background:url(/inroot.png)}'
29+
);
30+
})
31+
.once('end', function () {
32+
done();
33+
});
34+
});
35+
36+
// CLI: cleancss test/fixtures/rebasing/subdir/insub.css -o test/fixtures/rebasing/min.generated.css
37+
it('should by rebase files with target specified', function (done) {
38+
gulp.src(['test/fixtures/rebasing/subdir/insub.css'])
39+
.pipe(cleanCSS({target: 'test/fixtures/rebasing/min.generated.css'}))
40+
.on('data', function (file) {
41+
expect(file.contents.toString()).to.equal(
42+
'p.insub_same{background:url(subdir/insub.png)}' +
43+
'p.insub_parent{background:url(parent.png)}' +
44+
'p.insub_other{background:url(othersub/inother.png)}' +
45+
'p.insub_absolute{background:url(/inroot.png)}'
46+
);
47+
})
48+
.once('end', function () {
49+
done();
50+
});
51+
});
52+
53+
// CLI: cleancss test/fixtures/rebasing/subdir/insub.css -o test/fixtures/rebasing/subdir/min.generated.css
54+
it('should by rebase files with target in subdir specified', function (done) {
55+
gulp.src(['test/fixtures/rebasing/subdir/insub.css'])
56+
.pipe(cleanCSS({target: 'test/fixtures/rebasing/subdir/min.generated.css'}))
57+
.on('data', function (file) {
58+
expect(file.contents.toString()).to.equal(
59+
'p.insub_same{background:url(insub.png)}' +
60+
'p.insub_parent{background:url(../parent.png)}' +
61+
'p.insub_other{background:url(../othersub/inother.png)}' +
62+
'p.insub_absolute{background:url(/inroot.png)}'
63+
);
64+
})
65+
.once('end', function () {
66+
done();
67+
});
68+
});
69+
70+
// CLI: cleancss test/fixtures/rebasing/subdir/insub.css --root test/fixtures/rebasing/
71+
it('should rebase files with root specified', function (done) {
72+
gulp.src(['test/fixtures/rebasing/subdir/insub.css'])
73+
.pipe(cleanCSS({root: 'test/fixtures/rebasing/'}))
74+
.on('data', function (file) {
75+
expect(file.contents.toString()).to.equal(
76+
'p.insub_same{background:url(/subdir/insub.png)}' +
77+
'p.insub_parent{background:url(/parent.png)}' +
78+
'p.insub_other{background:url(/othersub/inother.png)}' +
79+
'p.insub_absolute{background:url(/inroot.png)}'
80+
);
81+
})
82+
.once('end', function () {
83+
done();
84+
})
85+
});
86+
87+
// CLI: cleancss test/fixtures/rebasing/subdir/insub.css --root test/fixtures/rebasing/ -o test/fixtures/rebasing/subdir/min.generated.css
88+
it('should rebase files with root and target specified', function (done) {
89+
gulp.src(['test/fixtures/rebasing/subdir/insub.css'])
90+
.pipe(cleanCSS({
91+
root : 'test/fixtures/rebasing/',
92+
target : 'test/fixtures/rebasing/subdir/min.generated.css'
93+
}))
94+
.on('data', function (file) {
95+
expect(file.contents.toString()).to.equal(
96+
'p.insub_same{background:url(/subdir/insub.png)}' +
97+
'p.insub_parent{background:url(/parent.png)}' +
98+
'p.insub_other{background:url(/othersub/inother.png)}' +
99+
'p.insub_absolute{background:url(/inroot.png)}'
100+
);
101+
})
102+
.once('end', function () {
103+
done();
104+
})
105+
});
106+
107+
// CLI: cleancss test/fixtures/rebasing/subdir/import.css
108+
it('should resolve imports correctly', function (done) {
109+
gulp.src(['test/fixtures/rebasing/subdir/import.css'])
110+
.pipe(cleanCSS())
111+
.on('data', function (file) {
112+
expect(file.contents.toString()).to.equal(
113+
'p.imported_nested{background:url(../otherdir/nestedsub/nested.png)}' +
114+
'p.imported_same{background:url(../otherdir/imported.png)}' +
115+
'p.imported_parent{background:url(../parent.png)}' +
116+
'p.imported_other{background:url(../othersub/inother.png)}' +
117+
'p.imported_absolute{background:url(/inroot.png)}' +
118+
'p.insub_same{background:url(insub.png)}' +
119+
'p.insub_parent{background:url(../parent.png)}' +
120+
'p.insub_other{background:url(../othersub/inother.png)}' +
121+
'p.insub_absolute{background:url(/inroot.png)}' +
122+
'p.import{background:url(import.png)}'
123+
);
124+
})
125+
.once('end', function () {
126+
done();
127+
})
128+
});
129+
130+
// CLI: cleancss test/fixtures/rebasing/subdir/import.css -o test/fixtures/root.generated.css
131+
it('should resolve imports with target set correctly', function (done) {
132+
gulp.src(['test/fixtures/rebasing/subdir/import.css'])
133+
.pipe(cleanCSS({target: 'test/fixtures/root.generated.css'}))
134+
.on('data', function (file) {
135+
expect(file.contents.toString()).to.equal(
136+
'p.imported_nested{background:url(rebasing/otherdir/nestedsub/nested.png)}' +
137+
'p.imported_same{background:url(rebasing/otherdir/imported.png)}' +
138+
'p.imported_parent{background:url(rebasing/parent.png)}' +
139+
'p.imported_other{background:url(rebasing/othersub/inother.png)}' +
140+
'p.imported_absolute{background:url(/inroot.png)}' +
141+
'p.insub_same{background:url(rebasing/subdir/insub.png)}' +
142+
'p.insub_parent{background:url(rebasing/parent.png)}' +
143+
'p.insub_other{background:url(rebasing/othersub/inother.png)}' +
144+
'p.insub_absolute{background:url(/inroot.png)}' +
145+
'p.import{background:url(rebasing/subdir/import.png)}'
146+
);
147+
})
148+
.once('end', function () {
149+
done();
150+
})
151+
});
152+
153+
// CLI: cleancss test/fixtures/rebasing/subdir/import_absolute.css --root test/fixtures/
154+
it('should resolve absolute imports with root set correctly', function (done) {
155+
gulp.src(['test/fixtures/rebasing/subdir/import_absolute.css'])
156+
.pipe(cleanCSS({root: 'test/fixtures/'}))
157+
.on('data', function (file) {
158+
expect(file.contents.toString()).to.equal(
159+
'p.imported_nested{background:url(/rebasing/otherdir/nestedsub/nested.png)}' +
160+
'p.imported_same{background:url(/rebasing/otherdir/imported.png)}' +
161+
'p.imported_parent{background:url(/rebasing/parent.png)}' +
162+
'p.imported_other{background:url(/rebasing/othersub/inother.png)}' +
163+
'p.imported_absolute{background:url(/inroot.png)}' +
164+
'p.insub_same{background:url(/rebasing/subdir/insub.png)}' +
165+
'p.insub_parent{background:url(/rebasing/parent.png)}' +
166+
'p.insub_other{background:url(/rebasing/othersub/inother.png)}' +
167+
'p.insub_absolute{background:url(/inroot.png)}' +
168+
'p.fromroot{background:url(/rebasing/sbudir/insubdir.png)}' +
169+
'p.root{background:url(/rebasing/inroot.png)}' +
170+
'p.import{background:url(/rebasing/subdir/import.png)}'
171+
);
172+
})
173+
.once('end', function () {
174+
done();
175+
})
176+
});
177+
178+
// CLI: cleancss test/fixtures/rebasing/subdir/import_absolute.css --root test/fixtures/ -o test/fixtures/rebasing/subdir/min.generated.css
179+
it('should resolve imports with root and target set correctly', function (done) {
180+
gulp.src(['test/fixtures/rebasing/subdir/import_absolute.css'])
181+
.pipe(cleanCSS({root: 'test/fixtures/', target : 'test/fixtures/rebasing/subdir/min.generated.css'}))
182+
.on('data', function (file) {
183+
expect(file.contents.toString()).to.equal(
184+
'p.imported_nested{background:url(/rebasing/otherdir/nestedsub/nested.png)}' +
185+
'p.imported_same{background:url(/rebasing/otherdir/imported.png)}' +
186+
'p.imported_parent{background:url(/rebasing/parent.png)}' +
187+
'p.imported_other{background:url(/rebasing/othersub/inother.png)}' +
188+
'p.imported_absolute{background:url(/inroot.png)}' +
189+
'p.insub_same{background:url(/rebasing/subdir/insub.png)}' +
190+
'p.insub_parent{background:url(/rebasing/parent.png)}' +
191+
'p.insub_other{background:url(/rebasing/othersub/inother.png)}' +
192+
'p.insub_absolute{background:url(/inroot.png)}' +
193+
'p.fromroot{background:url(/rebasing/sbudir/insubdir.png)}' +
194+
'p.root{background:url(/rebasing/inroot.png)}' +
195+
'p.import{background:url(/rebasing/subdir/import.png)}'
196+
);
197+
})
198+
.once('end', function () {
199+
done();
200+
})
201+
});
202+
});

0 commit comments

Comments
 (0)