Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(plugins/plugin-bash-like): unable to set variable to command output #7609

Merged
merged 1 commit into from
Jun 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions plugins/plugin-bash-like/src/lib/cmds/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,22 @@
*/

import { Arguments, Registrar, SymbolTable, eventBus } from '@kui-shell/core'
import { doExecWithStdoutViaPty } from './catchall'

/**
* export command
*
*/
const exportCommand = ({ tab, parsedOptions }: Arguments) => {
const exportCommand = async (args: Arguments) => {
const { command, tab, parsedOptions } = args
const curDic = SymbolTable.read(tab)

const toBeParsed = parsedOptions._[1]
const arr = toBeParsed.split('=')
const key = arr[0]
const value = arr[1]

const myArgs = Object.assign({}, args, { command: `${command}; echo $${key}` })
const value = await doExecWithStdoutViaPty(myArgs)
curDic[key] = value

SymbolTable.write(tab, curDic)
Expand Down
32 changes: 31 additions & 1 deletion plugins/plugin-bash-like/src/test/bash-like/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ import { Common, CLI, ReplExpect, Selectors, Util } from '@kui-shell/test'
const value1 = 'nnnnnn'
const value2 = 'bar baz'
const value3 = 'mmmmmm'
const value4 = '"$(echo hi) $(echo ho)"'
const _value4 = 'hi ho'
const value5 = '"`echo yo` $(echo yoyo)"'
const _value5 = 'yo yoyo'

describe('export command', function(this: Common.ISuite) {
before(Common.before(this))
Expand All @@ -44,6 +48,8 @@ describe('export command', function(this: Common.ISuite) {
.then(ReplExpect.okWithAny)
.then(() => CLI.command('pwd', this.app).then(ReplExpect.okWithString(process.env.HOME)))
.then(() => CLI.command('printenv HOME', this.app).then(ReplExpect.okWithPtyOutput(home)))
.then(() => CLI.command('export testpath=$HOME/bin', this.app).then(ReplExpect.okWithAny))
.then(() => CLI.command('printenv testpath', this.app).then(ReplExpect.okWithPtyOutput(`${home}/bin`)))
.catch(Common.oops(this))
})

Expand Down Expand Up @@ -91,6 +97,30 @@ describe('export command', function(this: Common.ISuite) {
.catch(Common.oops(this))
)

Common.pit(`should export foo ${value4}`, () =>
CLI.command(`export foo=${value4}`, this.app)
.then(ReplExpect.justOK)
.catch(Common.oops(this))
)

Common.pit('should printenv the new value for foo in the second tab', () =>
CLI.command('printenv foo', this.app)
.then(ReplExpect.okWithPtyOutput(_value4))
.catch(Common.oops(this))
)

Common.pit(`should export foo ${value5}`, () =>
CLI.command(`export foo=${value5}`, this.app)
.then(ReplExpect.justOK)
.catch(Common.oops(this))
)

Common.pit('should printenv the new value for foo in the second tab', () =>
CLI.command('printenv foo', this.app)
.then(ReplExpect.okWithPtyOutput(_value5))
.catch(Common.oops(this))
)

Common.pit('should switch back to the first tab', () =>
CLI.command('tab switch 1', this.app)
.then(() => this.app.client.$(Selectors.TAB_SELECTED_N(1)))
Expand All @@ -113,7 +143,7 @@ describe('export command', function(this: Common.ISuite) {

Common.pit('should show the second-tab value for foo in the second tab', () =>
CLI.command('printenv foo', this.app)
.then(ReplExpect.okWithPtyOutput(value3))
.then(ReplExpect.okWithPtyOutput(_value5))
.catch(Common.oops(this))
)
})