Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions pre_commit_hooks/insert_license.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ def license_not_found( # pylint: disable=too-many-arguments
"""
if not remove_header:
index = 0
for line in src_file_content:
for index, line in enumerate(src_file_content):
stripped_line = line.strip()
# Special treatment for user provided regex,
# or shebang, file encoding directive,
Expand All @@ -339,13 +339,14 @@ def license_not_found( # pylint: disable=too-many-arguments
index += 1 # Skip matched line
break # And insert after that line.
elif (
stripped_line.startswith("#!")
or stripped_line.startswith("# -*- coding")
or stripped_line == ""
not stripped_line.startswith("#!")
and not stripped_line.startswith("# -*- coding")
and not stripped_line == ""
):
index += 1
else:
break
else:
# We got all the way to the end without hitting `break`, reset it to line 0
index = 0
src_file_content = (
src_file_content[:index]
+ license_info.prefixed_license
Expand Down
9 changes: 9 additions & 0 deletions tests/insert_license_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,15 @@ def _convert_line_ending(file_path, new_line_endings):
True,
["--insert-license-after-regex", "^<\\?php$"],
),
(
"module_without_license.py",
"#",
"module_with_license.py",
"",
True,
# Test that when the regex is not found, the license is put at the first line
["--insert-license-after-regex", "^<\\?php$"],
),
(
"module_without_license.py",
"#",
Expand Down
2 changes: 2 additions & 0 deletions tests/resources/module_with_license.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
<!-- Hi -->
<html>
<?php
/*
* Copyright (C) 2017 Teela O'Malley
Expand Down
2 changes: 2 additions & 0 deletions tests/resources/module_without_license.php
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
<!-- Hi -->
<html>
<?php
print "Hello";