UNPKG

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