From 3525325c9a8c9aca5f215906c0685430b40c0be2 Mon Sep 17 00:00:00 2001 From: LianaHus Date: Fri, 13 Dec 2019 17:11:12 +0100 Subject: [PATCH] check if possible to use worker for unit tests --- src/app/compiler/compiler-utils.js | 43 ++++++++++++++++++++ src/app/tabs/compileTab/compilerContainer.js | 39 ++---------------- src/app/tabs/test-tab.js | 26 +++++++----- 3 files changed, 62 insertions(+), 46 deletions(-) create mode 100644 src/app/compiler/compiler-utils.js diff --git a/src/app/compiler/compiler-utils.js b/src/app/compiler/compiler-utils.js new file mode 100644 index 0000000000..8ea9f3ae6f --- /dev/null +++ b/src/app/compiler/compiler-utils.js @@ -0,0 +1,43 @@ +const semver = require('semver') +/* global Worker */ + +/** + * Checks if the worker can be used to load a compiler. + * checks a compiler whitelist, browser support and OS. + */ +export function canUseWorker (selectedVersion) { + // Following restrictions should be deleted when Solidity will release fixed versions of compilers. + // See https://github.com/ethereum/remix-ide/issues/2461 + const isChrome = !!window.chrome + const os = retrieveOS() + // define a whitelist for Linux + const linuxWL = ['0.4.26', '0.5.3', '0.5.4', '0.5.5'] + const version = semver.coerce(selectedVersion) + // defining whitelist for chrome + let isFromWhiteList = false + switch (os) { + case 'Windows': + isFromWhiteList = semver.gt(version, '0.5.2') || version === '0.4.26' + break + case 'Linux': + isFromWhiteList = semver.gt(version, '0.5.13') || linuxWL.includes(version) + break + default : + isFromWhiteList = true + } + return browserSupportWorker() && (!isChrome || (isChrome && isFromWhiteList)) +} + +function browserSupportWorker () { + return document.location.protocol !== 'file:' && Worker !== undefined +} + +function retrieveOS () { + let osName = 'Unknown OS' + if (navigator.platform.indexOf('Win') !== -1) { + osName = 'Windows' + } else if (navigator.platform.indexOf('Linux') !== -1) { + osName = 'Linux' + } + return osName +} diff --git a/src/app/tabs/compileTab/compilerContainer.js b/src/app/tabs/compileTab/compilerContainer.js index 5c4c0ca892..27c8db1503 100644 --- a/src/app/tabs/compileTab/compilerContainer.js +++ b/src/app/tabs/compileTab/compilerContainer.js @@ -1,4 +1,4 @@ -/* global Worker */ + const yo = require('yo-yo') const minixhr = require('minixhr') const helper = require('../../../lib/helper') @@ -6,6 +6,7 @@ const addTooltip = require('../../ui/tooltip') const semver = require('semver') const modalDialogCustom = require('../../ui/modal-dialog-custom') const css = require('../styles/compile-tab-styles') +import { canUseWorker } from '../../compiler/compiler-utils' class CompilerContainer { @@ -397,30 +398,10 @@ class CompilerContainer { url = `${this.data.baseurl}/${this.data.selectedVersion}` } - // Following restrictions should be deleted when Solidity will release fixed versions of compilers. - // See https://github.com/ethereum/remix-ide/issues/2461 - const isChrome = !!window.chrome - const os = this._retrieveOS() - // define a whitelist for Linux - const linuxWL = ['0.4.26', '0.5.3', '0.5.4', '0.5.5'] - const version = semver.coerce(this.data.selectedVersion) - // defining whitelist for chrome - let isFromWhiteList = false - switch (os) { - case 'Windows': - isFromWhiteList = semver.gt(version, '0.5.2') || version === '0.4.26' - break - case 'Linux': - isFromWhiteList = semver.gt(version, '0.5.13') || linuxWL.includes(version) - break - default : - isFromWhiteList = true - } - // Workers cannot load js on "file:"-URLs and we get a // "Uncaught RangeError: Maximum call stack size exceeded" error on Chromium, // resort to non-worker version in that case. - if (this.browserSupportWorker() && (!isChrome || (isChrome && isFromWhiteList))) { + if (canUseWorker(this.data.selectedVersion)) { this.compileTabLogic.compiler.loadVersion(true, url) this.setVersionText('(loading using worker)') } else { @@ -429,16 +410,6 @@ class CompilerContainer { } } - _retrieveOS () { - let osName = 'Unknown OS' - if (navigator.platform.indexOf('Win') !== -1) { - osName = 'Windows' - } else if (navigator.platform.indexOf('Linux') !== -1) { - osName = 'Linux' - } - return osName - } - _updateLanguageSelector () { // This is the first version when Yul is available if (!semver.valid(this._retrieveVersion()) || semver.lt(this._retrieveVersion(), 'v0.5.7+commit.6da8b019.js')) { @@ -481,10 +452,6 @@ class CompilerContainer { this.data.compileTimeout = window.setTimeout(() => this.compileIfAutoCompileOn(), this.data.timeout) } - browserSupportWorker () { - return document.location.protocol !== 'file:' && Worker !== undefined - } - } module.exports = CompilerContainer diff --git a/src/app/tabs/test-tab.js b/src/app/tabs/test-tab.js index e638653aa0..aa74a41230 100644 --- a/src/app/tabs/test-tab.js +++ b/src/app/tabs/test-tab.js @@ -4,6 +4,7 @@ var tooltip = require('../ui/tooltip') var css = require('./styles/test-tab-styles') var remixTests = require('remix-tests') import { ViewPlugin } from '@remixproject/engine' +import { canUseWorker } from '../compiler/compiler-utils' const TestTabLogic = require('./testTab/testTab') @@ -153,8 +154,7 @@ module.exports = class TestTab extends ViewPlugin { let runningTest = {} runningTest[path] = { content } let currentCompilerUrl = this.baseurl + '/' + this.compileTab.getCurrentVersion() - let usingWorker = this.compileTab.compilerContainer.browserSupportWorker() - remixTests.runTestSources(runningTest, currentCompilerUrl, usingWorker, () => {}, () => {}, (error, result) => { + remixTests.runTestSources(runningTest, currentCompilerUrl, canUseWorker(this.compileTab.getCurrentVersion()), () => {}, () => {}, (error, result) => { if (error) return reject(error) resolve(result) }, (url, cb) => { @@ -169,14 +169,20 @@ module.exports = class TestTab extends ViewPlugin { var runningTest = {} runningTest[testFilePath] = { content } let currentCompilerUrl = this.baseurl + '/' + this.compileTab.getCurrentVersion() - let usingWorker = this.compileTab.compilerContainer.browserSupportWorker() - remixTests.runTestSources(runningTest, currentCompilerUrl, usingWorker, (result) => { this.testCallback(result) }, (_err, result, cb) => { this.resultsCallback(_err, result, cb) }, (error, result) => { - this.updateFinalResult(error, result, testFilePath) - this.loading.hidden = true - callback(error) - }, (url, cb) => { - return this.compileTab.compileTabLogic.importFileCb(url, cb) - }) + remixTests.runTestSources( + runningTest, + currentCompilerUrl, + canUseWorker(this.compileTab.getCurrentVersion()), + (result) => { this.testCallback(result) }, + (_err, result, cb) => { this.resultsCallback(_err, result, cb) }, + (error, result) => { + this.updateFinalResult(error, result, testFilePath) + this.loading.hidden = true + callback(error) + }, (url, cb) => { + return this.compileTab.compileTabLogic.importFileCb(url, cb) + } + ) }).catch((error) => { if (error) return })