-
Notifications
You must be signed in to change notification settings - Fork 37.8k
test: Use pathlib over os path #28392
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
Conversation
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. Code CoverageFor detailed information about the code coverage, see the test coverage report. ReviewsSee the guideline for information on the review process.
If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update. ConflictsReviewers, this pull request conflicts with the following ones:
If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first. |
https://cirrus-ci.com/task/5520063122374656:
|
Thank you for pointing this out, seems like readlink was introduced in python 3.9 and we're using 3.8 here. I will fix that shortly. Secondly, it seems like this issue is causing 3 other failures as well but I will double check and once I fix this issue - is it ok for me to close this PR and open a new one because i will do a fresh fork and PR to make it cleaner (otherwise I can follow the squash commit instructions as per contribution guidelines). |
Links should be available via the "Checks" tab in this PR on the GitHub web-interface. Perhaps, a Cirrus CI account is required as well.
No need to close a PR and open another. The accepted routine is to update your local EDIT: While CI fails, you also might mark this PR as a draft. |
FYI: #28211 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Left some questions on the closed pull and here.
test/functional/wallet_backup.py
Outdated
os.path.join(self.nodes[0].wallets_path, self.default_wallet_name), | ||
os.path.join(self.nodes[0].wallets_path)] | ||
self.nodes[0].wallets_path / self.default_wallet_name / self.wallet_data_filename, | ||
self.nodes[0].wallets_path / 'x' / self.default_wallet_name / self.wallet_data_filename, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where is 'x'
from?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the original .
here pathlib was resolving it to remove it whereas os.path was keeping it and since it was passing as strings I mixed this up thinking we're trying to fail one of these source paths but you're right, I switched it back to .
and it works.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you are changing the behavior of a test, for example removing test coverage or changing it, it would be good to leave as-is for now. There is no need to remove all use of os.path.join
. The main goal should be to remove the TestNode.datadir
method
test/functional/wallet_backup.py
Outdated
|
||
for sourcePath in sourcePaths: | ||
assert_raises_rpc_error(-4, "backup failed", self.nodes[0].backupwallet, sourcePath) | ||
assert_raises_rpc_error(-4, "backup failed", self.nodes[0].backupwallet, str(sourcePath)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this needed? RPC methods should be able to be passed pathlib paths.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you're correct, removed
test/functional/feature_addrman.py
Outdated
@@ -44,7 +44,7 @@ def serialize_addrman( | |||
|
|||
|
|||
def write_addrman(peers_dat, **kwargs): | |||
with open(peers_dat, "wb") as f: | |||
with peers_dat.open(mode="wb") as f: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Is this change needed? Seems better to just leave as-is if there is no reason to change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ya i think in python3 both pathlib and os open use io.open so no difference, in general my thought was to remove import os
from files so that there's an extra step of adding the import is involved in using os.path.join
in the code just as a deterrent but ya I will revert this one.
EDIT: never mind, open is available without import os
so what I said i is incorrect
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
open
is a built-in, not os.open
, so I don't think this affects os
imports at all.
conf.write('server=1\nrpcuser=someuser\nmain.rpcpassword=some#pass') | ||
self.nodes[0].assert_start_raises_init_error(expected_msg='Error: Error reading configuration file: parse error on line 3, using # in rpcpassword can be ambiguous and should be avoided') | ||
|
||
with open(inc_conf_file_path, 'w', encoding='utf-8') as conf: | ||
with inc_conf_file_path.open(mode='w', encoding='utf-8') as conf: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
agreed, will revert
test/functional/combine_logs.py
Outdated
@@ -91,7 +91,7 @@ def read_logs(tmp_dir): | |||
files = [("test", "%s/test_framework.log" % tmp_dir)] | |||
for i in itertools.count(): | |||
logfile = "{}/node{}/{}/debug.log".format(tmp_dir, i, chain) | |||
if not os.path.isfile(logfile): | |||
if not Path(logfile).is_file(): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Not sure if it makes sense to change this. Either logfile
is a Path to begin with and is using the Path interface throughout, or it isn't. Converting a string to Path
just to call is_file
on it doesn't seem to add any value, does it? My preference for this pull would be to focus on removing the TestNode.datadir
helper only.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure about the random Path
constructors inserted, which clutter the code. I think it would be easier to review if no new Path
constructor was added.
test/functional/combine_logs.py
Outdated
os.path.isdir(fullpath) | ||
and basename.startswith(TMPDIR_PREFIX) | ||
fullpath.is_dir() | ||
and str(basename).startswith(TMPDIR_PREFIX) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
basename
is a str
already, no? Seems odd to change this
test/functional/wallet_signer.py
Outdated
@@ -202,7 +202,7 @@ def test_valid_signer(self): | |||
|
|||
assert hww.testmempoolaccept([mock_tx])[0]["allowed"] | |||
|
|||
with open(os.path.join(self.nodes[1].cwd, "mock_psbt"), "w", encoding="utf8") as f: | |||
with open(Path(self.nodes[1].cwd) / "mock_psbt", "w", encoding="utf8") as f: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if this makes sense. Either cwd
is a Path
to begin with, but wrapping a str
into Path
for no benefit, seems not worth it.
For now, my preference would be to focus on removing TestNode.datadir
only.
test/functional/feature_settings.py
Outdated
@@ -22,7 +22,7 @@ def set_test_params(self): | |||
def run_test(self): | |||
node, = self.nodes | |||
settings = Path(node.chain_path, "settings.json") | |||
conf = Path(node.datadir, "bitcoin.conf") | |||
conf = Path(node.datadir_path, "bitcoin.conf") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
conf = Path(node.datadir_path, "bitcoin.conf") | |
conf = node.datadir_path / "bitcoin.conf" |
nit: This is already a path, so no need to run a no-op on it again.
test/functional/feature_settings.py
Outdated
@@ -79,7 +79,7 @@ def run_test(self): | |||
self.stop_node(0) | |||
|
|||
# Test alternate settings path | |||
altsettings = Path(node.datadir, "altsettings.json") | |||
altsettings = Path(node.datadir_path, "altsettings.json") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same, etc ...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
@@ -99,7 +99,7 @@ def __init__(self, i, datadir, *, chain, rpchost, timewait, timeout_factor, bitc | |||
# spam debug.log. | |||
self.args = [ | |||
self.binary, | |||
"-datadir=" + self.datadir, | |||
"-datadir=" + str(self.datadir_path), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"-datadir=" + str(self.datadir_path), | |
f"-datadir={self.datadir_path}", |
@@ -412,7 +412,7 @@ def write_config(config_path, *, n, chain, extra_config="", disable_autoconnect= | |||
|
|||
|
|||
def get_datadir_path(dirname, n): | |||
return os.path.join(dirname, "node" + str(n)) | |||
return pathlib.Path(dirname) / f"node{n}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return pathlib.Path(dirname) / f"node{n}" | |
return pathlib.Path(dirname) / f"node{n}" |
@@ -127,7 +125,7 @@ def __init__(self, i, datadir, *, chain, rpchost, timewait, timeout_factor, bitc | |||
if self.version_is_at_least(239000): | |||
self.args.append("-loglevel=trace") | |||
|
|||
self.cli = TestNodeCLI(bitcoin_cli, self.datadir) | |||
self.cli = TestNodeCLI(bitcoin_cli, str(self.datadir_path)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
May be better to just fixup the send_cli
method?
If you are done with the changes and confident in them, you can take this pull out of draft. |
@ns-xvrn when you rebase this, can you update the commit message to drop all the now-redundant messages from your previously squashed commits? i.e we don't things like "reversions after PR review" included in the commit message. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you'll have to rebase on current master. Otherwise:
Nice, lgtm ACK 12c8a21 🔊
Show signature
Signature:
untrusted comment: signature from minisign secret key on empty file; verify via: minisign -Vm "${path_to_any_empty_file}" -P RWTRmVTMeKV5noAMqVlsMugDDCyyTSbA3Re5AkUrhvLVln0tSaFWglOw -x "${path_to_this_whole_four_line_signature_blob}"
RUTRmVTMeKV5npGrKx1nqXCw5zeVHdtdYURB/KlyA/LMFgpNCs+SkW9a8N95d+U4AP1RJMi+krxU1A3Yux4bpwZNLvVBKy0wLgM=
trusted comment: Nice, lgtm ACK 12c8a210fe8a9e1e4cce95e719da0728a53866ef 🔊
nRLN+xBFTudmjfgANKjDSiWaCvK/pfswAHKlWzf8NyiteLNzZhGP6l2wiZzab1v8twadrnY1ETOCUR7oAGDtDA==
Thanks, you'll need to update |
revert netutil chgs py3.8 compliant fixes based on PR review
re-ACK bfa0bd6 🐨 Show signatureSignature:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK bfa0bd6
Changes look good to me. I ran a few quick greps to try and find any places that this had been missed but couldn't find any.
I did spot one or two changed lines where we could have taken the opportunity to switch to format strings from .format()
, along with switching to double-quoted strings (which, if I am correct we are preferring and switching to?), but not relevant to the correctness of these changes.
In reference to issue #28362 refactoring of functional tests to use pathlib over os.path to reduce verbosity and increase the intuitiveness of managing file access.