forked from fsprojects/fantomas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFormatConfigEditorConfigurationFileTests.fs
483 lines (341 loc) · 12.3 KB
/
FormatConfigEditorConfigurationFileTests.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
module Fantomas.Tests.FormatConfigEditorConfigurationFileTests
open System
open Fantomas
open Fantomas.FormatConfig
open Fantomas.Extras
open Fantomas.Tests.TestHelper
open NUnit.Framework
open System.IO
let private defaultConfig = FormatConfig.Default
let tempName () = Guid.NewGuid().ToString("N")
type ConfigurationFile
internal
(
config: FormatConfig.FormatConfig,
rootFolderName: string,
?editorConfigHeader: string,
?subFolder: string,
?isRoot: bool,
?content: string
) =
let rootDir = Path.Join(Path.GetTempPath(), rootFolderName)
do
if not (Directory.Exists(rootDir)) then
Directory.CreateDirectory(rootDir) |> ignore
let editorConfigPath =
match subFolder with
| Some sf ->
let dirPath = Path.Join(rootDir, sf)
if not (Directory.Exists(dirPath)) then
Directory.CreateDirectory(dirPath) |> ignore
Path.Join(rootDir, sf, ".editorconfig")
| None -> Path.Join(rootDir, ".editorconfig")
let header = Option.defaultValue "[*.fs]" editorConfigHeader
let content =
match content with
| Some c -> c
| None ->
let root =
match isRoot with
| Some true -> "root=true"
| _ -> String.empty
sprintf "%s\n\n%s\n%s" root header (EditorConfig.configToEditorConfig config)
do File.WriteAllText(editorConfigPath, content)
interface IDisposable with
member this.Dispose() : unit =
if Directory.Exists(rootDir) then
Directory.Delete(rootDir, true)
type FSharpFile
internal
(
rootFolderName: string,
?fsharpFileExtension: string,
?subFolder: string,
?content: string,
?fileName: string
) =
let rootDir = Path.Join(Path.GetTempPath(), rootFolderName)
do
if not (Directory.Exists(rootDir)) then
Directory.CreateDirectory(rootDir) |> ignore
let extension = Option.defaultValue ".fs" fsharpFileExtension
let fsharpFile =
Option.defaultValue (sprintf "%s%s" (tempName ()) extension) fileName
let fsharpFilePath =
match subFolder with
| Some sf ->
let dirPath = Path.Join(rootDir, sf)
if not (Directory.Exists(dirPath)) then
Directory.CreateDirectory(dirPath) |> ignore
Path.Join(rootDir, sf, fsharpFile)
| None -> Path.Join(rootDir, fsharpFile)
let content = Option.defaultValue String.empty content
do File.WriteAllText(fsharpFilePath, content)
member __.FSharpFile: string = fsharpFilePath
interface IDisposable with
member this.Dispose() : unit =
if Directory.Exists(rootDir) then
Directory.Delete(rootDir, true)
[<Test>]
let ``single configuration file`` () =
let rootFolderName = tempName ()
use configFixture = new ConfigurationFile(defaultConfig, rootFolderName)
use fsharpFile = new FSharpFile(rootFolderName, fsharpFileExtension = ".fs")
let config = EditorConfig.readConfiguration fsharpFile.FSharpFile
config == defaultConfig
[<Test>]
let ``pointing to subfolder should return parent config file as well`` () =
let rootFolder = tempName ()
let subFolder = tempName ()
use parentConfig =
new ConfigurationFile({ defaultConfig with IndentSize = 3 }, rootFolder)
use childConfig =
new ConfigurationFile({ defaultConfig with IndentSize = 2 }, rootFolder, subFolder = subFolder)
use fsharpFile = new FSharpFile(rootFolder, subFolder = subFolder)
let config = EditorConfig.readConfiguration fsharpFile.FSharpFile
config.IndentSize == 2
[<Test>]
let ``parent config should not be taking into account when child is root`` () =
let rootFolder = tempName ()
let subFolder = tempName ()
use parentConfig =
new ConfigurationFile({ defaultConfig with MaxRecordWidth = 10 }, rootFolder)
use childConfig =
new ConfigurationFile({ defaultConfig with IndentSize = 2 }, rootFolder, subFolder = subFolder, isRoot = true)
use fsharpFile = new FSharpFile(rootFolder, subFolder = subFolder)
let config = EditorConfig.readConfiguration fsharpFile.FSharpFile
config.MaxRecordWidth
== defaultConfig.MaxRecordWidth
config.IndentSize == 2
[<Test>]
let ``configuration file should not affect file extension`` () =
let rootFolder = tempName ()
use configFixture =
new ConfigurationFile({ defaultConfig with MaxLineLength = 90 }, rootFolder)
use fsharpFile = new FSharpFile(rootFolder, fsharpFileExtension = ".fsx")
let config = EditorConfig.readConfiguration fsharpFile.FSharpFile
config.MaxLineLength
== defaultConfig.MaxLineLength
[<Test>]
let ``fantomas-tool configuration file`` () =
let rootDir = tempName ()
let myConfig =
"""
[*.fs]
fsharp_max_if_then_else_short_width=25
fsharp_max_value_binding_width=40
fsharp_max_function_binding_width=40
"""
use configFixture =
new ConfigurationFile(defaultConfig, rootDir, content = myConfig)
use fsharpFile = new FSharpFile(rootDir)
let config = EditorConfig.readConfiguration fsharpFile.FSharpFile
config.MaxIfThenElseShortWidth == 25
config.MaxValueBindingWidth == 40
config.MaxFunctionBindingWidth == 40
[<Test>]
let ``non existing file should return defaults for readConfiguration`` () =
let rootDir = tempName ()
use configFixture = new ConfigurationFile(defaultConfig, rootDir)
let config = EditorConfig.readConfiguration "bogus.fs"
config == defaultConfig
// In the future we could ensure that the Default config isn't
// being generated every time because it's a property getter
// Assert.That(Object.ReferenceEquals(config, defaultConfig))
[<Test>]
let ``non existing file should return None for tryReadConfiguration`` () =
let rootDir = tempName ()
use configFixture = new ConfigurationFile(defaultConfig, rootDir)
let config = EditorConfig.tryReadConfiguration "bogus.fs"
config == None
[<Test>]
let ``indent_style tab edge case`` () =
let rootDir = tempName ()
let editorConfig =
"""
[*.fs]
indent_style=tab
indent_size=tab
tab_width=5
fsharp_indent_on_try_with=true
"""
use configFixture =
new ConfigurationFile(defaultConfig, rootDir, content = editorConfig)
use fsharpFile = new FSharpFile(rootDir)
let config = EditorConfig.readConfiguration fsharpFile.FSharpFile
config.IndentSize == 5
[<Test>]
let ``print default editorconfig settings`` () =
FormatConfig.Default
|> EditorConfig.configToEditorConfig
|> printfn "%s"
[<Test>]
let ``list and array number_of_items parsing tests`` () =
let rootDir = tempName ()
let editorConfig =
"""
[*.fs]
fsharp_array_or_list_multiline_formatter = number_of_items
fsharp_max_array_or_list_number_of_items = 4
"""
use configFixture =
new ConfigurationFile(defaultConfig, rootDir, content = editorConfig)
use fsharpFile = new FSharpFile(rootDir)
let config = EditorConfig.readConfiguration fsharpFile.FSharpFile
config.MaxArrayOrListNumberOfItems == 4
config.ArrayOrListMultilineFormatter
== NumberOfItems
[<Test>]
let ``list and array character_width parsing test with single option`` () =
let rootDir = tempName ()
let editorConfig =
"""
[*.fs]
fsharp_max_array_or_list_width = 123
"""
use configFixture =
new ConfigurationFile(defaultConfig, rootDir, content = editorConfig)
use fsharpFile = new FSharpFile(rootDir)
let config = EditorConfig.readConfiguration fsharpFile.FSharpFile
config.MaxArrayOrListWidth == 123
[<Test>]
let ``record number_of_items parsing tests`` () =
let rootDir = tempName ()
let editorConfig =
"""
[*.fs]
fsharp_record_multiline_formatter = number_of_items
fsharp_max_record_number_of_items = 4
"""
use configFixture =
new ConfigurationFile(defaultConfig, rootDir, content = editorConfig)
use fsharpFile = new FSharpFile(rootDir)
let config = EditorConfig.readConfiguration fsharpFile.FSharpFile
config.MaxRecordNumberOfItems == 4
config.RecordMultilineFormatter == NumberOfItems
[<Test>]
let ``record character_width parsing test with single option`` () =
let rootDir = tempName ()
let editorConfig =
"""
[*.fs]
fsharp_max_record_width = 123
"""
use configFixture =
new ConfigurationFile(defaultConfig, rootDir, content = editorConfig)
use fsharpFile = new FSharpFile(rootDir)
let config = EditorConfig.readConfiguration fsharpFile.FSharpFile
config.MaxRecordWidth == 123
[<Test>]
let ``infix operator expression character_width parsing test with single option`` () =
let rootDir = tempName ()
let editorConfig =
"""
[*.fs]
fsharp_max_infix_operator_expression = 123
"""
use configFixture =
new ConfigurationFile(defaultConfig, rootDir, content = editorConfig)
use fsharpFile = new FSharpFile(rootDir)
let config = EditorConfig.readConfiguration fsharpFile.FSharpFile
config.MaxInfixOperatorExpression == 123
[<Test>]
let fsharp_disable_elmish_syntax () =
let rootDir = tempName ()
let editorConfig =
"""
[*.fs]
fsharp_disable_elmish_syntax = true
"""
use configFixture =
new ConfigurationFile(defaultConfig, rootDir, content = editorConfig)
use fsharpFile = new FSharpFile(rootDir)
let config = EditorConfig.readConfiguration fsharpFile.FSharpFile
Assert.IsTrue config.DisableElmishSyntax
[<Test>]
let ``end_of_line = cr should throw`` () =
let rootDir = tempName ()
let editorConfig =
"""
[*.fs]
end_of_line = cr
"""
use configFixture =
new ConfigurationFile(defaultConfig, rootDir, content = editorConfig)
use fsharpFile = new FSharpFile(rootDir)
let ex =
Assert.Throws (fun () ->
EditorConfig.readConfiguration fsharpFile.FSharpFile
|> ignore)
ex.Message
== "Carriage returns are not valid for F# code, please use one of 'lf' or 'crlf'"
let valid_eol_settings =
[ EndOfLineStyle.LF
EndOfLineStyle.CRLF ]
[<TestCaseSource("valid_eol_settings")>]
let can_parse_end_of_line_setting (eol: EndOfLineStyle) =
let rootDir = tempName ()
let editorConfig =
sprintf
"""
[*.fs]
end_of_line = %s
"""
(EndOfLineStyle.ToConfigString eol)
use configFixture =
new ConfigurationFile(defaultConfig, rootDir, content = editorConfig)
use fsharpFile = new FSharpFile(rootDir)
let config = EditorConfig.readConfiguration fsharpFile.FSharpFile
config.EndOfLine == eol
[<Test>]
let fsharp_multiLine_lambda_closing_newline () =
let rootDir = tempName ()
let editorConfig =
"""
[*.fs]
fsharp_multi_line_lambda_closing_newline = true
"""
use configFixture =
new ConfigurationFile(defaultConfig, rootDir, content = editorConfig)
use fsharpFile = new FSharpFile(rootDir)
let config = EditorConfig.readConfiguration fsharpFile.FSharpFile
Assert.IsTrue config.MultiLineLambdaClosingNewline
[<Test>]
let fsharp_keep_indent_in_branch () =
let rootDir = tempName ()
let editorConfig =
"""
[*.fs]
fsharp_keep_indent_in_branch = true
"""
use configFixture =
new ConfigurationFile(defaultConfig, rootDir, content = editorConfig)
use fsharpFile = new FSharpFile(rootDir)
let config = EditorConfig.readConfiguration fsharpFile.FSharpFile
Assert.IsTrue config.KeepIndentInBranch
[<Test>]
let fsharp_bar_before_discriminated_union_declaration () =
let rootDir = tempName ()
let editorConfig =
"""
[*.fs]
fsharp_bar_before_discriminated_union_declaration = true
"""
use configFixture =
new ConfigurationFile(defaultConfig, rootDir, content = editorConfig)
use fsharpFile = new FSharpFile(rootDir)
let config = EditorConfig.readConfiguration fsharpFile.FSharpFile
Assert.IsTrue config.BarBeforeDiscriminatedUnionDeclaration
[<Test>]
let insert_final_newline () =
let rootDir = tempName ()
let editorConfig =
"""
[*.fs]
insert_final_newline = false
"""
use configFixture =
new ConfigurationFile(defaultConfig, rootDir, content = editorConfig)
use fsharpFile = new FSharpFile(rootDir)
let config = EditorConfig.readConfiguration fsharpFile.FSharpFile
Assert.IsFalse config.InsertFinalNewline