forked from Glavin001/atom-beautify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
beautifier-php-cs-fixer-spec.coffee
144 lines (122 loc) · 4.63 KB
/
beautifier-php-cs-fixer-spec.coffee
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
PHPCSFixer = require "../src/beautifiers/php-cs-fixer"
Beautifier = require "../src/beautifiers/beautifier"
path = require 'path'
# Use the command `window:run-package-specs` (cmd-alt-ctrl-p) to run specs.
#
# To run a specific `it` or `describe` block add an `f` to the front (e.g. `fit`
# or `fdescribe`). Remove the `f` to unfocus the block.
# Check if Windows
isWindows = process.platform is 'win32' or
process.env.OSTYPE is 'cygwin' or
process.env.OSTYPE is 'msys'
describe "PHP-CS-Fixer Beautifier", ->
beforeEach ->
# Activate package
waitsForPromise ->
activationPromise = atom.packages.activatePackage('atom-beautify')
# Force activate package
pack = atom.packages.getLoadedPackage("atom-beautify")
pack.activateNow()
# Change logger level
# atom.config.set('atom-beautify._loggerLevel', 'verbose')
# Return promise
return activationPromise
describe "Beautifier::beautify", ->
beautifier = null
beforeEach ->
beautifier = new PHPCSFixer()
# console.log('new beautifier')
OSSpecificSpecs = ->
text = "<?php echo \"test\"; ?>"
it "should error when beautifier's program not found", ->
expect(beautifier).not.toBe(null)
expect(beautifier instanceof Beautifier).toBe(true)
waitsForPromise shouldReject: true, ->
language = "PHP"
options = {
fixers: ""
levels: ""
}
# Mock spawn
beautifier.spawn = (exe, args, options) ->
# console.log('spawn', exe, args, options)
er = new Error('ENOENT')
er.code = 'ENOENT'
return beautifier.Promise.reject(er)
# Beautify
p = beautifier.beautify(text, language, options)
expect(p).not.toBe(null)
expect(p instanceof beautifier.Promise).toBe(true)
cb = (v) ->
# console.log(v)
expect(v).not.toBe(null)
expect(v instanceof Error).toBe(true, \
"Expected '#{v}' to be instance of Error")
expect(v.code).toBe("CommandNotFound", \
"Expected to be CommandNotFound")
return v
p.then(cb, cb)
return p
failWhichProgram = (failingProgram) ->
it "should error when '#{failingProgram}' not found", ->
expect(beautifier).not.toBe(null)
expect(beautifier instanceof Beautifier).toBe(true)
if not beautifier.isWindows and failingProgram is "php"
# Only applicable on Windows
return
waitsForPromise shouldReject: true, ->
language = "PHP"
options = {
fixers: ""
levels: ""
}
cb = (v) ->
# console.log('cb value', v)
expect(v).not.toBe(null)
expect(v instanceof Error).toBe(true, \
"Expected '#{v}' to be instance of Error")
expect(v.code).toBe("CommandNotFound", \
"Expected to be CommandNotFound")
expect(v.file).toBe(failingProgram)
return v
# which = beautifier.which.bind(beautifier)
beautifier.which = (exe, options) ->
return beautifier.Promise.resolve(null) \
if not exe?
if exe is failingProgram
beautifier.Promise.resolve(failingProgram)
else
# which(exe, options)
# console.log('fake exe path', exe)
beautifier.Promise.resolve("/#{exe}")
oldSpawn = beautifier.spawn.bind(beautifier)
beautifier.spawn = (exe, args, options) ->
# console.log('spawn', exe, args, options)
if exe is failingProgram
er = new Error('ENOENT')
er.code = 'ENOENT'
return beautifier.Promise.reject(er)
else
return beautifier.Promise.resolve({
returnCode: 0,
stdout: 'stdout',
stderr: ''
})
p = beautifier.beautify(text, language, options)
expect(p).not.toBe(null)
expect(p instanceof beautifier.Promise).toBe(true)
p.then(cb, cb)
return p
# failWhichProgram('php')
failWhichProgram('php-cs-fixer')
unless isWindows
describe "Mac/Linux", ->
beforeEach ->
# console.log('mac/linx')
beautifier.isWindows = false
do OSSpecificSpecs
describe "Windows", ->
beforeEach ->
# console.log('windows')
beautifier.isWindows = true
do OSSpecificSpecs