Repository: facebookincubator/ptr

Issue:
Coverage >= 5.0 Breaks Coverage Analysis
## How is `ptr` not doing what you expect?

Analyzing coverage is failing on Mac OS X with coverage unable to find the `COVERAGE_FILE`
- May need to open an Issue / do a PR to coverage itself maybe here

## What is your suggestion to make this better?

Workout why the file can not be found. I think it will be something to do with the `COVERAGE_FILE` environment variable we set.
https://github.com/facebookincubator/ptr/blob/master/ptr.py#L705

## Code/Bug example?
Output: https://pastebin.com/2kdQQw8g

## How can a developer reproduce this?
Install coverage >= 5.0 and run ptr.

Discussion:
cooperlees: Guess we could just set both environment variables ..

Patch:
diff --git a/ptr.py b/ptr.py
index 7b24422..4bce8ed 100755
--- a/ptr.py
+++ b/ptr.py
@@ -702,7 +702,9 @@ async def _test_runner(
         else None
     )
     env = _set_build_env(extra_build_env_path)
-    env["COVERAGE_FILE"] = str(cov_data_path)
+    need_cov_env_var = await _using_coverage_5(venv_path)
+    if not need_cov_env_var:
+        env["COVERAGE_FILE"] = str(cov_data_path)
 
     while True:
         try:
@@ -743,6 +745,18 @@ async def _test_runner(
         queue.task_done()
 
 
+async def _using_coverage_5(venv_path: Path, timeout: float = 5) -> bool:
+    """ Check coverage version and set the correct environment var """
+    version_sub_str = "version 5."
+    if WINDOWS:
+        cov_exe = venv_path / "Scripts" / "coverage.exe"
+    else:
+        cov_exe = venv_path / "bin" / "coverage"
+
+    stdout, _stderr = await _gen_check_output((str(cov_exe), "--help"), timeout=timeout)
+    return version_sub_str in stdout.decode("utf-8")
+
+
 async def create_venv(
     mirror: str,
     py_exe: str = sys.executable,


Test patch:
diff --git a/ptr_tests.py b/ptr_tests.py
index 77d5c59..702a057 100644
--- a/ptr_tests.py
+++ b/ptr_tests.py
@@ -455,6 +455,7 @@ class TestPtr(unittest.TestCase):
             self.assertTrue("[global]" in conf_file)
             self.assertTrue("/simple" in conf_file)
 
+    @patch("ptr._using_coverage_5", async_none)
     @patch("ptr._test_steps_runner", fake_test_steps_runner)
     def test_test_runner(self) -> None:
         queue = asyncio.Queue()  # type: asyncio.Queue
@@ -538,6 +539,14 @@ class TestPtr(unittest.TestCase):
             # Ensure we've "printed coverage"
             self.assertEqual(mock_print.call_count, 1)
 
+    @patch("ptr._gen_check_output", return_bytes_output)
+    def test_using_coverage_5(self) -> None:
+        self.assertFalse(
+            self.loop.run_until_complete(
+                ptr._using_coverage_5(Path("venvs/test"), timeout=1)
+            )
+        )
+
     def test_validate_base_dir(self) -> None:
         path_str = gettempdir()
         expected_path = Path(path_str)