Skip to content

Commit

Permalink
Make pytest-changed-files include api tests when an __init__ file is …
Browse files Browse the repository at this point in the history
…changed (#2575)

Fixes #2506
  • Loading branch information
Strilanc authored and CirqBot committed Nov 21, 2019
1 parent e2446d4 commit 96f5f54
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 18 deletions.
13 changes: 9 additions & 4 deletions check/pytest-changed-files
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,16 @@ changed=$(git diff --name-only ${rev} -- \
| sort \
| uniq \
)
num_changed=$(echo -e "${changed}" | wc -w)
if git diff --name-only "${rev}" -- | grep "__init__\.py$" > /dev/null; then
# Include global API tests when an __init__ file is touched.
changed+=('docs/docs_coverage_test.py')
changed+=('cirq/protocols/json_test.py')
fi
num_changed=$(echo -e "${changed[@]}" | wc -w)

# Run it.
echo "Found ${num_changed} differing files with associated tests." >&2
if [ -z "${changed}" ]; then
echo "Found ${num_changed} test files associated with changes." >&2
if [ "${num_changed}" -eq 0 ]; then
exit 0
fi
pytest ${rest} ${changed}
pytest ${rest} "${changed[@]}"
5 changes: 5 additions & 0 deletions check/pytest-changed-files-and-incremental-coverage
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ changed_python_tests=$(git diff --name-only "${rev}" -- \
| sort \
| uniq \
)
if git diff --name-only "${rev}" -- | grep "__init__\.py$" > /dev/null; then
# Include global API tests when an __init__ file is touched.
changed_python_tests+=('docs/docs_coverage_test.py')
changed_python_tests+=('cirq/protocols/json_test.py')
fi
if [ "${#changed_python_tests[@]}" -eq 0 ]; then
echo -e "\033[33mNo changed files with associated python tests.\033[0m" >&2
exit 0
Expand Down
42 changes: 28 additions & 14 deletions dev_tools/bash_scripts_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def test_pytest_changed_files_file_selection(tmpdir_factory):
assert result.out == ''
assert result.err.split() == (
"Comparing against revision 'HEAD~1'.\n"
"Found 0 differing files with associated tests.\n").split()
"Found 0 test files associated with changes.\n").split()

result = run(script_file='check/pytest-changed-files',
tmpdir_factory=tmpdir_factory,
Expand All @@ -101,7 +101,7 @@ def test_pytest_changed_files_file_selection(tmpdir_factory):
assert result.out == 'INTERCEPTED pytest file_test.py\n'
assert result.err.split() == (
"Comparing against revision 'HEAD~1'.\n"
"Found 1 differing files with associated tests.\n").split()
"Found 1 test files associated with changes.\n").split()

result = run(script_file='check/pytest-changed-files',
tmpdir_factory=tmpdir_factory,
Expand All @@ -113,7 +113,7 @@ def test_pytest_changed_files_file_selection(tmpdir_factory):
assert result.out == 'INTERCEPTED pytest file_test.py\n'
assert result.err.split() == (
"Comparing against revision 'HEAD~1'.\n"
"Found 1 differing files with associated tests.\n").split()
"Found 1 test files associated with changes.\n").split()

result = run(script_file='check/pytest-changed-files',
tmpdir_factory=tmpdir_factory,
Expand All @@ -126,7 +126,7 @@ def test_pytest_changed_files_file_selection(tmpdir_factory):
assert result.out == 'INTERCEPTED pytest file_test.py\n'
assert result.err.split() == (
"Comparing against revision 'HEAD'.\n"
"Found 1 differing files with associated tests.\n").split()
"Found 1 test files associated with changes.\n").split()

result = run(script_file='check/pytest-changed-files',
tmpdir_factory=tmpdir_factory,
Expand All @@ -139,7 +139,21 @@ def test_pytest_changed_files_file_selection(tmpdir_factory):
assert result.out == 'INTERCEPTED pytest file_test.py\n'
assert result.err.split() == (
"Comparing against revision 'HEAD'.\n"
"Found 1 differing files with associated tests.\n").split()
"Found 1 test files associated with changes.\n").split()

result = run(script_file='check/pytest-changed-files',
tmpdir_factory=tmpdir_factory,
arg='HEAD',
setup='touch __init__.py\n'
'git add -A\n'
'git commit -m test --quiet --no-gpg-sign\n'
'echo x > __init__.py\n')
assert result.exit_code == 0
assert result.out == ('INTERCEPTED pytest docs/docs_coverage_test.py '
'cirq/protocols/json_test.py\n')
assert result.err.split() == (
"Comparing against revision 'HEAD'.\n"
"Found 2 test files associated with changes.\n").split()


@only_on_posix
Expand All @@ -152,7 +166,7 @@ def test_pytest_changed_files_branch_selection(tmpdir_factory):
assert result.out == ''
assert result.err.split() == (
"Comparing against revision 'HEAD'.\n"
"Found 0 differing files with associated tests.\n").split()
"Found 0 test files associated with changes.\n").split()

result = run(script_file='check/pytest-changed-files',
tmpdir_factory=tmpdir_factory,
Expand All @@ -167,7 +181,7 @@ def test_pytest_changed_files_branch_selection(tmpdir_factory):
assert result.out == ''
assert result.err.split() == (
"Comparing against revision 'master'.\n"
"Found 0 differing files with associated tests.\n").split()
"Found 0 test files associated with changes.\n").split()

result = run(script_file='check/pytest-changed-files',
tmpdir_factory=tmpdir_factory,
Expand All @@ -176,7 +190,7 @@ def test_pytest_changed_files_branch_selection(tmpdir_factory):
assert result.out == ''
assert result.err.split() == (
"Comparing against revision 'origin/master'.\n"
"Found 0 differing files with associated tests.\n").split()
"Found 0 test files associated with changes.\n").split()

result = run(script_file='check/pytest-changed-files',
tmpdir_factory=tmpdir_factory,
Expand All @@ -185,7 +199,7 @@ def test_pytest_changed_files_branch_selection(tmpdir_factory):
assert result.out == ''
assert result.err.split() == (
"Comparing against revision 'upstream/master'.\n"
"Found 0 differing files with associated tests.\n").split()
"Found 0 test files associated with changes.\n").split()

result = run(script_file='check/pytest-changed-files',
tmpdir_factory=tmpdir_factory,
Expand All @@ -194,7 +208,7 @@ def test_pytest_changed_files_branch_selection(tmpdir_factory):
assert result.out == ''
assert result.err.split() == (
"Comparing against revision 'upstream/master'.\n"
"Found 0 differing files with associated tests.\n").split()
"Found 0 test files associated with changes.\n").split()

result = run(script_file='check/pytest-changed-files',
tmpdir_factory=tmpdir_factory,
Expand Down Expand Up @@ -226,8 +240,8 @@ def test_pytest_changed_files_branch_selection(tmpdir_factory):
assert result.exit_code == 0
assert result.out == ''
assert result.err.split() == (
"Comparing against revision 'HEAD'.\n"
"Found 0 differing files with associated tests.\n").split()
"Comparing against revision 'HEAD'.\n"
"Found 0 test files associated with changes.\n").split()

result = run(script_file='check/pytest-changed-files',
tmpdir_factory=tmpdir_factory,
Expand All @@ -238,7 +252,7 @@ def test_pytest_changed_files_branch_selection(tmpdir_factory):
assert result.out == ''
assert result.err.split() == (
"Comparing against revision 'master'.\n"
"Found 0 differing files with associated tests.\n").split()
"Found 0 test files associated with changes.\n").split()

# Works on remotes.
result = run(script_file='check/pytest-changed-files',
Expand All @@ -254,7 +268,7 @@ def test_pytest_changed_files_branch_selection(tmpdir_factory):
assert result.out == ''
assert result.err.split() == (
"Comparing against revision 'origin/master'.\n"
"Found 0 differing files with associated tests.\n").split()
"Found 0 test files associated with changes.\n").split()


@only_on_posix
Expand Down

0 comments on commit 96f5f54

Please sign in to comment.