smc-hub
Version:
CoCalc: Backend webserver component
126 lines (119 loc) • 4.1 kB
JavaScript
// Generated by CoffeeScript 2.5.1
(function() {
//########################################################################
// This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
// License: AGPLv3 s.t. "Commons Clause" – see LICENSE.md for details
//########################################################################
/*
Using API to run shell command in project
*/
var api, expect, setup, teardown;
api = require('./apitest');
({setup, teardown} = api);
expect = require('expect');
describe('runs shell command in a project', function() {
var project_id;
before(setup);
after(teardown);
project_id = void 0;
it("creates test project", function(done) {
return api.call({
event: 'create_project',
body: {
title: 'ADVTEST',
description: 'Testing advanced API'
},
cb: function(err, resp) {
expect(resp != null ? resp.event : void 0).toBe('project_created');
project_id = resp.project_id;
return done(err);
}
});
});
it("does shell built-in", function(done) {
this.timeout(30000);
return api.call({
event: 'project_exec',
body: {
project_id: project_id,
command: 'pwd'
},
cb: function(err, resp) {
var re, ref;
expect(resp != null ? resp.event : void 0).toBe('project_exec_output');
re = new RegExp(project_id + '$');
expect(resp != null ? (ref = resp.stdout) != null ? ref.trim() : void 0 : void 0).toMatch(re);
return done(err);
}
});
});
it("runs command in different working directory", function(done) {
return api.call({
event: 'project_exec',
body: {
project_id: project_id,
command: 'pwd',
path: '/etc'
},
cb: function(err, resp) {
var ref;
expect(resp != null ? resp.event : void 0).toBe('project_exec_output');
expect(resp != null ? (ref = resp.stdout) != null ? ref.trim() : void 0 : void 0).toBe('/etc');
return done(err);
}
});
});
it("runs command with args option", function(done) {
return api.call({
event: 'project_exec',
body: {
project_id: project_id,
command: 'echo',
args: ['hello', 'world']
},
cb: function(err, resp) {
var ref;
expect(resp != null ? resp.event : void 0).toBe('project_exec_output');
expect(resp != null ? (ref = resp.stdout) != null ? ref.trim() : void 0 : void 0).toBe('hello world');
return done(err);
}
});
});
it("limits output to 5 characters", function(done) {
return api.call({
event: 'project_exec',
body: {
project_id: project_id,
command: 'echo',
args: ['hello', 'world'],
max_output: 5
},
cb: function(err, resp) {
var ref, ref1, ref2;
expect(resp != null ? resp.event : void 0).toBe('project_exec_output');
expect(resp != null ? (ref = resp.stdout) != null ? ref.trim() : void 0 : void 0).toMatch('hello');
expect(resp != null ? (ref1 = resp.stdout) != null ? ref1.trim() : void 0 : void 0).toNotMatch('world');
expect(resp != null ? (ref2 = resp.stdout) != null ? ref2.trim() : void 0 : void 0).toMatch('truncated at 5 characters');
return done(err);
}
});
});
return it("sets execution timeout", function(done) {
this.timeout(10000);
return api.call({
event: 'project_exec',
body: {
project_id: project_id,
command: 'sleep 5;echo done',
timeout: 2
},
cb: function(err, resp) {
expect(resp != null ? resp.event : void 0).toBe('error');
expect(resp != null ? resp.error : void 0).toMatch('killed command');
return done(err);
}
});
});
});
}).call(this);
//# sourceMappingURL=project_exec.js.map