From 4bccfc4e97fee0116a77d6587655aa47aff78408 Mon Sep 17 00:00:00 2001 From: Eduardo Kitamura Manrique Date: Sun, 28 Oct 2018 17:44:43 +0100 Subject: [PATCH] Fix source built in command --- src/shell/BuiltInCommands.ts | 33 ++++++++++++++++---------- test/e2e.ts | 17 +++++++++++++ test/test_files/scripts/test_source.sh | 1 + 3 files changed, 38 insertions(+), 13 deletions(-) create mode 100644 test/test_files/scripts/test_source.sh diff --git a/src/shell/BuiltInCommands.ts b/src/shell/BuiltInCommands.ts index d71c3beef..f69b42a6a 100644 --- a/src/shell/BuiltInCommands.ts +++ b/src/shell/BuiltInCommands.ts @@ -7,6 +7,7 @@ import {Session} from "./Session"; import {OrderedSet} from "../utils/OrderedSet"; import {parseAlias} from "./Aliases"; import {stringLiteralValue} from "./Scanner"; +import {exec} from "child_process"; const executors: Dictionary<(i: Job, a: string[]) => void> = { cd: (job: Job, args: string[]): void => { @@ -52,9 +53,13 @@ const executors: Dictionary<(i: Job, a: string[]) => void> = { }); } }, - // FIXME: make the implementation more reliable. source: (job: Job, args: string[]): void => { - sourceFile(job.session, args[0]); + const dir = job.session.directory; + sourceFile(job.session, args[0]) + .then(() => executors.cd(job, [dir])) + .catch((err) => { + job.output.write(err); + }); }, alias: (job: Job, args: string[]): void => { if (args.length === 0) { @@ -86,17 +91,19 @@ const executors: Dictionary<(i: Job, a: string[]) => void> = { }; export function sourceFile(session: Session, fileName: string) { - const content = readFileSync(resolveFile(session.directory, fileName)).toString(); - - content.split(EOL).forEach(line => { - if (line.startsWith("export ")) { - const [variableName, variableValueLiteral] = line.split(" ")[1].split("="); - - const variableValue = stringLiteralValue(variableValueLiteral); - if (variableValue) { - session.environment.set(variableName, variableValue); - } - } + return new Promise((resolve, reject) => { + const cmd = `source ${resolveFile(session.directory, fileName)}; env`; + const script = exec(cmd); + script.stdout.on("data", (data: string) => { + data.split(EOL).forEach((line: string) => { + const [key, value] = line.split("="); + session.environment.set(key, value); + }); + resolve(); + }); + script.stderr.on("data", function(data) { + reject(data); + }); }); } diff --git a/test/e2e.ts b/test/e2e.ts index 327798cae..7b80ab811 100644 --- a/test/e2e.ts +++ b/test/e2e.ts @@ -100,4 +100,21 @@ describe("application launch", function () { expect(await page.footer.presentDirectory.getText()).to.eql(newDirectory); }); }); + + describe("source", () => { + it("Valid source command", async () => { + const sourceFile = userFriendlyPath(__dirname + "/test_files/scripts/test_source.sh"); + await page.executeCommand(`source ${sourceFile}`); + await page.executeCommand(`echo $test_source_built_in`); + const output = await page.job.output.getText(); + expect(output.trim()).to.eq("OK"); + }); + it("Invalid source command", async () => { + await page.executeCommand(`cd ${__dirname}`); + const sourceFile = __dirname + "/invalid"; + await page.executeCommand(`source invalid`); + const output = await page.job.output.getText(); + expect(output.trim()).to.eq(`/bin/sh: ${sourceFile}: No such file or directory`); + }); + }); }); diff --git a/test/test_files/scripts/test_source.sh b/test/test_files/scripts/test_source.sh new file mode 100644 index 000000000..b2d3d9575 --- /dev/null +++ b/test/test_files/scripts/test_source.sh @@ -0,0 +1 @@ +export test_source_built_in=OK \ No newline at end of file