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

dev_tools/bash_scripts_test.py does not guard against interference from user's ~/.gitconfig #5773

Closed
mhucka opened this issue Jul 15, 2022 · 0 comments
Labels
kind/bug-report Something doesn't seem to work.

Comments

@mhucka
Copy link
Contributor

mhucka commented Jul 15, 2022

Description of the issue

When running pytest in the Cirq repo (or check/pytest), the tests in dev_tools/bash_scripts_test.py run a series of git commands and compares the output against expected values. A few of the tests will fail with very confusing errors if the user has configured init.templateDir in their global ~/.gitconfig file. The reason is that the command

git init --quiet --initial-branch master

will generate a "warning: re-init ignored" message, and that in turn causes the tests in bash_scripts_test.py to fail because the tests rely on matching exactly the stdout/stderr from the commands.

How to reproduce the issue

  1. Set init.templateDir, either using a git config command or by creating a ~/.gitconfig file manually and putting something like this in it:
    [init]
        templateDir = ~/.config/git/template
    
  2. Run the following command in a shell from the Cirq repo top level:
    check/pytest -vvv dev_tools/bash_scripts_test.py::test_pytest_changed_files_file_selection
    

The result will be a bunch of output ending in a failure. The full output is below.

> check/pytest -vvv dev_tools/bash_scripts_test.py::test_pytest_changed_files_file_selection                                                                                    quantum/repos/Cirq
=========================================================================================== test session starts ============================================================================================
platform darwin -- Python 3.9.13, pytest-7.1.2, pluggy-1.0.0 -- /Users/mhucka/project-files/quantum/repos/Cirq/env/bin/python3
cachedir: .pytest_cache
rootdir: /Users/mhucka/project-files/quantum/repos/Cirq
plugins: asyncio-0.18.3, forked-1.4.0, xdist-2.2.1, cov-3.0.0
asyncio: mode=legacy
[gw0] darwin Python 3.9.13 cwd: /Users/mhucka/project-files/quantum/repos/Cirq
[gw1] darwin Python 3.9.13 cwd: /Users/mhucka/project-files/quantum/repos/Cirq
[gw2] darwin Python 3.9.13 cwd: /Users/mhucka/project-files/quantum/repos/Cirq
[gw3] darwin Python 3.9.13 cwd: /Users/mhucka/project-files/quantum/repos/Cirq
[gw0] Python 3.9.13 (main, May 24 2022, 21:28:12)  -- [Clang 12.0.0 (clang-1200.0.32.29)]
[gw1] Python 3.9.13 (main, May 24 2022, 21:28:12)  -- [Clang 12.0.0 (clang-1200.0.32.29)]
[gw2] Python 3.9.13 (main, May 24 2022, 21:28:12)  -- [Clang 12.0.0 (clang-1200.0.32.29)]
[gw3] Python 3.9.13 (main, May 24 2022, 21:28:12)  -- [Clang 12.0.0 (clang-1200.0.32.29)]
gw0 [1] / gw1 [1] / gw2 [1] / gw3 [1]
scheduling tests via LoadScheduling

dev_tools/bash_scripts_test.py::test_pytest_changed_files_file_selection
[gw0] [100%] FAILED dev_tools/bash_scripts_test.py::test_pytest_changed_files_file_selection sys:1: ResourceWarning: unclosed file <_io.TextIOWrapper name=0 mode='r' encoding='UTF-8'>
sys:1: ResourceWarning: unclosed file <_io.TextIOWrapper name=1 mode='w' encoding='UTF-8'>
sys:1: ResourceWarning: unclosed file <_io.TextIOWrapper name=0 mode='r' encoding='UTF-8'>
sys:1: ResourceWarning: unclosed file <_io.TextIOWrapper name=1 mode='w' encoding='UTF-8'>
sys:1: ResourceWarning: unclosed file <_io.TextIOWrapper name=0 mode='r' encoding='UTF-8'>
sys:1: ResourceWarning: unclosed file <_io.TextIOWrapper name=1 mode='w' encoding='UTF-8'>
sys:1: ResourceWarning: unclosed file <_io.TextIOWrapper name=0 mode='r' encoding='UTF-8'>
sys:1: ResourceWarning: unclosed file <_io.TextIOWrapper name=1 mode='w' encoding='UTF-8'>


================================================================================================= FAILURES =================================================================================================
_________________________________________________________________________________ test_pytest_changed_files_file_selection _________________________________________________________________________________
[gw0] darwin -- Python 3.9.13 /Users/mhucka/project-files/quantum/repos/Cirq/env/bin/python3

tmpdir_factory = TempdirFactory(_tmppath_factory=TempPathFactory(_given_basetemp=PosixPath('/private/var/folders/m1/fxfnb93j1vv_nlxq8nd... _basetemp=PosixPath('/private/var/folders/m1/fxfnb93j1vv_nlxq8nd9brkr0000gz/T/pytest-of-mhucka/pytest-35/popen-gw0')))

    @only_on_posix
    def test_pytest_changed_files_file_selection(tmpdir_factory):

        result = run(
            script_file='check/pytest-changed-files',
            tmpdir_factory=tmpdir_factory,
            arg='HEAD~1',
            setup='touch file.py\ngit add -A\ngit commit -m test --quiet --no-gpg-sign\n',
        )
        assert result.returncode == 0
        assert result.stdout == ''
>       assert (
            result.stderr.split()
            == (
                "Comparing against revision 'HEAD~1'.\nFound 0 test files associated with changes.\n"
            ).split()
        )
E       assert ['warning:',\n 're-init:',\n 'ignored',\n '--initial-branch=master',\n 'Comparing',\n 'against',\n 'revision',\n "'HEAD~1'.",\n 'Found',\n '0',\n 'test',\n 'files',\n 'associated',\n 'with',\n 'changes.'] == ['Comparing',\n 'against',\n 'revision',\n "'HEAD~1'.",\n 'Found',\n '0',\n 'test',\n 'files',\n 'associated',\n 'with',\n 'changes.']
E         At index 0 diff: 'warning:' != 'Comparing'
E         Left contains 4 more items, first extra item: 'files'
E         Full diff:
E           [
E         +  'warning:',
E         +  're-init:',
E         +  'ignored',
E         +  '--initial-branch=master',
E            'Comparing',
E            'against',
E            'revision',
E            "'HEAD~1'.",
E            'Found',
E            '0',
E            'test',
E            'files',
E            'associated',
E            'with',
E            'changes.',
E           ]

dev_tools/bash_scripts_test.py:90: AssertionError
============================================================================================= warnings summary =============================================================================================
env/lib/python3.9/site-packages/pytest_asyncio/plugin.py:191
env/lib/python3.9/site-packages/pytest_asyncio/plugin.py:191
env/lib/python3.9/site-packages/pytest_asyncio/plugin.py:191
env/lib/python3.9/site-packages/pytest_asyncio/plugin.py:191
env/lib/python3.9/site-packages/pytest_asyncio/plugin.py:191
  /Users/mhucka/project-files/quantum/repos/Cirq/env/lib/python3.9/site-packages/pytest_asyncio/plugin.py:191: DeprecationWarning: The 'asyncio_mode' default value will change to 'strict' in future, please explicitly use 'asyncio_mode=strict' or 'asyncio_mode=auto' in pytest configuration file.
    config.issue_config_time_warning(LEGACY_MODE, stacklevel=2)

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
========================================================================================= short test summary info ==========================================================================================
FAILED dev_tools/bash_scripts_test.py::test_pytest_changed_files_file_selection - assert ['warning:',\n 're-init:',\n 'ignored',\n '--initial-branch=master',\n 'Comparing',\n 'against',\n 'revision',\n...
====================================================================================== 1 failed, 5 warnings in 1.22s =======================================================================================

Cirq version

0.16.0.dev

@mhucka mhucka added the kind/bug-report Something doesn't seem to work. label Jul 15, 2022
mhucka added a commit to mhucka/Cirq that referenced this issue Jul 15, 2022
If the user has a ~/.gitconfig file where init.templateDir is set,
then some of the tests in bash_scripts_test.py may fail because the
settings may cause the commands in the script to produce output that
doesn't match expectations. The changes here pass an explicit argument
to `git init` commands to override anything the user may have set in
their ~/.gitconfig file.
rht pushed a commit to rht/Cirq that referenced this issue May 1, 2023
…nit.templateDir` (quantumlib#5774)

This is a fix for issue quantumlib#5773. The approach involves passing an explicit `--template` parameter to the `git init` commands in `dev_tools/bash_scripts_test.py`. This will override any settings that the user happens to have for `init.templateDir` in their `~/.gitconfig` file.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug-report Something doesn't seem to work.
Projects
None yet
Development

No branches or pull requests

1 participant