Skip to content

Commit 3d7db4c

Browse files
authored
Merge pull request #216 from hkadakia/fix
2 parents 9f1f9b2 + 34bc2c2 commit 3d7db4c

File tree

2 files changed

+83
-7
lines changed

2 files changed

+83
-7
lines changed

dependabot_file.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ def build_dependabot_file(
6565
"github-actions": False,
6666
}
6767
DEFAULT_INDENT = 2 # pylint: disable=invalid-name
68-
68+
# create a local copy in order to avoid overwriting the global exemption list
69+
exempt_ecosystems_list = exempt_ecosystems.copy()
6970
if existing_config:
7071
dependabot_file = existing_config.decoded.decode("utf-8")
7172
ecosystem_line = next(
@@ -86,14 +87,14 @@ def build_dependabot_file(
8687
updates:
8788
"""
8889

89-
add_existing_ecosystem_to_exempt_list(exempt_ecosystems, existing_config)
90+
add_existing_ecosystem_to_exempt_list(exempt_ecosystems_list, existing_config)
9091

9192
# If there are repository specific exemptions,
9293
# overwrite the global exemptions for this repo only
9394
if repo_specific_exemptions and repo.full_name in repo_specific_exemptions:
94-
exempt_ecosystems = []
95+
exempt_ecosystems_list = []
9596
for ecosystem in repo_specific_exemptions[repo.full_name]:
96-
exempt_ecosystems.append(ecosystem)
97+
exempt_ecosystems_list.append(ecosystem)
9798

9899
package_managers = {
99100
"bundler": ["Gemfile", "Gemfile.lock"],
@@ -118,7 +119,7 @@ def build_dependabot_file(
118119

119120
# Detect package managers where manifest files have known names
120121
for manager, manifest_files in package_managers.items():
121-
if manager in exempt_ecosystems:
122+
if manager in exempt_ecosystems_list:
122123
continue
123124
for file in manifest_files:
124125
try:
@@ -132,7 +133,7 @@ def build_dependabot_file(
132133
pass
133134

134135
# detect package managers with variable file names
135-
if "terraform" not in exempt_ecosystems:
136+
if "terraform" not in exempt_ecosystems_list:
136137
try:
137138
for file in repo.directory_contents("/"):
138139
if file[0].endswith(".tf"):
@@ -143,7 +144,7 @@ def build_dependabot_file(
143144
break
144145
except github3.exceptions.NotFoundError:
145146
pass
146-
if "github-actions" not in exempt_ecosystems:
147+
if "github-actions" not in exempt_ecosystems_list:
147148
try:
148149
for file in repo.directory_contents(".github/workflows"):
149150
if file[0].endswith(".yml") or file[0].endswith(".yaml"):

test_dependabot_file.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,81 @@ def test_add_existing_ecosystem_to_exempt_list(self):
387387
for ecosystem in exempt_ecosystems:
388388
self.assertIn(ecosystem, exempt_ecosystems)
389389

390+
def test_build_dependabot_file_for_multiple_repos_with_few_existing_config(self):
391+
"""
392+
Test the case where there are multiple repos with few existing dependabot config
393+
"""
394+
existing_config_repo = MagicMock()
395+
existing_config_repo.file_contents.side_effect = (
396+
lambda f, filename="Gemfile": f == filename
397+
)
398+
399+
existing_config = MagicMock()
400+
existing_config.decoded = b'---\nversion: 2\nupdates:\n - package-ecosystem: "bundler"\n\
401+
directory: "/"\n schedule:\n interval: "weekly"\n commit-message:\n prefix: "chore(deps)"\n'
402+
exempt_ecosystems = []
403+
result = build_dependabot_file(
404+
existing_config_repo, False, exempt_ecosystems, {}, existing_config
405+
)
406+
self.assertEqual(result, None)
407+
408+
no_existing_config_repo = MagicMock()
409+
filename_list = ["package.json", "package-lock.json", "yarn.lock"]
410+
for filename in filename_list:
411+
no_existing_config_repo.file_contents.side_effect = (
412+
lambda f, filename=filename: f == filename
413+
)
414+
expected_result = """---
415+
version: 2
416+
updates:
417+
- package-ecosystem: 'npm'
418+
directory: '/'
419+
schedule:
420+
interval: 'weekly'
421+
"""
422+
result = build_dependabot_file(
423+
no_existing_config_repo, False, exempt_ecosystems, {}, None
424+
)
425+
self.assertEqual(result, expected_result)
426+
427+
def test_check_multiple_repos_with_no_dependabot_config(self):
428+
"""
429+
Test the case where there is a single repo
430+
"""
431+
mock_repo_1 = MagicMock()
432+
mock_repo_1.file_contents.side_effect = lambda filename: filename == "go.mod"
433+
434+
expected_result = """---
435+
version: 2
436+
updates:
437+
- package-ecosystem: 'gomod'
438+
directory: '/'
439+
schedule:
440+
interval: 'weekly'
441+
"""
442+
exempt_ecosystems = []
443+
result = build_dependabot_file(mock_repo_1, False, exempt_ecosystems, {}, None)
444+
self.assertEqual(result, expected_result)
445+
446+
no_existing_config_repo = MagicMock()
447+
filename_list = ["package.json", "package-lock.json", "yarn.lock"]
448+
for filename in filename_list:
449+
no_existing_config_repo.file_contents.side_effect = (
450+
lambda f, filename=filename: f == filename
451+
)
452+
expected_result = """---
453+
version: 2
454+
updates:
455+
- package-ecosystem: 'npm'
456+
directory: '/'
457+
schedule:
458+
interval: 'weekly'
459+
"""
460+
result = build_dependabot_file(
461+
no_existing_config_repo, False, exempt_ecosystems, {}, None
462+
)
463+
self.assertEqual(result, expected_result)
464+
390465

391466
if __name__ == "__main__":
392467
unittest.main()

0 commit comments

Comments
 (0)