Skip to content
This repository was archived by the owner on Feb 3, 2025. It is now read-only.

Commit 51f3dc3

Browse files
committed
Use file.relative for extractor filename
Close gabegorelick#3
1 parent 30515ae commit 51f3dc3

4 files changed

Lines changed: 85 additions & 107 deletions

File tree

lib/extract.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ module.exports = function (out, config) {
3636
return cb();
3737
}
3838

39-
extractor.parse(file.path, file.contents.toString());
39+
extractor.parse(file.relative, file.contents.toString());
4040
file.contents = new Buffer(extractor.toString());
4141

4242
var extension = path.extname(file.path);
@@ -68,7 +68,7 @@ module.exports = function (out, config) {
6868
firstFile = file;
6969
}
7070

71-
extractor.parse(file.path, file.contents.toString());
71+
extractor.parse(file.relative, file.contents.toString());
7272

7373
cb();
7474
};

test/fixtures/es.po

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ msgstr ""
1212
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
1313
"Language: es\n"
1414

15-
#: foo.html
15+
#: foo.html:1
1616
msgid "Hello world"
1717
msgstr "¡Hola, mundo"
1818

19-
#: bar.html
19+
#: bar.html:1
2020
msgid "Goodbye"
2121
msgstr "Adios"
2222

test/fixtures/relative.pot

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
msgid ""
2+
msgstr ""
3+
"Content-Type: text/plain; charset=UTF-8\n"
4+
"Content-Transfer-Encoding: 8bit\n"
5+
"Project-Id-Version: \n"
6+
7+
#: ../fixtures/partial1.html:1
8+
msgid "Hello"
9+
msgstr ""
10+
11+
#: ../fixtures/partial2.html:1
12+
msgid "world"
13+
msgstr ""

test/main.js

Lines changed: 68 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,30 @@ var fs = require('fs');
99
var path = require('path');
1010
var PO = require('pofile');
1111

12-
var fixturesDir = __dirname + '/fixtures';
12+
var fixturesDir = path.join(__dirname, 'fixtures');
13+
var anotherDir = path.join(__dirname, 'another');
14+
15+
var createFixtureFile = function(filename, content) {
16+
return new gutil.File({
17+
cwd: __dirname,
18+
base: fixturesDir,
19+
path: path.join(fixturesDir, filename),
20+
contents: new Buffer(content)
21+
});
22+
};
1323

1424
describe('gulp-angular-gettext', function () {
1525
describe('extract()', function () {
1626

1727
it('should work with no arguments', function (done) {
18-
var partial = new gutil.File({
19-
cwd: __dirname,
20-
base: fixturesDir,
21-
path: fixturesDir + '/partial.html',
22-
contents: new Buffer('<div translate>Hello</div><div translate>Goodbye</div>')
23-
});
28+
var partial = createFixtureFile('partial.html', '<div translate>Hello</div><div translate>Goodbye</div>');
2429

2530
var stream = extract();
2631
stream.on('error', done);
2732
stream.on('data', function (file) {
28-
expect(file.path).to.equal(fixturesDir + '/partial.pot');
33+
expect(file.path).to.equal(path.join(fixturesDir, 'partial.pot'));
2934

30-
PO.load(__dirname + '/fixtures/test.pot', function (err, expected) {
35+
PO.load(path.join(fixturesDir, 'test.pot'), function (err, expected) {
3136
if (err) {
3237
done(err);
3338
return;
@@ -52,29 +57,15 @@ describe('gulp-angular-gettext', function () {
5257
stream.end();
5358
});
5459

55-
var relativizeHeaders = function (po) {
56-
po.items.forEach(function (item) {
57-
item.references = item.references.map(function (ref) {
58-
return path.relative(path.join(__dirname, 'fixtures'), ref)
59-
.replace(/\\/g, '/'); // replace any Windows-style paths
60-
});
61-
});
62-
};
63-
6460
it('should work with just a string argument', function (done) {
65-
var partial = new gutil.File({
66-
cwd: __dirname,
67-
base: fixturesDir,
68-
path: fixturesDir + '/partial.html',
69-
contents: new Buffer('<div translate>Hello</div><div translate>Goodbye</div>')
70-
});
61+
var partial = createFixtureFile('partial.html', '<div translate>Hello</div><div translate>Goodbye</div>');
7162

7263
var stream = extract('foo.bar');
7364
stream.on('error', done);
7465
stream.on('data', function (file) {
7566
expect(file.path).to.equal(path.join(fixturesDir, 'foo.bar'));
7667

77-
PO.load(__dirname + '/fixtures/test.pot', function (err, expected) {
68+
PO.load(path.join(fixturesDir, 'test.pot'), function (err, expected) {
7869
if (err) {
7970
done(err);
8071
return;
@@ -100,21 +91,14 @@ describe('gulp-angular-gettext', function () {
10091
});
10192

10293
it('should work with just an object argument', function (done) {
103-
var partial = new gutil.File({
104-
cwd: __dirname,
105-
base: fixturesDir,
106-
path: path.join(fixturesDir, 'partial.html'),
107-
contents: new Buffer('<div translate>Hello</div><div translate>Goodbye</div>')
108-
});
94+
var partial = createFixtureFile('partial.html', '<div translate>Hello</div><div translate>Goodbye</div>');
10995

110-
var stream = extract({
111-
postProcess: relativizeHeaders
112-
});
96+
var stream = extract();
11397
stream.on('error', done);
11498
stream.on('data', function (file) {
11599
expect(file.path).to.equal(path.join(fixturesDir, 'partial.pot'));
116100

117-
PO.load(__dirname + '/fixtures/test.pot', function (err, expected) {
101+
PO.load(path.join(fixturesDir, 'test.pot'), function (err, expected) {
118102
if (err) {
119103
done(err);
120104
return;
@@ -131,21 +115,14 @@ describe('gulp-angular-gettext', function () {
131115
});
132116

133117
it('should work with a single input file', function (done) {
134-
var partial = new gutil.File({
135-
cwd: __dirname,
136-
base: fixturesDir,
137-
path: fixturesDir + '/partial.html',
138-
contents: new Buffer('<div translate>Hello</div><div translate>Goodbye</div>')
139-
});
118+
var partial = createFixtureFile('partial.html', '<div translate>Hello</div><div translate>Goodbye</div>');
140119

141-
var stream = extract({
142-
postProcess: relativizeHeaders
143-
});
120+
var stream = extract();
144121
stream.on('error', done);
145122
stream.on('data', function (file) {
146123
expect(file.isNull()).to.be.false;
147124

148-
fs.readFile(__dirname + '/fixtures/test.pot', {encoding: 'utf8'}, function (err, pot) {
125+
fs.readFile(path.join(fixturesDir, 'test.pot'), {encoding: 'utf8'}, function (err, pot) {
149126
if (err) {
150127
done(err);
151128
return;
@@ -161,27 +138,50 @@ describe('gulp-angular-gettext', function () {
161138
});
162139

163140
it('should work with multiple input files', function (done) {
141+
var partial1 = createFixtureFile('partial1.html', '<div translate>Hello</div>');
142+
var partial2 = createFixtureFile('partial2.html', '<div translate>world</div>');
143+
144+
var stream = extract('out.pot');
145+
stream.on('error', done);
146+
stream.on('data', function (file) {
147+
expect(file.isNull()).to.be.false;
148+
149+
fs.readFile(path.join(fixturesDir, 'multiple.pot'), {encoding: 'utf8'}, function (err, pot) {
150+
if (err) {
151+
done(err);
152+
return;
153+
}
154+
155+
expect(file.contents.toString()).to.equal(pot);
156+
157+
done();
158+
});
159+
});
160+
stream.write(partial1);
161+
stream.write(partial2);
162+
stream.end();
163+
});
164+
165+
it('should support relative paths properly', function (done) {
164166
var partial1 = new gutil.File({
165167
cwd: __dirname,
166-
base: fixturesDir,
167-
path: fixturesDir + '/partial1.html',
168+
base: anotherDir,
169+
path: path.join(fixturesDir, 'partial1.html'),
168170
contents: new Buffer('<div translate>Hello</div>')
169171
});
170172
var partial2 = new gutil.File({
171173
cwd: __dirname,
172-
base: fixturesDir,
173-
path: fixturesDir + '/partial2.html',
174+
base: anotherDir,
175+
path: path.join(fixturesDir, 'partial2.html'),
174176
contents: new Buffer('<div translate>world</div>')
175177
});
176178

177-
var stream = extract('out.pot', {
178-
postProcess: relativizeHeaders
179-
});
179+
var stream = extract('out.pot');
180180
stream.on('error', done);
181181
stream.on('data', function (file) {
182182
expect(file.isNull()).to.be.false;
183183

184-
fs.readFile(__dirname + '/fixtures/multiple.pot', {encoding: 'utf8'}, function (err, pot) {
184+
fs.readFile(path.join(fixturesDir, 'relative.pot'), {encoding: 'utf8'}, function (err, pot) {
185185
if (err) {
186186
done(err);
187187
return;
@@ -198,25 +198,13 @@ describe('gulp-angular-gettext', function () {
198198
});
199199

200200
it('should merge duplicate strings with references', function (done) {
201-
var partial1 = new gutil.File({
202-
cwd: __dirname,
203-
base: fixturesDir,
204-
path: fixturesDir + '/partial1.html',
205-
contents: new Buffer('<div translate>Hello</div><div translate>Hello</div>')
206-
});
207-
var partial2 = new gutil.File({
208-
cwd: __dirname,
209-
base: fixturesDir,
210-
path: fixturesDir + '/partial2.html',
211-
contents: new Buffer('<div translate>Hello</div><div translate>world</div>')
212-
});
201+
var partial1 = createFixtureFile('partial1.html', '<div translate>Hello</div><div translate>Hello</div>');
202+
var partial2 = createFixtureFile('partial2.html', '<div translate>Hello</div><div translate>world</div>');
213203

214-
var stream = extract('out.pot', {
215-
postProcess: relativizeHeaders
216-
});
204+
var stream = extract('out.pot');
217205
stream.on('error', done);
218206
stream.on('data', function (file) {
219-
fs.readFile(__dirname + '/fixtures/merge-duplicates.pot', {encoding: 'utf8'}, function (err, pot) {
207+
fs.readFile(path.join(fixturesDir, 'merge-duplicates.pot'), {encoding: 'utf8'}, function (err, pot) {
220208
if (err) {
221209
done(err);
222210
return;
@@ -233,19 +221,12 @@ describe('gulp-angular-gettext', function () {
233221
});
234222

235223
it('should extract plural strings', function (done) {
236-
var partial1 = new gutil.File({
237-
cwd: __dirname,
238-
base: fixturesDir,
239-
path: fixturesDir + '/partial1.html',
240-
contents: new Buffer('<div translate translate-n="count" translate-plural="Birds">Bird</div>')
241-
});
224+
var partial1 = createFixtureFile('partial1.html', '<div translate translate-n="count" translate-plural="Birds">Bird</div>');
242225

243-
var stream = extract('out.pot', {
244-
postProcess: relativizeHeaders
245-
});
226+
var stream = extract('out.pot');
246227
stream.on('error', done);
247228
stream.on('data', function (file) {
248-
fs.readFile(__dirname + '/fixtures/plural.pot', {encoding: 'utf8'}, function (err, pot) {
229+
fs.readFile(path.join(fixturesDir, 'plural.pot'), {encoding: 'utf8'}, function (err, pot) {
249230
if (err) {
250231
done(err);
251232
return;
@@ -261,20 +242,13 @@ describe('gulp-angular-gettext', function () {
261242
});
262243

263244
it('should merge singular and plural strings', function (done) {
264-
var partial1 = new gutil.File({
265-
cwd: __dirname,
266-
base: fixturesDir,
267-
path: fixturesDir + '/partial1.html',
268-
contents: new Buffer('<div translate translate-n="count" translate-plural="Birds">Bird</div>' +
269-
'<div translate>Bird</div>')
270-
});
245+
var partial1 = createFixtureFile('partial1.html',
246+
'<div translate translate-n="count" translate-plural="Birds">Bird</div><div translate>Bird</div>');
271247

272-
var stream = extract('out.pot', {
273-
postProcess: relativizeHeaders
274-
});
248+
var stream = extract('out.pot');
275249
stream.on('error', done);
276250
stream.on('data', function (file) {
277-
fs.readFile(__dirname + '/fixtures/plural.pot', {encoding: 'utf8'}, function (err, pot) {
251+
fs.readFile(path.join(fixturesDir, 'plural.pot'), {encoding: 'utf8'}, function (err, pot) {
278252
if (err) {
279253
done(err);
280254
return;
@@ -291,17 +265,8 @@ describe('gulp-angular-gettext', function () {
291265
});
292266

293267
describe('compile()', function () {
294-
var createFile = function (contents) {
295-
return new gutil.File({
296-
cwd: __dirname,
297-
base: fixturesDir,
298-
path: fixturesDir + '/es.po',
299-
contents: contents
300-
});
301-
};
302-
303268
it('should match es.po', function (done) {
304-
fs.readFile(__dirname + '/fixtures/es.po', function (err, esPo) {
269+
fs.readFile(path.join(fixturesDir, 'es.po'), function (err, esPo) {
305270
if (err) {
306271
done(err);
307272
return;
@@ -322,14 +287,14 @@ describe('gulp-angular-gettext', function () {
322287

323288
done();
324289
});
325-
stream.write(createFile(esPo));
290+
stream.write(createFixtureFile('es.po', esPo));
326291
stream.end();
327292
});
328293
});
329294

330295
// I prefer JSON, but JS is angular-gettext-tool's default
331296
it('should default to javascript', function (done) {
332-
fs.readFile(__dirname + '/fixtures/es.po', function (err, esPo) {
297+
fs.readFile(path.join(fixturesDir, 'es.po'), function (err, esPo) {
333298
if (err) {
334299
done(err);
335300
return;
@@ -341,7 +306,7 @@ describe('gulp-angular-gettext', function () {
341306

342307
done();
343308
});
344-
stream.write(createFile(esPo));
309+
stream.write(createFixtureFile('es.po', esPo));
345310
stream.end();
346311
});
347312
});

0 commit comments

Comments
 (0)