-
To avoid uncontrolled, unwanted UI/UX within a script, static module imports must not panic nor prompt. For any given module, I'm attempting to ensure compliance with this no-panic/no-prompt requirement by using tests. This is an initial implementation for no-panic, but I don't see a path to implementation of a test for the no-prompt condition. # modified from ['os_paths'](https://deno.land/x/os_paths)
const test = require('ava');
const commandExists = require('command-exists');
const pkg = require('../package.json');
const haveDeno = commandExists.sync('deno');
// ensure *no-panic* and *no-prompt* static module import (for Deno)
if (!haveDeno) {
test.skip('module load tests (Deno)...skipped (`deno` not found)', () => void 0);
} else {
// *no-panic*
test('module loads without panic (no permissions and `--no-prompt`; Deno)', (t) => {
const denoModulePath = pkg.exports['.'].deno;
const command = 'deno';
const args = ['run', '--no-prompt', denoModulePath];
const options = { shell: true, encoding: 'utf-8' };
const { error, status, stdout } = spawn.sync(command, args, options);
if (!(error === null && status === 0)) {
t.log({ denoModulePath, error, status, stdout });
}
t.deepEqual({ error, status }, { error: null, status: 0 });
});
// *no-prompt*
test('module loads without prompts (no permissions; Deno)', (t) => {
// ??? how can 'load without any prompts' be tested?
// * `deno --prompt ...` will *not* prompt with closed or redirected STDIN/STDERR
// ??? is there some other way to determine that a prompt would occur?
// ??? some sort of module analysis/reflection?
});
} A module such as... // foo.ts
// * idiom used in Yargs (<https://github.com/yargs/yargs/blob/main/lib/platform-shims/deno.ts>)
export const cwd = (() => {
try {
return Deno.cwd();
} catch (err) {
if (err.name !== 'PermissionDenied') {
throw err;
}
return undefined;
}
})();
// ... will pass the no-panic test, but will, by default, prompt when executed/imported (eg, This can be seen obviously just by running Any suggestions on how to determine that this code fails the no-prompt criteria? related: Don't unnecessarily request to read environment variable |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
I've solved this myself with external tooling. |
Beta Was this translation helpful? Give feedback.
I've solved this myself with external tooling.