From 1fa854585f957edf52acb3e341f64805503ab4fa Mon Sep 17 00:00:00 2001 From: Richard Walker Date: Mon, 29 Jul 2024 16:33:27 +1200 Subject: [PATCH 1/4] test: fix tests for node version > 16 --- test/alias.test.js | 5 +++- test/integration/alias.test.js | 5 +++- test/integration/integrity.test.js | 5 +++- test/integration/login.test.js | 5 +++- test/integration/map.test.js | 5 +++- test/integration/meta.test.js | 5 +++- test/integration/package.test.js | 5 +++- test/integrity.test.js | 5 +++- test/login.test.js | 5 +++- test/meta.test.js | 5 +++- test/publish-files-definitions.test.js | 5 +++- test/publish.map.test.js | 5 +++- test/publish.package.test.js | 5 +++- test/utils.test.js | 40 ++++++++++++++++++++------ test/version.test.js | 5 +++- 15 files changed, 88 insertions(+), 22 deletions(-) diff --git a/test/alias.test.js b/test/alias.test.js index 76393bed..195371de 100644 --- a/test/alias.test.js +++ b/test/alias.test.js @@ -17,7 +17,10 @@ beforeEach(async (t) => { const memSink = new sink.MEM(); const service = new EikService({ customSink: memSink }); server.register(service.api()); - const address = await server.listen(); + const address = await server.listen({ + host: '127.0.0.1', + port: 0, + }); const token = await cli.login({ server: address, diff --git a/test/integration/alias.test.js b/test/integration/alias.test.js index 2b5de754..63750187 100644 --- a/test/integration/alias.test.js +++ b/test/integration/alias.test.js @@ -26,7 +26,10 @@ beforeEach(async (t) => { const memSink = new sink.MEM(); const service = new EikService({ customSink: memSink }); server.register(service.api()); - const address = await server.listen(); + const address = await server.listen({ + host: '127.0.0.1', + port: 0, + }); const folder = await fs.mkdtemp(join(os.tmpdir(), basename(__filename))); const eik = join(__dirname, '../../index.js'); diff --git a/test/integration/integrity.test.js b/test/integration/integrity.test.js index 25ae60bc..90b8f09c 100644 --- a/test/integration/integrity.test.js +++ b/test/integration/integrity.test.js @@ -24,7 +24,10 @@ beforeEach(async (t) => { const server = fastify({ logger: false }); const service = new EikService({ customSink: memSink }); server.register(service.api()); - const address = await server.listen(); + const address = await server.listen({ + host: '127.0.0.1', + port: 0, + }); const folder = await fs.mkdtemp(join(os.tmpdir(), basename(__filename))); const token = await cli.login({ diff --git a/test/integration/login.test.js b/test/integration/login.test.js index 9abef613..f4d4cde4 100644 --- a/test/integration/login.test.js +++ b/test/integration/login.test.js @@ -25,7 +25,10 @@ beforeEach(async (t) => { const server = fastify({ logger: false }); const service = new EikService({ customSink: memSink }); server.register(service.api()); - const address = await server.listen(); + const address = await server.listen({ + host: '127.0.0.1', + port: 0, + }); const folder = await fs.mkdtemp(join(os.tmpdir(), basename(__filename))); diff --git a/test/integration/map.test.js b/test/integration/map.test.js index b85f2643..a27eafcb 100644 --- a/test/integration/map.test.js +++ b/test/integration/map.test.js @@ -25,7 +25,10 @@ beforeEach(async (t) => { const server = fastify({ logger: false }); const service = new EikService({ customSink: memSink }); server.register(service.api()); - const address = await server.listen(); + const address = await server.listen({ + host: '127.0.0.1', + port: 0, + }); const folder = await fs.mkdtemp(join(os.tmpdir(), basename(__filename))); const token = await cli.login({ diff --git a/test/integration/meta.test.js b/test/integration/meta.test.js index 7788daa8..83899c95 100644 --- a/test/integration/meta.test.js +++ b/test/integration/meta.test.js @@ -24,7 +24,10 @@ beforeEach(async (t) => { const server = fastify({ logger: false }); const service = new EikService({ customSink: memSink }); server.register(service.api()); - const address = await server.listen(); + const address = await server.listen({ + host: '127.0.0.1', + port: 0, + }); const folder = await fs.mkdtemp(join(os.tmpdir(), basename(__filename))); const eik = join(__dirname, '../../index.js'); diff --git a/test/integration/package.test.js b/test/integration/package.test.js index fd565ac2..ec48cd90 100644 --- a/test/integration/package.test.js +++ b/test/integration/package.test.js @@ -25,7 +25,10 @@ beforeEach(async (t) => { const server = fastify({ logger: false }); const service = new EikService({ customSink: memSink }); server.register(service.api()); - const address = await server.listen(); + const address = await server.listen({ + host: '127.0.0.1', + port: 0, + }); const folder = await fs.mkdtemp(join(os.tmpdir(), basename(__filename))); const token = await cli.login({ diff --git a/test/integrity.test.js b/test/integrity.test.js index de1ef022..55ce4c0b 100644 --- a/test/integrity.test.js +++ b/test/integrity.test.js @@ -14,7 +14,10 @@ beforeEach(async (t) => { const memSink = new sink.MEM(); const service = new EikService({ customSink: memSink }); server.register(service.api()); - const address = await server.listen(); + const address = await server.listen({ + host: '127.0.0.1', + port: 0, + }); const token = await cli.login({ server: address, diff --git a/test/login.test.js b/test/login.test.js index 1f863c58..e62e958d 100644 --- a/test/login.test.js +++ b/test/login.test.js @@ -14,7 +14,10 @@ beforeEach(async (t) => { const server = fastify({ logger: false }); const service = new EikService({ customSink: memSink }); server.register(service.api()); - const address = await server.listen(); + const address = await server.listen({ + host: '127.0.0.1', + port: 0, + }); t.context.server = server; t.context.address = address; }); diff --git a/test/meta.test.js b/test/meta.test.js index 2d8353ab..0d4a2c01 100644 --- a/test/meta.test.js +++ b/test/meta.test.js @@ -17,7 +17,10 @@ beforeEach(async (t) => { const memSink = new sink.MEM(); const service = new EikService({ customSink: memSink }); server.register(service.api()); - const address = await server.listen(); + const address = await server.listen({ + host: '127.0.0.1', + port: 0, + }); const token = await cli.login({ server: address, diff --git a/test/publish-files-definitions.test.js b/test/publish-files-definitions.test.js index 9b142422..877e3dda 100644 --- a/test/publish-files-definitions.test.js +++ b/test/publish-files-definitions.test.js @@ -26,7 +26,10 @@ beforeEach(async (t) => { const server = fastify({ logger: false }); const service = new EikService({ customSink: memSink }); server.register(service.api()); - const address = await server.listen(); + const address = await server.listen({ + host: '127.0.0.1', + port: 0, + }); const token = await cli.login({ server: address, diff --git a/test/publish.map.test.js b/test/publish.map.test.js index 250dc2c6..4e515cf2 100644 --- a/test/publish.map.test.js +++ b/test/publish.map.test.js @@ -17,7 +17,10 @@ beforeEach(async (t) => { const server = fastify({ logger: false }); const service = new EikService({ customSink: memSink }); server.register(service.api()); - const address = await server.listen(); + const address = await server.listen({ + host: '127.0.0.1', + port: 0, + }); const token = await cli.login({ server: address, diff --git a/test/publish.package.test.js b/test/publish.package.test.js index 679d530a..1f9cb35e 100644 --- a/test/publish.package.test.js +++ b/test/publish.package.test.js @@ -17,7 +17,10 @@ beforeEach(async (t) => { const server = fastify({ logger: false }); const service = new EikService({ customSink: memSink }); server.register(service.api()); - const address = await server.listen(); + const address = await server.listen({ + host: '127.0.0.1', + port: 0, + }); const token = await cli.login({ server: address, diff --git a/test/utils.test.js b/test/utils.test.js index c398ade3..4a147fe1 100644 --- a/test/utils.test.js +++ b/test/utils.test.js @@ -76,7 +76,10 @@ test('fetch latest version for a given published bundle', async (t) => { [2, { version: '2.1.8' }], ], })); - const address = await server.listen(); + const address = await server.listen({ + host: '127.0.0.1', + port: 0, + }); const version = await f.latestVersion(address, 'pkg', 'foo'); @@ -93,7 +96,10 @@ test('fetch latest version, filtered by major, for a given published bundle', as [2, { version: '2.1.8' }], ], })); - const address = await server.listen(); + const address = await server.listen({ + host: '127.0.0.1', + port: 0, + }); const version = await f.latestVersion(address, 'pkg', 'foo', 1); @@ -104,7 +110,10 @@ test('fetch latest version, filtered by major, for a given published bundle', as test('fetch latest version for a given published bundle, non existant bundle on server', async (t) => { const server = fastify(); - const address = await server.listen(); + const address = await server.listen({ + host: '127.0.0.1', + port: 0, + }); try { await f.latestVersion(address, 'pkg', 'foo'); @@ -122,7 +131,10 @@ test('fetch latest version for a given published bundle, non existant bundle on test('fetch latest version, filtered by major, for a given published bundle', async (t) => { const server = fastify(); server.get('/pkg/foo', async () => ''); - const address = await server.listen(); + const address = await server.listen({ + host: '127.0.0.1', + port: 0, + }); try { await f.latestVersion(address, 'pkg', 'foo'); @@ -140,7 +152,10 @@ test('fetch latest version, filtered by major, for a given published bundle', as test('fetch latest version, invalid versions returned by server', async (t) => { const server = fastify(); server.get('/pkg/foo', async () => ({ versions: 1 })); - const address = await server.listen(); + const address = await server.listen({ + host: '127.0.0.1', + port: 0, + }); t.rejects( f.latestVersion(address, 'pkg', 'foo'), @@ -158,7 +173,10 @@ test('fetch latest version, invalid versions keys returned by server', async (t) ['also not a number', 2], ], })); - const address = await server.listen(); + const address = await server.listen({ + host: '127.0.0.1', + port: 0, + }); t.rejects( f.latestVersion(address, 'pkg', 'foo'), @@ -174,7 +192,10 @@ test('fetch latest version, no bundles yet published', async (t) => { latest: {}, versions: [], })); - const address = await server.listen(); + const address = await server.listen({ + host: '127.0.0.1', + port: 0, + }); const version = await f.latestVersion(address, 'pkg', 'foo'); @@ -199,7 +220,10 @@ test('fetch remote hash for a given version', async (t) => { }, ], })); - const address = await server.listen(); + const address = await server.listen({ + host: '127.0.0.1', + port: 0, + }); const result = await f.integrity(address, 'pkg', 'foo', '1.0.0'); t.equal( diff --git a/test/version.test.js b/test/version.test.js index 9f0a9fcc..370258fb 100644 --- a/test/version.test.js +++ b/test/version.test.js @@ -16,7 +16,10 @@ beforeEach(async (t) => { const server = fastify({ logger: false }); const service = new EikService({ customSink: memSink }); server.register(service.api()); - const address = await server.listen(); + const address = await server.listen({ + host: '127.0.0.1', + port: 0, + }); const token = await cli.login({ server: address, From 8072a7ca8f32e0084304a3e3ac3f1d8800dc6141 Mon Sep 17 00:00:00 2001 From: Richard Walker Date: Mon, 29 Jul 2024 16:35:43 +1200 Subject: [PATCH 2/4] chore: update version to Node v20 --- .github/workflows/publish.yml | 2 +- .github/workflows/test.yml | 2 +- .nvmrc | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 7eef01b9..2af82353 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -18,7 +18,7 @@ jobs: - name: Setup Node.js uses: actions/setup-node@v4 with: - node-version: 16 + node-version: 20 - name: Install dependencies run: npm install diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 0726a41c..cfc0b08e 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -13,7 +13,7 @@ jobs: strategy: matrix: os: [ubuntu-latest, macOS-latest, windows-latest] - node-version: [16] + node-version: [20] runs-on: ${{ matrix.os }} diff --git a/.nvmrc b/.nvmrc index 19c7bdba..2edeafb0 100644 --- a/.nvmrc +++ b/.nvmrc @@ -1 +1 @@ -16 \ No newline at end of file +20 \ No newline at end of file From 1608cf98c2fce51ce97849dcdcb6b2f37c5337cd Mon Sep 17 00:00:00 2001 From: Richard Walker Date: Mon, 29 Jul 2024 19:05:55 +1200 Subject: [PATCH 3/4] test: convert all tests to ESM --- package.json | 8 +-- test/{alias.test.js => alias.test.mjs} | 28 ++++---- .../{alias.test.js => alias.test.mjs} | 30 ++++----- .../{init.test.js => init.test.mjs} | 23 ++++--- .../{integrity.test.js => integrity.test.mjs} | 28 ++++---- .../{login.test.js => login.test.mjs} | 28 ++++---- .../integration/{map.test.js => map.test.mjs} | 28 ++++---- .../{meta.test.js => meta.test.mjs} | 26 +++---- .../{package.test.js => package.test.mjs} | 29 ++++---- .../{integrity.test.js => integrity.test.mjs} | 22 +++--- test/{login.test.js => login.test.mjs} | 24 ++++--- test/{meta.test.js => meta.test.mjs} | 25 +++---- ....js => publish-files-definitions.test.mjs} | 27 ++++---- ...blish.map.test.js => publish.map.test.mjs} | 25 +++---- ...ckage.test.js => publish.package.test.mjs} | 27 ++++---- .../{cleanup.test.js => cleanup.test.mjs} | 17 ++--- test/{utils.js => utils.mjs} | 6 +- test/{utils.test.js => utils.test.mjs} | 67 ++++++++++++------- test/{version.test.js => version.test.mjs} | 28 ++++---- 19 files changed, 268 insertions(+), 228 deletions(-) rename test/{alias.test.js => alias.test.mjs} (89%) rename test/integration/{alias.test.js => alias.test.mjs} (91%) rename test/integration/{init.test.js => init.test.mjs} (78%) rename test/integration/{integrity.test.js => integrity.test.mjs} (85%) rename test/integration/{login.test.js => login.test.mjs} (71%) rename test/integration/{map.test.js => map.test.mjs} (78%) rename test/integration/{meta.test.js => meta.test.mjs} (75%) rename test/integration/{package.test.js => package.test.mjs} (93%) rename test/{integrity.test.js => integrity.test.mjs} (75%) rename test/{login.test.js => login.test.mjs} (72%) rename test/{meta.test.js => meta.test.mjs} (76%) rename test/{publish-files-definitions.test.js => publish-files-definitions.test.mjs} (93%) rename test/{publish.map.test.js => publish.map.test.mjs} (74%) rename test/{publish.package.test.js => publish.package.test.mjs} (92%) rename test/tasks/{cleanup.test.js => cleanup.test.mjs} (68%) rename test/{utils.js => utils.mjs} (85%) rename test/{utils.test.js => utils.test.mjs} (84%) rename test/{version.test.js => version.test.mjs} (81%) diff --git a/package.json b/package.json index 99058bc7..b068bfbb 100644 --- a/package.json +++ b/package.json @@ -17,10 +17,10 @@ "formatters" ], "scripts": { - "test": "cross-env HTTP_PORT=0 LOG_LEVEL=fatal tap --no-coverage-report --no-cov --timeout 0 --test-regex=.test.js --test-ignore=node_modules", - "test:integration": "cross-env HTTP_PORT=0 LOG_LEVEL=fatal tap --no-coverage-report --no-cov --timeout 0 test/integration/**/*.test.js", - "test:unit": "cross-env HTTP_PORT=0 LOG_LEVEL=fatal tap --no-coverage-report --no-cov --timeout 0 test/*.test.js", - "test:tasks": "cross-env HTTP_PORT=0 LOG_LEVEL=fatal tap --no-coverage-report --no-cov --timeout 0 test/tasks/*.test.js", + "test": "cross-env HTTP_PORT=0 LOG_LEVEL=fatal tap --no-coverage-report --no-cov --timeout 0 --test-regex=.test.mjs --test-ignore=node_modules", + "test:integration": "cross-env HTTP_PORT=0 LOG_LEVEL=fatal tap --no-coverage-report --no-cov --timeout 0 test/integration/**/*.test.mjs", + "test:unit": "cross-env HTTP_PORT=0 LOG_LEVEL=fatal tap --no-coverage-report --no-cov --timeout 0 test/*.test.mjs", + "test:tasks": "cross-env HTTP_PORT=0 LOG_LEVEL=fatal tap --no-coverage-report --no-cov --timeout 0 test/tasks/*.test.mjs", "test:ci:integration": "npm run test:integration -- --jobs=1", "test:ci:unit": "npm run test:unit -- --jobs=1", "test:ci:tasks": "npm run test:tasks -- --jobs=1", diff --git a/test/alias.test.js b/test/alias.test.mjs similarity index 89% rename from test/alias.test.js rename to test/alias.test.mjs index 195371de..7ccb0847 100644 --- a/test/alias.test.js +++ b/test/alias.test.mjs @@ -1,16 +1,18 @@ -/* eslint-disable no-param-reassign */ - -'use strict'; - -const os = require('os'); -const fs = require('fs').promises; -const { join, basename } = require('path'); -const { test, beforeEach, afterEach } = require('tap'); -const EikService = require('@eik/service'); -const fastify = require('fastify'); -const { sink } = require('@eik/core'); -const { mockLogger } = require('./utils'); -const cli = require('..'); +import fastify from 'fastify'; +import { promises as fs } from 'fs'; +import os from 'os'; +import { exec as execCallback } from 'child_process'; +import { join, basename } from 'path'; +import { mockLogger } from './utils.mjs'; +import { test, beforeEach, afterEach } from 'tap'; +import EikService from '@eik/service'; +import { sink } from '@eik/core'; +import { fileURLToPath } from 'url'; +import { dirname } from 'path'; +import cli from '../classes/index.js'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); beforeEach(async (t) => { const server = fastify({ logger: false }); diff --git a/test/integration/alias.test.js b/test/integration/alias.test.mjs similarity index 91% rename from test/integration/alias.test.js rename to test/integration/alias.test.mjs index 63750187..6b8f7b68 100644 --- a/test/integration/alias.test.js +++ b/test/integration/alias.test.mjs @@ -1,21 +1,21 @@ -'use strict'; - -/* eslint-disable no-param-reassign */ - -const fastify = require('fastify'); -const fs = require('fs').promises; -const os = require('os'); -const cp = require('child_process'); -const { join, basename } = require('path'); -const { test, beforeEach, afterEach } = require('tap'); -const fetch = require('node-fetch'); -const EikService = require('@eik/service'); -const { sink } = require('@eik/core'); -const cli = require('../..'); +import fastify from 'fastify'; +import { promises as fs } from 'node:fs'; +import os from 'node:os'; +import { exec as execCallback } from 'child_process'; +import { join, basename } from 'node:path'; +import { test, beforeEach, afterEach } from 'tap'; +import EikService from '@eik/service'; +import { sink } from '@eik/core'; +import cli from '../../classes/index.js'; +import { fileURLToPath } from 'url'; +import { dirname } from 'path'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); function exec(cmd) { return new Promise((resolve) => { - cp.exec(cmd, (error, stdout, stderr) => { + execCallback(cmd, (error, stdout, stderr) => { resolve({ error, stdout, stderr }); }); }); diff --git a/test/integration/init.test.js b/test/integration/init.test.mjs similarity index 78% rename from test/integration/init.test.js rename to test/integration/init.test.mjs index 77ba7ef3..4276ae41 100644 --- a/test/integration/init.test.js +++ b/test/integration/init.test.mjs @@ -1,15 +1,18 @@ -'use strict'; +import { promises as fs } from 'fs'; +import os from 'os'; +import { test } from 'tap'; +import { join, basename } from 'path'; +import { readFileSync } from 'fs'; +import { exec as execCallback } from 'child_process'; +import { fileURLToPath } from 'url'; +import { dirname } from 'path'; -const fs = require('fs').promises; -const os = require('os'); -const { test } = require('tap'); -const { join, basename } = require('path'); -const { readFileSync } = require('fs'); -const cp = require('child_process'); +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); function exec(cmd) { return new Promise((resolve) => { - cp.exec(cmd, (error, stdout, stderr) => { + execCallback(cmd, (error, stdout, stderr) => { resolve({ error, stdout, stderr }); }); }); @@ -22,7 +25,7 @@ test('Initializing a new eik.json file', async (t) => { const publishCmd = `${eik} init --cwd ${folder}`; await exec(publishCmd); - const assets = JSON.parse(readFileSync(join(folder, 'eik.json'))); + const assets = JSON.parse(readFileSync(join(folder, 'eik.json'), { encoding: 'utf8' })); t.equal(assets.name, '', 'eik.json "name" field should be empty'); t.equal( @@ -45,7 +48,7 @@ test('Initializing a new eik.json file passing custom values', async (t) => { --server http://localhost:4001`; await exec(publishCmd.split('\n').join(' ')); - const assets = JSON.parse(readFileSync(join(folder, 'eik.json'))); + const assets = JSON.parse(readFileSync(join(folder, 'eik.json'), { encoding: 'utf8' })); t.equal( assets.name, diff --git a/test/integration/integrity.test.js b/test/integration/integrity.test.mjs similarity index 85% rename from test/integration/integrity.test.js rename to test/integration/integrity.test.mjs index 90b8f09c..dd1fc1e1 100644 --- a/test/integration/integrity.test.js +++ b/test/integration/integrity.test.mjs @@ -1,19 +1,21 @@ -'use strict'; - -/* eslint-disable no-param-reassign */ -const fastify = require('fastify'); -const fs = require('fs').promises; -const os = require('os'); -const cp = require('child_process'); -const { join, basename } = require('path'); -const { test, beforeEach, afterEach } = require('tap'); -const EikService = require('@eik/service'); -const { sink } = require('@eik/core'); -const cli = require('../..'); +import fastify from 'fastify'; +import { promises as fs } from 'fs'; +import os from 'os'; +import { exec as execCallback } from 'child_process'; +import { join, basename } from 'path'; +import { test, beforeEach, afterEach } from 'tap'; +import EikService from '@eik/service'; +import { sink } from '@eik/core'; +import cli from '../../classes/index.js'; +import { fileURLToPath } from 'url'; +import { dirname } from 'path'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); function exec(cmd) { return new Promise((resolve) => { - cp.exec(cmd, (error, stdout, stderr) => { + execCallback(cmd, (error, stdout, stderr) => { resolve({ error, stdout, stderr }); }); }); diff --git a/test/integration/login.test.js b/test/integration/login.test.mjs similarity index 71% rename from test/integration/login.test.js rename to test/integration/login.test.mjs index f4d4cde4..b6811c5a 100644 --- a/test/integration/login.test.js +++ b/test/integration/login.test.mjs @@ -1,20 +1,20 @@ -/* eslint-disable no-param-reassign */ - -'use strict'; - -// const util = require('util'); -const fastify = require('fastify'); -const fs = require('fs').promises; -const os = require('os'); -const cp = require('child_process'); -const { join, basename } = require('path'); -const { test, beforeEach, afterEach } = require('tap'); -const EikService = require('@eik/service'); -const { sink } = require('@eik/core'); +import fastify from 'fastify'; +import { promises as fs } from 'fs'; +import os from 'os'; +import { exec as execCallback } from 'child_process'; +import { join, basename } from 'path'; +import { test, beforeEach, afterEach } from 'tap'; +import EikService from '@eik/service'; +import { sink } from '@eik/core'; +import { fileURLToPath } from 'url'; +import { dirname } from 'path'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); function exec(cmd) { return new Promise((resolve) => { - cp.exec(cmd, (error, stdout, stderr) => { + execCallback(cmd, (error, stdout, stderr) => { resolve({ error, stdout, stderr }); }); }); diff --git a/test/integration/map.test.js b/test/integration/map.test.mjs similarity index 78% rename from test/integration/map.test.js rename to test/integration/map.test.mjs index a27eafcb..64d6c882 100644 --- a/test/integration/map.test.js +++ b/test/integration/map.test.mjs @@ -1,20 +1,22 @@ -'use strict'; +import fastify from 'fastify'; +import { promises as fs } from 'fs'; +import os from 'os'; +import { exec as execCallback } from 'child_process'; +import { join, basename } from 'path'; +import { test, beforeEach, afterEach } from 'tap'; +import fetch from 'node-fetch'; +import EikService from '@eik/service'; +import { sink } from '@eik/core'; +import cli from '../../classes/index.js'; +import { fileURLToPath } from 'url'; +import { dirname } from 'path'; -/* eslint-disable no-param-reassign */ -const fastify = require('fastify'); -const fs = require('fs').promises; -const os = require('os'); -const cp = require('child_process'); -const { join, basename } = require('path'); -const { test, beforeEach, afterEach } = require('tap'); -const fetch = require('node-fetch'); -const EikService = require('@eik/service'); -const { sink } = require('@eik/core'); -const cli = require('../..'); +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); function exec(cmd) { return new Promise((resolve) => { - cp.exec(cmd, (error, stdout, stderr) => { + execCallback(cmd, (error, stdout, stderr) => { resolve({ error, stdout, stderr }); }); }); diff --git a/test/integration/meta.test.js b/test/integration/meta.test.mjs similarity index 75% rename from test/integration/meta.test.js rename to test/integration/meta.test.mjs index 83899c95..aa1a2f4e 100644 --- a/test/integration/meta.test.js +++ b/test/integration/meta.test.mjs @@ -1,19 +1,21 @@ -'use strict'; +import fastify from 'fastify'; +import { promises as fs } from 'fs'; +import os from 'os'; +import { exec as execCallback } from 'child_process'; +import { join, basename } from 'path'; +import { test, beforeEach, afterEach } from 'tap'; +import EikService from '@eik/service'; +import { sink } from '@eik/core'; +import cli from '../../classes/index.js'; +import { fileURLToPath } from 'url'; +import { dirname } from 'path'; -/* eslint-disable no-param-reassign */ -const fastify = require('fastify'); -const fs = require('fs').promises; -const os = require('os'); -const cp = require('child_process'); -const { join, basename } = require('path'); -const { test, beforeEach, afterEach } = require('tap'); -const EikService = require('@eik/service'); -const { sink } = require('@eik/core'); -const cli = require('../..'); +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); function exec(cmd) { return new Promise((resolve) => { - cp.exec(cmd, (error, stdout, stderr) => { + execCallback(cmd, (error, stdout, stderr) => { resolve({ error, stdout, stderr }); }); }); diff --git a/test/integration/package.test.js b/test/integration/package.test.mjs similarity index 93% rename from test/integration/package.test.js rename to test/integration/package.test.mjs index ec48cd90..7cdbd68c 100644 --- a/test/integration/package.test.js +++ b/test/integration/package.test.mjs @@ -1,20 +1,21 @@ -'use strict'; - -/* eslint-disable no-param-reassign */ -const fastify = require('fastify'); -const fs = require('fs').promises; -const os = require('os'); -const cp = require('child_process'); -const { join, basename } = require('path'); -const { test, beforeEach, afterEach } = require('tap'); -const fetch = require('node-fetch'); -const EikService = require('@eik/service'); -const { sink } = require('@eik/core'); -const cli = require('../..'); +import fastify from 'fastify'; +import { promises as fs } from 'fs'; +import os from 'os'; +import { exec as execCallback } from 'child_process'; +import { join, basename } from 'path'; +import { test, beforeEach, afterEach } from 'tap'; +import EikService from '@eik/service'; +import { sink } from '@eik/core'; +import cli from '../../classes/index.js'; +import { fileURLToPath } from 'url'; +import { dirname } from 'path'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); function exec(cmd) { return new Promise((resolve) => { - cp.exec(cmd, (error, stdout, stderr) => { + execCallback(cmd, (error, stdout, stderr) => { resolve({ error, stdout, stderr }); }); }); diff --git a/test/integrity.test.js b/test/integrity.test.mjs similarity index 75% rename from test/integrity.test.js rename to test/integrity.test.mjs index 55ce4c0b..6bc58840 100644 --- a/test/integrity.test.js +++ b/test/integrity.test.mjs @@ -1,13 +1,17 @@ -/* eslint-disable no-param-reassign */ +import fastify from 'fastify'; +import { promises as fs } from 'fs'; +import os from 'os'; +import { join, basename } from 'path'; +import { mockLogger } from './utils.mjs'; +import { test, beforeEach, afterEach } from 'tap'; +import EikService from '@eik/service'; +import { sink } from '@eik/core'; +import { fileURLToPath } from 'url'; +import { dirname } from 'path'; +import cli from '../classes/index.js'; -'use strict'; - -const { join } = require('path'); -const fastify = require('fastify'); -const { test, beforeEach, afterEach } = require('tap'); -const EikService = require('@eik/service'); -const { sink } = require('@eik/core'); -const cli = require('..'); +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); beforeEach(async (t) => { const server = fastify({ logger: false }); diff --git a/test/login.test.js b/test/login.test.mjs similarity index 72% rename from test/login.test.js rename to test/login.test.mjs index e62e958d..27364524 100644 --- a/test/login.test.js +++ b/test/login.test.mjs @@ -1,13 +1,17 @@ -/* eslint-disable no-param-reassign */ - -'use strict'; - -const { test, beforeEach, afterEach } = require('tap'); -const fastify = require('fastify'); -const EikService = require('@eik/service'); -const { sink } = require('@eik/core'); -const { mockLogger } = require('./utils'); -const cli = require('..'); +import fastify from 'fastify'; +import { promises as fs } from 'fs'; +import os from 'os'; +import { join, basename } from 'path'; +import { mockLogger } from './utils.mjs'; +import { test, beforeEach, afterEach } from 'tap'; +import EikService from '@eik/service'; +import { sink } from '@eik/core'; +import { fileURLToPath } from 'url'; +import { dirname } from 'path'; +import cli from '../classes/index.js'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); beforeEach(async (t) => { const memSink = new sink.MEM(); diff --git a/test/meta.test.js b/test/meta.test.mjs similarity index 76% rename from test/meta.test.js rename to test/meta.test.mjs index 0d4a2c01..8fe80e44 100644 --- a/test/meta.test.js +++ b/test/meta.test.mjs @@ -1,16 +1,17 @@ -/* eslint-disable no-param-reassign */ +import fastify from 'fastify'; +import { promises as fs } from 'fs'; +import os from 'os'; +import { join, basename } from 'path'; +import { mockLogger } from './utils.mjs'; +import { test, beforeEach, afterEach } from 'tap'; +import EikService from '@eik/service'; +import { sink } from '@eik/core'; +import { fileURLToPath } from 'url'; +import { dirname } from 'path'; +import cli from '../classes/index.js'; -'use strict'; - -const os = require('os'); -const fs = require('fs').promises; -const { join, basename } = require('path'); -const fastify = require('fastify'); -const { test, beforeEach, afterEach } = require('tap'); -const EikService = require('@eik/service'); -const { sink } = require('@eik/core'); -const { mockLogger } = require('./utils'); -const cli = require('..'); +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); beforeEach(async (t) => { const server = fastify({ logger: false }); diff --git a/test/publish-files-definitions.test.js b/test/publish-files-definitions.test.mjs similarity index 93% rename from test/publish-files-definitions.test.js rename to test/publish-files-definitions.test.mjs index 877e3dda..9816fc3f 100644 --- a/test/publish-files-definitions.test.js +++ b/test/publish-files-definitions.test.mjs @@ -1,14 +1,17 @@ -/* eslint-disable no-param-reassign */ - -const os = require('os'); -const fs = require('fs').promises; -const { join, basename } = require('path'); -const { test, beforeEach, afterEach } = require('tap'); -const fastify = require('fastify'); -const EikService = require('@eik/service'); -const { sink } = require('@eik/core'); -const { copySync } = require('fs-extra'); -const cli = require('../classes'); +import fastify from 'fastify'; +import { promises as fs } from 'fs'; +import os from 'os'; +import { join, basename } from 'path'; +import { test, beforeEach, afterEach } from 'tap'; +import EikService from '@eik/service'; +import { sink } from '@eik/core'; +import fsExtra from 'fs-extra'; +import { fileURLToPath } from 'url'; +import { dirname } from 'path'; +import cli from '../classes/index.js'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); const config = (files, server, token, cwd) => ({ logger: null, @@ -37,7 +40,7 @@ beforeEach(async (t) => { }); const cwd = await fs.mkdtemp(join(os.tmpdir(), basename(__filename))); - copySync(join(__dirname, './fixtures'), join(cwd, 'fixtures')); + fsExtra.copySync(join(__dirname, './fixtures'), join(cwd, 'fixtures')); t.context.server = server; t.context.address = address; diff --git a/test/publish.map.test.js b/test/publish.map.test.mjs similarity index 74% rename from test/publish.map.test.js rename to test/publish.map.test.mjs index 4e515cf2..ab1d71ff 100644 --- a/test/publish.map.test.js +++ b/test/publish.map.test.mjs @@ -1,16 +1,17 @@ -/* eslint-disable no-param-reassign */ +import fastify from 'fastify'; +import { promises as fs } from 'fs'; +import os from 'os'; +import { join, basename } from 'path'; +import { mockLogger } from './utils.mjs'; +import { test, beforeEach, afterEach } from 'tap'; +import EikService from '@eik/service'; +import { sink } from '@eik/core'; +import { fileURLToPath } from 'url'; +import { dirname } from 'path'; +import cli from '../classes/index.js'; -'use strict'; - -const os = require('os'); -const fs = require('fs').promises; -const { join, basename } = require('path'); -const fastify = require('fastify'); -const { test, beforeEach, afterEach } = require('tap'); -const EikService = require('@eik/service'); -const { sink } = require('@eik/core'); -const { mockLogger } = require('./utils'); -const cli = require('..'); +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); beforeEach(async (t) => { const memSink = new sink.MEM(); diff --git a/test/publish.package.test.js b/test/publish.package.test.mjs similarity index 92% rename from test/publish.package.test.js rename to test/publish.package.test.mjs index 1f9cb35e..cabc552b 100644 --- a/test/publish.package.test.js +++ b/test/publish.package.test.mjs @@ -1,16 +1,17 @@ -/* eslint-disable no-param-reassign */ - -'use strict'; - -const os = require('os'); -const fs = require('fs').promises; -const { join, basename } = require('path'); -const { test, beforeEach, afterEach } = require('tap'); -const fastify = require('fastify'); -const EikService = require('@eik/service'); -const { sink } = require('@eik/core'); -const { mockLogger } = require('./utils'); -const cli = require('..'); +import fastify from 'fastify'; +import { promises as fs } from 'fs'; +import os from 'os'; +import { join, basename } from 'path'; +import { mockLogger } from './utils.mjs'; +import { test, beforeEach, afterEach } from 'tap'; +import EikService from '@eik/service'; +import { sink } from '@eik/core'; +import { fileURLToPath } from 'url'; +import { dirname } from 'path'; +import cli from '../classes/index.js'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); beforeEach(async (t) => { const memSink = new sink.MEM(); diff --git a/test/tasks/cleanup.test.js b/test/tasks/cleanup.test.mjs similarity index 68% rename from test/tasks/cleanup.test.js rename to test/tasks/cleanup.test.mjs index d0fff319..45fa6b5c 100644 --- a/test/tasks/cleanup.test.js +++ b/test/tasks/cleanup.test.mjs @@ -1,12 +1,13 @@ -/* eslint-disable no-param-reassign */ +import { promises as fs } from 'fs'; +import { join } from 'path'; +import { test, beforeEach, afterEach } from 'tap'; +import rimraf from 'rimraf'; +import CleanupTask from '../../classes/publish/package/tasks/cleanup.js'; +import { fileURLToPath } from 'url'; +import { dirname } from 'path'; -'use strict'; - -const fs = require('fs').promises; -const { join } = require('path'); -const { test, beforeEach, afterEach } = require('tap'); -const rimraf = require('rimraf'); -const CleanupTask = require('../../classes/publish/package/tasks/cleanup'); +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); beforeEach(async (t) => { const path = join(__dirname, '.eik'); diff --git a/test/utils.js b/test/utils.mjs similarity index 85% rename from test/utils.js rename to test/utils.mjs index c752acf6..04dec061 100644 --- a/test/utils.js +++ b/test/utils.mjs @@ -1,7 +1,3 @@ -/* eslint-disable no-underscore-dangle */ - -'use strict'; - const mockLogger = () => { const logs = { fatal: '', @@ -34,4 +30,4 @@ const mockLogger = () => { return { logs, logger }; }; -module.exports.mockLogger = mockLogger; +export { mockLogger }; diff --git a/test/utils.test.js b/test/utils.test.mjs similarity index 84% rename from test/utils.test.js rename to test/utils.test.mjs index 4a147fe1..5cc6d798 100644 --- a/test/utils.test.js +++ b/test/utils.test.mjs @@ -1,14 +1,28 @@ -'use strict'; - -const os = require('os'); -const { join, basename } = require('path'); -const fs = require('fs'); -const crypto = require('crypto'); -const { test } = require('tap'); -const fastify = require('fastify'); -const j = require('../utils/json'); -const h = require('../utils/hash'); -const f = require('../utils/http'); +import fastify from 'fastify'; +import { promises as fs } from 'fs'; +import os from 'os'; +import { join, basename } from 'path'; +import { test, beforeEach, afterEach } from 'tap'; +import { fileURLToPath } from 'url'; +import { dirname } from 'path'; +import crypto from 'crypto'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); + +// const os = require('os'); +// const { join, basename } = require('path'); +// const fs = require('fs'); +// const crypto = require('crypto'); +// const { test } = require('tap'); +// const fastify = require('fastify'); +// const j = require('../utils/json'); +// const h = require('../utils/hash'); +// const f = require('../utils/http'); + +import j from '../utils/json/index.js'; +import h from '../utils/hash/index.js'; +import f from '../utils/http/index.js'; test('calculate file hash', async (t) => { const hash = await h.file(join(__dirname, 'fixtures', 'client.js')); @@ -235,14 +249,14 @@ test('fetch remote hash for a given version', async (t) => { }); test('write JSON file - object - file relative to cwd', async (t) => { - const cwd = await fs.promises.mkdtemp( + const cwd = await fs.mkdtemp( join(os.tmpdir(), basename(__filename)), ); await j.write( { version: '1.0.0', integrity: [] }, { cwd, filename: '.eikrc' }, ); - const eikrc = fs.readFileSync(join(cwd, '.eikrc')); + const eikrc = await fs.readFile(join(cwd, '.eikrc')); const { version, integrity } = JSON.parse(eikrc); t.equal(version, '1.0.0', 'Version should be 1.0.0'); @@ -250,11 +264,11 @@ test('write JSON file - object - file relative to cwd', async (t) => { }); test('write JSON file - object - file absolute path', async (t) => { - const cwd = await fs.promises.mkdtemp( + const cwd = await fs.mkdtemp( join(os.tmpdir(), basename(__filename)), ); await j.write({ prop: 'val' }, { filename: join(cwd, 'test.json') }); - const eikrc = fs.readFileSync(join(cwd, 'test.json')); + const eikrc = await fs.readFile(join(cwd, 'test.json'), { encoding: 'utf8' }); const { prop } = JSON.parse(eikrc); t.equal(prop, 'val', 'Prop should equal val'); @@ -262,48 +276,49 @@ test('write JSON file - object - file absolute path', async (t) => { test('write JSON file - string - file relative path', async (t) => { await j.write({ prop: 'val' }, './test-using-relative.json'); - const eikrc = fs.readFileSync( + const eikrc = await fs.readFile( join(__dirname, '../test-using-relative.json'), + { encoding: 'utf8' }, ); const { prop } = JSON.parse(eikrc); - await fs.unlinkSync(join(__dirname, '../test-using-relative.json')); + await fs.unlink(join(__dirname, '../test-using-relative.json')); t.equal(prop, 'val', 'Prop should equal val'); }); test('write JSON file - string - file absolute path', async (t) => { - const cwd = await fs.promises.mkdtemp( + const cwd = await fs.mkdtemp( join(os.tmpdir(), basename(__filename)), ); await j.write({ prop: 'val' }, join(cwd, 'test3.json')); - const eikrc = fs.readFileSync(join(cwd, 'test3.json')); + const eikrc = await fs.readFile(join(cwd, 'test3.json'), { encoding: 'utf8' }); const { prop } = JSON.parse(eikrc); t.equal(prop, 'val', 'Prop should equal val'); }); test('read JSON file - object - file relative path', async (t) => { - const cwd = await fs.promises.mkdtemp( + const cwd = await fs.mkdtemp( join(os.tmpdir(), basename(__filename)), ); - fs.writeFileSync(join(cwd, 'test3.json'), JSON.stringify({ key: 'val' })); + await fs.writeFile(join(cwd, 'test3.json'), JSON.stringify({ key: 'val' })); const json = await j.read({ cwd, filename: './test3.json' }); t.equal(json.key, 'val', 'Key should equal val'); }); test('read JSON file - object - file absolute path', async (t) => { - const cwd = await fs.promises.mkdtemp( + const cwd = await fs.mkdtemp( join(os.tmpdir(), basename(__filename)), ); - fs.writeFileSync(join(cwd, 'test3.json'), JSON.stringify({ key: 'val' })); + await fs.writeFile(join(cwd, 'test3.json'), JSON.stringify({ key: 'val' })); const json = await j.read({ filename: join(cwd, './test3.json') }); t.equal(json.key, 'val', 'Key should equal val'); }); test('read JSON file - string - file relative path', async (t) => { - fs.writeFileSync( + await fs.writeFile( join(__dirname, '../test-read-json.json'), JSON.stringify({ key: 'val' }), ); @@ -313,10 +328,10 @@ test('read JSON file - string - file relative path', async (t) => { }); test('read JSON file - string - file absolute path', async (t) => { - const cwd = await fs.promises.mkdtemp( + const cwd = await fs.mkdtemp( join(os.tmpdir(), basename(__filename)), ); - fs.writeFileSync( + await fs.writeFile( join(cwd, './test-read-json-2.json'), JSON.stringify({ key: 'val' }), ); diff --git a/test/version.test.js b/test/version.test.mjs similarity index 81% rename from test/version.test.js rename to test/version.test.mjs index 370258fb..dfbff693 100644 --- a/test/version.test.js +++ b/test/version.test.mjs @@ -1,15 +1,17 @@ -/* eslint-disable no-param-reassign */ - -'use strict'; - -const fs = require('fs'); -const os = require('os'); -const { join, basename } = require('path'); -const { test, beforeEach, afterEach } = require('tap'); -const fastify = require('fastify'); -const EikService = require('@eik/service'); -const { sink } = require('@eik/core'); -const cli = require('..'); +import fastify from 'fastify'; +import { promises as fs } from 'fs'; +import os from 'os'; +import { join, basename } from 'path'; +import { mockLogger } from './utils.mjs'; +import { test, beforeEach, afterEach } from 'tap'; +import EikService from '@eik/service'; +import { sink } from '@eik/core'; +import { fileURLToPath } from 'url'; +import { dirname } from 'path'; +import cli from '../classes/index.js'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); beforeEach(async (t) => { const memSink = new sink.MEM(); @@ -26,7 +28,7 @@ beforeEach(async (t) => { key: 'change_me', }); - const cwd = await fs.mkdtempSync(join(os.tmpdir(), basename(__filename))); + const cwd = await fs.mkdtemp(join(os.tmpdir(), basename(__filename))); t.context.server = server; t.context.address = address; From 711c42eb2070aca5818bb2383b2d50c7b63e27c0 Mon Sep 17 00:00:00 2001 From: Richard Walker Date: Mon, 29 Jul 2024 19:07:55 +1200 Subject: [PATCH 4/4] test: remove dead code --- test/utils.test.mjs | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/test/utils.test.mjs b/test/utils.test.mjs index 5cc6d798..1fdbfe80 100644 --- a/test/utils.test.mjs +++ b/test/utils.test.mjs @@ -6,24 +6,13 @@ import { test, beforeEach, afterEach } from 'tap'; import { fileURLToPath } from 'url'; import { dirname } from 'path'; import crypto from 'crypto'; - -const __filename = fileURLToPath(import.meta.url); -const __dirname = dirname(__filename); - -// const os = require('os'); -// const { join, basename } = require('path'); -// const fs = require('fs'); -// const crypto = require('crypto'); -// const { test } = require('tap'); -// const fastify = require('fastify'); -// const j = require('../utils/json'); -// const h = require('../utils/hash'); -// const f = require('../utils/http'); - import j from '../utils/json/index.js'; import h from '../utils/hash/index.js'; import f from '../utils/http/index.js'; +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); + test('calculate file hash', async (t) => { const hash = await h.file(join(__dirname, 'fixtures', 'client.js')); t.equal(