coveragepy icon indicating copy to clipboard operation
coveragepy copied to clipboard

An err with .coverage database

Open bromiao123 opened this issue 2 years ago • 3 comments

Hi,First of all, thanks for this library!

The question

When I was testing with Coverage.py, something went wrong:

Traceback (most recent call last): File "/home/bromiao/.local/lib/python3.8/site-packages/coverage/sqldata.py", line 1107, in execute return self.con.execute(sql, parameters) sqlite3.OperationalError: no such table: arc

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/home/bromiao/.local/lib/python3.8/site-packages/coverage/sqldata.py", line 1112, in execute return self.con.execute(sql, parameters) sqlite3.OperationalError: no such table: arc

The above exception was the direct cause of the following exception:

Traceback (most recent call last): File "test.py", line 22, in cov.json_report(outfile=report_str, pretty_print=True) File "/home/bromiao/.local/lib/python3.8/site-packages/coverage/control.py", line 1075, in json_report return render_report(self.config.json_output, JsonReporter(self), morfs, self._message) File "/home/bromiao/.local/lib/python3.8/site-packages/coverage/report.py", line 35, in render_report return reporter.report(morfs, outfile=outfile) File "/home/bromiao/.local/lib/python3.8/site-packages/coverage/jsonreport.py", line 45, in report for file_reporter, analysis in get_analysis_to_report(self.coverage, morfs): File "/home/bromiao/.local/lib/python3.8/site-packages/coverage/report.py", line 72, in get_analysis_to_report analysis = coverage._analyze(fr) File "/home/bromiao/.local/lib/python3.8/site-packages/coverage/control.py", line 854, in _analyze return Analysis(data, self.config.precision, it, self._file_mapper) File "/home/bromiao/.local/lib/python3.8/site-packages/coverage/results.py", line 24, in init executed = self.data.lines(self.filename) or [] File "/home/bromiao/.local/lib/python3.8/site-packages/coverage/sqldata.py", line 889, in lines arcs = self.arcs(filename) File "/home/bromiao/.local/lib/python3.8/site-packages/coverage/sqldata.py", line 940, in arcs arcs = con.execute(query, data) File "/home/bromiao/.local/lib/python3.8/site-packages/coverage/sqldata.py", line 1129, in execute raise DataError(f"Couldn't use data file {self.filename!r}: {msg}") from exc coverage.exceptions.DataError: Couldn't use data file '/home/bromiao/catkin_ws/src/test_rospy/test/unit/.coverage': no such table: arc '}

I used coverage.py like this:

cov = coverage.Coverage()
cov.set_option("run:branch", True)
cov.start()
do_something()
cov.stop()
cov.json_report(outfile=report_str, pretty_print=True)

This file named “usingcoverage.py”

When testing , I used threading and subprocess like this: Threading:

t=threading.Thread(target=target_func,args=(arg,))
t.start()

Subprocess:

def target_func(self,arg):
        ret = subprocess.run(“python3 usingcoverage.py”, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding="utf-8",timeout=100)

I think this might be causing the concurrent use of the .coverage database to throw the error

I did my work in Ubuntu 20.04, python 3.8.10 Coverage.py version 6.3.2 with C extension

bromiao123 avatar Aug 02 '22 06:08 bromiao123

Can you provide the full code to reproduce the problem? It would help to explain why you are using the API instead of the command-line interface, why you are using a subprocess from Python, and why you are using a thread to run the subprocess.

nedbat avatar Aug 02 '22 10:08 nedbat

The full code is sample. Can I use e-mail?

bromiao123 avatar Aug 03 '22 04:08 bromiao123

Have you got the e-mail?

bromiao123 avatar Aug 12 '22 05:08 bromiao123

Hi, I also have this problem. It is not happening consistently, using the PYTEST_ADDOPTS="--cov --cov-report=html" python setup.py test command line. Experiencing on wsl Ubuntu 20.04.5 with python 3.9.13, coverage==6.5.0, pytest-cov==2.7.1 and pytest==5.0.0

I get a warning instead of an error (paths abbreviated):

(...)/.virtualenv/lib/python3.9/site-packages/coverage/report.py:87: CoverageWarning: Couldn't parse '(...)/somepackage/somefile.py': Couldn't use data file '(...)/.coverage': no such table: arc (couldnt-parse)
  coverage._warn(msg, slug="couldnt-parse")

pavel-rezabek avatar Nov 09 '22 15:11 pavel-rezabek

I don't know about the pytest, But would you like to see if the ".coverage" data file exist? And if your ".coverage" data file is rewriting when you excuting your command?

bromiao123 avatar Nov 10 '22 12:11 bromiao123

Hi, I figured out the problem with my .coverage file. I was launching my tests from a commandline in VSCode and due to some reason, VSCode was launching pytest discovery at the same time, resulting in the coverage file being overwritten. I used sqlite database viewer to look into the .coverage file to find this in the meta table:

sys_argv ['/home/<user_name>/.vscode-server/extensions/ms-python.python-2022.18.2/pythonFiles/testing_tools/run_adapter.py', 'discover', 'pytest', '--', '--rootdir', '/home/<user_name>/dev/<project_name>', '-s', '--cache-clear', '-c=setup.cfg']
version 6.5.0
when 2022-11-10 17:58:54
has_arcs 1

While when the coverage was alright, the meta table looks like this:

sys_argv ['setup.py']
version 6.5.0
when 2022-11-10 17:18:15
has_arcs 1

Adding --no-cov to the VSCode setting "python.testing.pytestArgs" fixed it for me.

pavel-rezabek avatar Nov 10 '22 17:11 pavel-rezabek

I don't have a way to reproduce this yet. If you can provide me with code I can run, I can re-open it.

nedbat avatar Mar 19 '23 10:03 nedbat

I'm sorry for that, this is my problem. I started multiple programs at the same time, but they used the same database ".coverage". Writing different coverage data over a short period of time causes this problem. Then I tried to use different database for each program. Finally the problem was solved.

bromiao123 avatar Mar 20 '23 01:03 bromiao123