Skip to content

Commit

Permalink
Allowing a test to skip if it is being run outside a git repo
Browse files Browse the repository at this point in the history
  • Loading branch information
john-science committed Dec 8, 2023
1 parent adb6cbd commit 3bc59ac
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions armi/bookkeeping/db/tests/test_database3.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
"""Tests for the Database3 class."""
from distutils.spawn import find_executable
import subprocess
import unittest

Expand All @@ -28,6 +29,13 @@
from armi.utils import getPreviousTimeNode
from armi.utils.directoryChangers import TemporaryDirectoryChanger

# determine if this is a parallel run, and git is installed
GIT_EXE = None
if find_executable("git") is not None:
GIT_EXE = "git"
elif find_executable("git.exe") is not None:
GIT_EXE = "git.exe"


class TestDatabase3(unittest.TestCase):
"""Tests for the Database3 class."""
Expand Down Expand Up @@ -553,18 +561,24 @@ def test_splitDatabase(self):
[(c, n) for c in (0, 1) for n in range(2)], "-all-iterations"
)

@unittest.skipIf(GIT_EXE is None, "This test needs Git.")
def test_grabLocalCommitHash(self):
"""Test of static method to grab a local commit hash with ARMI version."""
# 1. test outside a Git repo
localHash = database3.Database3.grabLocalCommitHash()
self.assertEqual(localHash, "unknown")

# 2. test inside an empty git repo
code = subprocess.run(
["git", "init", "."],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
).returncode
try:
code = subprocess.run(
["git", "init", "."],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
).returncode
except FileNotFoundError:
print("Skipping this test because it is being run outside a git repo.")
return

self.assertEqual(code, 0)
localHash = database3.Database3.grabLocalCommitHash()
self.assertEqual(localHash, "unknown")
Expand Down

0 comments on commit 3bc59ac

Please sign in to comment.