-
-
Notifications
You must be signed in to change notification settings - Fork 41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow tests for shebang
rule
#119
Comments
🤔 We could test if the file is executable? Something like: import { access, constants } from 'node:fs/promises';
export async function isExecutable(path) {
try {
await access(path, constants.X_OK);
return true;
} catch (error) {
if (error.code === 'EACCES') {
return false;
}
throw error;
}
}
// Then just do this in the rule
if (hasShebang && !isExecutable) {
context.report();
} |
Yes, but that's not enough, we would also need to check for the import of |
The shebang is valid... if you add executable permissions :-) So the rule should check and fix it for the |
mmm, I see! |
So, my thoughts on this are we could add the following: {
"n/shebang": [
"error",
// Defaults for the rule (mostly to prevent a breaking change)
{
// Leave the default check
checkPackageBin: true,
// Add the ability to enable test files
checkTestFiles: false,
// Check all executable files
checkExecutable: false,
}
]
} For each of the options, if the following is matched we should check the shebang: checkPackageBin:
checkTestFiles:
checkExecutable:
👨💻 A quick code example for checking for `node:test`, and `test`I roughly validated this in https://astexplorer.net/ export default {
create: function (context) {
let foundTest = false;
let fileExecutable = false;
let validShebang = true;
return {
"Program:exit"(node) {
if (foundTest === false) {
return;
}
if (fileExecutable === false) {
return;
}
if (validShebang === true) {
return;
}
context.report({ node, message: "Program needs a valid shebang" });
},
/* import 'test'; */
'ImportDeclaration[source.value="test"]'(node) {
foundTest = true;
},
/* import 'node:test'; */
'ImportDeclaration[source.value="node:test"]'(node) {
foundTest = true;
},
/* await import('test'); */
'ImportExpression[source.value="test"]'(node) {
foundTest = true;
},
/* await import('node:test'); */
'ImportExpression[source.value="node:test"]'(node) {
foundTest = true;
},
/* require('test'); */
'CallExpression[callee.name="require"][arguments.0.value="test"]'(node) {
foundTest = true;
},
/* require('node:test'); */
'CallExpression[callee.name="require"][arguments.0.value="node:test"]'(node) {
foundTest = true;
},
/* Hacks for babel */
/* await import('test'); */
'CallExpression[callee.name="import"][arguments.0.value="test"]'(node) {
foundTest = true;
},
/* await import('node:test'); */
'CallExpression[callee.name="import"][arguments.0.value="node:test"]'(node) {
foundTest = true;
},
/* await import('test'); */
'CallExpression[callee.type="Import"][arguments.0.value="test"]'(node) {
foundTest = true;
},
/* await import('node:test'); */
'CallExpression[callee.type="Import"][arguments.0.value="node:test"]'(node) {
foundTest = true;
}
};
}
}; @aladdin-add What do you think? |
If file is in
Also 3. check it's run from
Not sure what could be this checking... |
it's something like guessing the users' intent - IMHO, not all the tests are executable. i would suggest to adding a new option {
"n/shebang": [
"error",
{
"additionalExcutable": ["tests/*.js"]
}
]
} |
Exactly, not all tests would be executable, with executions bit and shebang, only the ones that are run directly as commands on the test script. |
Somewhat related to this, I usually have some deploy scripts that I want them to be executable as part of my |
🤔 I think I like that approach. |
well after thinking for a while, I'd more prefer the way mentioned earlier: #119 (comment) There is a difference that |
@scagood wdyt? |
We can add a couple of settings I think 🤔 |
shebang rule only check for the
bin
field inpackage.json
, but with the new Node.js built-intest
test runner, it's valid that they are run as standalone executables, with a shebang and an executable bit.My suggestion is that files that import
test
ornode:test
can have a shebang, and if so, check and enforce that they have enabled the executable permission bit. In the other way could not work, importingtest
built-in and have enabled the executable permission bit is NOT enough to consider it should have a shebang, because Windows don't have permission bits and the file can be importing the test runner but not intended to be used standalone, except is thetest
script onpackage.json
file is running the test file as if would be an executable, in that case file must have the shebang.The text was updated successfully, but these errors were encountered: