Skip to content

Commit

Permalink
test: remove test inter-dependencies
Browse files Browse the repository at this point in the history
A bunch of tests had interdependencies since we rely on chdir'ing to
tempdirs (and then deleting those dirs) for various tests -- by running
the tests in randomized orders, we'd get randomized failures as it turns
out we were depending on eg. the existence of a previously-generated
.coverage file or being in a still-existing directory.

None of these randomized failures were actual functional issues, but
this should improve the consistency of our test runs in the future.
  • Loading branch information
TheKevJames committed Nov 4, 2021
1 parent e03a2de commit 17f52d2
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 49 deletions.
13 changes: 8 additions & 5 deletions tests/api/configuration_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# pylint: disable=no-self-use
import os
import shutil
import tempfile
import unittest
from unittest import mock
Expand All @@ -17,17 +16,21 @@

@mock.patch.object(Coveralls, 'config_filename', '.coveralls.mock')
class Configuration(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.old_cwd = os.getcwd()

@classmethod
def tearDownClass(cls):
os.chdir(cls.old_cwd)

def setUp(self):
self.dir = tempfile.mkdtemp()

os.chdir(self.dir)
with open('.coveralls.mock', 'w+') as fp:
fp.write('repo_token: xxx\n')
fp.write('service_name: jenkins\n')

def tearDown(self):
shutil.rmtree(self.dir)

@pytest.mark.skipif(yaml is None, reason='requires PyYAML')
@mock.patch.dict(os.environ, {}, clear=True)
def test_local_with_config(self):
Expand Down
63 changes: 38 additions & 25 deletions tests/api/encoding_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
import os
import subprocess
import unittest

from coveralls import Coveralls

Expand All @@ -9,28 +10,40 @@
NONUNICODE_DIR = os.path.join(BASE_DIR, 'nonunicode')


def test_non_unicode():
os.chdir(NONUNICODE_DIR)
subprocess.call(['coverage', 'run', 'nonunicode.py'], cwd=NONUNICODE_DIR)

actual_json = json.dumps(Coveralls(repo_token='xxx').get_coverage())
expected_json_part = (
'"source": "# coding: iso-8859-15\\n\\n'
'def hello():\\n'
' print(\'I like P\\u00f3lya distribution.\')')
assert expected_json_part in actual_json


def test_malformed_encoding_declaration_py3_or_coverage4():
os.chdir(NONUNICODE_DIR)
subprocess.call(['coverage', 'run', 'malformed.py'], cwd=NONUNICODE_DIR)

result = Coveralls(repo_token='xxx').get_coverage()
assert len(result) == 1

assert result[0]['coverage'] == [None, None, 1, 0]
assert result[0]['name'] == 'malformed.py'
assert result[0]['source'].strip() == ('# -*- cоding: utf-8 -*-\n\n'
'def hello():\n'
' return 1')
assert 'branches' not in result[0]
class EncodingTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.old_cwd = os.getcwd()

@classmethod
def tearDownClass(cls):
os.chdir(cls.old_cwd)

@staticmethod
def test_non_unicode():
os.chdir(NONUNICODE_DIR)
subprocess.call(['coverage', 'run', 'nonunicode.py'],
cwd=NONUNICODE_DIR)

actual_json = json.dumps(Coveralls(repo_token='xxx').get_coverage())
expected_json_part = (
'"source": "# coding: iso-8859-15\\n\\n'
'def hello():\\n'
' print(\'I like P\\u00f3lya distribution.\')')
assert expected_json_part in actual_json

@staticmethod
def test_malformed_encoding_declaration_py3_or_coverage4():
os.chdir(NONUNICODE_DIR)
subprocess.call(['coverage', 'run', 'malformed.py'],
cwd=NONUNICODE_DIR)

result = Coveralls(repo_token='xxx').get_coverage()
assert len(result) == 1

assert result[0]['coverage'] == [None, None, 1, 0]
assert result[0]['name'] == 'malformed.py'
assert result[0]['source'].strip() == ('# -*- cоding: utf-8 -*-\n\n'
'def hello():\n'
' return 1')
assert 'branches' not in result[0]
8 changes: 8 additions & 0 deletions tests/api/reporter_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ def assert_coverage(actual, expected):


class ReporterTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.old_cwd = os.getcwd()

@classmethod
def tearDownClass(cls):
os.chdir(cls.old_cwd)

def setUp(self):
os.chdir(EXAMPLE_DIR)

Expand Down
44 changes: 25 additions & 19 deletions tests/git_test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# pylint: disable=no-self-use
import os
import re
import shutil
import subprocess
import tempfile
import unittest
Expand All @@ -18,10 +17,18 @@


class GitTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.old_cwd = os.getcwd()

@classmethod
def tearDownClass(cls):
os.chdir(cls.old_cwd)

def setUp(self):
self.dir = tempfile.mkdtemp()

os.chdir(self.dir)

# TODO: switch to pathlib
open('README', 'a').close() # pylint: disable=consider-using-with

Expand All @@ -35,9 +42,6 @@ def setUp(self):
subprocess.call(['git', 'remote', 'add', GIT_REMOTE, GIT_URL],
cwd=self.dir)

def tearDown(self):
shutil.rmtree(self.dir)

@mock.patch.dict(os.environ, {'TRAVIS_BRANCH': 'master'}, clear=True)
def test_git(self):
git_info = coveralls.git.git_info()
Expand Down Expand Up @@ -74,7 +78,19 @@ def test_gitlog(self):
assert coveralls.git.gitlog('%s') == GIT_COMMIT_MSG


class GitInfoTestEnvVars(unittest.TestCase):
class GitInfoTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.old_cwd = os.getcwd()

@classmethod
def tearDownClass(cls):
os.chdir(cls.old_cwd)

def setUp(self):
self.dir = tempfile.mkdtemp()
os.chdir(self.dir)

@mock.patch.dict(os.environ, {
'GIT_ID': '5e837ce92220be64821128a70f6093f836dd2c05',
'GIT_BRANCH': 'master',
Expand All @@ -86,7 +102,7 @@ class GitInfoTestEnvVars(unittest.TestCase):
'GIT_URL': GIT_URL,
'GIT_REMOTE': GIT_REMOTE,
}, clear=True)
def test_gitlog_envvars(self):
def test_gitinfo_envvars(self):
git_info = coveralls.git.git_info()
commit_id = git_info['git']['head'].pop('id')
assert re.match(r'^[a-f0-9]{40}$', commit_id)
Expand All @@ -108,24 +124,14 @@ def test_gitlog_envvars(self):
},
}


class GitInfoTestNotAGitRepository(unittest.TestCase):
def setUp(self):
self.dir = tempfile.mkdtemp()

os.chdir(self.dir)

def tearDown(self):
shutil.rmtree(self.dir)

def test_gitlog_not_a_git_repo(self):
def test_gitinfo_not_a_git_repo(self):
git_info = coveralls.git.git_info()

self.assertRaises(CoverallsException)
assert git_info == {}


class GitInfoTestBranch(GitTest):
class GitInfoOverridesTest(unittest.TestCase):
@mock.patch.dict(os.environ, {
'GITHUB_ACTIONS': 'true',
'GITHUB_REF': 'refs/pull/1234/merge',
Expand Down
8 changes: 8 additions & 0 deletions tests/integration_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ class IntegrationTest(unittest.TestCase):
'GIT_MESSAGE': 'Ran the integration tests',
}

@classmethod
def setUpClass(cls):
cls.old_cwd = os.getcwd()

@classmethod
def tearDownClass(cls):
os.chdir(cls.old_cwd)

def _test_harness(self, num, hits):
with tempfile.TemporaryDirectory() as tempdir:
os.chdir(tempdir)
Expand Down

0 comments on commit 17f52d2

Please sign in to comment.