-
Notifications
You must be signed in to change notification settings - Fork 392
test: detect unexpected OLDPWD change #527
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
40f582a
to
50ecafa
Compare
Makes sense in principle. Unfortunately I had made a bunch of changes yesterdayish and failed to push them, did that now so there are some conflicts to resolved. There are some other unrelated changes here such as changing The detail of your explanations in PR's is great! Including the elaborations in the actual corresponding commit messages would be even better :) GitHub might be a thing of the past one day, but I don't think the project would ever change to a VCS that doesn't preserve commit history and messages. |
Seems I've broken the |
Now we can use "@pytest.mark.bashcomp(temp_cwd=True)" for a cleaner handling of the test-purpose temporary directory. We now create dummy directories and files in the directory provided by "temp_cwd". For this purpose, the code in "prepare_fixture_dir" (test/t/conftest.py) has been extracted to an independent function "create_dummy_filedirs". Tests at "test/t/test_{kdvi,kpdf,evince}.py" and "prepare_fixture_dir" now call "create_dummy_filedirs".
To properly detect unwanted changes of OLDPWD by completions, we remove OLDPWD from the white list of mutable variables. Now, we need to properly save and restore the value of OLDPWD when we change the testing directory on purpose. We add variable names "_bash_completion_test_*" in the white list and use "_bash_completion_test_OLDPWD" to save the value of "OLDPWD". Remark: Another suggested solution was to use "pushd" and "popd" in order to save/restore the directory, but this idea was discarded because they turned out to affect "OLDPWD" too.
50ecafa
to
397e6b0
Compare
Thank you for the comments. I have rebased the commits, squashed one commit, dropped unrelated changes, and force-pushed. The changes to
OK, I have added descriptions in the commit messages. |
c532a16
to
18ab3b7
Compare
bash-completion/test/t/test_man.py Lines 104 to 115 in 72900ba
I have found the error messages on the bottom. This is unrelated to the finalizer. Sorry for the noise. Commit 18ab3b7: New functions
|
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.
This is starting to look excellent, thanks!
In addition to the one cosmetic regex comment below, just one question: what do you think, should we turn the save/restore variable functionality to a context manager, for more succinct code, and so that restoring couldn't be forgotten, and it'd happen also on errors? I think it would be a good idea, but it doesn't necessarily have to be in this PR, we can do it in a followup one.
e22e409
to
e379234
Compare
fa60361
to
58f02b9
Compare
I have implemented. 58f02b9 Actually, I recognized that restoration could be skipped when the error happens, but I didn't take it seriously because it only happens when there are other errors so just causes an additional failure in the later check of environment changes. But if we can properly handle them without messing up the codes, that would be nice. It is a bit big enhancement but I created a new context manager, which finally became relatively large. The simplest context manager might have been something like this commit 2c337a8 which creates one context manager for one saved variable, but I soon gave up this simple one because it turned out to be practically not useful. [The problem is that ...]
save()
do_something()
restore() can be easily turned into a context manager: with var_saved():
do_something() but if condition1:
save1()
do_something()
if condition2:
restore2() can't be if condition1:
with var_saved():
do_something()
with var_saved1():
with var_saved2():
with var_saved3():
do_something() Instead, I decided to create one real context manager that handles multiple variables as well as the current working directory and with bash_env_saved(bash) as bash_env:
bash_env.write_variable(var1, value1)
bash_env.write_variable(var2, value2) # multiple variables
bash_env.write_env(var3, value3) # exported variables
bash_env.chdir(path1) # change directory
do_something()
bash_env.chdir(path2) # It is possible to again change the directory
bash_env.write_variable(var1, value1a) # again change the variable
bash_env.write_variable(var4, value4)
bash_env.shopt("failglob", True) # change shopt settings
do_another_thing() Remarks
|
Great stuff, thanks a lot! BTW I sent you mail about contributor access a couple of days ago but haven't received a reply, I wonder if you received it? |
Thank you!
Yes, I received it! Sorry for the late reply, and thank you for the invitation (though I guess it is Collaborator access in GitHub term? I'm already Contributor in the GitHub terminology.). I'm willing to continue to contribute to Of course, I don't have enough time to process all of the issues and PRs because, for example, I'm not necessarily familiar with the related commands (such as |
Collaborator is what I meant indeed, sorry about the confusion. Invitation sent, welcome! Don't worry about it, there's no expectation to be involved with everything. And nobody else is familiar with all the commands either. I've enabled the discussions project feature, we can talk about things not related to specific issues/PR's there. First post too: #530 |
Thank you! I have accepted the invitation. Thanks also for the Discussion feature. Maybe I can open a discussion on the design policy of this project when I have questions. For example, in #491, new variables |
Please do open such a discussion. I'll refrain from replying here :) |
I made changes for this one. There were three places where
cd
was used to change the working directory for tests. I separated them into two commits.1. kdvi, kpdf, evince (a22afe0): Use
temp_cwd
(with refactoringprepare_fixture_dir
->create_dummy_filedirs
for dummy files)One place is the
cd
into the directory created byprepare_fixture_dir
, which was used intest_{kdvi,kpdf,evince}
. I changed these tests to use the temporary directory created bytemp_cwd
to reduce the placescd
is called. Nowtest_{kdvi,kpdf,evince}
don't need to change and restore directories by themselves. The dummy files and directories are now created by a new functioncreate_dummy_filedirs
, which is a refactored version ofprepare_fixture_dir
.I thought maybe I should apply the same prefix of the temporary directories to
temp_cwd
asprepare_fixture_dir
but found that it is already applied inmacos-ci
branch 41c6fb8, so skipped it this time.I also noticed that the ordering of dummy-file creation and dummy-directory creation was changed on 328287d, so I followed that ordering in the new
create_dummy_filedirs
.2. conftest (013140c): save/restore OLDPWD
The second place is
assert_complete
(conftest.py
). I triedpushd
andpopd
, but they turned out to changeOLDPWD
too. So I decided to just continue to use the plaincd
but save and restore the value ofOLDPWD
in another variable.The third place is
test/t/unit/test_unit_known_hosts_real.py
where one testtest_no_globbing
is performed in the directorytest/fixtures/_known_hosts_real
. I directly modified this test to save/restoreOLDPWD
.