Skip to content

Commit d566581

Browse files
author
Andres
committed
Global env variables #393
1 parent c1d7a4d commit d566581

File tree

8 files changed

+29
-46
lines changed

8 files changed

+29
-46
lines changed

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
* Overhaul of menu and context menu options
66
* Suite-related actions moved to context menu
77
* "Import suite" is not part of settings
8-
* Global Env variables in settings
8+
* Global Env variables can be set in settings
9+
* Variable env form validation
910
* Adds cut-copy-paste items to context menu
1011
* Settings shortcuts menu is now collapsed by default
1112
* Minor fix in "Desktopify"

src/app/common/suite.js

-6
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,6 @@ class Suite {
4949
return Promise.all(promises);
5050
}
5151

52-
runAll() {
53-
for (const task of this.tasks) {
54-
if (!task.isRunning()) task.run();
55-
}
56-
}
57-
5852
setTitle(title) {
5953
this.title = utils.truncate(title || "", constants.maxSuiteNameLength);
6054
}

src/app/common/task.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const yerbamate = require('yerbamate');
55

66
const TaskStatus = require('./task_status');
77
const {TaskTimer, InverseTaskTimer} = require('./task_timer');
8+
const EnvVariableHandler = require('../api/env_variables_handler');
89

910
const outputMaxSize = 6000;
1011

@@ -13,7 +14,7 @@ class Task {
1314
this.title = options.title.trim() || "";
1415
this.command = options.command || "";
1516
this.path = options.path || "";
16-
this.env = options.env || [];
17+
this.env = EnvVariableHandler.filterInvalidEnvVariables(options.env || []);
1718
this.status = TaskStatus.idle;
1819
this.scheduled = false;
1920

@@ -28,7 +29,7 @@ class Task {
2829
return this.timer.elapsedSeconds;
2930
}
3031

31-
run(done) {
32+
run(globalVariables, done) {
3233
this._clearSchedulerTimeout();
3334
if (this.isRunning()) {
3435
throw new Error("Trying to run task without stopping it first");
@@ -47,7 +48,7 @@ class Task {
4748
stderr: onOutput,
4849
stdout: onOutput,
4950
maxOutputSize: 1,
50-
env: this._getEnvVariables()
51+
env: this._getEnvVariablesForExecution(globalVariables)
5152
},
5253
(code) => {
5354
if (this.status !== TaskStatus.stopped) this.status = yerbamate.successCode(code) ? TaskStatus.ok : TaskStatus.error;
@@ -80,7 +81,7 @@ class Task {
8081
title: this.title,
8182
command: this.command
8283
};
83-
if (this.env && this.env.length > 0) res.env = this.env;
84+
if (this.env && this.env.length > 0) res.env = this.env;// EnvVariableHandler.filterInvalidEnvVariables(this.env);
8485
if (this.path !== "") res.path = this.path;
8586
return res;
8687
}
@@ -127,8 +128,9 @@ class Task {
127128
this.scheduled = false;
128129
}
129130

130-
_getEnvVariables() {
131-
const res = this.env.reduce((acc, envVar) => {
131+
_getEnvVariablesForExecution(globalVariables) {
132+
const envVariables = globalVariables.concat(this.env); // Local env variables will override global env
133+
const res = envVariables.reduce((acc, envVar) => {
132134
if (!envVar[0] || !envVar[1]) return acc;
133135
acc[envVar[0]] = envVar[1];
134136
return acc;

src/app/common/task_importer.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,23 @@
22

33
const fs = require('fs');
44

5+
const EnvVariableHandler = require('../api/env_variables_handler');
6+
57
module.exports = {
6-
parseToJson(suites, version) {
8+
parseToJson(suites, version, globalEnv) {
79
const parsedSuites = suites.map((suite) => suite.getData());
810
const content = {
911
"suites": parsedSuites,
1012
"version": version
1113
};
14+
if (globalEnv) {
15+
content.globalEnv = EnvVariableHandler.filterInvalidEnvVariables(globalEnv);
16+
}
1217
return JSON.stringify(content);
1318
},
14-
export(filename, suites, version) {
19+
export(filename, suites, version, globalEnv) {
1520
return new Promise((resolve, reject) => {
16-
const content = this.parseToJson(suites, version);
21+
const content = this.parseToJson(suites, version, globalEnv);
1722
fs.writeFile(filename, content, (err) => {
1823
if (err) reject(err);
1924
else resolve();

src/app/components/settings/settings_actions.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<div class="columns is-centered">
33
<div class="column is-two-thirds">
44
<button-item @select="resetSettings">Reset Settings</button-item>
5-
<button-item @select="exportTasks">Export Tasks</button-item>
5+
<button-item @select="exportTasks" label="Export all suites and global env variables to a file">Export Tasks</button-item>
66
<button-item @select="importTasks" label="Warning: This will override your previous tasks">Import Tasks</button-item>
77
<button-item @select="importSuite" :disabled="!canImportSuite">Import Suite</button-item>
88
<button-item @select="clearTasks" label="Warning: This will remove all your suites and tasks">Clear Tasks</button-item>

src/app/store/task_store.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ module.exports = {
135135
});
136136
},
137137
importTasks(context, filename) {
138+
// TODO: import global env
138139
return TaskImporter.import(filename).then((data) => {
139140
const loadedSuites = TasksHandler.loadTasksFromData(data);
140141
return context.dispatch("stopAllTasks").then(() => {
@@ -152,7 +153,7 @@ module.exports = {
152153
}
153154
},
154155
exportTasks(context, filename) {
155-
return TaskImporter.export(filename, context.getters.suites, context.getters.version);
156+
return TaskImporter.export(filename, context.getters.suites, context.getters.version, context.state.globalEnv);
156157
},
157158
exportSuite(context, {filename, suiteIndex}) {
158159
const suite = context.getters.suites[suiteIndex];
@@ -166,9 +167,10 @@ module.exports = {
166167
},
167168
runTask(context, index) {
168169
const task = context.getters.currentSuite.getTask(index);
170+
const globalEnv = context.state.globalEnv;
169171
if (!task.isRunning() && !task.isScheduled()) {
170172
context.commit('increaseRunningTasks');
171-
task.run(() => {
173+
task.run(globalEnv, () => {
172174
context.commit('decreaseRunningTasks');
173175
});
174176
}

tests/suite.test.js

-21
Original file line numberDiff line numberDiff line change
@@ -55,27 +55,6 @@ describe("Suite", () => {
5555
assert.strictEqual(testSuite.length, 0);
5656
});
5757

58-
it("Run Tasks", () => {
59-
const taskStub2 = new Task({
60-
title: "test2",
61-
command: "command"
62-
});
63-
createStub(taskStub2);
64-
taskStub2.status = TaskStatus.running;
65-
66-
testSuite.addTask(taskStub);
67-
testSuite.addTask(taskStub2);
68-
assert.isFalse(taskStub.isRunning());
69-
assert.isTrue(taskStub2.isRunning());
70-
testSuite.runAll();
71-
assert.isTrue(taskStub.isRunning());
72-
assert.isTrue(taskStub2.isRunning());
73-
assert.isTrue(taskStub.run.called);
74-
assert.isFalse(taskStub2.run.called);
75-
76-
restoreStub(taskStub2);
77-
});
78-
7958
it("Stop All Tasks", () => {
8059
const taskStub2 = new Task({
8160
title: "test2",

tests/task.test.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ describe("Tasks", () => {
3333
assert.isFalse(testTask.isRunning());
3434
assert.strictEqual(testTask.status, TaskStatus.idle);
3535

36-
testTask.run(() => {
36+
testTask.run([], () => {
3737
assert.isFalse(testTask.isRunning());
3838
assert.strictEqual(testTask.status, TaskStatus.ok);
3939
done();
@@ -74,7 +74,7 @@ describe("Tasks", () => {
7474
command: "invalidTask"
7575
});
7676

77-
invalidTask.run(() => {
77+
invalidTask.run([], () => {
7878
assert.strictEqual(invalidTask.status, TaskStatus.error);
7979
done();
8080
});
@@ -86,7 +86,7 @@ describe("Tasks", () => {
8686
});
8787
assert.isNull(testTask.elapsedTime);
8888

89-
testTask.run(() => {
89+
testTask.run([], () => {
9090
// Do nothing
9191
});
9292
assert.doesNotThrow(() => {
@@ -96,7 +96,7 @@ describe("Tasks", () => {
9696
});
9797

9898
it("Stop task", (done) => {
99-
testTask.run(() => {
99+
testTask.run([], () => {
100100
assert.isFalse(testTask.isRunning());
101101
assert.strictEqual(testTask.status, TaskStatus.stopped);
102102
testTask.stop();
@@ -110,13 +110,13 @@ describe("Tasks", () => {
110110
assert.isFalse(testTask.isRunning());
111111
assert.strictEqual(testTask.status, TaskStatus.idle);
112112

113-
testTask.run(() => {
113+
testTask.run([], () => {
114114
assert.isFalse(testTask.isRunning());
115115
assert.strictEqual(testTask.status, TaskStatus.ok);
116116
done();
117117
});
118118
assert.isTrue(testTask.isRunning());
119-
assert.throws(() => testTask.run(() => {
119+
assert.throws(() => testTask.run([], () => {
120120
// Do nothing
121121
}));
122122
});

0 commit comments

Comments
 (0)