Skip to content

Commit c5c55e3

Browse files
authored
feat: Use config data to build profile menu (#4)
BREAKING CHANGE: - Use config data to build profiles and profile menu - no more adding new config settings in multiple places - add Project Cwd setting - add enable addon settings - disable webgl by default since it is not stable
2 parents 09c8b4a + 8d6abe7 commit c5c55e3

17 files changed

+1068
-1021
lines changed

dist/lib/x-terminal.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/lib/x-terminal.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/config-spec.js

+94-94
Large diffs are not rendered by default.

spec/custom-runner.js

+10
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,14 @@ prunk.suppress(/\.(?:sa|s?c)ss$/)
2626

2727
module.exports = createRunner({
2828
reporter: new SpecReporter(),
29+
}, () => {
30+
const warn = console.warn.bind(console)
31+
beforeEach(() => {
32+
spyOn(console, 'warn').and.callFake((...args) => {
33+
if (args[0].includes('not attached to the DOM')) {
34+
return
35+
}
36+
warn(...args)
37+
})
38+
})
2939
})

spec/element-spec.js

+71-11
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ temp.track()
3434
describe('XTerminalElement', () => {
3535
const savedPlatform = process.platform
3636
this.element = null
37-
this.tmpdirObj = null
37+
this.tmpdir = null
3838

3939
const createNewElement = async (uri = 'x-terminal://somesessionid/') => {
4040
const terminalsSet = new Set()
@@ -100,7 +100,7 @@ describe('XTerminalElement', () => {
100100
})
101101

102102
it('getShellCommand()', () => {
103-
expect(this.element.getShellCommand()).toBe(configDefaults.getDefaultShellCommand())
103+
expect(this.element.getShellCommand()).toBe(configDefaults.command)
104104
})
105105

106106
it('getShellCommand() command set in uri', async () => {
@@ -129,7 +129,7 @@ describe('XTerminalElement', () => {
129129
})
130130

131131
it('getTermType()', () => {
132-
expect(this.element.getTermType()).toBe(configDefaults.getDefaultTermType())
132+
expect(this.element.getTermType()).toBe(configDefaults.termType)
133133
})
134134

135135
it('getTermType() name set in uri', async () => {
@@ -167,7 +167,7 @@ describe('XTerminalElement', () => {
167167

168168
it('getCwd()', async () => {
169169
const cwd = await this.element.getCwd()
170-
expect(cwd).toBe(configDefaults.getDefaultCwd())
170+
expect(cwd).toBe(configDefaults.cwd)
171171
})
172172

173173
it('getCwd() cwd set in uri', async () => {
@@ -179,6 +179,16 @@ describe('XTerminalElement', () => {
179179
expect(cwd).toBe(expected)
180180
})
181181

182+
it('getCwd() ignore cwd in uri if projectCwd is set', async () => {
183+
const expected = await temp.mkdir('projectCwd')
184+
spyOn(atom.project, 'getPaths').and.returnValue([expected])
185+
const params = new URLSearchParams({ projectCwd: true, cwd: this.tmpdir })
186+
const url = new URL('x-terminal://?' + params.toString())
187+
const element = await createNewElement(url.href)
188+
const cwd = await element.getCwd()
189+
expect(cwd).toBe(expected)
190+
})
191+
182192
it('getCwd() model getPath() returns valid path', async () => {
183193
const previousActiveItem = jasmine.createSpyObj(
184194
'previousActiveItem',
@@ -204,7 +214,7 @@ describe('XTerminalElement', () => {
204214
)
205215
const element = await createNewElement()
206216
const cwd = await element.getCwd()
207-
expect(cwd).toBe(configDefaults.getDefaultCwd())
217+
expect(cwd).toBe(configDefaults.cwd)
208218
})
209219

210220
it('getCwd() non-existent cwd set in uri', async () => {
@@ -213,14 +223,14 @@ describe('XTerminalElement', () => {
213223
const url = new URL('x-terminal://?' + params.toString())
214224
await createNewElement(url.href)
215225
const cwd = await this.element.getCwd()
216-
expect(cwd).toBe(configDefaults.getDefaultCwd())
226+
expect(cwd).toBe(configDefaults.cwd)
217227
})
218228

219229
it('getCwd() non-existent project path added', async () => {
220230
spyOn(atom.project, 'getPaths').and.returnValue([path.join(this.tmpdir, 'non-existent-dir')])
221231
const element = await createNewElement()
222232
const cwd = await element.getCwd()
223-
expect(cwd).toBe(configDefaults.getDefaultCwd())
233+
expect(cwd).toBe(configDefaults.cwd)
224234
})
225235

226236
it('getEnv()', () => {
@@ -1054,6 +1064,56 @@ describe('XTerminalElement', () => {
10541064
expect(this.element.ptyProcess).toBeTruthy()
10551065
})
10561066

1067+
describe('loaded addons', () => {
1068+
const { Terminal } = require('xterm')
1069+
const { WebLinksAddon } = require('xterm-addon-web-links')
1070+
const { WebglAddon } = require('xterm-addon-webgl')
1071+
1072+
beforeEach(() => {
1073+
spyOn(Terminal.prototype, 'loadAddon').and.callThrough()
1074+
})
1075+
1076+
it('createTerminal() enable web-link addon', async () => {
1077+
const params = new URLSearchParams({ webLinks: true })
1078+
const url = new URL('x-terminal://?' + params.toString())
1079+
await createNewElement(url.href)
1080+
const wasAdded = Terminal.prototype.loadAddon.calls.all().some(call => {
1081+
return call.args[0] instanceof WebLinksAddon
1082+
})
1083+
expect(wasAdded).toBe(true)
1084+
})
1085+
1086+
it('createTerminal() disable web-link addon', async () => {
1087+
const params = new URLSearchParams({ webLinks: false })
1088+
const url = new URL('x-terminal://?' + params.toString())
1089+
await createNewElement(url.href)
1090+
const wasAdded = Terminal.prototype.loadAddon.calls.all().some(call => {
1091+
return call.args[0] instanceof WebLinksAddon
1092+
})
1093+
expect(wasAdded).toBe(false)
1094+
})
1095+
1096+
it('createTerminal() enable webgl addon', async () => {
1097+
const params = new URLSearchParams({ webgl: true })
1098+
const url = new URL('x-terminal://?' + params.toString())
1099+
await createNewElement(url.href)
1100+
const wasAdded = Terminal.prototype.loadAddon.calls.all().some(call => {
1101+
return call.args[0] instanceof WebglAddon
1102+
})
1103+
expect(wasAdded).toBe(true)
1104+
})
1105+
1106+
it('createTerminal() disable webgl addon', async () => {
1107+
const params = new URLSearchParams({ webgl: false })
1108+
const url = new URL('x-terminal://?' + params.toString())
1109+
await createNewElement(url.href)
1110+
const wasAdded = Terminal.prototype.loadAddon.calls.all().some(call => {
1111+
return call.args[0] instanceof WebglAddon
1112+
})
1113+
expect(wasAdded).toBe(false)
1114+
})
1115+
})
1116+
10571117
it('restartPtyProcess() check new pty process created', async () => {
10581118
const oldPtyProcess = this.element.ptyProcess
10591119
const newPtyProcess = jasmine.createSpyObj('ptyProcess',
@@ -1820,23 +1880,23 @@ describe('XTerminalElement', () => {
18201880
})
18211881

18221882
it('use ctrl+wheelScrollUp font already at maximum', () => {
1823-
this.element.model.profile.fontSize = configDefaults.getMaximumFontSize()
1883+
this.element.model.profile.fontSize = configDefaults.maximumFontSize
18241884
const wheelEvent = new WheelEvent('wheel', {
18251885
deltaY: -150,
18261886
ctrlKey: true,
18271887
})
18281888
this.element.terminalDiv.dispatchEvent(wheelEvent)
1829-
expect(this.element.model.profile.fontSize).toBe(configDefaults.getMaximumFontSize())
1889+
expect(this.element.model.profile.fontSize).toBe(configDefaults.maximumFontSize)
18301890
})
18311891

18321892
it('use ctrl+wheelScrollDown font already at minimum', () => {
1833-
this.element.model.profile.fontSize = configDefaults.getMinimumFontSize()
1893+
this.element.model.profile.fontSize = configDefaults.minimumFontSize
18341894
const wheelEvent = new WheelEvent('wheel', {
18351895
deltaY: 150,
18361896
ctrlKey: true,
18371897
})
18381898
this.element.terminalDiv.dispatchEvent(wheelEvent)
1839-
expect(this.element.model.profile.fontSize).toBe(configDefaults.getMinimumFontSize())
1899+
expect(this.element.model.profile.fontSize).toBe(configDefaults.minimumFontSize)
18401900
})
18411901

18421902
it('getXtermOptions() default options', () => {

spec/model-spec.js

+19-4
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ describe('XTerminalModel', () => {
6868
terminals_set: new Set(),
6969
})
7070
await model.initializedPromise
71-
expect(model.getPath()).toBe(configDefaults.getDefaultCwd())
71+
expect(model.getPath()).toBe(configDefaults.cwd)
7272
})
7373

7474
it('constructor with valid cwd passed in uri', async () => {
@@ -83,6 +83,21 @@ describe('XTerminalModel', () => {
8383
expect(model.getPath()).toBe(this.tmpdir)
8484
})
8585

86+
it('use projectCwd with valid cwd passed in uri', async () => {
87+
const expected = await temp.mkdir('projectCwd')
88+
spyOn(atom.project, 'getPaths').and.returnValue([expected])
89+
spyOn(atom.workspace, 'getActivePaneItem').and.returnValue({})
90+
const url = XTerminalProfilesSingleton.instance.generateNewUrlFromProfileData({})
91+
url.searchParams.set('projectCwd', true)
92+
url.searchParams.set('cwd', this.tmpdir)
93+
const model = new XTerminalModel({
94+
uri: url.href,
95+
terminals_set: new Set(),
96+
})
97+
await model.initializedPromise
98+
expect(model.getPath()).toBe(expected)
99+
})
100+
86101
it('constructor with invalid cwd passed in uri', async () => {
87102
spyOn(atom.workspace, 'getActivePaneItem').and.returnValue({})
88103
const url = XTerminalProfilesSingleton.instance.generateNewUrlFromProfileData({})
@@ -92,7 +107,7 @@ describe('XTerminalModel', () => {
92107
terminals_set: new Set(),
93108
})
94109
await model.initializedPromise
95-
expect(model.getPath()).toBe(configDefaults.getDefaultCwd())
110+
expect(model.getPath()).toBe(configDefaults.cwd)
96111
})
97112

98113
it('constructor with previous active item that has getPath() method', async () => {
@@ -130,7 +145,7 @@ describe('XTerminalModel', () => {
130145
terminals_set: new Set(),
131146
})
132147
await model.initializedPromise
133-
expect(model.getPath()).toBe(configDefaults.getDefaultCwd())
148+
expect(model.getPath()).toBe(configDefaults.cwd)
134149
})
135150

136151
it('constructor with previous active item which exists in project path', async () => {
@@ -277,7 +292,7 @@ describe('XTerminalModel', () => {
277292
})
278293

279294
it('getPath()', () => {
280-
expect(this.model.getPath()).toBe(configDefaults.getDefaultCwd())
295+
expect(this.model.getPath()).toBe(configDefaults.cwd)
281296
})
282297

283298
it('getPath() cwd set', () => {

spec/profile-menu-element-spec.js

-47
Original file line numberDiff line numberDiff line change
@@ -82,45 +82,6 @@ describe('XTerminalProfileMenuElement', () => {
8282
expect(this.element.getModelProfile()).toBe(mock)
8383
})
8484

85-
it('parseJson() array type', () => {
86-
const expected = ['foo']
87-
const actual = this.element.parseJson(
88-
'["foo"]',
89-
null,
90-
Array,
91-
)
92-
expect(actual).toEqual(expected)
93-
})
94-
95-
it('parseJson() object type', () => {
96-
const expected = { foo: 'bar' }
97-
const actual = this.element.parseJson(
98-
'{"foo": "bar"}',
99-
null,
100-
Object,
101-
)
102-
expect(actual).toEqual(expected)
103-
})
104-
105-
it('parseJson() default value', () => {
106-
const expected = ['foo']
107-
const actual = this.element.parseJson(
108-
'null',
109-
expected,
110-
Array,
111-
)
112-
expect(actual).toEqual(expected)
113-
})
114-
115-
it('parseJson() syntax error', () => {
116-
const actual = this.element.parseJson(
117-
'[[',
118-
'foo',
119-
Array,
120-
)
121-
expect(actual).toBe('foo')
122-
})
123-
12485
it('getMenuElements()', () => {
12586
expect(this.element.getMenuElements()).toBeTruthy()
12687
})
@@ -324,14 +285,6 @@ describe('XTerminalProfileMenuElement', () => {
324285
this.element.promptForNewProfileName('foo', 'bar')
325286
})
326287

327-
it('convertNullToEmptyString() value is null', () => {
328-
expect(this.element.convertNullToEmptyString(null)).toBe('')
329-
})
330-
331-
it('convertNullToEmptyString() value is not null', () => {
332-
expect(this.element.convertNullToEmptyString('foo')).toBe('"foo"')
333-
})
334-
335288
it('setNewMenuSettings()', () => {
336289
this.element.setNewMenuSettings(this.element.profilesSingleton.getBaseProfile())
337290
})

0 commit comments

Comments
 (0)