Skip to content

Commit

Permalink
fix: use the path of the tree view's selected project as the terminal…
Browse files Browse the repository at this point in the history
… cwd (#51)

Co-authored-by: Spiker985 <[email protected]>
  • Loading branch information
tom-power and Spiker985 authored Aug 28, 2023
1 parent 38bd060 commit bdbdb06
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
43 changes: 38 additions & 5 deletions spec/model-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,15 @@ describe('XTerminalModel', () => {
expect(model.getPath()).toBe(tmpdir)
})

it('constructor with previous active item that has getPath() method returns file path', async () => {
it('constructor with previous active item that has selectedPath property', async () => {
const previousActiveItem = jasmine.createSpyObj('somemodel', {}, { selectedPath: '' })
Object.getOwnPropertyDescriptor(previousActiveItem, 'selectedPath').get.and.returnValue(tmpdir)
spyOn(atom.workspace, 'getActivePaneItem').and.returnValue(previousActiveItem)
const model = await createNewModel({ projectCwd: true })
expect(model.getPath()).toBe(tmpdir)
})

it('constructor with previous active item that has getPath() method that returns file path', async () => {
const previousActiveItem = jasmine.createSpyObj('somemodel', ['getPath'])
const filePath = path.join(tmpdir, 'somefile')
await fs.writeFile(filePath, '')
Expand All @@ -114,6 +122,16 @@ describe('XTerminalModel', () => {
expect(model.getPath()).toBe(tmpdir)
})

it('constructor with previous active item that has selectedPath() property that returns file path', async () => {
const previousActiveItem = jasmine.createSpyObj('somemodel', {}, { selectedPath: '' })
const filePath = path.join(tmpdir, 'somefile')
await fs.writeFile(filePath, '')
Object.getOwnPropertyDescriptor(previousActiveItem, 'selectedPath').get.and.returnValue(filePath)
spyOn(atom.workspace, 'getActivePaneItem').and.returnValue(previousActiveItem)
const model = await createNewModel({ projectCwd: true })
expect(model.getPath()).toBe(tmpdir)
})

it('constructor with previous active item that has getPath() returning invalid path', async () => {
const previousActiveItem = jasmine.createSpyObj('somemodel', ['getPath'])
previousActiveItem.getPath.and.returnValue(path.join(tmpdir, 'non-existent-dir'))
Expand All @@ -122,13 +140,28 @@ describe('XTerminalModel', () => {
expect(model.getPath()).toBe(configDefaults.cwd)
})

it('constructor with previous active item which exists in project path', async () => {
it('constructor with previous active item that has selectedPath returning invalid path', async () => {
const previousActiveItem = jasmine.createSpyObj('somemodel', {}, { selectedPath: '' })
Object.getOwnPropertyDescriptor(previousActiveItem, 'selectedPath').get.and.returnValue(path.join(tmpdir, 'non-existent-dir'))
spyOn(atom.workspace, 'getActivePaneItem').and.returnValue(previousActiveItem)
const model = await createNewModel({ projectCwd: true })
expect(model.getPath()).toBe(configDefaults.cwd)
})

it('constructor with previous active item which exists in project path and calls getPath', async () => {
const previousActiveItem = jasmine.createSpyObj('somemodel', ['getPath'])
spyOn(atom.workspace, 'getActivePaneItem').and.returnValue(previousActiveItem)
const expected = ['/some/dir', null]
spyOn(atom.project, 'relativizePath').and.returnValue(expected)
spyOn(atom.project, 'relativizePath').and.returnValue(['/some/dir', null])
const model = await createNewModel({ projectCwd: true })
expect(model.getPath()).toBe('/some/dir')
})

it('constructor with previous active item which exists in project path and calls selectedPath', async () => {
const previousActiveItem = jasmine.createSpyObj('somemodel', {}, { selectedPath: '' })
spyOn(atom.workspace, 'getActivePaneItem').and.returnValue(previousActiveItem)
spyOn(atom.project, 'relativizePath').and.returnValue(['/some/dir', null])
const model = await createNewModel({ projectCwd: true })
expect(model.getPath()).toBe(expected[0])
expect(model.getPath()).toBe('/some/dir')
})

it('constructor with custom title', async () => {
Expand Down
7 changes: 7 additions & 0 deletions src/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ class XTerminalModel {
this.profile.cwd = dir
return
}
} else if (typeof previousActiveItem !== 'undefined' && typeof previousActiveItem.selectedPath === 'string') {
cwd = previousActiveItem.selectedPath
const dir = atom.project.relativizePath(cwd)[0]
if (dir) {
this.profile.cwd = dir
return
}
} else {
cwd = atom.project.getPaths()[0]
}
Expand Down

0 comments on commit bdbdb06

Please sign in to comment.