Skip to content
This repository has been archived by the owner on May 21, 2019. It is now read-only.

Fix source built in command #1317

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
33 changes: 20 additions & 13 deletions src/shell/BuiltInCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
});
});
}

Expand Down
17 changes: 17 additions & 0 deletions test/e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
});
});
});
1 change: 1 addition & 0 deletions test/test_files/scripts/test_source.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export test_source_built_in=OK