Skip to content

Commit bc21afd

Browse files
committed
Add Executable class to abstract CLI beautifiers
1 parent 8000669 commit bc21afd

10 files changed

+453
-280
lines changed

docs/index.coffee

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ Handlebars.registerHelper('beautifiers-info', (beautifiers, options) ->
157157
rows = _.map(beautifiers, (beautifier, k) ->
158158
name = beautifier.name
159159
isPreInstalled = beautifier.isPreInstalled
160+
if typeof isPreInstalled is "function"
161+
isPreInstalled = beautifier.isPreInstalled()
160162
link = beautifier.link
161163
installationInstructions = if isPreInstalled then "Nothing!" else "Go to #{link} and follow the instructions."
162164
return "| #{name} | #{if isPreInstalled then ':white_check_mark:' else ':x:'} | #{installationInstructions} |"

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,8 @@
179179
"pug-beautify": "^0.1.1",
180180
"remark": "^6.0.1",
181181
"season": "^5.3.0",
182+
"semver": "^5.3.0",
183+
"shell-env": "^0.3.0",
182184
"space-pen": "^5.1.1",
183185
"strip-json-comments": "^2.0.1",
184186
"temp": "^0.8.3",

spec/atom-beautify-spec.coffee

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
Beautifiers = require "../src/beautifiers"
2+
Executable = require "../src/beautifiers/executable"
23
beautifiers = new Beautifiers()
34
Beautifier = require "../src/beautifiers/beautifier"
45
Languages = require('../src/languages/')
@@ -124,15 +125,15 @@ describe "Atom-Beautify", ->
124125
pathOption: "Lang - Test Program Path"
125126
}
126127
# Force to be Windows
127-
beautifier.isWindows = true
128+
Executable.isWindows = () ->true
128129
terminal = 'CMD prompt'
129130
whichCmd = "where.exe"
130131
# Process
131132
p = beautifier.run("program", [], help: help)
132133
expect(p).not.toBe(null)
133134
expect(p instanceof beautifier.Promise).toBe(true)
134135
cb = (v) ->
135-
# console.log(v)
136+
console.log("error", v, v.description)
136137
expect(v).not.toBe(null)
137138
expect(v instanceof Error).toBe(true)
138139
expect(v.code).toBe("CommandNotFound")
@@ -167,7 +168,7 @@ describe "Atom-Beautify", ->
167168
pathOption: "Lang - Test Program Path"
168169
}
169170
# Force to be Mac/Linux (not Windows)
170-
beautifier.isWindows = false
171+
Executable.isWindows = () ->false
171172
terminal = "Terminal"
172173
whichCmd = "which"
173174
# Process

spec/beautifier-php-cs-fixer-spec.coffee

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
PHPCSFixer = require "../src/beautifiers/php-cs-fixer"
22
Beautifier = require "../src/beautifiers/beautifier"
3+
Executable = require "../src/beautifiers/executable"
34
path = require 'path'
45

56
# Use the command `window:run-package-specs` (cmd-alt-ctrl-p) to run specs.
@@ -30,10 +31,15 @@ describe "PHP-CS-Fixer Beautifier", ->
3031
describe "Beautifier::beautify", ->
3132

3233
beautifier = null
34+
execSpawn = null
3335

3436
beforeEach ->
3537
beautifier = new PHPCSFixer()
3638
# console.log('new beautifier')
39+
execSpawn = Executable.prototype.spawn
40+
41+
afterEach ->
42+
Executable.prototype.spawn = execSpawn
3743

3844
OSSpecificSpecs = ->
3945
text = "<?php echo \"test\"; ?>"
@@ -49,13 +55,14 @@ describe "PHP-CS-Fixer Beautifier", ->
4955
levels: ""
5056
}
5157
# Mock spawn
52-
beautifier.spawn = (exe, args, options) ->
58+
# beautifier.spawn
59+
Executable.prototype.spawn = (exe, args, options) ->
5360
# console.log('spawn', exe, args, options)
5461
er = new Error('ENOENT')
5562
er.code = 'ENOENT'
5663
return beautifier.Promise.reject(er)
5764
# Beautify
58-
p = beautifier.beautify(text, language, options)
65+
p = beautifier.loadExecutables().then(() -> beautifier.beautify(text, language, options))
5966
expect(p).not.toBe(null)
6067
expect(p instanceof beautifier.Promise).toBe(true)
6168
cb = (v) ->
@@ -74,7 +81,7 @@ describe "PHP-CS-Fixer Beautifier", ->
7481
expect(beautifier).not.toBe(null)
7582
expect(beautifier instanceof Beautifier).toBe(true)
7683

77-
if not beautifier.isWindows and failingProgram is "php"
84+
if not Executable.isWindows and failingProgram is "php"
7885
# Only applicable on Windows
7986
return
8087

@@ -104,8 +111,9 @@ describe "PHP-CS-Fixer Beautifier", ->
104111
# console.log('fake exe path', exe)
105112
beautifier.Promise.resolve("/#{exe}")
106113

107-
oldSpawn = beautifier.spawn.bind(beautifier)
108-
beautifier.spawn = (exe, args, options) ->
114+
# oldSpawn = beautifier.spawn.bind(beautifier)
115+
# beautifier.spawn
116+
Executable.prototype.spawn = (exe, args, options) ->
109117
# console.log('spawn', exe, args, options)
110118
if exe is failingProgram
111119
er = new Error('ENOENT')
@@ -117,28 +125,28 @@ describe "PHP-CS-Fixer Beautifier", ->
117125
stdout: 'stdout',
118126
stderr: ''
119127
})
120-
p = beautifier.beautify(text, language, options)
128+
p = beautifier.loadExecutables().then(() -> beautifier.beautify(text, language, options))
121129
expect(p).not.toBe(null)
122130
expect(p instanceof beautifier.Promise).toBe(true)
123131
p.then(cb, cb)
124132
return p
125133

126-
# failWhichProgram('php')
127-
failWhichProgram('php-cs-fixer')
134+
failWhichProgram('PHP')
135+
# failWhichProgram('php-cs-fixer')
128136

129137
unless isWindows
130138
describe "Mac/Linux", ->
131139

132140
beforeEach ->
133141
# console.log('mac/linx')
134-
beautifier.isWindows = false
142+
Executable.isWindows = () -> false
135143

136144
do OSSpecificSpecs
137145

138146
describe "Windows", ->
139147

140148
beforeEach ->
141149
# console.log('windows')
142-
beautifier.isWindows = true
150+
Executable.isWindows = () -> true
143151

144152
do OSSpecificSpecs

0 commit comments

Comments
 (0)