Skip to content

Conversation

@guotin
Copy link
Contributor

@guotin guotin commented Mar 22, 2021

Here's a version that gets the tests from git working directory as it did previously. So now it should run tests from working directory changes + new tests. Requires .coveragerc file to be in place with relative_files=True when initialization is done.

Example .coveragerc:

[run] relative_files = True 

Usage:

  1. Create .coveragerc
  2. Run pytest --cov=<path to code> --cov-context=test
  3. Rename .coverage to 'mapping.db`
  4. Make changes and run pytest --rts --rts-coverage-db=mapping.db
select tests based on changed lines in git working directory
if line_number in changed_lines:
tests.update(
strip_pytest_cov_testname(testname)
for testname in coverage_data.contexts_by_lineno(changed_file)[
Copy link
Contributor

Choose a reason for hiding this comment

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

it is possible that coverage_data.contexts_by_lineno() queries DB, so it is better to store the resulting value before line 54 to avoid extra queries.

tests: Set[str] = set()
for changed_file in file_diffs:
changed_lines = get_changed_lines(file_diffs[changed_file])
for line_number in coverage_data.contexts_by_lineno(changed_file):
Copy link
Contributor

Choose a reason for hiding this comment

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

Would something like that work?

contexts = coverage_data.contexts_by_lineno(changed_file) if not contexts: # is there a better check if dict is empty? continue # skipping diff parsing for uncovered/moved/renamed file changed_lines = get_changed_lines(file_diffs[changed_file]).intersection(contexts.keys()) tests.update( strip_pytest_cov_testname(contexts[line]) for line in changed_lines )
Copy link
Contributor

Choose a reason for hiding this comment

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

If I read it correctly contexts_by_lineno() opens a connection to DB every time you call it and there is no method to pass several file names and get results. To solve possible performance problems we could:

  1. read DB directly
  2. create a PR in coveragepy repo

But let's think about it later.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Your suggestion seems much better. Working on it.

@matveypashkovskiy
Copy link
Contributor

Shall we mention in README.md:

[run] relative_files = True 
@guotin
Copy link
Contributor Author

guotin commented Mar 23, 2021

Yeah I can add it

guotin and others added 5 commits March 23, 2021 15:19
improve code for test selection to prevent multiple database calls
simplify testcase for changed lines parsing to get rid of bloat
pass gitrepository object around in functions to prevent extra calls
@matveypashkovskiy matveypashkovskiy merged commit 51f3e7a into master Mar 24, 2021
@matveypashkovskiy
Copy link
Contributor

Good, I suppose the only thing left to do it is run tests from commits done after --rts-from-commit=[SHA]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants