Skip to content
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

Add test suite for all current config loading systems #301

Merged
merged 8 commits into from Oct 17, 2023

Conversation

beaussan
Copy link
Contributor

This PR introduce two things :

A test suite to validate all config provider system, in a e2e manner :

  • knip with a knip.js file that expose an object
  • knip with a knip.js file that expose an async function
  • knip with a knip.json file
  • knip -c knip.mjs with a knip.mjs file that expose an object
  • knip -c knip.mjs with a knip.mjs file that expose an async function
  • knip with a config into a package.json#knip
  • knip with a knip.ts file that expose an async function
  • knip with a knip.ts file that expose a function
  • knip with a knip.ts file that expose an object
  • knip -c knip.yaml with a knip.yaml file

And enhance the current config loading mechanism to support js function & async function as a config export. I've added :

  • Documentation
  • e2e test
  • Error handling
  • Debug of the error message if the function exposed failed

If you need anything modified, feel free to reach out ! And thanks for your work on Knip ❤️

Copy link
Collaborator

@webpro webpro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for putting this together, Nicolas!

A few comments, but I'm sure we can merge this swiftly.

README.md Outdated Show resolved Hide resolved
src/ConfigurationChief.ts Outdated Show resolved Hide resolved
src/util/unwrapFunction.ts Outdated Show resolved Hide resolved
return error['stdout'].toString();
}
// Unknown error
throw error;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This helper is great! Looks like it resolves #253 :)

Will be very useful going forward and testing CLI arguments, etc.

This looks like using the same exec function in the new test files, do you think it could be a single reusable test helper? There's a test/helpers folder for this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure thing! Will do that ASAP 😁

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done ! I've used a "dumb" way where you need to pass the cli path, this could be enhanced further where the execFactory crawls back up to find the cli itself, but for a first iteration, I opted to keeping it KISS. Let me know what you think !

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolving the path to cli.js from the helper seems more ergonomic and clean, so would be great if you could still add that. Happy to merge either way!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See the other comment, I've added it in the least intrusive way, since doing it the proper way could have an impact on test speed (fs apis to check for file and read files, for every test that uses the exec util)

console.error(`Error during execution of command: ${command}`);
}
};
const exec = execFactory(cwd, '../../dist/cli.js');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be an idea to resolve ../dist/cli.js in the helper module using import.meta.url from there? That would mean an argument less here :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can be done yes, but then if the files are in a different location (like the npm run test script), then there need to be extra logic

const thisFileUrl = fileURLToPath(import.meta.url);

const cliPath = resolve(
  thisFileUrl,
  // Needed if run in the /tmp (like in the npm test script) to find the correct dist folder
  thisFileUrl.endsWith('tmp/test/helpers/execKnip.js') ? '..' : '.',
  '../../../dist/cli.js'
);

The safest way is to go back up to find the package.json with the name knip, this way we are sure we are finding the right directory and is less prone to error if the test are moved around for example.

I've added the simple way, and I'll let you decide if it's worth the effort to look for the package.json

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes, that /tmp dir I forgot about.

Maybe something like require.resolve('dist/cli.js) to make an absolute path that should be OK from anywhere?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be indeed ! Thank you for pushing the change directly to the branch :)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hah, I was waiting for CI to pass 💚

Wanted to get it done 🏓 Let's merge it?

Many many thanks for the great improvements!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's merge it !

My pleasure ! I will experiment further with knip and the new dynamic capabilities ! I'll post my experiment on discord ;)

@webpro webpro merged commit a18f2a6 into webpro-nl:main Oct 17, 2023
10 checks passed
@beaussan beaussan deleted the experimental-findings branch October 17, 2023 18:42
@webpro
Copy link
Collaborator

webpro commented Oct 17, 2023

🚀 This pull request is included in v2.34.0. See Release 2.34.0 for release notes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants