From 7b491c7110a2e2234ca19ff8b8d66f7efb1422fe Mon Sep 17 00:00:00 2001 From: Jesse Bona <37656694+jessebona@users.noreply.github.com> Date: Fri, 1 Feb 2019 19:15:59 +1100 Subject: [PATCH 1/5] Update migrate_config.py Added if statement to prevent looping through header lines if configuration file is empty --- pre_commit/commands/migrate_config.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pre_commit/commands/migrate_config.py b/pre_commit/commands/migrate_config.py index 3f73bb83..47bb7695 100644 --- a/pre_commit/commands/migrate_config.py +++ b/pre_commit/commands/migrate_config.py @@ -21,8 +21,10 @@ def _migrate_map(contents): # Find the first non-header line lines = contents.splitlines(True) i = 0 - while _is_header_line(lines[i]): - i += 1 + # Only loop on non empty configuration file + if i < len(lines): + while _is_header_line(lines[i]): + i += 1 header = ''.join(lines[:i]) rest = ''.join(lines[i:]) From f2be2ead352cab90718d73638f11aa8e4b070ca9 Mon Sep 17 00:00:00 2001 From: Jesse Bona <37656694+jessebona@users.noreply.github.com> Date: Sat, 2 Feb 2019 10:34:53 +1100 Subject: [PATCH 2/5] Update migrate_config.py Corrected loop condition to not run if configuration file only contains new lines. --- pre_commit/commands/migrate_config.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/pre_commit/commands/migrate_config.py b/pre_commit/commands/migrate_config.py index 47bb7695..bac42319 100644 --- a/pre_commit/commands/migrate_config.py +++ b/pre_commit/commands/migrate_config.py @@ -22,9 +22,8 @@ def _migrate_map(contents): lines = contents.splitlines(True) i = 0 # Only loop on non empty configuration file - if i < len(lines): - while _is_header_line(lines[i]): - i += 1 + while i < len(lines) and _is_header_line(lines[i]): + i += 1 header = ''.join(lines[:i]) rest = ''.join(lines[i:]) From 8a7142d7632372fe5493aa8bead9723462a9d86b Mon Sep 17 00:00:00 2001 From: Jesse Bona <37656694+jessebona@users.noreply.github.com> Date: Sat, 2 Feb 2019 10:38:04 +1100 Subject: [PATCH 3/5] Added test for blank configuration file --- tests/commands/migrate_config_test.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/commands/migrate_config_test.py b/tests/commands/migrate_config_test.py index 8f9153fd..e07f721f 100644 --- a/tests/commands/migrate_config_test.py +++ b/tests/commands/migrate_config_test.py @@ -147,3 +147,12 @@ def test_migrate_config_sha_to_rev(tmpdir): ' rev: v1.2.0\n' ' hooks: []\n' ) + +@pytest.mark.parametrize('contents', ('', '\n')) +def test_empty_configuration_file_user_error(tmpdir, contents): + cfg = tmpdir.join(C.CONFIG_FILE) + cfg.write(contents) + with tmpdir.as_cwd(): + assert not migrate_config(C.CONFIG_FILE) + # even though the config is invalid, this should be a noop + assert cfg.read() == contents From e2ee95d9b2f1bda70b1573bd9daa3c936d18dd49 Mon Sep 17 00:00:00 2001 From: Jesse Bona <37656694+jessebona@users.noreply.github.com> Date: Sat, 2 Feb 2019 11:32:09 +1100 Subject: [PATCH 4/5] Update migrate_config_test.py Added second blank line between test_migrate_config_sha_to_rev and test_empty_configuration_file_user_error --- tests/commands/migrate_config_test.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/commands/migrate_config_test.py b/tests/commands/migrate_config_test.py index e07f721f..945d8b4a 100644 --- a/tests/commands/migrate_config_test.py +++ b/tests/commands/migrate_config_test.py @@ -148,6 +148,7 @@ def test_migrate_config_sha_to_rev(tmpdir): ' hooks: []\n' ) + @pytest.mark.parametrize('contents', ('', '\n')) def test_empty_configuration_file_user_error(tmpdir, contents): cfg = tmpdir.join(C.CONFIG_FILE) From 1a3d296d8750deffe9688885380e12b175409d7b Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Fri, 1 Feb 2019 16:47:08 -0800 Subject: [PATCH 5/5] Trailing whitespace too Github editor is a fickle beast --- tests/commands/migrate_config_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/commands/migrate_config_test.py b/tests/commands/migrate_config_test.py index 945d8b4a..c58b9f74 100644 --- a/tests/commands/migrate_config_test.py +++ b/tests/commands/migrate_config_test.py @@ -148,7 +148,7 @@ def test_migrate_config_sha_to_rev(tmpdir): ' hooks: []\n' ) - + @pytest.mark.parametrize('contents', ('', '\n')) def test_empty_configuration_file_user_error(tmpdir, contents): cfg = tmpdir.join(C.CONFIG_FILE)