Skip to content

Commit

Permalink
Add removeFile to interactive compute sessions. Closes #1844 (#1847)
Browse files Browse the repository at this point in the history
  • Loading branch information
brollb authored Aug 6, 2020
1 parent 38119f0 commit 91e2b32
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/common/compute/interactive/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
}
}(this, function() {
const Constants = makeEnum('STDOUT', 'STDERR', 'RUN', 'ADD_ARTIFACT',
'ADD_FILE', 'ADD_USER_DATA', 'COMPLETE', 'ERROR', 'SET_ENV');
'ADD_FILE', 'REMOVE_FILE', 'ADD_USER_DATA', 'COMPLETE', 'ERROR', 'SET_ENV');

function makeEnum() {
const names = Array.prototype.slice.call(arguments);
Expand Down
7 changes: 7 additions & 0 deletions src/common/compute/interactive/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,13 @@ define([
await this.runTask(task);
}

async removeFile(filepath) {
this.ensureIdle('remove file');
const msg = new Message(Message.REMOVE_FILE, [filepath]);
const task = new Task(this.ws, msg);
await this.runTask(task);
}

async setEnvVar(name, value) {
this.ensureIdle('set env var');
const msg = new Message(Message.SET_ENV, [name, value]);
Expand Down
20 changes: 20 additions & 0 deletions src/routers/InteractiveCompute/job-files/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,33 @@ class InteractiveClient {
} else if (msg.type === Message.ADD_FILE) {
this.runTask(() => {
const [filepath, content] = msg.data;
this.ensureValidPath(filepath);
this.writeFile(filepath, content);
});
} else if (msg.type === Message.SET_ENV) {
this.runTask(() => {
const [name, value] = msg.data;
process.env[name] = value;
});
} else if (msg.type === Message.REMOVE_FILE) {
this.runTask(async () => {
const [filepath] = msg.data;
this.ensureValidPath(filepath);
await fsp.unlink(filepath);
});
} else {
this.sendMessage(Message.COMPLETE, 2);
}
}

ensureValidPath(filepath) {
const isOutsideWorkspace = path.relative(
path.resolve(__dirname),
path.resolve(filepath)
).startsWith('..');

if (isOutsideWorkspace) {
throw new Error('Cannot edit files outside workspace: ' + filepath);
}
}

Expand Down
11 changes: 11 additions & 0 deletions test/integration/InteractiveCompute.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,15 @@ describe('InteractiveCompute', function() {
const {stdout} = await session.exec('cat test.txt');
assert.equal(stdout, 'hello world');
});

it('should remove file', async function() {
try {
await session.addFile('test.txt', 'hello world');
await session.removeFile('test.txt');
const {stdout} = await session.exec('cat test.txt');
assert(false);
} catch (err) {
assert(err.jobResult.stderr.includes('No such file'));
}
});
});

0 comments on commit 91e2b32

Please sign in to comment.