Skip to content

Commit

Permalink
fix: use cwd param for target (#227)
Browse files Browse the repository at this point in the history
  • Loading branch information
UziTech committed Dec 3, 2020
1 parent 32fd45b commit 309619f
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 35 deletions.
19 changes: 3 additions & 16 deletions spec/element-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,24 +178,11 @@ describe('XTerminalElement', () => {
expect(cwd).toBe(expected)
})

it('getCwd() cwd set if target in uri', async () => {
const expected = tmpdir
const element = await createNewElement(null, { target: expected })
const cwd = await element.getCwd()
expect(cwd).toBe(expected)
})

it('getCwd() ignore cwd in target if projectCwd is set', async () => {
const expected = await temp.mkdir('targetCwd')
const element = await createNewElement({ cwd: tmpdir }, { target: expected })
const cwd = await element.getCwd()
expect(cwd).toBe(expected)
})

it('getCwd() ignore cwd in uri if projectCwd is set', async () => {
it('getCwd() ignore cwd base profile if projectCwd is set', async () => {
atom.config.set('x-terminal.spawnPtySettings.cwd', tmpdir)
const expected = await temp.mkdir('projectCwd')
spyOn(atom.project, 'getPaths').and.returnValue([expected])
const element = await createNewElement({ projectCwd: true, cwd: tmpdir })
const element = await createNewElement({ projectCwd: true })
const cwd = await element.getCwd()
expect(cwd).toBe(expected)
})
Expand Down
9 changes: 2 additions & 7 deletions spec/model-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,12 @@ describe('XTerminalModel', () => {
expect(model.getPath()).toBe(tmpdir)
})

it('use target with valid cwd passed in uri', async () => {
const expected = await temp.mkdir('targetCwd')
const model = await createNewModel({ cwd: tmpdir }, { target: expected })
expect(model.getPath()).toBe(expected)
})

it('use projectCwd with valid cwd passed in uri', async () => {
atom.config.set('x-terminal.spawnPtySettings.cwd', tmpdir)
const expected = await temp.mkdir('projectCwd')
spyOn(atom.project, 'getPaths').and.returnValue([expected])
spyOn(atom.workspace, 'getActivePaneItem').and.returnValue({})
const model = await createNewModel({ projectCwd: true, cwd: tmpdir })
const model = await createNewModel({ projectCwd: true })
expect(model.getPath()).toBe(expected)
})

Expand Down
27 changes: 27 additions & 0 deletions spec/x-terminal-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,4 +240,31 @@ describe('x-terminal', () => {
expect(activeTerminal.clear).toHaveBeenCalled()
})
})

describe('open()', () => {
let uri
beforeEach(() => {
uri = xTerminalInstance.profilesSingleton.generateNewUri()
spyOn(atom.workspace, 'open')
})

it('simple', async () => {
await xTerminalInstance.open(uri)

expect(atom.workspace.open).toHaveBeenCalledWith(uri, {})
})

it('target to cwd', async () => {
const testPath = '/test/path'
spyOn(xTerminalInstance, 'getPath').and.returnValue(testPath)
await xTerminalInstance.open(
uri,
{ target: true },
)

const url = new URL(atom.workspace.open.calls.mostRecent().args[0])

expect(url.searchParams.get('cwd')).toBe(testPath)
})
})
})
5 changes: 3 additions & 2 deletions src/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class XTerminalModel {
this.uri = this.options.uri
const url = new URL(this.uri)
this.sessionId = url.host
this.uriCwd = url.searchParams.get('cwd')
this.profilesSingleton = XTerminalProfilesSingleton.instance
this.profile = this.profilesSingleton.createProfileDataFromUri(this.uri)
this.terminals_set = this.options.terminals_set
Expand Down Expand Up @@ -73,8 +74,8 @@ class XTerminalModel {
async initialize () {
let cwd

if (this.options.target) {
cwd = this.options.target
if (this.uriCwd) {
cwd = this.uriCwd
} else if (this.profile.projectCwd) {
const previousActiveItem = atom.workspace.getActivePaneItem()
if (typeof previousActiveItem !== 'undefined' && typeof previousActiveItem.getPath === 'function') {
Expand Down
13 changes: 3 additions & 10 deletions src/x-terminal.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,8 @@ class XTerminalSingleton {
// Add opener for terminal emulator item.
atom.workspace.addOpener((uri) => {
if (uri.startsWith(X_TERMINAL_BASE_URI)) {
const { searchParams } = new URL(uri)
const target = searchParams.get('target')
searchParams.delete('target')

const item = new XTerminalModel({
uri,
target,
terminals_set: this.terminals_set,
})
return item
Expand Down Expand Up @@ -441,18 +436,16 @@ class XTerminalSingleton {

async open (uri, options = {}) {
const url = new URL(uri)
let relaunchTerminalOnStartup = url.searchParams.get('relaunchTerminalOnStartup')
if (relaunchTerminalOnStartup === null) {
relaunchTerminalOnStartup = this.profilesSingleton.getBaseProfile().relaunchTerminalOnStartup
if (!relaunchTerminalOnStartup) {
if (url.searchParams.get('relaunchTerminalOnStartup') === null) {
if (!this.profilesSingleton.getBaseProfile().relaunchTerminalOnStartup) {
url.searchParams.set('relaunchTerminalOnStartup', false)
}
}

if (options.target) {
const target = this.getPath(options.target)
if (target) {
url.searchParams.set('target', target)
url.searchParams.set('cwd', target)
}
}

Expand Down

0 comments on commit 309619f

Please sign in to comment.