Initial commit

This commit is contained in:
alexpete
2021-03-05 11:26:34 -08:00
commit a10351f38d
27091 changed files with 5521199 additions and 0 deletions
@@ -0,0 +1,10 @@
#
# All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
# its licensors.
#
# For complete copyright and license terms please see the LICENSE at the root of this
# distribution (the "License"). All use of this software is governed by the License,
# or, if provided, by the license below or the license accompanying this file. Do not
# remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#
@@ -0,0 +1,10 @@
#
# All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
# its licensors.
#
# For complete copyright and license terms please see the LICENSE at the root of this
# distribution (the "License"). All use of this software is governed by the License,
# or, if provided, by the license below or the license accompanying this file. Do not
# remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#
@@ -0,0 +1,38 @@
#
# All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
# its licensors.
#
# For complete copyright and license terms please see the LICENSE at the root of this
# distribution (the "License"). All use of this software is governed by the License,
# or, if provided, by the license below or the license accompanying this file. Do not
# remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#
from typing import Dict, List
from commit_validation.commit_validation import Commit
class MockCommit(Commit):
def __init__(self, files=None, removed_files=None, file_diffs=None, description: str = '', author: str = ''):
self.files = [] if files is None else files
self.removed_files = [] if removed_files is None else removed_files
self.file_diffs = {} if file_diffs is None else file_diffs
self.description = description
self.author = author
def get_files(self) -> List[str]:
return self.files
def get_removed_files(self) -> List[str]:
return self.removed_files
def get_file_diff(self, file) -> Dict[str, str]:
return self.file_diffs[file]
def get_description(self) -> str:
return self.description
def get_author(self) -> str:
return self.author
@@ -0,0 +1,40 @@
#
# All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
# its licensors.
#
# For complete copyright and license terms please see the LICENSE at the root of this
# distribution (the "License"). All use of this software is governed by the License,
# or, if provided, by the license below or the license accompanying this file. Do not
# remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#
import unittest
from unittest.mock import patch, mock_open
import commit_validation.pal_allowedlist as pal_allowedlist
class PALAllowedTests(unittest.TestCase):
@patch('builtins.open', mock_open(read_data='*/some/*\n'
'*/set/*\n'
'*/of/*\n'
'*/patterns/*'))
def setUp(self):
self.allowedlist = pal_allowedlist.load()
def test_load_allowedlistFile_patternsLoaded(self):
self.assertEqual(self.allowedlist.patterns[0], '*/some/*')
self.assertEqual(self.allowedlist.patterns[1], '*/set/*')
self.assertEqual(self.allowedlist.patterns[2], '*/of/*')
self.assertEqual(self.allowedlist.patterns[3], '*/patterns/*')
def test_isMatch_pathMatchesPattern_returnsTrue(self):
self.assertTrue(self.allowedlist.is_match('/path/to/some/file.cpp'))
def test_isMatch_pathDoesNotMatchPattern_returnsFalse(self):
self.assertFalse(self.allowedlist.is_match('/path/to/another/file.cpp'))
if __name__ == '__main__':
unittest.main()
@@ -0,0 +1,12 @@
"""
All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
its licensors.
For complete copyright and license terms please see the LICENSE at the root of this
distribution (the "License"). All use of this software is governed by the License,
or, if provided, by the license below or the license accompanying this file. Do not
remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
"""
@@ -0,0 +1,72 @@
#
# All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
# its licensors.
#
# For complete copyright and license terms please see the LICENSE at the root of this
# distribution (the "License"). All use of this software is governed by the License,
# or, if provided, by the license below or the license accompanying this file. Do not
# remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#
import unittest
from unittest.mock import patch, mock_open
from commit_validation import pal_allowedlist
from commit_validation.tests.mocks.mock_commit import MockCommit
from commit_validation.validators.az_platform_validator import AzPlatformValidator
class AzPlatformValidatorTests(unittest.TestCase):
def test_fileWithNoAzPlatformMacro_passes(self):
commit = MockCommit(
files=['someCppFile.cpp'],
file_diffs = {
'someCppFile.cpp' : '+This file does not contain\n+AZ_PLATFORM_MACRO\n'
}
)
error_list = []
self.assertTrue(AzPlatformValidator().run(commit, error_list))
self.assertEqual(len(error_list), 0, f"Unexpected errors: {error_list}")
# make sure it only examines parts that have actually been changed by this commit
# note that the line does not start with +
def test_fileWithMacroInNonChangedSection_passes(self):
commit = MockCommit(
files=['someCppFile.cpp', 'otherfile.cpp'],
file_diffs = { # one file has no diff marker, one file indicates its removed.
'someCppFile.cpp' : 'Stuff\n#if defined(AZ_PLATFORM_MACRO\n)',
'otherfile.cpp' : 'Stuff\n-#if defined(AZ_PLATFORM_MACRO\n)'
}
)
error_list = []
self.assertTrue(AzPlatformValidator().run(commit, error_list))
self.assertEqual(len(error_list), 0, f"Unexpected errors: {error_list}")
def test_fileWithAzPlatformMacro_fails(self):
commit = MockCommit(
files=['someCppFile.cpp', 'otherfile.cpp'],
file_diffs = {
'someCppFile.cpp' : '+This file does contain\n'
'+#if defined(AZ_PLATFORM_MACRO)\n',
'otherfile.cpp' : 'This File has a different form\n'
'+# if defined(AZ_PLATFORM_MACRO)\n'
})
error_list = []
self.assertFalse(AzPlatformValidator().run(commit, error_list))
self.assertNotEqual(len(error_list), 0, f"Errors were expected but none were returned.")
def test_fileExtensionIgnored_passes(self):
commit = MockCommit(files=['someCppFile.waf_files'])
error_list = []
self.assertTrue(AzPlatformValidator().run(commit, error_list))
self.assertEqual(len(error_list), 0, f"Unexpected errors: {error_list}")
@patch('commit_validation.pal_allowedlist.load', return_value=pal_allowedlist.PALAllowedlist(['*/some/path/*']))
def test_fileAllowedlisted_passes(self, mocked_load):
commit = MockCommit(files=['/path/to/some/path/someCppFile.cpp'])
error_list = []
self.assertTrue(AzPlatformValidator().run(commit, error_list))
self.assertEqual(len(error_list), 0, f"Unexpected errors: {error_list}")
if __name__ == '__main__':
unittest.main()
@@ -0,0 +1,95 @@
#
# All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
# its licensors.
#
# For complete copyright and license terms please see the LICENSE at the root of this
# distribution (the "License"). All use of this software is governed by the License,
# or, if provided, by the license below or the license accompanying this file. Do not
# remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#
import unittest
from unittest.mock import patch, mock_open
from commit_validation import pal_allowedlist
from commit_validation.tests.mocks.mock_commit import MockCommit
from commit_validation.validators.az_trait_validator import AzTraitValidator
import pytest
class Test_AzTraitValidatorTests():
def test_fileDoesntCheckAzTraitIsDefined_passes(self):
commit = MockCommit(
files=['someCppFile.cpp'],
file_diffs={ 'someCppFile.cpp' : ''}
)
error_list = []
assert AzTraitValidator().run(commit, error_list)
assert len(error_list) == 0, f"Unexpected errors: {error_list}"
@pytest.mark.parametrize(
'file_diffs,expect_success', [
pytest.param('+This file does contain\n'
'+a trait existence check\n'
'+#ifdef AZ_TRAIT_USED_INCORRECTLY\n',
False,
id="AZ_TRAIT_inside_ifdef_fails" ), # gives the test a friendly name!
pytest.param('+This file does contain\n'
'+a trait existence check\n'
'+#if defined(AZ_TRAIT_USED_INCORRECTLY)\n',
False,
id="AZ_TRAIT_inside_if_defined_fails" ),
pytest.param('+This file does contain\n'
'+a trait existence check\n'
'+#ifndef AZ_TRAIT_USED_INCORRECTLY\n',
False,
id="AZ_TRAIT_inside_ifndef_fails" ),
pytest.param('+This file contains a diff which REMOVES an incorrect usage\n'
'-#ifndef AZ_TRAIT_USED_INCORRECTLY\n',
True,
id="AZ_TRAIT_removed_in_diff_passes" ),
pytest.param('+This file contains a diff which has an old already okayed usage\n'
'+which is not actually part of the diff.\n'
'#ifndef AZ_TRAIT_USED_INCORRECTLY\n',
True,
id="AZ_TRAIT_in_unmodified_section_passes"),
pytest.param('+This file contains the correct usage\n'
'+#if AZ_TRAIT_USED_CORRECTLY\n',
True,
id="AZ_TRAIT_correct_usage_passes"),
])
def test_fileChecksAzTraitIsDefined(self, file_diffs, expect_success):
commit = MockCommit(
files=['someCppFile.cpp'],
file_diffs={ 'someCppFile.cpp' : file_diffs })
error_list = []
if expect_success:
assert AzTraitValidator().run(commit, error_list)
assert len(error_list) == 0, f"Unexpected errors: {error_list}"
else:
assert not AzTraitValidator().run(commit, error_list)
assert len(error_list) != 0, f"Errors were expected but none were returned."
def test_fileExtensionIgnored_passes(self):
commit = MockCommit(files=['someCppFile.waf_files'])
error_list = []
assert AzTraitValidator().run(commit, error_list)
assert len(error_list) == 0, f"Unexpected errors: {error_list}"
@patch('commit_validation.pal_allowedlist.load', return_value=pal_allowedlist.PALAllowedlist(['*/some/path/*']))
def test_fileAllowedlisted_passes(self, mocked_load):
commit = MockCommit(files=['/path/to/some/path/someCppFile.cpp'])
error_list = []
assert AzTraitValidator().run(commit, error_list)
assert len(error_list) == 0, f"Unexpected errors: {error_list}"
if __name__ == '__main__':
unittest.main()
@@ -0,0 +1,75 @@
#
# All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
# its licensors.
#
# For complete copyright and license terms please see the LICENSE at the root of this
# distribution (the "License"). All use of this software is governed by the License,
# or, if provided, by the license below or the license accompanying this file. Do not
# remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#
import unittest
from unittest.mock import patch, mock_open
from commit_validation.tests.mocks.mock_commit import MockCommit
from commit_validation.validators.copyright_header_validator import CopyrightHeaderValidator
class CopyrightHeaderValidatorTests(unittest.TestCase):
@patch('builtins.open', mock_open(read_data='This file does contain\n'
'Copyright (c) Amazon.com, so it should pass\n'))
def test_fileWithCopyrightHeader_passes(self):
commit = MockCommit(files=['/someCppFile.cpp'])
files = [
'This file does contain\n'
'Copyright (c) Amazon.com, so it should pass\n',
'This file does contain\n'
'Copyright(c) Amazon.com, so it should pass\n'
'and there\'s no space between "Copyright" and "(c)"',
'This file has a upper-case C between the parenthesis\n'
'// Copyright (C) Amazon.com, Inc. or its affiliates.',
]
for file in files:
with patch('builtins.open', mock_open(read_data=file)):
error_list = []
self.assertTrue(CopyrightHeaderValidator().run(commit, error_list))
self.assertEqual(len(error_list), 0, f"Unexpected errors: {error_list}")
def test_fileWithCopyrightHeaderExternalDir_passes(self):
commit = MockCommit(files=['/External/someCppFile.cpp'])
error_list = []
self.assertTrue(CopyrightHeaderValidator().run(commit, error_list))
self.assertEqual(len(error_list), 0, f"Unexpected errors: {error_list}")
@patch('builtins.open', mock_open(read_data='This file does not contain\n'
'the copyright header\n'))
def test_fileWithNoCopyrightHeader_fails(self):
commit = MockCommit(files=['/someCppFile.cpp'])
error_list = []
self.assertFalse(CopyrightHeaderValidator().run(commit, error_list))
self.assertNotEqual(len(error_list), 0, f"Errors were expected but none were returned.")
def test_fileExtensionIgnored_passes(self):
commit = MockCommit(files=['/someCppFile.waf_files'])
error_list = []
self.assertTrue(CopyrightHeaderValidator().run(commit, error_list))
self.assertEqual(len(error_list), 0, f"Unexpected errors: {error_list}")
def test_fileWith3rdPartyPath_passes(self):
commit = MockCommit(files=['/3rdParty/someCppFile.cpp'])
error_list = []
self.assertTrue(CopyrightHeaderValidator().run(commit, error_list))
self.assertEqual(len(error_list), 0, f"Unexpected errors: {error_list}")
def test_fileWithExternal_passes(self):
commit = MockCommit(files=['/External/someCppFile.cpp'])
error_list = []
self.assertTrue(CopyrightHeaderValidator().run(commit, error_list))
self.assertEqual(len(error_list), 0, f"Unexpected errors: {error_list}")
if __name__ == '__main__':
unittest.main()
@@ -0,0 +1,102 @@
#
# All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
# its licensors.
#
# For complete copyright and license terms please see the LICENSE at the root of this
# distribution (the "License"). All use of this software is governed by the License,
# or, if provided, by the license below or the license accompanying this file. Do not
# remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#
import unittest
from commit_validation.tests.mocks.mock_commit import MockCommit
from commit_validation.validators.diff_whitespace_validator import WhitespaceValidator
class WhitespaceValidatorTests(unittest.TestCase):
def test_DiffWhitespace_WhitespaceLinesContiguousWithCodeChange_pass(self):
commit = MockCommit(
files = ['/someCppFile.cpp'],
file_diffs={'/someCppFile.cpp':
'This file\n'
'-was edited'
'+ and has a diff including plus and minus symbols\n'
'+ \n'
'and should pass\n'})
error_list = []
self.assertTrue(WhitespaceValidator().run(commit, error_list))
self.assertEqual(len(error_list), 0, f"Unexpected errors: {error_list}")
def test_DiffWhitespace_WhitespaceLinesSeparateAtEOF_fail(self):
commit = MockCommit(
files = ['/someCppFile.cpp'],
file_diffs={'/someCppFile.cpp':
'This file\n'
'-was edited'
'+ and has an implementation diff\n'
'but should not pass because of a whitespace only change:\n'
'- \n'})
error_list = []
self.assertFalse(WhitespaceValidator().run(commit, error_list))
self.assertNotEqual(len(error_list), 0, f"Errors were expected but none were returned.")
def test_DiffWhitespace_WhitespaceLinesSeparateAtEOF_fail_More(self):
commit = MockCommit(
files = ['/someCppFile.cpp'],
file_diffs={'/someCppFile.cpp':
'+ \n'
'This file\n'
'-was edited'
'+ and has an implementation diff\n'
'but should not pass because of a whitespace only change\n'
'on the first line\n'})
error_list = []
self.assertFalse(WhitespaceValidator().run(commit, error_list))
self.assertNotEqual(len(error_list), 0, f"Errors were expected but none were returned.")
def test_DiffWhitespace_WhitespaceLinesSeparateFromCode_fail(self):
commit = MockCommit(
files = ['/someCppFile.cpp'],
file_diffs={'/someCppFile.cpp':
'This file\n'
'should not pass because of a whitespace only change:\n'
'- \n'
'since was edited'
'+ and has an implementation diff\n'
'- later on'})
error_list = []
self.assertFalse(WhitespaceValidator().run(commit, error_list))
self.assertNotEqual(len(error_list), 0, f"Errors were expected but none were returned.")
def test_DiffWhitespace_WhitespaceOnlyChange_pass(self):
commit = MockCommit(
files = ['/someCppFile.cpp'],
file_diffs={'/someCppFile.cpp':
'This file has whitespace only changes\n'
'+ \n'
'+ \n'
'and should pass\n'
'- \n'})
error_list = []
self.assertTrue(WhitespaceValidator().run(commit, error_list))
self.assertEqual(len(error_list), 0, f"Unexpected errors: {error_list}")
def test_DiffWhitespace_CodeChangeOnly_pass(self):
commit = MockCommit(
files = ['/someCppFile.cpp'],
file_diffs={'/someCppFile.cpp':
'This file has'
'+ no whitespace only lines changed\n'
' \n'
'-and should pass\n'
' \n'})
error_list = []
self.assertTrue(WhitespaceValidator().run(commit, error_list))
self.assertEqual(len(error_list), 0, f"Unexpected errors: {error_list}")
if __name__ == '__main__':
unittest.main()
@@ -0,0 +1,96 @@
#
# All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
# its licensors.
#
# For complete copyright and license terms please see the LICENSE at the root of this
# distribution (the "License"). All use of this software is governed by the License,
# or, if provided, by the license below or the license accompanying this file. Do not
# remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#
import unittest
from unittest.mock import patch, mock_open
from commit_validation.tests.mocks.mock_commit import MockCommit
from commit_validation.validators.generated_files_validator import GeneratedFilesValidator
@patch('builtins.open', mock_open(read_data='unused\n'))
class GeneratedFilesValidatorTests(unittest.TestCase):
def test_IsGenerated_NormalFile_pass(self):
commit = MockCommit(files=['/someFile.cpp'])
error_list = []
self.assertTrue(GeneratedFilesValidator().run(commit, error_list))
self.assertEqual(len(error_list), 0, f"Unexpected errors: {error_list}")
def test_IsGenerated_DotGeneratedFile_fail(self):
commit = MockCommit(files=['/someFile.generated.cpp'])
error_list = []
self.assertFalse(GeneratedFilesValidator().run(commit, error_list))
self.assertNotEqual(len(error_list), 0, f"Errors were expected but none were returned.")
def test_IsGenerated_QtMOC_fail(self):
commit = MockCommit(files=['/moc_someFile.cpp'])
error_list = []
self.assertFalse(GeneratedFilesValidator().run(commit, error_list))
self.assertNotEqual(len(error_list), 0, f"Errors were expected but none were returned.")
def test_IsGenerated_QtQRC_fail(self):
commit = MockCommit(files=['/qrc_someFile.cpp'])
error_list = []
self.assertFalse(GeneratedFilesValidator().run(commit, error_list))
self.assertNotEqual(len(error_list), 0, f"Errors were expected but none were returned.")
def test_IsGenerated_QtUiDotH_fail(self):
commit = MockCommit(files=['/ui_someFile.h'])
error_list = []
self.assertFalse(GeneratedFilesValidator().run(commit, error_list))
self.assertNotEqual(len(error_list), 0, f"Errors were expected but none were returned.")
def test_IsGenerated_CMakeCacheFile_fail(self):
commit = MockCommit(files=['/CMakeCache.txt'])
error_list = []
self.assertFalse(GeneratedFilesValidator().run(commit, error_list))
self.assertNotEqual(len(error_list), 0, f"Errors were expected but none were returned.")
def test_IsGenerated_CMakeCacheExtension_fail(self):
commit = MockCommit(files=['/AzCore.Benchmarks.rule'])
error_list = []
self.assertFalse(GeneratedFilesValidator().run(commit, error_list))
self.assertNotEqual(len(error_list), 0, f"Errors were expected but none were returned.")
def test_IsGenerated_Tempfile_fail(self):
commit = MockCommit(files=['/someFile.tmp'])
error_list = []
self.assertFalse(GeneratedFilesValidator().run(commit, error_list))
self.assertNotEqual(len(error_list), 0, f"Errors were expected but none were returned.")
def test_IsGenerated_UnixObject_fail(self):
commit = MockCommit(files=['/someFile.o'])
error_list = []
self.assertFalse(GeneratedFilesValidator().run(commit, error_list))
self.assertNotEqual(len(error_list), 0, f"Errors were expected but none were returned.")
def test_IsGenerated_WindowsObject_fail(self):
commit = MockCommit(files=['/someFile.obj'])
error_list = []
self.assertFalse(GeneratedFilesValidator().run(commit, error_list))
self.assertNotEqual(len(error_list), 0, f"Errors were expected but none were returned.")
def test_IsGenerated_MicrosoftSolution_fail(self):
commit = MockCommit(files=['/someFile.sln'])
error_list = []
self.assertFalse(GeneratedFilesValidator().run(commit, error_list))
self.assertNotEqual(len(error_list), 0, f"Errors were expected but none were returned.")
def test_IsGenerated_Logfile_fail(self):
commit = MockCommit(files=['/someFile.log'])
error_list = []
self.assertFalse(GeneratedFilesValidator().run(commit, error_list))
self.assertNotEqual(len(error_list), 0, f"Errors were expected but none were returned.")
if __name__ == '__main__':
unittest.main()
@@ -0,0 +1,99 @@
#
# All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
# its licensors.
#
# For complete copyright and license terms please see the LICENSE at the root of this
# distribution (the "License"). All use of this software is governed by the License,
# or, if provided, by the license below or the license accompanying this file. Do not
# remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#
import unittest
from unittest.mock import patch, mock_open
from commit_validation.tests.mocks.mock_commit import MockCommit
from commit_validation.validators.git_conflict_validator import GitConflictValidator
class NewlineValidatorTests(unittest.TestCase):
@patch('builtins.open', mock_open(read_data='This file is completely normal\n'
'and should pass\n'))
def test_HasConflictMarkers_NoMarkers_Pass(self):
commit = MockCommit(files=['/someFile.cpp'])
error_list = []
self.assertTrue(GitConflictValidator().run(commit, error_list))
self.assertEqual(len(error_list), 0, f"Unexpected errors: {error_list}")
@patch('builtins.open', mock_open(read_data='This file has a start marker\n'
'<<<<<<< ours\n'
'and should fail\n'))
def test_HasConflictMarkers_StartMarker_Fail(self):
commit = MockCommit(files=['/someFile.cpp'])
error_list = []
self.assertFalse(GitConflictValidator().run(commit, error_list))
self.assertNotEqual(len(error_list), 0, f"Errors were expected but none were returned.")
@patch('builtins.open', mock_open(read_data='This file has a diff3 marker from using --conflict=diff3\n'
'||||||| base\n'
'and should fail\n'))
def test_HasConflictMarkers_BaseMarker_Fail(self):
commit = MockCommit(files=['/someFile.cpp'])
error_list = []
self.assertFalse(GitConflictValidator().run(commit, error_list))
self.assertNotEqual(len(error_list), 0, f"Errors were expected but none were returned.")
@patch('builtins.open', mock_open(read_data='This file has a diff marker\n'
'=======\n'
'and should fail\n'))
def test_HasConflictMarkers_DiffMarker_Fail(self):
commit = MockCommit(files=['/someFile.cpp'])
error_list = []
self.assertFalse(GitConflictValidator().run(commit, error_list))
self.assertNotEqual(len(error_list), 0, f"Errors were expected but none were returned.")
@patch('builtins.open', mock_open(read_data='This file has and end marker\n'
'>>>>>>> theirs\n'
'and should fail\n'))
def test_HasConflictMarkers_EndMarker_Fail(self):
commit = MockCommit(files=['/someFile.cpp'])
error_list = []
self.assertFalse(GitConflictValidator().run(commit, error_list))
self.assertNotEqual(len(error_list), 0, f"Errors were expected but none were returned.")
@patch('builtins.open', mock_open(read_data='This file has a equals sign divider of length seven, but is indented\n'
' /*\n'
' =======\n'
' */'
'and should pass\n'))
def test_HasConflictMarkers_IndentedCommentDivider_Pass(self):
commit = MockCommit(files=['/someFile.cpp'])
error_list = []
self.assertTrue(GitConflictValidator().run(commit, error_list))
self.assertEqual(len(error_list), 0, f"Unexpected errors: {error_list}")
@patch('builtins.open', mock_open(read_data='This file has an unindented equals sign divider, of length eight\n'
'/*\n'
'========\n'
'*/'
'and should pass\n'))
def test_HasConflictMarkers_LongCommentDivider_Pass(self):
commit = MockCommit(files=['/someFile.cpp'])
error_list = []
self.assertTrue(GitConflictValidator().run(commit, error_list))
self.assertEqual(len(error_list), 0, f"Unexpected errors: {error_list}")
@patch('builtins.open', mock_open(read_data='This file has an unindented equals sign divider, of length six\n'
'/*\n'
'======\n'
'*/'
'and should pass\n'))
def test_HasConflictMarkers_ShortCommentDivider_Pass(self):
commit = MockCommit(files=['/someFile.cpp'])
error_list = []
self.assertTrue(GitConflictValidator().run(commit, error_list))
self.assertEqual(len(error_list), 0, f"Unexpected errors: {error_list}")
if __name__ == '__main__':
unittest.main()
@@ -0,0 +1,88 @@
#
# All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
# its licensors.
#
# For complete copyright and license terms please see the LICENSE at the root of this
# distribution (the "License"). All use of this software is governed by the License,
# or, if provided, by the license below or the license accompanying this file. Do not
# remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#
import unittest
from unittest.mock import patch, mock_open
from commit_validation.tests.mocks.mock_commit import MockCommit
from commit_validation.validators.newline_validator import NewlineValidator
class NewlineValidatorTests(unittest.TestCase):
@patch('builtins.open', mock_open(read_data='This file uses carriage returns with line feeds\r\n'
'and should fail since only line feeds are allowed\r\n'
'despite appropriately ending with an extra line\r\n'))
def test_LineEndings_CRLF_fail(self):
commit = MockCommit(files=['/someFile.cpp'])
error_list = []
self.assertFalse(NewlineValidator().run(commit, error_list))
self.assertNotEqual(len(error_list), 0, f"Errors were expected but none were returned.")
@patch('builtins.open', mock_open(read_data='This file uses line feeds\n'
'and ends with an extra line\n'))
def test_LineEndings_LF_pass(self):
commit = MockCommit(files=['/someFile.cpp'])
error_list = []
self.assertTrue(NewlineValidator().run(commit, error_list))
self.assertTrue(not error_list)
@patch('builtins.open', mock_open(read_data='This file is full of garbage\n'
'which would normally fail\r\n'
'though it should not due to its file extension\n\r'
'which should skip validation'))
def test_LineEndings_RandomFileExtension_skip(self):
commit = MockCommit(files=['/someFile.garbage'])
error_list = []
self.assertTrue(NewlineValidator().run(commit, error_list))
self.assertEqual(len(error_list), 0, f"Unexpected errors: {error_list}")
@patch('builtins.open', mock_open(read_data='This file mixes line feed endings\n'
'with carriage return and line feed endings\r\n'))
def test_LineEndings_Mixed_fail(self):
commit = MockCommit(files=['/someFile.cpp'])
error_list = []
self.assertFalse(NewlineValidator().run(commit, error_list))
self.assertNotEqual(len(error_list), 0, f"Errors were expected but none were returned.")
@patch('builtins.open', mock_open(read_data='This file ends\n'
'with no newline'))
def test_LineEndings_NoFinalNewline_fail(self):
commit = MockCommit(files=['/someFile.cpp'])
error_list = []
self.assertFalse(NewlineValidator().run(commit, error_list))
self.assertNotEqual(len(error_list), 0, f"Errors were expected but none were returned.")
@patch('builtins.open', mock_open(read_data='This file ends with CRLF newlines\r\n'
'but not at the end'))
def test_LineEndings_NoFinalCRLF_fail(self):
commit = MockCommit(files=['/someFile.cpp'])
error_list = []
self.assertFalse(NewlineValidator().run(commit, error_list))
self.assertNotEqual(len(error_list), 0, f"Errors were expected but none were returned.")
@patch('builtins.open', mock_open(read_data='This file ends with extra newlines\n'
'\n'))
def test_LineEndings_ExtraFinalNewline_fail(self):
commit = MockCommit(files=['/someFile.cpp'])
error_list = []
self.assertFalse(NewlineValidator().run(commit, error_list))
self.assertNotEqual(len(error_list), 0, f"Errors were expected but none were returned.")
@patch('builtins.open', mock_open(read_data='This file ends with CRLF extra newlines\r\n'
'\r\n'))
def test_LineEndings_ExtraFinalCRLF_fail(self):
commit = MockCommit(files=['/someFile.cpp'])
error_list = []
self.assertFalse(NewlineValidator().run(commit, error_list))
self.assertNotEqual(len(error_list), 0, f"Errors were expected but none were returned.")
if __name__ == '__main__':
unittest.main()
@@ -0,0 +1,77 @@
#
# All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
# its licensors.
#
# For complete copyright and license terms please see the LICENSE at the root of this
# distribution (the "License"). All use of this software is governed by the License,
# or, if provided, by the license below or the license accompanying this file. Do not
# remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#
import unittest
from unittest.mock import patch, mock_open
from commit_validation import pal_allowedlist
from commit_validation.tests.mocks.mock_commit import MockCommit
from commit_validation.validators.platform_macro_validator import PlatformMacroValidator
class PlatformMacroValidatorTests(unittest.TestCase):
def test_fileWithNoPlatformMacro_passes(self):
commit = MockCommit(
files=['someCppFile.cpp'],
file_diffs={ 'someCppFile.cpp' : '+This file does not contain\n'
'+a platform macro\n'
'+#define ACCEPTABLE_MACRO\n'
})
error_list = []
self.assertTrue(PlatformMacroValidator().run(commit, error_list))
self.assertEqual(len(error_list), 0, f"Unexpected errors: {error_list}")
def test_fileWithPlatformMacro_errors(self):
commit = MockCommit(
files=['someCppFile.cpp'],
file_diffs={ 'someCppFile.cpp' : '+This file does contain\n'
'+a platform macro\n'
'+#if defined(APPLE)\n'
})
error_list = []
self.assertFalse(PlatformMacroValidator().run(commit, error_list))
self.assertNotEqual(len(error_list), 0, f"Errors were expected but none were returned.")
def test_fileWithPlatformMacro_ButNotInChangedPart_passes(self):
commit = MockCommit(
files=['someCppFile.cpp'],
file_diffs={ 'someCppFile.cpp' : '+This file does contain\n'
'+a platform macro\n'
'-#if defined(APPLE)\n'
'But not in a part thats relevant to the diff'
'#if defined(APPLE)\n'
})
error_list = []
self.assertTrue(PlatformMacroValidator().run(commit, error_list))
self.assertEqual(len(error_list), 0, f"Unexpected errors: {error_list}")
def test_fileExtensionIgnored_passes(self):
commit = MockCommit(files=['someCppFile.waf_files'])
error_list = []
self.assertTrue(PlatformMacroValidator().run(commit, error_list))
self.assertEqual(len(error_list), 0, f"Unexpected errors: {error_list}")
def test_platformFolderIgnored_passes(self):
commit = MockCommit(files=['/path/to/Platform/SomePlatform/someCppFile.cpp'])
error_list = []
self.assertTrue(PlatformMacroValidator().run(commit, error_list))
self.assertEqual(len(error_list), 0, f"Unexpected errors: {error_list}")
@patch('commit_validation.pal_allowedlist.load', return_value=pal_allowedlist.PALAllowedlist(['*/some/path/*']))
def test_fileAllowedlisted_passes(self, mocked_load):
commit = MockCommit(files=['/path/to/some/path/someCppFile.cpp'])
error_list = []
self.assertTrue(PlatformMacroValidator().run(commit, error_list))
self.assertEqual(len(error_list), 0, f"Unexpected errors: {error_list}")
if __name__ == '__main__':
unittest.main()
@@ -0,0 +1,89 @@
#
# All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
# its licensors.
#
# For complete copyright and license terms please see the LICENSE at the root of this
# distribution (the "License"). All use of this software is governed by the License,
# or, if provided, by the license below or the license accompanying this file. Do not
# remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#
import unittest
from unittest.mock import patch, mock_open
from commit_validation.tests.mocks.mock_commit import MockCommit
from commit_validation.validators.pragma_optimize_validator import PragmaOptimizeValidator
class PragmaOptimizeValidatorTests(unittest.TestCase):
def test_fileWithNoPragmaOptimize_passes(self):
commit = MockCommit(
files=['/someCppFile.cpp'],
file_diffs={
'/someCppFile.cpp' : '+// This file does not contain\n'
'+// the p r a g m a o p t i m i z e'
})
error_list = []
self.assertTrue(PragmaOptimizeValidator().run(commit, error_list))
self.assertEqual(len(error_list), 0, f"Unexpected errors: {error_list}")
def test_fileWithNoPragma3rdPartyPath_passes(self):
commit = MockCommit(files=['/3rdParty/someCppFile.cpp'])
error_list = []
self.assertTrue(PragmaOptimizeValidator().run(commit, error_list))
self.assertEqual(len(error_list), 0, f"Unexpected errors: {error_list}")
def test_fileWithNoPragmaExternal_passes(self):
commit = MockCommit(files=['/External/someCppFile.cpp'])
error_list = []
self.assertTrue(PragmaOptimizeValidator().run(commit, error_list))
self.assertEqual(len(error_list), 0, f"Unexpected errors: {error_list}")
def test_fileWithPragmaOptimize_fails(self):
commit = MockCommit(
files=['/someCppFile.cpp'],
file_diffs={
'/someCppFile.cpp' : '+// This file contains\n'
'+// a line\n'
'+// with #pragma optimize("", off)'
})
error_list = []
self.assertFalse(PragmaOptimizeValidator().run(commit, error_list))
self.assertNotEqual(len(error_list), 0, f"Errors were expected but none were returned.")
def test_fileWithPragmaOptimize_in_unchanged_part_passes(self):
commit = MockCommit(
files=['/someCppFile.cpp'],
file_diffs={
'/someCppFile.cpp' : '+// This file contains\n'
'+// a line\n'
'-#pragma optimize("", off)\n'
'but its in a deleted line or a line that is not part of the diff\n'
'#pragma optimize("", off)\n'
})
error_list = []
self.assertTrue(PragmaOptimizeValidator().run(commit, error_list))
self.assertEqual(len(error_list), 0, f"Unexpected errors: {error_list}")
def test_fileExtensionIgnored_passes(self):
commit = MockCommit(files=['/someCppFile.waf_files'])
error_list = []
self.assertTrue(PragmaOptimizeValidator().run(commit, error_list))
self.assertEqual(len(error_list), 0, f"Unexpected errors: {error_list}")
def test_fileWithPragma3rdPartyPath_passes(self):
commit = MockCommit(files=['/3rdParty/someCppFile.cpp'])
error_list = []
self.assertTrue(PragmaOptimizeValidator().run(commit, error_list))
self.assertEqual(len(error_list), 0, f"Unexpected errors: {error_list}")
def test_fileWithPragmaExternal_passes(self):
commit = MockCommit(files=['/External/someCppFile.cpp'])
error_list = []
self.assertTrue(PragmaOptimizeValidator().run(commit, error_list))
self.assertEqual(len(error_list), 0, f"Unexpected errors: {error_list}")
if __name__ == '__main__':
unittest.main()
@@ -0,0 +1,78 @@
#
# All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
# its licensors.
#
# For complete copyright and license terms please see the LICENSE at the root of this
# distribution (the "License"). All use of this software is governed by the License,
# or, if provided, by the license below or the license accompanying this file. Do not
# remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#
import unittest
from unittest.mock import patch, mock_open
from commit_validation.tests.mocks.mock_commit import MockCommit
from commit_validation.validators.tabs_validator import TabsValidator
class TabsValidatorTests(unittest.TestCase):
def test_fileWithNoTabs_passes(self):
commit = MockCommit(
files=['/someCppFile.cpp'],
file_diffs={ '/someCppFile.cpp' : '+This file contains\n'
'+ no tabs\n'
'+ but does have spaces\n'})
error_list = []
self.assertTrue(TabsValidator().run(commit, error_list))
self.assertEqual(len(error_list), 0, f"Unexpected errors: {error_list}")
def test_fileWithTabs_fails(self):
commit = MockCommit(
files=['/someCppFile.cpp'],
file_diffs={ '/someCppFile.cpp' : '+This file contains\n'
'+\tTHE DREADED TAB CHARACTER\n'})
error_list = []
self.assertFalse(TabsValidator().run(commit, error_list))
self.assertNotEqual(len(error_list), 0, f"Errors were expected but none were returned.")
def test_fileWithTabs_butNotInDiffArea_passes(self):
commit = MockCommit(
files=['/someCppFile.cpp'],
file_diffs={ '/someCppFile.cpp' : '+This file contains\n'
'\tTHE DREADED TAB CHARACTER\n'
'But since its not in a diff line\n'
'It does not matter not even if in \n'
'-\tA REMOVED LINE with the tab character\n'})
error_list = []
self.assertTrue(TabsValidator().run(commit, error_list))
self.assertEqual(len(error_list), 0, f"Unexpected errors: {error_list}")
def test_fileExtensionIgnored_passes(self):
commit = MockCommit(files=['/someCppFile.waf_files'])
error_list = []
self.assertTrue(TabsValidator().run(commit, error_list))
self.assertEqual(len(error_list), 0, f"Unexpected errors: {error_list}")
def test_fileWithNoTabsbutAnEmoji_passes(self):
commit = MockCommit(
files=['/someCppFile_trap.cpp'],
file_diffs={ '/someCppFile_trap.cpp' : '+This file contains\n'
'+ no tabs\n'
'+ but has an emoji \U0001F4A9\n'})
error_list = []
self.assertTrue(TabsValidator().run(commit, error_list))
self.assertEqual(len(error_list), 0, f"Unexpected errors: {error_list}")
def test_fileWithTabsAndEmoji_fails(self):
commit = MockCommit(
files=['/another_trap.cpp'],
file_diffs={ '/another_trap.cpp' : '+This file contains an emoji \U0001F4A9 \n'
'+\tbut also a tab!\n'})
error_list = []
self.assertFalse(TabsValidator().run(commit, error_list))
self.assertNotEqual(len(error_list), 0, f"Errors were expected but none were returned.")
if __name__ == '__main__':
unittest.main()