Skip to content

Commit

Permalink
Fix sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleFromNVIDIA committed Aug 28, 2024
1 parent b2efc79 commit 064073a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 16 deletions.
37 changes: 28 additions & 9 deletions src/rapids_pre_commit_hooks/pr_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,21 +163,40 @@ def check_jobs(
(pr_builder_key.start_mark.index, pr_builder_key.end_mark.index),
"place pr-builder job before all other jobs",
)
assert self.linter.content[pr_builder_value.end_mark.index] == "\n"
end = pr_builder_value.end_mark.index
previous_newline = self.linter.content[:end].rfind("\n")
if all(
c == " " for c in self.linter.content[previous_newline + 1 : end]
):
end = previous_newline
previous_newline_pr_builder = self.linter.content[
: pr_builder_key.start_mark.index
].rfind("\n")
assert all(
c == " "
for c in self.linter.content[
previous_newline + 1 : pr_builder_key.start_mark.index
]
)
w.add_replacement(
(
pr_builder_key.start_mark.index,
pr_builder_value.end_mark.index + 1,
previous_newline_pr_builder + 1,
end + 1,
),
"",
)
previous_newline_first = self.linter.content[
: first_key.start_mark.index
].rfind("\n")
assert all(
c == " "
for c in self.linter.content[
previous_newline + 1 : first_key.start_mark.index
]
)
w.add_replacement(
(first_key.start_mark.index, first_key.start_mark.index),
self.linter.content[
pr_builder_key.start_mark.index : (
pr_builder_value.end_mark.index + 1
)
],
(previous_newline_first + 1, previous_newline_first + 1),
self.linter.content[previous_newline_pr_builder + 1 : end + 1],
)

def check_root(self) -> None:
Expand Down
17 changes: 10 additions & 7 deletions test/rapids_pre_commit_hooks/test_pr_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ def test_check_pr_builder_job(content, warnings):
dedent(
"""\
jobs:
pr-builder: {}
pr-builder:
if: always()
other-job-1: {}
other-job-2: {}
"""
Expand All @@ -144,7 +145,8 @@ def test_check_pr_builder_job(content, warnings):
"""\
jobs:
other-job-1: {}
pr-builder: {}
pr-builder:
if: always()
other-job-2: {}
"""
),
Expand All @@ -154,8 +156,8 @@ def test_check_pr_builder_job(content, warnings):
(26, 36),
"place pr-builder job before all other jobs",
replacements=[
Replacement((26, 41), ""),
Replacement((8, 8), "pr-builder: {}\n"),
Replacement((24, 55), ""),
Replacement((6, 6), " pr-builder:\n if: always()\n"),
],
),
],
Expand All @@ -166,7 +168,8 @@ def test_check_pr_builder_job(content, warnings):
jobs:
other-job-1: {}
other-job-2: {}
pr-builder: {}
pr-builder:
if: always()
"""
),
2,
Expand All @@ -175,8 +178,8 @@ def test_check_pr_builder_job(content, warnings):
(44, 54),
"place pr-builder job before all other jobs",
replacements=[
Replacement((44, 59), ""),
Replacement((8, 8), "pr-builder: {}\n"),
Replacement((42, 73), ""),
Replacement((6, 6), " pr-builder:\n if: always()\n"),
],
),
],
Expand Down

0 comments on commit 064073a

Please sign in to comment.