.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / prettier-stylelint / test.js
1 'use strict';
2
3 const fs = require('fs');
4 const test = require('ava');
5 const tempWrite = require('temp-write');
6 const stylelint = require('stylelint');
7 const resolveFrom = require('resolve-from');
8 const { format, resolveConfig, getPrettierConfig } = require('./index');
9
10 const linterAPI = stylelint.createLinter({ fix: true });
11
12 test('resolveConfig', t =>
13     resolveConfig({ filePath: './fixtures/style.css' }).then(config =>
14         t.deepEqual(config[1], {
15             rules: {
16                 'string-quotes': ['single'],
17                 'indentation': [4, { except: ['value'] }],
18                 'color-hex-case': ['upper'],
19                 'color-hex-length': ['short'],
20                 'block-no-empty': null,
21                 'color-no-invalid-hex': [true],
22                 'comment-empty-line-before': [
23                     'always',
24                     { ignore: ['stylelint-commands', 'after-comment'] }
25                 ],
26                 'declaration-colon-space-after': ['always'],
27                 'max-empty-lines': [2],
28                 'rule-empty-line-before': [
29                     'always',
30                     {
31                         except: ['first-nested'],
32                         ignore: ['after-comment']
33                     }
34                 ],
35                 'unit-whitelist': [['em', 'rem', '%', 's']]
36             }
37         })
38     ));
39
40 test('resolveConfig not found fallback process.cwd', (t) => {
41     const tempPath = tempWrite.sync(
42         'a[id="foo"] { content: "x"; }',
43         'test.css'
44     );
45
46     return resolveConfig({ filePath: tempPath }).then((config) => {
47         t.is(config[1].rules['function-comma-newline-after'], null);
48
49         return config;
50     });
51 });
52
53 test('resolveConfig shortcircuit ', t =>
54     resolveConfig({ stylelintConfig: { rules: { 'max-line-length': [20] } } }).then((config) => {
55         t.is(config[0].printWidth, 20);
56
57         return config;
58     }));
59
60 test('resolve string quotes === double ', (t) => {
61     const config = resolveConfig.resolve({ rules: { 'string-quotes': ['double'] } });
62
63     t.is(config[0].singleQuote, undefined);
64 });
65
66 test('resolve indentation === tab', (t) => {
67     const config = resolveConfig.resolve({ rules: { indentation: ['tab'] } });
68
69     t.plan(2);
70     t.is(config[0].useTabs, true);
71     t.is(config[0].tabWidth, 2);
72 });
73
74 test('resolveConfig prettier merge', t =>
75     resolveConfig({
76         filePath: './fixtures/style.css',
77         prettierOptions: getPrettierConfig('./fixtures/style.css')
78     }).then((config) => {
79         t.is(config[0].semi, false);
80
81         return config;
82     }));
83
84 test('format', (t) => {
85     const source = fs.readFileSync('./fixtures/style.css', 'utf8');
86
87     return format({
88         text: source,
89         filePath: './fixtures/style.css'
90     }).then((source) => {
91         t.is(
92             source,
93             `@media print {
94     a {
95         color: #FFF;
96         background-position: top left, top right;
97     }
98 }
99
100 a[id='foo'] {
101     content: 'x';
102 }
103 `
104         );
105
106         return source;
107     });
108 });
109
110 test('format without code but with filePath', t =>
111     format({ filePath: './fixtures/style.css' }).then((source) => {
112         t.is(
113             source,
114             `@media print {
115     a {
116         color: #FFF;
117         background-position: top left, top right;
118     }
119 }
120
121 a[id='foo'] {
122     content: 'x';
123 }
124 `
125         );
126
127         return source;
128     }));
129
130 test('format less', (t) => {
131     const source = fs.readFileSync('./fixtures/less.less', 'utf8');
132
133     return format({
134         text: source,
135         filePath: './fixtures/less.less'
136     }).then((source) => {
137         t.is(
138             source,
139             `@base: #F938AB;
140
141 .box-shadow(@style, @c) when (iscolor(@c)) {
142     -webkit-box-shadow: @style @c;
143     box-shadow: @style @c;
144 }
145 .box-shadow(@style, @alpha: 50%) when (isnumber(@alpha)) {
146     .box-shadow(@style, rgba(0, 0, 0, @alpha));
147 }
148
149 .box {
150     color: saturate(@base, 5%);
151     border-color: lighten(@base, 30%);
152
153     div {
154         .box-shadow(0 0 5px, 30%);
155     }
156 }
157 `
158         );
159
160         return source;
161     });
162 });
163
164 test('format with syntax error from prettier', (t) => {
165     const source = fs.readFileSync('./tests/error-syntax.css', 'utf8');
166
167     return format({
168         text: source,
169         filePath: './tests/error-syntax.css'
170     }).catch((err) => {
171         t.is(err.name, 'SyntaxError');
172     });
173 });
174
175 test('alternate stylelint format', (t) => {
176     const source = fs.readFileSync('./fixtures/style.css', 'utf8');
177
178     return linterAPI
179         ._lintSource({
180             code: source
181             // codeFilename: process.cwd()
182             // codeFilename: path.resolve(process.cwd(), './tests/less.less')
183             // filePath: path.resolve(process.cwd(), './fixtures/style.css')
184         })
185         .then((result) => {
186             const fixed = result.root.toString(result.opts.syntax);
187
188             t.is(
189                 result.root.toString(result.opts.syntax),
190                 `@media print {
191     a {
192         color: #FFF;
193         background-position: top left,
194         top right;
195     }
196 }
197
198
199
200
201 a[id="foo"] { content: "x"; }
202 `
203             );
204
205             return fixed;
206         });
207 });
208
209 test('resolve relative package', (t) => {
210     const path = resolveFrom('./fixtures/find-package/style.css', 'prettier');
211
212     t.is('1.6.0', require(path).version);
213 });
214
215 test('resolve relative package deep', (t) => {
216     const path = resolveFrom(
217         './fixtures/find-package/deep/style.css',
218         'prettier'
219     );
220
221     t.is('1.6.0', require(path).version);
222 });
223
224 test('resolve relative package fallback', (t) => {
225     const path = resolveFrom('./fixtures/style.css', 'prettier');
226
227     t.is('1.7.0', require(path).version);
228 });
229
230 test('resolve relative package null', (t) => {
231     const path = resolveFrom(__filename, 'prettier');
232
233     t.is('1.7.0', require(path).version);
234 });