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

Remove uncommon functions from common module #17781

Closed
wants to merge 4 commits into from

Conversation

Trott
Copy link
Member

@Trott Trott commented Dec 20, 2017

In an effort to make the common module slightly less of a monolith and junk drawer, and to reduce the needed parsing/loading of some function from around 2000 times per test run to just 1, this PR removes functions that are only used once.

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • documentation is changed or added
  • commit message follows commit guidelines
Affected core subsystem(s)

test

@Trott Trott added the test Issues and PRs related to the tests. label Dec 20, 2017
@nodejs-github-bot nodejs-github-bot added doc Issues and PRs related to the documentations. test Issues and PRs related to the tests. tools Issues and PRs related to the tools directory. labels Dec 20, 2017
@Trott
Copy link
Member Author

Trott commented Dec 20, 2017

let tty_fd = 0;
if (!tty.isatty(tty_fd)) tty_fd++;
else if (!tty.isatty(tty_fd)) tty_fd++;
else if (!tty.isatty(tty_fd)) tty_fd++;
Copy link
Member

@TimothyGu TimothyGu Dec 20, 2017

Choose a reason for hiding this comment

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

Can someone explain to me how this works?

Copy link
Member

Choose a reason for hiding this comment

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

I didn't write the code but I assume:

The default file descriptors for I/O are stdin, stdout and stderr (0, 1 and 2 respectively). It's not always guaranteed to be the case, so we try the first 3 and then try /dev/tty which usually works (it maps for the controlling terminal of a process for each process) but it's slower to call than isatty it if it's 0.

Copy link
Member Author

@Trott Trott Dec 20, 2017

Choose a reason for hiding this comment

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

It was a cut-and-paste, I didn't write it, but yeah, it's not just hard to follow, but also buggy. If the first if fails, on line 253, it increments tty_fd but doesn't then check the condition on line 254 because that's an else so it skips that line. Ugh.

If 0 and 1 are not TTYs but 2 is, we won't get 2 (unless that's what's returned by the try/catch block subsequently).

I think something more like this is what we want:

let tty_fd = [0, 1, 2].find(tty.isatty);
if (tty_fd === undefined) {
     try {
        tty_fd = fs.openSync('/dev/tty');
      } catch (e) {
        // There aren't any tty fd's available to use.
        return -1;
      }
}
return tty_fd;

(And while I'm in there fixing this function so it hopefully works correctly, I should probably refactor tty_fd to have a name that doesn't include _.)

Copy link
Member Author

Choose a reason for hiding this comment

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

Added a commit here to fix this. PTAL.

common.projectDir is used in one test, so it's not so common. Remove
from common module to the one test file that needs it.
common.getTTYfd() is used in one test only. Move it's definition to that
test and out of the common module.
common.firstInvalidFD() is used in only one test. Move it out of the
common module and into the one test that uses it.
@Trott
Copy link
Member Author

Trott commented Dec 20, 2017

Some code refactored and bug-fixed.

CI: https://ci.nodejs.org/job/node-test-pull-request/12229/

if (tty_fd >= 0) {
function getTTYfd() {
const tty = require('tty');
let ttyFd = [0, 1, 2].find(tty.isatty);
Copy link
Member

Choose a reason for hiding this comment

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

Now that I read it - I'm surprised isatty is synchronous :D Change LGTM

const tty = require('tty');
let tty_fd = 0;
if (!tty.isatty(tty_fd)) tty_fd++;
else if (!tty.isatty(tty_fd)) tty_fd++;
Copy link
Member

Choose a reason for hiding this comment

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

Was this branch ever reached?

Copy link
Member

@lpinca lpinca Dec 20, 2017

Choose a reason for hiding this comment

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

Nvm, 8be1259 is the answer.

@Trott
Copy link
Member Author

Trott commented Dec 20, 2017

(CI failures are unrelated.)

@BridgeAR BridgeAR added fast-track PRs that do not need to wait for 48 hours to land. author ready PRs that have at least one approval, no pending requests for changes, and a CI started. labels Dec 21, 2017
@Trott Trott removed the author ready PRs that have at least one approval, no pending requests for changes, and a CI started. label Dec 21, 2017
@Trott
Copy link
Member Author

Trott commented Dec 21, 2017

Landed in fc8e821 ddf3655 40bab30 and 1af82f3

@Trott Trott closed this Dec 21, 2017
Trott added a commit to Trott/io.js that referenced this pull request Dec 21, 2017
common.projectDir is used in one test, so it's not so common. Remove
from common module to the one test file that needs it.

PR-URL: nodejs#17781
Reviewed-By: Michaël Zasso <[email protected]>
Reviewed-By: Gireesh Punathil <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Timothy Gu <[email protected]>
Reviewed-By: Jon Moss <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
Trott added a commit to Trott/io.js that referenced this pull request Dec 21, 2017
common.getTTYfd() is used in one test only. Move it's definition to that
test and out of the common module.

PR-URL: nodejs#17781
Reviewed-By: Michaël Zasso <[email protected]>
Reviewed-By: Gireesh Punathil <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Timothy Gu <[email protected]>
Reviewed-By: Jon Moss <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
Trott added a commit to Trott/io.js that referenced this pull request Dec 21, 2017
common.firstInvalidFD() is used in only one test. Move it out of the
common module and into the one test that uses it.

PR-URL: nodejs#17781
Reviewed-By: Michaël Zasso <[email protected]>
Reviewed-By: Gireesh Punathil <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Timothy Gu <[email protected]>
Reviewed-By: Jon Moss <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
Trott added a commit to Trott/io.js that referenced this pull request Dec 21, 2017
PR-URL: nodejs#17781
Ref: nodejs#17781 (comment)
Reviewed-By: Michaël Zasso <[email protected]>
Reviewed-By: Gireesh Punathil <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Timothy Gu <[email protected]>
Reviewed-By: Jon Moss <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
@MylesBorins
Copy link
Contributor

This does not cleanly land on v9.x, would someone be willing to manually backport?

@Trott
Copy link
Member Author

Trott commented Jan 11, 2018

@MylesBorins This will land cleanly once #17401 is backported.

@Trott
Copy link
Member Author

Trott commented Jan 11, 2018

Once #18096 (backport of #17401) lands, these commits will land cleanly and won't need to be backported.

targos pushed a commit that referenced this pull request Mar 24, 2018
common.projectDir is used in one test, so it's not so common. Remove
from common module to the one test file that needs it.

PR-URL: #17781
Reviewed-By: Michaël Zasso <[email protected]>
Reviewed-By: Gireesh Punathil <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Timothy Gu <[email protected]>
Reviewed-By: Jon Moss <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
targos pushed a commit that referenced this pull request Mar 24, 2018
common.getTTYfd() is used in one test only. Move it's definition to that
test and out of the common module.

PR-URL: #17781
Reviewed-By: Michaël Zasso <[email protected]>
Reviewed-By: Gireesh Punathil <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Timothy Gu <[email protected]>
Reviewed-By: Jon Moss <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
targos pushed a commit that referenced this pull request Mar 24, 2018
common.firstInvalidFD() is used in only one test. Move it out of the
common module and into the one test that uses it.

PR-URL: #17781
Reviewed-By: Michaël Zasso <[email protected]>
Reviewed-By: Gireesh Punathil <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Timothy Gu <[email protected]>
Reviewed-By: Jon Moss <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
targos pushed a commit that referenced this pull request Mar 24, 2018
PR-URL: #17781
Ref: #17781 (comment)
Reviewed-By: Michaël Zasso <[email protected]>
Reviewed-By: Gireesh Punathil <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Timothy Gu <[email protected]>
Reviewed-By: Jon Moss <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
@Trott Trott deleted the uncommon-fn branch January 13, 2022 22:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
doc Issues and PRs related to the documentations. fast-track PRs that do not need to wait for 48 hours to land. test Issues and PRs related to the tests. tools Issues and PRs related to the tools directory.
Projects
None yet
Development

Successfully merging this pull request may close these issues.