Phedenskog has uploaded a new change for review.
https://gerrit.wikimedia.org/r/239080
Change subject: Make WPT script files use placeholders.
......................................................................
Make WPT script files use placeholders.
Replace placeholders looking like <%NAME>
in WPT script files and batch file with corresponding
node env variable. The placeholder <%NAME> will
be replaced with the value of process.env.NAME
If there's no match, there's an error log message.
Bug: T112733
Change-Id: I1d4e3a62922f05da2093a9dc9e779ce0694bb310
---
M lib/cli.js
M lib/index.js
M lib/util.js
A test/files/scriptingWithEnv.txt
M test/utilTest.js
5 files changed, 105 insertions(+), 58 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/performance/WebPageTest
refs/changes/80/239080/1
diff --git a/lib/cli.js b/lib/cli.js
index c7f75e2..e0e3612 100644
--- a/lib/cli.js
+++ b/lib/cli.js
@@ -82,7 +82,8 @@
getInputURLorFile: function(arg) {
// is it a file or URL we wanna test?
if (arg.indexOf('http') === -1) {
- return util.readFile(arg);
+ var fileContent = util.readFile(arg);
+ return util.replaceWithEnv(fileContent);
} else {
return arg;
}
diff --git a/lib/index.js b/lib/index.js
index 87c493a..378dfb7 100644
--- a/lib/index.js
+++ b/lib/index.js
@@ -43,7 +43,11 @@
lines.forEach(function(line) {
// only run tests where we have something on that line
if (line.indexOf('#') !== 0 && line.length > 1) {
- var myArgs = minimist(line.split(' '), {
+
+ var replaced = util.replaceWithEnv(line);
+ console.log(replaced);
+
+ var myArgs = minimist(replaced.split(' '), {
boolean: ['sendMetrics','verbose']
});
diff --git a/lib/util.js b/lib/util.js
index 97d5c03..6be9884 100644
--- a/lib/util.js
+++ b/lib/util.js
@@ -83,6 +83,23 @@
path.sep, filename);
return fs.readFileSync(fullPathToFile, 'utf-8');
},
+ replaceWithEnv: function(text) {
+ var matches = text.match(/<(.*?)>/g);
+ if (matches) {
+ matches.forEach(function(match) {
+ // do we have a matching ENV?
+ var env = match.substring(2, match.length - 1);
+ if (process.env[env]) {
+ text = text.replace(match, process.env[env]);
+ } else {
+ console.error('No ENV set for ' + env + ' the expr ' +
match +
+ ' will not be replaced');
+ }
+
+ });
+ }
+ return text;
+ },
collectMetrics: function(wptJson, userStatus, namespace) {
var self = this;
diff --git a/test/files/scriptingWithEnv.txt b/test/files/scriptingWithEnv.txt
new file mode 100644
index 0000000..c27a1a8
--- /dev/null
+++ b/test/files/scriptingWithEnv.txt
@@ -0,0 +1,14 @@
+logData 0
+
+// https://sites.google.com/a/webpagetest.org/docs/using-webpagetest/scripting
+
+// put any urls you want to navigate
+navigate <%MY_URL>
+
+logData 1
+
+// this step will get recorded
+navigate <%MY_SECOND_URL>
+
+// and then navigate back
+navigate <%MY_URL>
diff --git a/test/utilTest.js b/test/utilTest.js
index 47f335a..f2b49b8 100644
--- a/test/utilTest.js
+++ b/test/utilTest.js
@@ -26,83 +26,94 @@
describe('Test util', function() {
- it('WebPageTest options should be added', function() {
+ it('WebPageTest options should be added', function() {
- var args = {
- location: 'ap-northeast-1_IE10',
- connectivity: '3G'
- };
- var wptOptions = util.setupWPTOptions(args);
- assert.deepEqual(wptOptions.location, 'ap-northeast-1_IE10');
- assert.deepEqual(wptOptions.connectivity, '3G');
- });
-
- it('Parameters specific for wptstatsv should be cleaned out from WebPageTest
options', function() {
-
- var keysToBeRemoved = ['webPageTestKey', 'webPageTestHost', '_',
'verbose', 'userStatus', 'sendMetrics', 'customMetrics', 'namespace'];
- var args = {
- webPageTestKey: 'aSupERSecrEtKey',
- webPageTestHost: 'http://our.wpt.org',
- _: ['all', 'extra', 'args'],
- verbose: true,
- userStatus: 'anonymous',
- dryRun: true,
- customMetrics: 'javascript that collects custom metrics',
- namespace: 'super.special.namespace'
- };
-
- var wptOptions = util.setupWPTOptions(args);
- keysToBeRemoved.forEach(function(key) {
- assert.strictEqual(wptOptions[key], undefined);
+ var args = {
+ location: 'ap-northeast-1_IE10',
+ connectivity: '3G'
+ };
+ var wptOptions = util.setupWPTOptions(args);
+ assert.deepEqual(wptOptions.location, 'ap-northeast-1_IE10');
+ assert.deepEqual(wptOptions.connectivity, '3G');
});
- });
+ it('Parameters specific for wptstatsv should be cleaned out from
WebPageTest options', function() {
- it('We should be able to parse a JSON from WebPageTest collecting data from
mobile', function() {
- var userStatus = 'anonymous';
- var namespace = 'webpagetest';
- var metrics = util.collectMetrics(mobileJson, userStatus, namespace);
- metrics.forEach(function(metric) {
- // verify that we aren't fetching any undefined values = values missing
in the WPT file
- assert.strictEqual(metric.indexOf('undefined'),-1,'We have an undefined
value in ' + metric);
+ var keysToBeRemoved = ['webPageTestKey', 'webPageTestHost', '_',
'verbose', 'userStatus', 'sendMetrics', 'customMetrics', 'namespace'];
+ var args = {
+ webPageTestKey: 'aSupERSecrEtKey',
+ webPageTestHost: 'http://our.wpt.org',
+ _: ['all', 'extra', 'args'],
+ verbose: true,
+ userStatus: 'anonymous',
+ dryRun: true,
+ customMetrics: 'javascript that collects custom metrics',
+ namespace: 'super.special.namespace'
+ };
+
+ var wptOptions = util.setupWPTOptions(args);
+ keysToBeRemoved.forEach(function(key) {
+ assert.strictEqual(wptOptions[key], undefined);
+ });
+
});
- // verify that we collect all the metrics that we want
- util.METRICS.forEach(function(definedMetric) {
+ it('We should be able to parse a JSON from WebPageTest collecting data
from mobile', function() {
+ var userStatus = 'anonymous';
+ var namespace = 'webpagetest';
+ var metrics = util.collectMetrics(mobileJson, userStatus, namespace);
+ metrics.forEach(function(metric) {
+ // verify that we aren't fetching any undefined values = values
missing in the WPT file
+ assert.strictEqual(metric.indexOf('undefined'),-1,'We have an
undefined value in ' + metric);
+ });
+
+ // verify that we collect all the metrics that we want
+ util.METRICS.forEach(function(definedMetric) {
var metricIncluded = false;
metrics.forEach(function(metric) {
- if (metric.indexOf(definedMetric) > -1) {
- metricIncluded = true;
- }
+ if (metric.indexOf(definedMetric) > -1) {
+ metricIncluded = true;
+ }
});
assert.strictEqual(metricIncluded, true, 'We are missing metric ' +
definedMetric);
});
- });
-
- it('We should be able to parse a JSON from WebPageTest collecting data from
desktop', function() {
-
- var userStatus = 'anonymous';
- var namespace = 'webpagetest';
- var metrics = util.collectMetrics(desktopJson, userStatus, namespace);
- metrics.forEach(function(metric) {
- // verify that we aren't fetching any undefined values = values missing
in the WPT file
- assert.strictEqual(metric.indexOf('undefined'),-1,'We have an undefined
value in ' + metric);
});
- // verify that we collect all the metrics that we want
- util.METRICS.forEach(function(definedMetric) {
+ it('We should be able to parse a JSON from WebPageTest collecting data
from desktop', function() {
+
+ var userStatus = 'anonymous';
+ var namespace = 'webpagetest';
+ var metrics = util.collectMetrics(desktopJson, userStatus, namespace);
+ metrics.forEach(function(metric) {
+ // verify that we aren't fetching any undefined values = values
missing in the WPT file
+ assert.strictEqual(metric.indexOf('undefined'),-1,'We have an
undefined value in ' + metric);
+ });
+
+ // verify that we collect all the metrics that we want
+ util.METRICS.forEach(function(definedMetric) {
var metricIncluded = false;
metrics.forEach(function(metric) {
- if (metric.indexOf(definedMetric) > -1) {
- metricIncluded = true;
- }
+ if (metric.indexOf(definedMetric) > -1) {
+ metricIncluded = true;
+ }
});
assert.strictEqual(metricIncluded, true, 'We are missing metric ' +
definedMetric);
});
+ });
- });
+ it('We should be able to replace ENV variables', function() {
+
+ process.env.MY_URL = 'VAR1';
+ process.env.MY_SECOND_URL = 'VAR2';
+
+ var text = util.readFile('test/files/scriptingWithEnv.txt');
+ var replacedText = util.replaceWithEnv(text);
+ assert.strictEqual(replacedText.match(/VAR1/g).length, 2);
+ assert.strictEqual(replacedText.match(/VAR2/g).length, 1);
+ });
+
});
--
To view, visit https://gerrit.wikimedia.org/r/239080
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I1d4e3a62922f05da2093a9dc9e779ce0694bb310
Gerrit-PatchSet: 1
Gerrit-Project: performance/WebPageTest
Gerrit-Branch: master
Gerrit-Owner: Phedenskog <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits