This is an automated email from the ASF dual-hosted git repository. suyanhanx pushed a commit to branch random-root-nodejs-test in repository https://gitbox.apache.org/repos/asf/incubator-opendal.git
commit 4729c1739384934e2ac44170148e275b51207ed8 Author: suyanhanx <[email protected]> AuthorDate: Wed Nov 1 19:38:31 2023 +0800 fix: nodejs test adapt `OPENDAL_DISABLE_RANDOM_ROOT` Signed-off-by: suyanhanx <[email protected]> --- bindings/nodejs/.env.example | 5 ---- bindings/nodejs/package.json | 7 +++-- bindings/nodejs/tests/service.test.mjs | 6 ++++ bindings/nodejs/tests/suites/async.suite.mjs | 16 +++++------ bindings/nodejs/tests/suites/index.mjs | 28 ++++++++++--------- bindings/nodejs/tests/suites/sync.suite.mjs | 16 +++++------ bindings/nodejs/tests/utils.mjs | 41 ++++++++++++++++------------ 7 files changed, 65 insertions(+), 54 deletions(-) diff --git a/bindings/nodejs/.env.example b/bindings/nodejs/.env.example deleted file mode 100644 index 88f75b60a..000000000 --- a/bindings/nodejs/.env.example +++ /dev/null @@ -1,5 +0,0 @@ -export AWS_ACCESS_KEY_ID= -export AWS_SECRET_ACCESS_KEY= -export AWS_S3_REGION= -export AWS_S3_ENDPOINT= -export AWS_BUCKET= \ No newline at end of file diff --git a/bindings/nodejs/package.json b/bindings/nodejs/package.json index b99b268e8..86626db33 100644 --- a/bindings/nodejs/package.json +++ b/bindings/nodejs/package.json @@ -74,12 +74,13 @@ { "files": "./**/*.{js,ts,mjs}", "options": { + "arrowParens": "always", + "parser": "typescript", "printWidth": 120, "semi": false, - "trailingComma": "all", "singleQuote": true, - "arrowParens": "always", - "parser": "typescript" + "tabWidth": 2, + "trailingComma": "all" } } ] diff --git a/bindings/nodejs/tests/service.test.mjs b/bindings/nodejs/tests/service.test.mjs index 2dce9f4ea..bcaed0945 100644 --- a/bindings/nodejs/tests/service.test.mjs +++ b/bindings/nodejs/tests/service.test.mjs @@ -17,7 +17,13 @@ * under the License. */ +import path from 'path' + +import dotenv from 'dotenv' + import { runner } from './suites/index.mjs' import { loadTestSchemeFromEnv } from './utils.mjs' +dotenv.config({ path: path.resolve(__dirname, '../../../.env'), debug: true }) + runner('Behavior Test', loadTestSchemeFromEnv()) diff --git a/bindings/nodejs/tests/suites/async.suite.mjs b/bindings/nodejs/tests/suites/async.suite.mjs index cd01f1d22..8214c1b17 100644 --- a/bindings/nodejs/tests/suites/async.suite.mjs +++ b/bindings/nodejs/tests/suites/async.suite.mjs @@ -22,13 +22,13 @@ import { expect, test } from 'vitest' import { generateBytes } from '../utils.mjs' export function run(operator) { - test('async stat not exist files', async () => { - const filename = `random_file_${randomUUID()}` + test('async stat not exist files', async () => { + const filename = `random_file_${randomUUID()}` - try { - await operator.stat(filename) - } catch (error) { - assert.ok(error.message.includes('NotFound')) - } - }) + try { + await operator.stat(filename) + } catch (error) { + assert.ok(error.message.includes('NotFound')) + } + }) } diff --git a/bindings/nodejs/tests/suites/index.mjs b/bindings/nodejs/tests/suites/index.mjs index 4b535ca4e..7ed5f613e 100644 --- a/bindings/nodejs/tests/suites/index.mjs +++ b/bindings/nodejs/tests/suites/index.mjs @@ -19,25 +19,27 @@ import { describe } from 'vitest' import { Operator } from '../../index.js' -import { loadConfigFromEnv } from '../utils.mjs' +import { checkRandomRootEnabled, generateRandomRoot, loadConfigFromEnv } from '../utils.mjs' import { run as AsyncIOTestRun } from './async.suite.mjs' import { run as SyncIOTestRun } from './sync.suite.mjs' export function runner(testName, scheme) { - if (testName === null || testName === undefined) { - throw new Error('The scheme should not be `null` or `undefined`. ') - } + if (!scheme) { + console.warn('The scheme is empty. Test will be skipped.') + return + } - if (testName === '') { - return - } + const config = loadConfigFromEnv(scheme) - const config = loadConfigFromEnv(scheme) - const operator = scheme ? new Operator(scheme, config) : undefined + if (checkRandomRootEnabled()) { + config.root = generateRandomRoot() + } - describe.skipIf(!operator)(testName, () => { - AsyncIOTestRun(operator) - SyncIOTestRun(operator) - }) + const operator = scheme ? new Operator(scheme, config) : undefined + + describe.skipIf(!operator)(testName, () => { + AsyncIOTestRun(operator) + SyncIOTestRun(operator) + }) } diff --git a/bindings/nodejs/tests/suites/sync.suite.mjs b/bindings/nodejs/tests/suites/sync.suite.mjs index 3ca859cee..473d121c6 100644 --- a/bindings/nodejs/tests/suites/sync.suite.mjs +++ b/bindings/nodejs/tests/suites/sync.suite.mjs @@ -22,13 +22,13 @@ import { expect, test } from 'vitest' import { generateBytes } from '../utils.mjs' export function run(operator) { - test('sync stat not exist files', () => { - const filename = `random_file_${randomUUID()}` + test('sync stat not exist files', () => { + const filename = `random_file_${randomUUID()}` - try { - operator.statSync(filename) - } catch (error) { - assert.ok(error.message.includes('NotFound')) - } - }) + try { + operator.statSync(filename) + } catch (error) { + assert.ok(error.message.includes('NotFound')) + } + }) } diff --git a/bindings/nodejs/tests/utils.mjs b/bindings/nodejs/tests/utils.mjs index 01a94b42b..8998f6cd1 100644 --- a/bindings/nodejs/tests/utils.mjs +++ b/bindings/nodejs/tests/utils.mjs @@ -17,33 +17,40 @@ * under the License. */ -const path = require('path') +import crypto from 'node:crypto' export function generateBytes() { - const size = Math.floor(Math.random() * 1024) + 1 - const content = [] + const size = Math.floor(Math.random() * 1024) + 1 + const content = [] - for (let i = 0; i < size; i++) { - content.push(Math.floor(Math.random() * 256)) - } + for (let i = 0; i < size; i++) { + content.push(Math.floor(Math.random() * 256)) + } - return Buffer.from(content) + return Buffer.from(content) } export function loadTestSchemeFromEnv() { - require('dotenv').config({ path: path.resolve(__dirname, '../../../.env'), debug: true }) - return process.env.OPENDAL_TEST + return process.env.OPENDAL_TEST +} + +export function checkRandomRootEnabled() { + return process.env.OPENDAL_DISABLE_RANDOM_ROOT !== 'false' +} + +export function generateRandomRoot() { + return `/opendal_${crypto.randomUUID()}` } export function loadConfigFromEnv(scheme) { - if (!scheme) return {} + if (!scheme) return {} - const prefix = `opendal_${scheme}_` + const prefix = `opendal_${scheme}_` - return Object.fromEntries( - Object.entries(process.env) - .map(([key, value]) => [key.toLowerCase(), value]) - .filter(([key]) => key.startsWith(prefix)) - .map(([key, value]) => [key.replace(prefix, ''), value]), - ) + return Object.fromEntries( + Object.entries(process.env) + .map(([key, value]) => [key.toLowerCase(), value]) + .filter(([key]) => key.startsWith(prefix)) + .map(([key, value]) => [key.replace(prefix, ''), value]), + ) }
