Commit 4fdc5f31 authored by Chris Mumford's avatar Chris Mumford Committed by Commit Bot

Unit tests for sqlite_cherry_picker.py.

These tests required some refactoring to sqlite_cherry_picker.py
to support testing.

Bug: 945204
Change-Id: I66eb10ca94a736a58ca75475d2fee3ba65f62dac
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2131249
Commit-Queue: Chris Mumford <cmumford@google.com>
Reviewed-by: default avatarDarwin Huang <huangdarwin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#758885}
parent 296f6fba
...@@ -8,11 +8,17 @@ Runs Python unit tests in /third_party/sqlite/scripts on upload. ...@@ -8,11 +8,17 @@ Runs Python unit tests in /third_party/sqlite/scripts on upload.
def CheckChangeOnUpload(input_api, output_api): def CheckChangeOnUpload(input_api, output_api):
results = [] results = []
this_dir = input_api.PresubmitLocalPath()
results += input_api.RunTests( results += input_api.RunTests(
input_api.canned_checks.GetUnitTests(input_api, output_api, [ input_api.canned_checks.GetUnitTestsInDirectory(
'scripts/extract_sqlite_api_unittest.py' input_api,
], env=None, run_on_python2=False, run_on_python3=True)) output_api,
input_api.os_path.join(this_dir, 'scripts'),
whitelist=['.*unittest.py$'],
env=None,
run_on_python2=False,
run_on_python3=True))
return results return results
[style]
based_on_style = pep8
# TODO(cmumford): Format older files in a future CL.
extract_sqlite_api.py
extract_sqlite_api_unittest.py
#!/usr/bin/env python3
# Copyright 2020 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Tests for sqlite_cherry_picker.py.
These tests should be getting picked up by the PRESUBMIT.py in the parent
directory.
"""
from pathlib import Path
import io
import os
import shutil
import tempfile
import unittest
import sqlite_cherry_picker
# pylint: disable=W0212,C0103,C0115,C0116
class CherryPickerUnittest(unittest.TestCase):
def setUp(self):
self.test_root = tempfile.mkdtemp()
def tearDown(self):
if self.test_root:
shutil.rmtree(self.test_root)
def testManifestEntryConstructor(self):
with self.assertRaises(sqlite_cherry_picker.IncorrectType):
entry = sqlite_cherry_picker.ManifestEntry('DD', [
'vsixtest/vsixtest.tcl',
'6a9a6ab600c25a91a7acc6293828957a386a8a93'
])
del entry # unused because exception will be raised.
def testManifestEntryHashType(self):
entry = sqlite_cherry_picker.ManifestEntry('F', [
'vsixtest/vsixtest.tcl', '6a9a6ab600c25a91a7acc6293828957a386a8a93'
])
self.assertEqual(entry.get_hash_type(), 'sha1')
entry = sqlite_cherry_picker.ManifestEntry('F', [
'tool/warnings-clang.sh',
'bbf6a1e685e534c92ec2bfba5b1745f34fb6f0bc2a362850723a9ee87c1b31a7'
])
self.assertEqual(entry.get_hash_type(), 'sha3')
# test a bad hash which is too short and cannot be identified as either
# a sha1 or sha3 hash.
with self.assertRaises(sqlite_cherry_picker.UnknownHash):
entry = sqlite_cherry_picker.ManifestEntry(
'F', ['file/path.sh', '12345678'])
entry.get_hash_type()
def testManifestEntryHashCalc(self):
data = 'abcdefghijklmnopqrstuvwxyDEFGHIJKLMUVWXYZ0123456789'.encode(
'utf-8')
self.assertEqual(
sqlite_cherry_picker.ManifestEntry.calc_hash(data, 'sha1'),
'e117bfe4bcb1429cf8a0f72f8f4ea322a9a500eb')
self.assertEqual(
sqlite_cherry_picker.ManifestEntry.calc_hash(data, 'sha3'),
'2cb59ee01402c45e7c56008b7ca33d006dbd980a5e222fac3a584f5639a450d7')
def testManifestFindEntry(self):
manifest = sqlite_cherry_picker.Manifest()
self.assertIsNone(manifest.find_file_entry('nonexistent'))
manifest.entries = [
sqlite_cherry_picker.ManifestEntry('F', [
'tool/warnings-clang.sh',
'bbf6a1e685e534c92ec2bfba5b1745f34fb6f0bc2a362850723a9ee87c1b31a7'
]),
sqlite_cherry_picker.ManifestEntry('T',
['+bgcolor', '*', '#d0c0ff'])
]
entry = manifest.find_file_entry('tool/warnings-clang.sh')
self.assertIsNotNone(entry)
self.assertEqual(entry.items[0], 'tool/warnings-clang.sh')
# Should only find files.
self.assertIsNone(manifest.find_file_entry('+bgcolor'))
def testManifestDeserialize(self):
manfest_path = os.path.join(
os.path.dirname(os.path.realpath(__file__)), 'test_data',
'test_manifest')
manifest_data = Path(manfest_path).read_text()
input_stream = io.StringIO(manifest_data)
manifest = sqlite_cherry_picker.ManifestSerializer.read_stream(
input_stream)
self.assertEqual(len(manifest.entries), 9)
output_stream = io.StringIO()
sqlite_cherry_picker.ManifestSerializer.write_stream(
manifest, output_stream)
self.assertEqual(output_stream.getvalue(), manifest_data)
def testGitHash(self):
valid_git_commit_id = '61c3ca1b1c77bbcaa35f9326decf3658bdb5626a'
self.assertTrue(
sqlite_cherry_picker.CherryPicker._is_git_commit_id(
valid_git_commit_id))
invalid_git_commit_id = 'f3658bdb5626a'
self.assertFalse(
sqlite_cherry_picker.CherryPicker._is_git_commit_id(
invalid_git_commit_id))
def testIsBinaryFile(self):
self.assertTrue(
sqlite_cherry_picker.CherryPicker._is_binary_file(
'path/test.data'))
self.assertTrue(
sqlite_cherry_picker.CherryPicker._is_binary_file('path/test.db'))
self.assertTrue(
sqlite_cherry_picker.CherryPicker._is_binary_file('path/test.ico'))
self.assertTrue(
sqlite_cherry_picker.CherryPicker._is_binary_file('path/test.jpg'))
self.assertTrue(
sqlite_cherry_picker.CherryPicker._is_binary_file('path/test.png'))
self.assertFalse(
sqlite_cherry_picker.CherryPicker._is_binary_file('path/test.c'))
self.assertFalse(
sqlite_cherry_picker.CherryPicker._is_binary_file('path/test.h'))
if __name__ == '__main__':
unittest.main()
C Version\s3.31.1
D 2020-01-27T19:55:54.490
F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
R bf075f6bcc1758c5c1ecd13052997456
T +bgcolor * #d0c0ff
T +sym-release *
T +sym-version-3.31.1 *
U drh
Z 7c50801eed3eaef969e028ef5a0a641a
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment