From a6198b54c54270a3881611e4e81318f178075041 Mon Sep 17 00:00:00 2001 From: GermanBluefox Date: Sun, 14 Apr 2024 09:17:31 +0800 Subject: [PATCH] Try to fix tests --- .github/workflows/test-and-release.yml | 8 ++ gulpfile.js | 127 ++++++++----------------- test/testAdapter.gui.js | 2 +- test/testAdapter.js | 98 ++----------------- 4 files changed, 56 insertions(+), 179 deletions(-) diff --git a/.github/workflows/test-and-release.yml b/.github/workflows/test-and-release.yml index eba23b8e8..642a58466 100644 --- a/.github/workflows/test-and-release.yml +++ b/.github/workflows/test-and-release.yml @@ -74,6 +74,14 @@ jobs: - name: Run local tests run: npm test + + - name: Archive screenshots + if: always() + # if: failure() + uses: actions/upload-artifact@v3 + with: + path: tmp/screenshots/**/*.png + retention-days: 3 # - name: Run unit tests # run: npm run test:unit # - name: Run integration tests # (linux/osx) diff --git a/gulpfile.js b/gulpfile.js index 3cc10a33f..1f1948959 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -53,8 +53,8 @@ function lang2data(lang, isFlat) { function readWordJs(src) { try { let words; - if (fs.existsSync(src + 'js/' + fileName)) { - words = fs.readFileSync(src + 'js/' + fileName).toString(); + if (fs.existsSync(`${src}js/${fileName}`)) { + words = fs.readFileSync(`${src}js/${fileName}`).toString(); } else { words = fs.readFileSync(src + fileName).toString(); } @@ -81,7 +81,7 @@ function readWordJs(src) { lines[lines.length - 1] = lines[lines.length - 1].trim().replace(/}\);$/, '}'); lines[lines.length - 1] = lines[lines.length - 1].trim().replace(/};$/, '}'); words = lines.join('\n'); - const resultFunc = new Function('return ' + words + ';'); + const resultFunc = new Function(`return ${words};`); return resultFunc(); } catch (e) { @@ -151,7 +151,7 @@ function words2languages(src) { for (const lang in data[word]) { if (data[word].hasOwnProperty(lang)) { if (!langs[lang]) { - console.error('No data for ' + lang + ' and ' + word); + console.error(`No data for ${lang} and ${word}`); } langs[lang][word] = data[word][lang]; // pre-fill all other languages @@ -164,8 +164,8 @@ function words2languages(src) { } } } - if (!fs.existsSync(src + 'i18n/')) { - fs.mkdirSync(src + 'i18n/'); + if (!fs.existsSync(`${src}i18n/`)) { + fs.mkdirSync(`${src}i18n/`); } for (const l in langs) { if (!langs.hasOwnProperty(l)) continue; @@ -175,14 +175,14 @@ function words2languages(src) { for (let k = 0; k < keys.length; k++) { obj[keys[k]] = langs[l][keys[k]]; } - if (!fs.existsSync(src + 'i18n/' + l)) { - fs.mkdirSync(src + 'i18n/' + l); + if (!fs.existsSync(`${src}i18n/${l}`)) { + fs.mkdirSync(`${src}i18n/${l}`); } - fs.writeFileSync(src + 'i18n/' + l + '/translations.json', lang2data(obj)); + fs.writeFileSync(`${src}i18n/${l}/translations.json`, lang2data(obj)); } } else { - console.error('Cannot read or parse ' + fileName); + console.error(`Cannot read or parse ${fileName}`); } } function words2languagesFlat(src) { @@ -194,7 +194,7 @@ function words2languagesFlat(src) { for (const lang in data[word]) { if (data[word].hasOwnProperty(lang)) { if (!langs[lang]) { - console.error('No data for ' + lang + ' and ' + word); + console.error(`No data for ${lang} and ${word}`); } langs[lang][word] = data[word][lang]; // pre-fill all other languages @@ -226,7 +226,7 @@ function words2languagesFlat(src) { fs.mkdirSync(src + 'i18n/' + ll); } - fs.writeFileSync(src + 'i18n/' + ll + '/flat.txt', lang2data(langs[ll], langs.en)); + fs.writeFileSync(`${src}i18n/${ll}/flat.txt`, lang2data(langs[ll], langs.en)); } fs.writeFileSync(src + 'i18n/flat.txt', keys.join('\n')); } else { @@ -235,7 +235,7 @@ function words2languagesFlat(src) { } function languagesFlat2words(src) { - const dirs = fs.readdirSync(src + 'i18n/'); + const dirs = fs.readdirSync(`${src}i18n/`); const langs = {}; const bigOne = {}; const order = Object.keys(languages); @@ -256,12 +256,12 @@ function languagesFlat2words(src) { return 0; } }); - const keys = fs.readFileSync(src + 'i18n/flat.txt').toString().split('\n'); + const keys = fs.readFileSync(`${src}i18n/flat.txt`).toString().split('\n'); for (let l = 0; l < dirs.length; l++) { if (dirs[l] === 'flat.txt') continue; const lang = dirs[l]; - const values = fs.readFileSync(src + 'i18n/' + lang + '/flat.txt').toString().split('\n'); + const values = fs.readFileSync(`${src}i18n/${lang}/flat.txt`).toString().split('\n'); langs[lang] = {}; keys.forEach((word, i) => langs[lang][word] = values[i]); @@ -284,13 +284,13 @@ function languagesFlat2words(src) { for (const w in aWords) { if (aWords.hasOwnProperty(w)) { if (!bigOne[w]) { - console.warn('Take from actual words.js: ' + w); + console.warn(`Take from actual words.js: ${w}`); bigOne[w] = aWords[w] } dirs.forEach(function (lang) { if (temporaryIgnore.indexOf(lang) !== -1) return; if (!bigOne[w][lang]) { - console.warn('Missing "' + lang + '": ' + w); + console.warn(`Missing "${lang}": ${w}`); } }); } @@ -302,7 +302,7 @@ function languagesFlat2words(src) { } function languages2words(src) { - const dirs = fs.readdirSync(src + 'i18n/'); + const dirs = fs.readdirSync(`${src}i18n/`); const langs = {}; const bigOne = {}; const order = Object.keys(languages); @@ -326,11 +326,11 @@ function languages2words(src) { for (let l = 0; l < dirs.length; l++) { if (dirs[l] === 'flat.txt') continue; const lang = dirs[l]; - langs[lang] = fs.readFileSync(src + 'i18n/' + lang + '/translations.json').toString(); + langs[lang] = fs.readFileSync(`${src}i18n/${lang}/translations.json`).toString(); try { langs[lang] = JSON.parse(langs[lang]); } catch (e) { - console.error('Cannot parse ' + src + 'i18n/' + lang + '/translations.json: ' + e); + console.error(`Cannot parse ${src}i18n/${lang}/translations.json: ${e}`); } const words = langs[lang]; for (const word in words) { @@ -351,13 +351,13 @@ function languages2words(src) { for (const w in aWords) { if (aWords.hasOwnProperty(w)) { if (!bigOne[w]) { - console.warn('Take from actual words.js: ' + w); + console.warn(`Take from actual words.js: ${w}`); bigOne[w] = aWords[w] } dirs.forEach(function (lang) { if (temporaryIgnore.indexOf(lang) !== -1) return; if (!bigOne[w][lang]) { - console.warn('Missing "' + lang + '": ' + w); + console.warn(`Missing "${lang}": ${w}`); } }); } @@ -414,21 +414,21 @@ gulp.task('replacePkg', () => srcDir + 'package.json', srcDir + 'io-package.json' ]) - .pipe(replace(/"version": *"[.0-9]*",/g, '"version": "' + version + '",')) + .pipe(replace(/"version": *"[.0-9]*",/g, `"version": "${version}",`)) .pipe(gulp.dest(srcDir))); gulp.task('replaceVis', () => gulp.src([ - srcDir + 'www/js/vis.js' + `${srcDir}www/js/vis.js` ]) - .pipe(replace(/const version = *'[.0-9]*';/g, 'const version = "' + version + '";')) - .pipe(replace(/"version": *"[.0-9]*",/g, '"version": "' + version + '",')) - .pipe(replace(/version: *"[.0-9]*",/g, 'version: "' + version + '",')) - .pipe(replace(/version: *'[.0-9]*',/g, 'version: \'' + version + '\',')) - .pipe(replace(//g, '')) - .pipe(replace(/# vis Version [.0-9]+/g, '# vis Version ' + version)) + .pipe(replace(/const version = *'[.0-9]*';/g, `const version = "${version}";`)) + .pipe(replace(/"version": *"[.0-9]*",/g, `"version": "${version}",`)) + .pipe(replace(/version: *"[.0-9]*",/g, `version: "${version}",`)) + .pipe(replace(/version: *'[.0-9]*',/g, `version: '${version}',`)) + .pipe(replace(//g, ``)) + .pipe(replace(/# vis Version [.0-9]+/g, `# vis Version ${version}`)) .pipe(replace(/ dev build [.0-9]+/g, '# dev build 0')) - .pipe(gulp.dest( srcDir + '/www/js'))); + .pipe(gulp.dest( `${srcDir}/www/js`))); gulp.task('replaceHtml', () => gulp.src([ @@ -436,64 +436,15 @@ gulp.task('replaceHtml', () => srcDir + 'www/index.html', srcDir + 'www/edit.html' ]) - .pipe(replace(//g, '')) - .pipe(replace(/const version = *'[.0-9]*';/g, 'const version = \'' + version + '\';')) - .pipe(replace(/"version": *"[.0-9]*",/g, '"version": "' + version + '",')) - .pipe(replace(/version: *"[.0-9]*",/g, 'version: "' + version + '",')) - .pipe(replace(/version: *'[.0-9]*',/g, 'version: \'' + version + '\',')) - .pipe(replace(/# vis Version [.0-9]+/g, '# vis Version ' + version)) + .pipe(replace(//g, ``)) + .pipe(replace(/const version = *'[.0-9]*';/g, `const version = '${version}';`)) + .pipe(replace(/"version": *"[.0-9]*",/g, `"version": "${version}",`)) + .pipe(replace(/version: *"[.0-9]*",/g, `version: "${version}",`)) + .pipe(replace(/version: *'[.0-9]*',/g, `version: '${version}',`)) + .pipe(replace(/# vis Version [.0-9]+/g, `# vis Version ${version}`)) .pipe(replace(/# dev build [.0-9]+/g, '# dev build 0')) - .pipe(gulp.dest(srcDir + '/www'))); - -gulp.task('updatePackages', done => { - iopackage.common.version = pkg.version; - iopackage.common.news = iopackage.common.news || {}; - if (!iopackage.common.news[pkg.version]) { - const news = iopackage.common.news; - const newNews = {}; - - newNews[pkg.version] = { - 'en': 'news', - 'de': 'neues', - 'ru': 'новое', - 'pt': 'notícia', - 'nl': 'nieuws', - 'fr': 'nouvelles', - 'it': 'notizia', - 'es': 'Noticias', - 'pl': 'Aktualności', - 'zh-cn': '消息' - }; - iopackage.common.news = Object.assign(newNews, news); - } - fs.writeFileSync('io-package.json', JSON.stringify(iopackage, null, 4)); - done(); -}); - -gulp.task('updateReadme', done => { - const readme = fs.readFileSync('README.md').toString(); - const pos = readme.indexOf('## Changelog\n'); - if (pos !== -1) { - const readmeStart = readme.substring(0, pos + '## Changelog\n'.length); - const readmeEnd = readme.substring(pos + '## Changelog\n'.length); - - if (readme.indexOf(version) === -1) { - const timestamp = new Date(); - const date = timestamp.getFullYear() + '-' + - ('0' + (timestamp.getMonth() + 1).toString(10)).slice(-2) + '-' + - ('0' + (timestamp.getDate()).toString(10)).slice(-2); - - let news = ''; - if (iopackage.common.news && iopackage.common.news[pkg.version]) { - news += '* ' + iopackage.common.news[pkg.version].en; - } - - fs.writeFileSync('README.md', readmeStart + '### ' + version + ' (' + date + ')\n' + (news ? news + '\n\n' : '\n') + readmeEnd); - } - } - done(); -}); + .pipe(gulp.dest(`${srcDir}/www`))); gulp.task('replace', gulp.series('replacePkg', 'replaceVis', 'replaceHtml')); -gulp.task('default', gulp.series('updatePackages', 'updateReadme', 'replace')); +gulp.task('default', gulp.series('replace')); diff --git a/test/testAdapter.gui.js b/test/testAdapter.gui.js index 9acaf169f..16bed9227 100644 --- a/test/testAdapter.gui.js +++ b/test/testAdapter.gui.js @@ -55,7 +55,7 @@ describe('vis', () => { }); after(async function () { - this.timeout(5_000); + this.timeout(10_000); await helper.stopBrowser(); console.log('BROWSER stopped'); await helper.stopIoBroker(); diff --git a/test/testAdapter.js b/test/testAdapter.js index fe08352d0..373ad3154 100644 --- a/test/testAdapter.js +++ b/test/testAdapter.js @@ -1,82 +1,20 @@ -/* jshint -W097 */// jshint strict:false -/*jslint node: true */ const expect = require('chai').expect; -const setup = require('@iobroker/legacy-testing'); +const setup = require('@iobroker/legacy-testing'); let objects = null; -let states = null; +let states = null; let onStateChanged = null; -let sendToID = 1; const adapterShortName = setup.adapterName.substring(setup.adapterName.indexOf('.') + 1); -const runningMode = require(`${__dirname}/../io-package.json`).common.mode; -function checkConnectionOfAdapter(cb, counter) { - counter = counter || 0; - console.log(`Try check #${counter}`); - if (counter > 30) { - cb && cb('Cannot check connection'); - return; - } - - states.getState(`system.adapter.${adapterShortName}.0.alive`, function (err, state) { - err && console.error(err); - if (state && state.val) { - cb && cb(); - } else { - setTimeout(() => checkConnectionOfAdapter(cb, counter + 1), 1000); - } - }); -} - -function checkValueOfState(id, value, cb, counter) { - counter = counter || 0; - if (counter > 20) { - cb && cb(`Cannot check value Of State ${id}`); - return; - } - - states.getState(id, function (err, state) { - err && console.error(err); - if (value === null && !state) { - cb && cb(); - } else - if (state && (value === undefined || state.val === value)) { - cb && cb(); - } else { - setTimeout(() => checkValueOfState(id, value, cb, counter + 1), 500); - } - }); -} - -function sendTo(target, command, message, callback) { - onStateChanged = function (id, state) { - if (id === 'messagebox.system.adapter.test.0') { - callback(state.message); - } - }; - - states.pushMessage(`system.adapter.${target}`, { - command: command, - message: message, - from: 'system.adapter.test.0', - callback: { - message: message, - id: sendToID++, - ack: false, - time: Date.now(), - } - }); -} - -describe.skip(`Test ${adapterShortName} adapter`, function() { +describe.skip(`Test ${adapterShortName} adapter`, function () { before(`Test ${adapterShortName} adapter: Start js-controller`, function (_done) { this.timeout(600000); // because of the first installation from npm setup.setupController(async function () { const config = await setup.getAdapterConfig(); // enable adapter - config.common.enabled = true; + config.common.enabled = true; config.common.loglevel = 'debug'; //config.native.dbtype = 'sqlite'; @@ -85,11 +23,12 @@ describe.skip(`Test ${adapterShortName} adapter`, function() { setup.startController( true, - (id, obj) => {}, + (id, obj) => { + }, (id, state) => onStateChanged && onStateChanged(id, state), (_objects, _states) => { objects = _objects; - states = _states; + states = _states; _done(); }); }); @@ -104,28 +43,7 @@ describe.skip(`Test ${adapterShortName} adapter`, function() { }); }); - it(`Test ${adapterShortName} adapter: Check if adapter started`, function (done) { - this.timeout(60000); - checkConnectionOfAdapter(res => { - res && console.log(res); - if (runningMode === 'daemon') { - expect(res).not.to.be.equal('Cannot check connection'); - } else { - //?? - } - done(); - }); - }); -/**/ - -/* - PUT YOUR OWN TESTS HERE USING - it('Testname', function ( done) { - ... - }); - - You can also use "sendTo" method to send messages to the started adapter -*/ + // we cannot check if the adapter was started, as it stops immediately after(`Test ${adapterShortName} adapter: Stop js-controller`, function (done) { this.timeout(10000);