From 7472af714d1766f6a4f62cfdfe038d3291b5cd72 Mon Sep 17 00:00:00 2001 From: Daniel Lando Date: Fri, 16 Feb 2024 16:42:44 +0100 Subject: [PATCH] fix: tests (#34) --- package.json | 4 +- test/test-79-npm/main.js | 1 + test/test-80-compression-node-opcua/main.js | 60 ++++++++++++++----- .../package.json | 5 +- test/utils.js | 18 +++++- 5 files changed, 68 insertions(+), 20 deletions(-) diff --git a/package.json b/package.json index 5704916de..138ffdba3 100644 --- a/package.json +++ b/package.json @@ -74,10 +74,10 @@ "fix": "npm run lint:style -- -w && npm run lint:code -- --fix", "prepare": "npm run build", "prepublishOnly": "npm run lint", - "test": "npm run build && npm run test:18 && npm run test:16 && npm run test:14 && npm run test:host", + "test": "npm run build && npm run test:18 && npm run test:16 && npm run test:host", + "test:20": "node test/test.js node20 no-npm", "test:18": "node test/test.js node18 no-npm", "test:16": "node test/test.js node16 no-npm", - "test:14": "node test/test.js node14 no-npm", "test:host": "node test/test.js host only-npm", "release": "read -p 'GITHUB_TOKEN: ' GITHUB_TOKEN && export GITHUB_TOKEN=$GITHUB_TOKEN && release-it" }, diff --git a/test/test-79-npm/main.js b/test/test-79-npm/main.js index 65372754d..06f875976 100644 --- a/test/test-79-npm/main.js +++ b/test/test-79-npm/main.js @@ -28,6 +28,7 @@ const npm = { 14: 6, 16: 7, 18: 8, + 20: 10, }[hostVersion]; assert(npm !== undefined); diff --git a/test/test-80-compression-node-opcua/main.js b/test/test-80-compression-node-opcua/main.js index 04bfa8610..c93558611 100644 --- a/test/test-80-compression-node-opcua/main.js +++ b/test/test-80-compression-node-opcua/main.js @@ -12,6 +12,9 @@ const fs = require('fs'); const path = require('path'); const assert = require('assert'); const utils = require('../utils.js'); +const pkgJson = require('./package.json'); + +const buildDir = 'build'; assert(!module.parent); assert(__dirname === process.cwd()); @@ -20,9 +23,14 @@ if (utils.shouldSkipPnpm()) { return; } +function clean() { + utils.vacuum.sync(buildDir); + utils.vacuum.sync('node_modules'); + utils.vacuum.sync('./pnpm-lock.yaml'); +} + // remove any possible left-over -utils.vacuum.sync('./node_modules'); -utils.vacuum.sync('./pnpm-lock.yaml'); +clean(); // launch `pnpm install` const pnpmlog = utils.spawn.sync( @@ -47,11 +55,11 @@ assert( const input = 'package.json'; const target = process.argv[2] || 'host'; const ext = process.platform === 'win32' ? '.exe' : ''; -const outputRef = 'test-output-empty' + ext; -const outputNone = 'test-output-None' + ext; -const outputGZip = 'test-output-GZip' + ext; -const outputBrotli = 'test-output-Brotli' + ext; -const outputBrotliDebug = 'test-output-Brotli-debug' + ext; +const outputRef = path.join(buildDir, 'test-output-empty' + ext); +const outputNone = path.join(buildDir, 'test-output-None' + ext); +const outputGZip = path.join(buildDir, 'test-output-GZip' + ext); +const outputBrotli = path.join(buildDir, 'test-output-Brotli' + ext); +const outputBrotliDebug = path.join(buildDir, 'test-output-Brotli-debug' + ext); const inspect = ['ignore', 'ignore', 'pipe']; @@ -78,13 +86,40 @@ function pkgCompress(compressMode, output) { ); // check that produced executable is running and produce the expected output. const log = utils.spawn.sync(path.join(__dirname, output), [], { - cwd: __dirname, + cwd: path.join(__dirname, buildDir), expect: 0, }); assert(log === '42\n'); return fs.statSync(output).size; } +function esbuildBuild(entryPoint) { + const log = utils.spawn.sync( + path.join( + path.dirname(process.argv[0]), + 'npx' + (process.platform === 'win32' ? '.cmd' : ''), + ), + [ + 'esbuild', + entryPoint, + '--bundle', + '--outfile=' + path.join(buildDir, pkgJson.main), + '--platform=node', + ], + { cwd: __dirname, expect: 0 }, + ); + + console.log(log); + + // copy folder 'node_modules/node-opcua-nodesets' to build folder + utils.copyRecursiveSync( + 'node_modules/node-opcua-nodesets/nodesets', + path.join(buildDir, 'nodesets'), + ); +} + +esbuildBuild(pkgJson.main); + const sizeNoneFull = pkgCompress('None', outputNone); const sizeGZipFull = pkgCompress('GZip', outputGZip); const sizeBrotliFull = pkgCompress('Brotli', outputBrotli); @@ -121,15 +156,8 @@ const logPkg5 = utils.pkg.sync( { expect: 2 }, ); -// xx console.log(logPkg4); assert(logPkg5.match(/Invalid compression algorithm/g)); -utils.vacuum.sync(outputRef); -utils.vacuum.sync(outputNone); -utils.vacuum.sync(outputBrotli); -utils.vacuum.sync(outputGZip); -utils.vacuum.sync(outputBrotliDebug); -utils.vacuum.sync('node_modules'); -utils.vacuum.sync('./pnpm-lock.yaml'); +clean(); console.log('OK'); diff --git a/test/test-80-compression-node-opcua/package.json b/test/test-80-compression-node-opcua/package.json index fa88fb8f1..6a60ac5fb 100644 --- a/test/test-80-compression-node-opcua/package.json +++ b/test/test-80-compression-node-opcua/package.json @@ -2,6 +2,7 @@ "name": "test-12-compression", "version": "1.0.0", "description": "", + "bin": "build/test-x.js", "main": "test-x.js", "scripts": { "preinstall": "npx only-allow pnpm", @@ -15,7 +16,9 @@ "node-opcua-crypto": "^1.7.1", "node-opcua-nodesets": "^2.36.0" }, - "bin": "test-x.js", + "devDependencies": { + "esbuild": "^0.20.0" + }, "pkg": { "assets": [ "./node_modules/node-opcua-nodesets/nodesets/*.xml" diff --git a/test/utils.js b/test/utils.js index e699afac4..ec586fe11 100644 --- a/test/utils.js +++ b/test/utils.js @@ -7,7 +7,7 @@ const rimraf = require('rimraf'); const globby = require('globby'); const { execSync } = require('child_process'); const { spawnSync } = require('child_process'); -const { existsSync } = require('fs'); +const { existsSync, statSync, copyFileSync, readdirSync } = require('fs'); const stableStringify = require('json-stable-stringify'); module.exports.mkdirp = mkdirp; @@ -20,6 +20,22 @@ module.exports.pause = function (seconds) { ]); }; +module.exports.copyRecursiveSync = function (origin, dest) { + const stats = statSync(origin); + if (stats.isDirectory()) { + mkdirp.sync(dest); + const files = readdirSync(origin); + for (const file of files) { + module.exports.copyRecursiveSync( + path.join(origin, file), + path.join(dest, file), + ); + } + } else { + copyFileSync(origin, dest); + } +}; + module.exports.vacuum = function () { throw new Error('Async vacuum not implemented'); };