Skip to content

Refactor test script for improved readability and modularity #1956

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

Merged
merged 12 commits into from
Apr 18, 2025

Conversation

Bashamega
Copy link
Contributor

@Bashamega Bashamega commented Mar 31, 2025

This PR refactors the test script to enhance maintainability, readability, and efficiency.

Changes:

  • Extracted reusable functions (getFiles, readFileContent) to reduce redundancy.
  • Simplified file comparison logic for better maintainability.
  • Introduced compileSets array to streamline and organize compilation tasks dynamically.
  • Improved error handling and logging for better debugging.

Benefits:

  • Cleaner code: Eliminates duplication and improves readability.
  • Easier maintenance: More modular and extensible structure.
  • Improved debugging: Enhanced error messages for easier troubleshooting.

Copy link
Contributor

Thanks for the PR!

This section of the codebase is owned by @saschanaz - if they write a comment saying "LGTM" then it will be merged.

@Bashamega Bashamega marked this pull request as ready for review March 31, 2025 12:24
Copy link
Contributor

@saschanaz saschanaz left a comment

Choose a reason for hiding this comment

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

First of all can you keep the existing function style instead of moving to const? A style change like that should be done with a good consensus and should be done for all files for consistency. And I don't think we have consensus.

@Bashamega
Copy link
Contributor Author

First of all can you keep the existing function style instead of moving to const? A style change like that should be done with a good consensus and should be done for all files for consistency. And I don't think we have consensus.

Sure

@Bashamega
Copy link
Contributor Author

Done @saschanaz

src/test.ts Outdated
function normalizeLineEndings(text: string): string {
return text.replace(/\r\n?/g, "\n");
function normalizeLineEndings(text: string) {
text.replace(/\r\n?/g, "\n");
Copy link
Contributor

Choose a reason for hiding this comment

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

return?

src/test.ts Outdated
try {
outputFiles = fs.readdirSync(outputFolder);
return normalizeLineEndings(fs.readFileSync(filePath, "utf-8"));
Copy link
Contributor

Choose a reason for hiding this comment

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

We already stat the file before calling this and thus doesn't expect any error here. Thus we should skip try-catch here to see actual failures if happens.

src/test.ts Outdated
const childBaselineFolder = new URL(`${file}/`, baselineFolder);
const childOutputFolder = new URL(`${file}/`, outputFolder);
if (!compareToBaselines(childBaselineFolder, childOutputFolder)) {
if (fs.existsSync(baselinePath) || fs.existsSync(outputPath)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This is not same as detecting directory?

src/test.ts Outdated
}
}
return true;
}

function compileGeneratedFiles(lib: string, ...files: string[]) {
function compileGeneratedFiles(lib: string[] | string, ...files: string[]) {
Copy link
Contributor

Choose a reason for hiding this comment

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

But is lib ever an array here?

src/test.ts Outdated
Comment on lines 99 to 100
console.error(e.stdout.toString());
console.error();
Copy link
Contributor

Choose a reason for hiding this comment

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

Skipping this would hide the actual error which would make debugging harder.

@Bashamega
Copy link
Contributor Author

I have fixed them @saschanaz

@Bashamega
Copy link
Contributor Author

Hello @saschanaz
Can you please review this? I have updated the PR

@Bashamega Bashamega requested a review from saschanaz April 16, 2025 03:30
@jakebailey
Copy link
Member

Why do we need to do this? This just seems like a plain refactor, but no behavior is changing. Just seems like churn?

@Bashamega
Copy link
Contributor Author

Why do we need to do this? This just seems like a plain refactor, but no behavior is changing. Just seems like churn?

This is just a refactor, and making the code more readable and easy to maintain.

@jakebailey
Copy link
Member

Personally, I would not try and work on this kind of change; it means us maintainers have to review PRs to see what they're doing, if they contain any new bugs, for code which theoretically is already working fine. Other improvements are of course welcome, but for plain code changes like this, I don't really think it's worth the time spent compared to other changes (e.g., you've sent some good other ones!).

@saschanaz
Copy link
Contributor

It's indeed a bit hard to make sure all changes are correct, would be much easier to check when each change is done separately.

src/test.ts Outdated
)
: null;
const isBaselineFile =
fs.existsSync(baselinePath) && fs.statSync(baselinePath).isFile();
Copy link
Contributor

@saschanaz saschanaz Apr 17, 2025

Choose a reason for hiding this comment

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

It's actually advised to not call exists before other calls, because things can potentially be racy and the file can go away after that call.

https://nodejs.org/docs/latest/api/fs.html#fsexistspath-callback

Using fs.exists() to check for the existence of a file before calling fs.open(), fs.readFile(), or fs.writeFile() is not recommended. Doing so introduces a race condition, since other processes may change the file's state between the two calls. Instead, user code should open/read/write the file directly and handle the error raised if the file does not exist.

@Bashamega
Copy link
Contributor Author

No problem. I will close this one

@saschanaz
Copy link
Contributor

I don't think this should be thrown away; just splitting this would be fine.

@Bashamega
Copy link
Contributor Author

I have made it smaller @saschanaz

Copy link
Contributor

@saschanaz saschanaz left a comment

Choose a reason for hiding this comment

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

Now this is what I call a great refactoring.

Co-authored-by: Kagami Sascha Rosylight <[email protected]>
@saschanaz
Copy link
Contributor

LGTM

Copy link
Contributor

There was an issue merging, maybe try again saschanaz. Details

Copy link
Member

@jakebailey jakebailey left a comment

Choose a reason for hiding this comment

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

Thanks, this seems much more obviously correct.

@jakebailey jakebailey merged commit 7304e04 into microsoft:main Apr 18, 2025
5 checks passed
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.

3 participants