Skip to content

Commit

Permalink
Tests: Reduce number of individual test cases to improve maintainability
Browse files Browse the repository at this point in the history
Now, `jenkins_env` and `github_action_env` will only be tested through
`get_ci_info`. That is the main adapter function anyway.
  • Loading branch information
amotl committed Dec 19, 2021
1 parent fc76777 commit 5a6313c
Showing 1 changed file with 11 additions and 49 deletions.
60 changes: 11 additions & 49 deletions tests/test_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ def test_search_env_notfound():
assert value is None


@mock.patch.dict(os.environ, {"JENKINS_URL": "https://jenkins.example.org/job/3f786850e3"})
@mock.patch.dict(os.environ, {"ghprbSourceBranch": "testdrive"})
@mock.patch.dict(os.environ, {"GIT_COMMIT": "3f786850e387550fdab836ed7e6dc881de23001b"})
@mock.patch.dict(os.environ, {"ghprbPullId": "111"})
@mock.patch.dict(os.environ, {"BUILD_NUMBER": "42"})
def test_jenkins_env():
data = jenkins_env()
def test_get_ci_info_jenkins():
data = get_ci_info()
assert data == {
"service": "jenkins",
"branch": "testdrive",
Expand All @@ -31,36 +32,13 @@ def test_jenkins_env():
}


@mock.patch.dict(os.environ, {"GITHUB_ACTION": "true"})
@mock.patch.dict(os.environ, {"GITHUB_SHA": "3f786850e387550fdab836ed7e6dc881de23001b"})
@mock.patch.dict(os.environ, {"GITHUB_RUN_ID": "42"})
def test_github_action_env():
data = github_action_env()
assert data == {
"service": "github-actions",
"commit": "3f786850e387550fdab836ed7e6dc881de23001b",
"build": "42",
}


@mock.patch.dict(os.environ, {"GITHUB_SHA": "3f786850e387550fdab836ed7e6dc881de23001b"})
@mock.patch.dict(os.environ, {"GITHUB_RUN_ID": "42"})
@mock.patch.dict(os.environ, {"GITHUB_REF": "refs/heads/feature-branch-1"})
def test_github_action_env_with_branch():
data = github_action_env()
assert data == {
"service": "github-actions",
"commit": "3f786850e387550fdab836ed7e6dc881de23001b",
"build": "42",
"branch": "feature-branch-1",
}


@mock.patch.dict(os.environ, {"GITHUB_SHA": "3f786850e387550fdab836ed7e6dc881de23001b"})
@mock.patch.dict(os.environ, {"GITHUB_RUN_ID": "42"})
@mock.patch.dict(os.environ, {"GITHUB_REF": "refs/pull/111/merge"})
@mock.patch.dict(os.environ, {"GITHUB_HEAD_REF": "feature-branch-1"})
def test_github_action_env_with_pr():
data = github_action_env()
@mock.patch.dict(os.environ, {"GITHUB_RUN_ID": "42"})
def test_get_ci_info_github_with_pr():
data = get_ci_info()
assert data == {
"service": "github-actions",
"commit": "3f786850e387550fdab836ed7e6dc881de23001b",
Expand All @@ -70,34 +48,18 @@ def test_github_action_env_with_pr():
}


@mock.patch.dict(
os.environ,
{"JENKINS_URL": "https://jenkins.example.org/job/3f786850e387550fdab836ed7e6dc881de23001b"},
)
@mock.patch.dict(os.environ, {"ghprbSourceBranch": "testdrive"})
@mock.patch.dict(os.environ, {"GIT_COMMIT": "3f786850e387550fdab836ed7e6dc881de23001b"})
@mock.patch.dict(os.environ, {"ghprbPullId": "9999"})
@mock.patch.dict(os.environ, {"BUILD_NUMBER": "42"})
def test_get_ci_info_jenkins():
data = get_ci_info()
assert data == {
"service": "jenkins",
"branch": "testdrive",
"commit": "3f786850e387550fdab836ed7e6dc881de23001b",
"pr": "9999",
"build": "42",
}


@mock.patch.dict(os.environ, {"GITHUB_ACTION": "true"})
@mock.patch.dict(os.environ, {"GITHUB_SHA": "3f786850e387550fdab836ed7e6dc881de23001b"})
@mock.patch.dict(os.environ, {"GITHUB_REF": "refs/heads/feature-branch-1"})
@mock.patch.dict(os.environ, {"GITHUB_HEAD_REF": ""})
@mock.patch.dict(os.environ, {"GITHUB_RUN_ID": "42"})
def test_get_ci_info_github():
def test_get_ci_info_github_with_branch():
data = get_ci_info()
assert data == {
"service": "github-actions",
"commit": "3f786850e387550fdab836ed7e6dc881de23001b",
"build": "42",
"branch": "feature-branch-1",
}


Expand Down

0 comments on commit 5a6313c

Please sign in to comment.