From 7f1d99538baa489ecc3ae042f265a30198f18665 Mon Sep 17 00:00:00 2001 From: Zack Koppert Date: Thu, 26 Sep 2024 11:07:19 -0700 Subject: [PATCH 1/2] test: write test to test existing configs without a newline update properly Signed-off-by: Zack Koppert --- test_dependabot_file.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/test_dependabot_file.py b/test_dependabot_file.py index fc078fe..a31c537 100644 --- a/test_dependabot_file.py +++ b/test_dependabot_file.py @@ -108,6 +108,36 @@ def test_build_dependabot_file_with_2_space_indent_existing_config_bundler_with_ ) self.assertEqual(result, expected_result) + def test_build_dependabot_file_with_2_space_indent_existing_config_bundler_with_update_and_no_newline( + self, + ): + """Test that the dependabot.yml file is built correctly with bundler""" + repo = MagicMock() + repo.file_contents.side_effect = lambda f, filename="Gemfile": f == filename + + # expected_result maintains existing ecosystem with custom configuration + # and adds new ecosystem + expected_result = """--- +version: 2 +updates: + - package-ecosystem: "pip" + directory: "/" + schedule: + interval: "weekly" + commit-message: + prefix: "chore(deps)" + - package-ecosystem: 'bundler' + directory: '/' + schedule: + interval: 'weekly'""" + existing_config = MagicMock() + existing_config.decoded = b'---\nversion: 2\nupdates:\n - package-ecosystem: "pip"\n directory: "/"\n\ + schedule:\n interval: "weekly"\n commit-message:\n prefix: "chore(deps)"\n' + result = build_dependabot_file( + repo, False, [], {}, existing_config, "weekly", "" + ) + self.assertEqual(result, expected_result) + def test_build_dependabot_file_with_weird_space_indent_existing_config_bundler_with_update( self, ): From ed2fb7323b2354c45c103d71aa761ab8f33e5e87 Mon Sep 17 00:00:00 2001 From: Zack Koppert Date: Thu, 26 Sep 2024 11:23:39 -0700 Subject: [PATCH 2/2] fix: code no longer relies on existing end of file newline in dependabot config Signed-off-by: Zack Koppert --- dependabot_file.py | 4 ++++ test_dependabot_file.py | 5 +++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/dependabot_file.py b/dependabot_file.py index 56ef456..c20fd30 100644 --- a/dependabot_file.py +++ b/dependabot_file.py @@ -139,6 +139,10 @@ def build_dependabot_file( try: if repo.file_contents(file): package_managers_found[manager] = True + # If the last thing in the file is not a newline, + # add one before adding a new language config to the file + if dependabot_file and dependabot_file[-1] != "\n": + dependabot_file += "\n" dependabot_file += make_dependabot_config( manager, group_dependencies, indent, schedule, schedule_day ) diff --git a/test_dependabot_file.py b/test_dependabot_file.py index a31c537..cb018b7 100644 --- a/test_dependabot_file.py +++ b/test_dependabot_file.py @@ -129,10 +129,11 @@ def test_build_dependabot_file_with_2_space_indent_existing_config_bundler_with_ - package-ecosystem: 'bundler' directory: '/' schedule: - interval: 'weekly'""" + interval: 'weekly' +""" existing_config = MagicMock() existing_config.decoded = b'---\nversion: 2\nupdates:\n - package-ecosystem: "pip"\n directory: "/"\n\ - schedule:\n interval: "weekly"\n commit-message:\n prefix: "chore(deps)"\n' + schedule:\n interval: "weekly"\n commit-message:\n prefix: "chore(deps)"' result = build_dependabot_file( repo, False, [], {}, existing_config, "weekly", "" )