This is an automated email from the ASF dual-hosted git repository.

shenyi pushed a commit to branch feature/clip
in repository https://gitbox.apache.org/repos/asf/incubator-echarts.git


The following commit(s) were added to refs/heads/feature/clip by this push:
     new 9cfe402  test: fix random in the echarts caused random in demo 
inconsistent between versions.
9cfe402 is described below

commit 9cfe402330065ac79df96829a0d8f43e3416d72f
Author: pissang <bm2736...@gmail.com>
AuthorDate: Wed Sep 18 21:20:54 2019 +0800

    test: fix random in the echarts caused random in demo inconsistent between 
versions.
---
 test/runTest/cli.js          | 40 ++++++++++++++++++++++++----------------
 test/runTest/config.js       |  1 +
 test/runTest/runtime/main.js | 11 ++++++++++-
 test/runTest/util.js         | 23 ++++++++++++++++++-----
 4 files changed, 53 insertions(+), 22 deletions(-)

diff --git a/test/runTest/cli.js b/test/runTest/cli.js
index a0940d4..39f85b9 100644
--- a/test/runTest/cli.js
+++ b/test/runTest/cli.js
@@ -24,7 +24,7 @@ const fs = require('fs');
 const path = require('path');
 const program = require('commander');
 const compareScreenshot = require('./compareScreenshot');
-const {testNameFromFile, fileNameFromTest, getVersionDir, buildRuntimeCode, 
waitTime} = require('./util');
+const {testNameFromFile, fileNameFromTest, getVersionDir, buildRuntimeCode, 
waitTime, getEChartsTestFileName} = require('./util');
 const {origin} = require('./config');
 const Timeline = require('./Timeline');
 
@@ -65,9 +65,9 @@ function getClientRelativePath(absPath) {
 function replaceEChartsVersion(interceptedRequest, version) {
     // TODO Extensions and maps
     if (interceptedRequest.url().endsWith('dist/echarts.js')) {
-        console.log('Use echarts version: ' + getVersionDir(version));
+        console.log('Use echarts version: ' + version);
         interceptedRequest.continue({
-            url: `${origin}/test/runTest/${getVersionDir(version)}/echarts.js`
+            url: 
`${origin}/test/runTest/${getVersionDir(version)}/${getEChartsTestFileName()}`
         });
     }
     else {
@@ -210,22 +210,30 @@ async function runTest(browser, testOpt, runtimeCode, 
expectedVersion, actualVer
     for (let shot of expectedResult.screenshots) {
         let expected = shot;
         let actual = actualResult.screenshots[idx++];
-        let {diffRatio, diffPNG} = await compareScreenshot(
-            expected.screenshotPath,
-            actual.screenshotPath
-        );
-
-        let diffPath = `${path.resolve(__dirname, 
getScreenshotDir())}/${shot.screenshotName}-diff.png`;
-        await writePNG(diffPNG, diffPath);
-
-        screenshots.push({
+        let result = {
             actual: getClientRelativePath(actual.screenshotPath),
             expected: getClientRelativePath(expected.screenshotPath),
-            diff: getClientRelativePath(diffPath),
             name: actual.screenshotName,
-            desc: actual.desc,
-            diffRatio
-        });
+            desc: actual.desc
+        };
+        try {
+            let {diffRatio, diffPNG} = await compareScreenshot(
+                expected.screenshotPath,
+                actual.screenshotPath
+            );
+
+            let diffPath = `${path.resolve(__dirname, 
getScreenshotDir())}/${shot.screenshotName}-diff.png`;
+            await writePNG(diffPNG, diffPath);
+
+            result.diff = getClientRelativePath(diffPath);
+            result.diffRatio = diffRatio;
+        }
+        catch(e) {
+            result.diff = '';
+            result.diffRatio = 1;
+            console.log(e);
+        }
+        screenshots.push(result);
     }
 
     testOpt.results = screenshots;
diff --git a/test/runTest/config.js b/test/runTest/config.js
index dc4ddb6..71de615 100644
--- a/test/runTest/config.js
+++ b/test/runTest/config.js
@@ -18,6 +18,7 @@
 */
 
 module.exports = {
+    testVersion: 'test-v0.1',
     port: 8866,
     origin: 'http://localhost:8866'
 };
\ No newline at end of file
diff --git a/test/runTest/runtime/main.js b/test/runTest/runtime/main.js
index d78d639..50af624 100644
--- a/test/runTest/runtime/main.js
+++ b/test/runTest/runtime/main.js
@@ -24,13 +24,22 @@ if (typeof __TEST_PLAYBACK_SPEED__ === 'undefined') {
     window.__TEST_PLAYBACK_SPEED__ = 1;
 }
 
-let myRandom = new seedrandom('echarts-test');
+let myRandom = new seedrandom('echarts-random');
+// Random for echarts code.
+// In case different echarts version called random different times.
+// It will cause following random number all wrong.
+let myRandom2 = new seedrandom('echarts-random-inner');
 // Fixed random generator
 Math.random = function () {
     const val = myRandom();
     return val;
 };
 
+window.__random__inner__ = function () {
+    const val = myRandom2();
+    return val;
+};
+
 window.addEventListener('DOMContentLoaded', () => {
     let style = document.createElement('style');
     // Disable all css animation since it will cause screenshot inconsistent.
diff --git a/test/runTest/util.js b/test/runTest/util.js
index 2e551e3..9bac45e 100644
--- a/test/runTest/util.js
+++ b/test/runTest/util.js
@@ -24,6 +24,12 @@ const fs = require('fs');
 const rollup = require('rollup');
 const resolve = require('rollup-plugin-node-resolve');
 const commonjs = require('rollup-plugin-commonjs');
+const util = require('util');
+const config = require('./config');
+
+function modifyEChartsCode(code) {
+    return code.replace(/Math.random/g, '__random__inner__');
+}
 
 module.exports.testNameFromFile = function(fileName) {
     return path.basename(fileName, '.html');
@@ -43,19 +49,24 @@ module.exports.getActionsFullPath = function (testName) {
     return path.join(__dirname, 'actions', testName + '.json');
 };
 
+module.exports.getEChartsTestFileName = function () {
+    return `echarts.test-${config.testVersion}.js`;
+};
 
 module.exports.prepareEChartsLib = function (version) {
     let versionFolder = path.join(__dirname, getVersionDir(version));
     fse.ensureDirSync(versionFolder);
     if (!version || version === 'local') {
         // Developing version, make sure it's new build
-        return fse.copy(
-            path.join(__dirname, '../../dist/echarts.js'),
-            `${versionFolder}/echarts.js`
-        );
+        fse.copySync(path.join(__dirname, '../../dist/echarts.js'), 
`${versionFolder}/echarts.js`);
+        let code = 
modifyEChartsCode(fs.readFileSync(`${versionFolder}/echarts.js`, 'utf-8'));
+        
fs.writeFileSync(`${versionFolder}/${module.exports.getEChartsTestFileName()}`, 
code, 'utf-8');
+        return Promise.resolve();
+
     }
     return new Promise(resolve => {
-        if (!fs.existsSync(`${versionFolder}/echarts.js`)) {
+        let testLibPath = 
`${versionFolder}/${module.exports.getEChartsTestFileName()}`;
+        if (!fs.existsSync(testLibPath)) {
             const file = fs.createWriteStream(`${versionFolder}/echarts.js`);
 
             console.log(`Downloading echarts@${version} from `, 
`https://cdn.jsdelivr.net/npm/echarts@${version}/dist/echarts.js`);
@@ -63,6 +74,8 @@ module.exports.prepareEChartsLib = function (version) {
                 response.pipe(file);
 
                 file.on('finish', () => {
+                    let code = 
modifyEChartsCode(fs.readFileSync(`${versionFolder}/echarts.js`, 'utf-8'));
+                    fs.writeFileSync(testLibPath, code, 'utf-8');
                     resolve();
                 });
             });


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@echarts.apache.org
For additional commands, e-mail: commits-h...@echarts.apache.org

Reply via email to