Skip to content

Commit

Permalink
tests ~ fix npx biome lint . complaint (lint/complexity/noForEach)
Browse files Browse the repository at this point in the history
  • Loading branch information
rivy committed Apr 21, 2024
1 parent 358f19a commit 96dbbfa
Show file tree
Hide file tree
Showing 3 changed files with 207 additions and 216 deletions.
24 changes: 12 additions & 12 deletions test/dist.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ function isObject(obj) {
function flattenToValues(obj) {
const values = [];
if (isObject(obj) || Array.isArray(obj)) {
Object.keys(obj).forEach((key) => {
for (const key of Object.keys(obj)) {

Check failure on line 42 in test/dist.test.js

View workflow job for this annotation

GitHub Actions / Build (windows-latest, 14.x)

Unexpected loop, use map or reduce instead

Check failure on line 42 in test/dist.test.js

View workflow job for this annotation

GitHub Actions / Build (windows-latest, 16.x)

Unexpected loop, use map or reduce instead
// eslint-disable-next-line security/detect-object-injection
values.push(...flattenToValues(obj[key]));
});
}
} else values.push(obj);
return values;
}
Expand All @@ -65,13 +65,13 @@ if (!process.env.npm_config_test_dist) {
t.is(typeof mCJS, typeof mESM);
t.is(Object.keys(mCJS).length, packageAPI.length);
t.is(Object.keys(mCJS).length, Object.keys(mESM).length);
packageAPI.forEach((key) => {
for (const key of packageAPI) {

Check failure on line 68 in test/dist.test.js

View workflow job for this annotation

GitHub Actions / Build (windows-latest, 14.x)

Unexpected loop, use map or reduce instead

Check failure on line 68 in test/dist.test.js

View workflow job for this annotation

GitHub Actions / Build (windows-latest, 16.x)

Unexpected loop, use map or reduce instead
/* eslint-disable security/detect-object-injection */
t.is(typeof mCJS[key], 'function');
t.is(typeof mCJS[key], typeof mESM[key]);
t.deepEqual(mCJS[key](), mESM[key]());
/* eslint-enable security/detect-object-injection */
});
}
});
}

Expand Down Expand Up @@ -111,15 +111,15 @@ if (!process.env.npm_config_test_dist) {
const exports_ = pkg.exports;
const paths = flattenToValues(exports_);
t.log({ exportsPaths: paths });
paths.forEach((p) => {
for (const p of paths) {

Check failure on line 114 in test/dist.test.js

View workflow job for this annotation

GitHub Actions / Build (windows-latest, 14.x)

Unexpected loop, use map or reduce instead

Check failure on line 114 in test/dist.test.js

View workflow job for this annotation

GitHub Actions / Build (windows-latest, 16.x)

Unexpected loop, use map or reduce instead
const path_ = path.resolve(__dirname, packagePath, '..', p);
// eslint-disable-next-line security/detect-non-literal-fs-filename
const exists = fs.existsSync(path_);
if (!exists) {
t.log({ path_, exists });
}
t.true(exists);
});
}
});

test("package 'exports' sub-paths support older tools", (t) => {
Expand All @@ -129,38 +129,38 @@ if (!process.env.npm_config_test_dist) {
const subPaths = Object.keys(exports_);
t.log({ subPaths });
// test for sub-path file/directory existence
subPaths.forEach((p) => {
for (const p of subPaths) {

Check failure on line 132 in test/dist.test.js

View workflow job for this annotation

GitHub Actions / Build (windows-latest, 14.x)

Unexpected loop, use map or reduce instead

Check failure on line 132 in test/dist.test.js

View workflow job for this annotation

GitHub Actions / Build (windows-latest, 16.x)

Unexpected loop, use map or reduce instead
const path_ = path.resolve(__dirname, packagePath, '..', p);
// eslint-disable-next-line security/detect-non-literal-fs-filename
const exists = fs.existsSync(path_);
if (!exists) {
t.log({ exists, path_ });
}
t.true(exists);
});
}
const files = pkg.files;
// test that sub-path file/directory is included in 'files'
subPaths.forEach((p) => {
for (const p of subPaths) {

Check failure on line 143 in test/dist.test.js

View workflow job for this annotation

GitHub Actions / Build (windows-latest, 14.x)

Unexpected loop, use map or reduce instead

Check failure on line 143 in test/dist.test.js

View workflow job for this annotation

GitHub Actions / Build (windows-latest, 16.x)

Unexpected loop, use map or reduce instead
const included = p === '.' || files.includes(p.replace(/^.\//, ''));
if (!included) {
t.log({ included, p, files });
}
t.true(included);
});
}
});

test("package 'files' all exist", (t) => {
const files_ = pkg.files;
t.log({ files: files_ });
files_.forEach((p) => {
for (const p of files_) {

Check failure on line 155 in test/dist.test.js

View workflow job for this annotation

GitHub Actions / Build (windows-latest, 14.x)

Unexpected loop, use map or reduce instead

Check failure on line 155 in test/dist.test.js

View workflow job for this annotation

GitHub Actions / Build (windows-latest, 16.x)

Unexpected loop, use map or reduce instead
const path_ = path.resolve(__dirname, packagePath, '..', p);
// eslint-disable-next-line security/detect-non-literal-fs-filename
const exists = fs.existsSync(path_);
if (!exists) {
t.log({ path_, exists });
}
t.true(exists);
});
}
});

test("package 'audit' has no warnings", (t) => {
Expand Down
229 changes: 109 additions & 120 deletions test/integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ test('api', (t) => {

t.is(typeof mod, 'function');
t.deepEqual(Object.keys(mod).sort(), api.sort());
api.forEach((key) => {
for (const key of api) {

Check failure on line 68 in test/integration.test.js

View workflow job for this annotation

GitHub Actions / Build (windows-latest, 14.x)

Unexpected loop, use map or reduce instead

Check failure on line 68 in test/integration.test.js

View workflow job for this annotation

GitHub Actions / Build (windows-latest, 16.x)

Unexpected loop, use map or reduce instead
// eslint-disable-next-line security/detect-object-injection
t.is(typeof mod[key], 'function');
});
}
});

// ensure *no-panic* static load for Deno
Expand Down Expand Up @@ -105,29 +105,26 @@ test('correctly derive script name (JavaScript)', (t) => {
const extensions = ['.js', '.cjs', '.mjs'];

const files = fs.readdirSync(fixtureDirPath);
for (const file of files.filter((file) => {

Check failure on line 108 in test/integration.test.js

View workflow job for this annotation

GitHub Actions / Build (windows-latest, 14.x)

Unexpected loop, use map or reduce instead

Check failure on line 108 in test/integration.test.js

View workflow job for this annotation

GitHub Actions / Build (windows-latest, 16.x)

Unexpected loop, use map or reduce instead
return extensions.includes(path.extname(file));
})) {
if (settledSupportForESMs || path.extname(file) === '.js') {
const command = 'node';
const script = path.join(fixtureDirPath, file);
const args = [script];
const options = { shell: true, encoding: 'utf-8' };

files
.filter((file) => {
return extensions.includes(path.extname(file));
})
.forEach((file) => {
if (settledSupportForESMs || path.extname(file) === '.js') {
const command = 'node';
const script = path.join(fixtureDirPath, file);
const args = [script];
const options = { shell: true, encoding: 'utf-8' };

t.log({ script });
t.log({ script });

const { error, status, stdout, stderr } = spawn.sync(command, args, options);
const { error, status, stdout, stderr } = spawn.sync(command, args, options);

t.log({ error, status, stdout, stderr });
t.log({ error, status, stdout, stderr });

t.deepEqual({ error, status }, { error: null, status: 0 });
t.deepEqual({ error, status }, { error: null, status: 0 });

t.is(stdout.toString().trim(), path.parse(script).name);
}
});
t.is(stdout.toString().trim(), path.parse(script).name);
}
}
});

test('correctly derive script name (TypeScript)', (t) => {
Expand All @@ -136,32 +133,30 @@ test('correctly derive script name (TypeScript)', (t) => {

const files = fs.readdirSync(fixtureDirPath);

files
.filter((file) => {
const extension = path.extname(file);
const name = path.basename(file, extension);
const nameExtension = path.extname(name);
const isDenoTS = extension === '.ts' && nameExtension === '.deno';
return extensions.includes(extension) && !isDenoTS;
})
.forEach((file) => {
if (settledSupportForESMs || path.extname(file) === '.js' || path.extname(file) === '.ts') {
const command = 'node';
const script = path.join(fixtureDirPath, file);
const args = ['node_modules/ts-node/dist/bin.js', script];
const options = { shell: true, encoding: 'utf8' };

t.log({ script });
for (const file of files.filter((file) => {

Check failure on line 136 in test/integration.test.js

View workflow job for this annotation

GitHub Actions / Build (windows-latest, 14.x)

Unexpected loop, use map or reduce instead

Check failure on line 136 in test/integration.test.js

View workflow job for this annotation

GitHub Actions / Build (windows-latest, 16.x)

Unexpected loop, use map or reduce instead
const extension = path.extname(file);
const name = path.basename(file, extension);
const nameExtension = path.extname(name);
const isDenoTS = extension === '.ts' && nameExtension === '.deno';
return extensions.includes(extension) && !isDenoTS;
})) {
if (settledSupportForESMs || path.extname(file) === '.js' || path.extname(file) === '.ts') {
const command = 'node';
const script = path.join(fixtureDirPath, file);
const args = ['node_modules/ts-node/dist/bin.js', script];
const options = { shell: true, encoding: 'utf8' };

t.log({ script });

const { error, status, stdout, stderr } = spawn.sync(command, args, options);
const { error, status, stdout, stderr } = spawn.sync(command, args, options);

t.log({ error, status, stdout, stderr });
t.log({ error, status, stdout, stderr });

t.deepEqual({ error, status }, { error: null, status: 0 });
t.deepEqual({ error, status }, { error: null, status: 0 });

t.is(stdout.toString().trim(), path.parse(script).name);
}
});
t.is(stdout.toString().trim(), path.parse(script).name);
}
}
});

// test examples when using `--test-dist` (ie, with version changes or prior to distribution)
Expand All @@ -183,29 +178,27 @@ if (!process.env.npm_config_test_dist) {

const files = fs.readdirSync(egDirPath);

files
.filter((file) => {
return extensionRxs.find((re) => path.basename(file).match(re));
})
.forEach((file) => {
const command = 'deno';
const script = path.join(egDirPath, file);
const args = ['run', '--allow-all', script];
const options = { shell: true, encoding: 'utf-8' };

const { error, status, stdout, stderr } = spawn.sync(command, args, options);

if (error === null && status === 0) {
t.log(
util.inspect(script, /* showHidden */ void 0, /* depth */ void 0, /* color */ true),
`(exit_status=${status})`,
);
} else {
t.log({ script, error, status, stdout, stderr });
}

t.deepEqual({ error, status }, { error: null, status: 0 });
});
for (const file of files.filter((file) => {
return extensionRxs.find((re) => path.basename(file).match(re));
})) {
const command = 'deno';
const script = path.join(egDirPath, file);
const args = ['run', '--allow-all', script];
const options = { shell: true, encoding: 'utf-8' };

const { error, status, stdout, stderr } = spawn.sync(command, args, options);

if (error === null && status === 0) {
t.log(
util.inspect(script, /* showHidden */ void 0, /* depth */ void 0, /* color */ true),
`(exit_status=${status})`,
);
} else {
t.log({ script, error, status, stdout, stderr });
}

t.deepEqual({ error, status }, { error: null, status: 0 });
}
});
}

Expand All @@ -217,31 +210,29 @@ if (!process.env.npm_config_test_dist) {

const files = fs.readdirSync(egDirPath);

files
.filter((file) => {
return extensions.includes(path.extname(file));
})
.forEach((file) => {
if (settledSupportForESMs || path.extname(file) === '.js') {
const command = 'node';
const script = path.join(egDirPath, file);
const args = [script];
const options = { shell: true, encoding: 'utf-8' };

const { error, status, stdout, stderr } = spawn.sync(command, args, options);

if (error === null && status === 0) {
t.log(
util.inspect(script, /* showHidden */ void 0, /* depth */ void 0, /* color */ true),
`(exit_status=${status})`,
);
} else {
t.log({ script, error, status, stdout, stderr });
}

t.deepEqual({ error, status }, { error: null, status: 0 });
for (const file of files.filter((file) => {
return extensions.includes(path.extname(file));
})) {
if (settledSupportForESMs || path.extname(file) === '.js') {
const command = 'node';
const script = path.join(egDirPath, file);
const args = [script];
const options = { shell: true, encoding: 'utf-8' };

const { error, status, stdout, stderr } = spawn.sync(command, args, options);

if (error === null && status === 0) {
t.log(
util.inspect(script, /* showHidden */ void 0, /* depth */ void 0, /* color */ true),
`(exit_status=${status})`,
);
} else {
t.log({ script, error, status, stdout, stderr });
}
});

t.deepEqual({ error, status }, { error: null, status: 0 });
}
}
});

test('examples are executable without error (TypeScript)', (t) => {
Expand All @@ -252,40 +243,38 @@ if (!process.env.npm_config_test_dist) {

const files = fs.readdirSync(egDirPath);

files
.filter((file) => {
for (const file of files.filter((file) => {
const extension = path.extname(file);
const name = path.basename(file, extension);
const nameExtension = path.extname(name);
const isDenoTS = extension === '.ts' && nameExtension === '.deno';
return extensions.includes(extension) && !isDenoTS;
})) {
if (settledSupportForESMs || path.extname(file) === '.js' || path.extname(file) === '.ts') {
const command = 'node';
const script = path.join(egDirPath, file);
const args = ['node_modules/ts-node/dist/bin.js', script];
const options = { shell: true, encoding: 'utf8' };

const { error, status, stdout, stderr } = spawn.sync(command, args, options);

const basename = path.basename(file);
const extension = path.extname(file);
const name = path.basename(file, extension);
const nameExtension = path.extname(name);
const isDenoTS = extension === '.ts' && nameExtension === '.deno';
return extensions.includes(extension) && !isDenoTS;
})
.forEach((file) => {
if (settledSupportForESMs || path.extname(file) === '.js' || path.extname(file) === '.ts') {
const command = 'node';
const script = path.join(egDirPath, file);
const args = ['node_modules/ts-node/dist/bin.js', script];
const options = { shell: true, encoding: 'utf8' };

const { error, status, stdout, stderr } = spawn.sync(command, args, options);

const basename = path.basename(file);
const extension = path.extname(file);
const name = path.basename(file, extension);
const nameExtension = path.extname(name);

if (error === null && status === 0) {
t.log(
util.inspect(script, /* showHidden */ void 0, /* depth */ void 0, /* color */ true),
`(exit_status=${status})`,
);
} else {
t.log({ script, basename, name, extension, nameExtension });
t.log({ script, error, status, stdout, stderr });
}

t.deepEqual({ error, status }, { error: null, status: 0 });

if (error === null && status === 0) {
t.log(
util.inspect(script, /* showHidden */ void 0, /* depth */ void 0, /* color */ true),
`(exit_status=${status})`,
);
} else {
t.log({ script, basename, name, extension, nameExtension });
t.log({ script, error, status, stdout, stderr });
}
});

t.deepEqual({ error, status }, { error: null, status: 0 });
}
}
});
}
Loading

0 comments on commit 96dbbfa

Please sign in to comment.