-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a keep_sorted presubmit check inspired by the corresponding internal tool: http://go/keep-sorted. Also add 'pw keep-sorted' which sorts these blocks automatically. Bug: b/250875082 Change-Id: Id8d74c942c93176df8b2881339fbda2cc797346e Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/112211 Commit-Queue: Auto-Submit <[email protected]> Reviewed-by: Wyatt Hepler <[email protected]> Pigweed-Auto-Submit: Rob Mohr <[email protected]>
- Loading branch information
Showing
8 changed files
with
446 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
#!/usr/bin/env python3 | ||
# Copyright 2022 The Pigweed Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
# use this file except in compliance with the License. You may obtain a copy of | ||
# the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
# License for the specific language governing permissions and limitations under | ||
# the License. | ||
"""Tests for keep_sorted.""" | ||
|
||
from pathlib import Path | ||
import tempfile | ||
import unittest | ||
from unittest.mock import MagicMock | ||
|
||
from pw_presubmit import keep_sorted | ||
|
||
# Only include these literals here so keep_sorted doesn't try to reorder later | ||
# test lines. | ||
START = keep_sorted.START | ||
END = keep_sorted.END | ||
|
||
# pylint: disable=attribute-defined-outside-init | ||
|
||
|
||
class TestKeepSorted(unittest.TestCase): | ||
"""Test KeepSorted class""" | ||
def _run(self, contents: str) -> None: | ||
self.ctx = MagicMock() | ||
self.ctx.fail = MagicMock() | ||
|
||
with tempfile.TemporaryDirectory() as tempdir: | ||
path = Path(tempdir) / 'foo' | ||
|
||
with path.open('w') as outs: | ||
outs.write(contents) | ||
|
||
# pylint: disable=protected-access | ||
self.sorter = keep_sorted._FileSorter(self.ctx, path) | ||
|
||
# pylint: enable=protected-access | ||
|
||
self.sorter.sort() | ||
|
||
# Truncate the file so it's obvious whether write() changed | ||
# anything. | ||
with path.open('w') as outs: | ||
outs.write('') | ||
|
||
self.sorter.write(path) | ||
with path.open() as ins: | ||
self.contents = ins.read() | ||
|
||
def test_missing_end(self) -> None: | ||
with self.assertRaises(keep_sorted.KeepSortedParsingError): | ||
self._run(f'{START}\n') | ||
|
||
def test_missing_start(self) -> None: | ||
with self.assertRaises(keep_sorted.KeepSortedParsingError): | ||
self._run(f'{END}: end\n') | ||
|
||
def test_repeated_start(self) -> None: | ||
with self.assertRaises(keep_sorted.KeepSortedParsingError): | ||
self._run(f'{START}\n{START}\n') | ||
|
||
def test_already_sorted(self) -> None: | ||
self._run(f'{START}\n1\n2\n3\n4\n{END}\n') | ||
self.ctx.fail.assert_not_called() | ||
self.assertEqual(self.contents, '') | ||
|
||
def test_not_sorted(self) -> None: | ||
self._run(f'{START}\n4\n3\n2\n1\n{END}\n') | ||
self.ctx.fail.assert_called() | ||
self.assertEqual(self.contents, f'{START}\n1\n2\n3\n4\n{END}\n') | ||
|
||
def test_prefix_sorted(self) -> None: | ||
self._run(f'foo\nbar\n{START}\n1\n2\n{END}\n') | ||
self.ctx.fail.assert_not_called() | ||
self.assertEqual(self.contents, '') | ||
|
||
def test_prefix_not_sorted(self) -> None: | ||
self._run(f'foo\nbar\n{START}\n2\n1\n{END}\n') | ||
self.ctx.fail.assert_called() | ||
self.assertEqual(self.contents, f'foo\nbar\n{START}\n1\n2\n{END}\n') | ||
|
||
def test_suffix_sorted(self) -> None: | ||
self._run(f'{START}\n1\n2\n{END}\nfoo\nbar\n') | ||
self.ctx.fail.assert_not_called() | ||
self.assertEqual(self.contents, '') | ||
|
||
def test_suffix_not_sorted(self) -> None: | ||
self._run(f'{START}\n2\n1\n{END}\nfoo\nbar\n') | ||
self.ctx.fail.assert_called() | ||
self.assertEqual(self.contents, f'{START}\n1\n2\n{END}\nfoo\nbar\n') | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
Oops, something went wrong.