UNPKG

6.71 kBJavaScriptView Raw
1/* eslint max-nested-callbacks:0 */
2
3'use strict';
4
5const metadata = require('../src/metadata.js');
6const fs = require('fs');
7const path = require('path');
8const assert = require('assert');
9
10describe('Metadata service', () => {
11 it('should throw error when using old options', () => {
12 assert.throws(
13 metadata.bind(metadata, {
14 appendUnicode: true,
15 })
16 );
17 });
18
19 describe('for code generation', () => {
20 it('should extract right unicodes from files', (done) => {
21 const metadataService = metadata();
22
23 metadataService('/var/plop/hello.svg', (err, infos) => {
24 if (err) {
25 done(err);
26 return;
27 }
28 assert.deepEqual(infos, {
29 path: '/var/plop/hello.svg',
30 name: 'hello',
31 unicode: [String.fromCharCode(0xea01)],
32 renamed: false,
33 });
34 done();
35 });
36 });
37
38 it('should append unicodes to files when the option is set', (done) => {
39 const metadataService = metadata({
40 prependUnicode: true,
41 log: () => {},
42 error: () => {
43 done(new Error('Not supposed to be here'));
44 },
45 });
46
47 fs.writeFileSync(
48 path.join(__dirname, 'results', 'plop.svg'),
49 'plop',
50 'utf-8'
51 );
52 metadataService(
53 path.join(__dirname, 'results', 'plop.svg'),
54 (err, infos) => {
55 if (err) {
56 done(err);
57 return;
58 }
59 assert.deepEqual(infos, {
60 path: path.join(__dirname, 'results', 'uEA01-plop.svg'),
61 name: 'plop',
62 unicode: [String.fromCharCode(0xea01)],
63 renamed: true,
64 });
65 assert(
66 fs.existsSync(path.join(__dirname, 'results', 'uEA01-plop.svg'))
67 );
68 assert(!fs.existsSync(path.join(__dirname, 'results', 'plop.svg')));
69 fs.unlinkSync(path.join(__dirname, 'results', 'uEA01-plop.svg'));
70 done();
71 }
72 );
73 });
74
75 it('should log file rename errors', (done) => {
76 const metadataService = metadata({
77 prependUnicode: true,
78 startUnicode: 0xea02,
79 error: () => {},
80 log: () => {
81 done(new Error('Not supposed to be here'));
82 },
83 });
84
85 metadataService(
86 path.join(__dirname, 'results', 'plop.svg'),
87 (err, infos) => {
88 assert(!infos);
89 assert(err);
90 assert(
91 !fs.existsSync(path.join(__dirname, 'results', 'uEA02-plop.svg'))
92 );
93 done();
94 }
95 );
96 });
97 });
98
99 describe('for code extraction', () => {
100 it('should work for simple codes', (done) => {
101 const metadataService = metadata();
102
103 metadataService('/var/plop/u0001-hello.svg', (err, infos) => {
104 assert(!err);
105 assert.deepEqual(infos, {
106 path: '/var/plop/u0001-hello.svg',
107 name: 'hello',
108 unicode: [String.fromCharCode(0x0001)],
109 renamed: false,
110 });
111 done();
112 });
113 });
114
115 it('should work for several codes', (done) => {
116 const metadataService = metadata();
117
118 metadataService('/var/plop/u0001,u0002-hello.svg', (err, infos) => {
119 assert(!err);
120 assert.deepEqual(infos, {
121 path: '/var/plop/u0001,u0002-hello.svg',
122 name: 'hello',
123 unicode: [String.fromCharCode(0x0001), String.fromCharCode(0x0002)],
124 renamed: false,
125 });
126 done();
127 });
128 });
129
130 it('should work for higher codepoint codes', (done) => {
131 const metadataService = metadata();
132
133 metadataService('/var/plop/u1F63A-hello.svg', (err, infos) => {
134 assert(!err);
135 assert.deepEqual(infos, {
136 path: '/var/plop/u1F63A-hello.svg',
137 name: 'hello',
138 unicode: [String.fromCodePoint(0x1f63a)],
139 renamed: false,
140 });
141 done();
142 });
143 });
144
145 it('should work for ligature codes', (done) => {
146 const metadataService = metadata();
147
148 metadataService('/var/plop/u0001u0002-hello.svg', (err, infos) => {
149 assert(!err);
150 assert.deepEqual(infos, {
151 path: '/var/plop/u0001u0002-hello.svg',
152 name: 'hello',
153 unicode: [String.fromCharCode(0x0001) + String.fromCharCode(0x0002)],
154 renamed: false,
155 });
156 done();
157 });
158 });
159
160 it('should work for nested codes', (done) => {
161 const metadataService = metadata();
162
163 metadataService('/var/plop/u0001u0002,u0001-hello.svg', (err, infos) => {
164 assert(!err);
165 assert.deepEqual(infos, {
166 path: '/var/plop/u0001u0002,u0001-hello.svg',
167 name: 'hello',
168 unicode: [
169 String.fromCharCode(0x0001) + String.fromCharCode(0x0002),
170 String.fromCharCode(0x0001),
171 ],
172 renamed: false,
173 });
174 done();
175 });
176 });
177
178 it('should not set the same codepoint twice', (done) => {
179 const metadataService = metadata();
180
181 metadataService('/var/plop/uEA01-hello.svg', (err, infos) => {
182 assert(!err);
183 assert.deepEqual(infos, {
184 path: '/var/plop/uEA01-hello.svg',
185 name: 'hello',
186 unicode: [String.fromCharCode(0xea01)],
187 renamed: false,
188 });
189 metadataService('/var/plop/plop.svg', (err2, infos2) => {
190 assert(!err2);
191 assert.deepEqual(infos2, {
192 path: '/var/plop/plop.svg',
193 name: 'plop',
194 unicode: [String.fromCharCode(0xea02)],
195 renamed: false,
196 });
197 done();
198 });
199 });
200 });
201
202 it('should not set the same codepoint twice with different cases', (done) => {
203 const metadataService = metadata();
204
205 metadataService('/var/plop/UEA01-hello.svg', (err, infos) => {
206 assert(!err);
207 assert.deepEqual(infos, {
208 path: '/var/plop/UEA01-hello.svg',
209 name: 'hello',
210 unicode: [String.fromCharCode(0xea01)],
211 renamed: false,
212 });
213 metadataService('/var/plop/uEA02-hello.svg', (err2, infos2) => {
214 assert(!err2);
215 assert.deepEqual(infos2, {
216 path: '/var/plop/uEA02-hello.svg',
217 name: 'hello',
218 unicode: [String.fromCharCode(0xea02)],
219 renamed: false,
220 });
221 metadataService('/var/plop/bell-o.svg', (err3, infos3) => {
222 assert(!err3);
223 assert.deepEqual(infos3, {
224 path: '/var/plop/bell-o.svg',
225 name: 'bell-o',
226 unicode: [String.fromCharCode(0xea03)],
227 renamed: false,
228 });
229 done();
230 });
231 });
232 });
233 });
234 });
235});