1#!/usr/bin/env python3 2# Copyright 2023 The Chromium Authors 3# Use of this source code is governed by a BSD-style license that can be 4# found in the LICENSE file. 5 6import datetime 7import os.path 8import sys 9import tempfile 10import unittest 11 12sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..')) 13 14from PRESUBMIT_test_mocks import MockInputApi, MockOutputApi 15from build.ios import presubmit_support 16 17_TEMP_FILELIST_CONTENTS = """# Copyright %d The Chromium Authors 18# Use of this source code is governed by a BSD-style license that can be 19# found in the LICENSE file. 20# NOTE: this file is generated by build/ios/update_bundle_filelist.py 21# If it requires updating, you should get a presubmit error with 22# instructions on how to regenerate. Otherwise, do not edit. 23""" % (datetime.datetime.now().year) 24 25_TEMP_GLOBLIST_CONTENTS = """** 26-*.globlist 27-*.filelist 28""" 29 30 31class BundleDataPresubmit(unittest.TestCase): 32 def setUp(self): 33 self.mock_input_api = MockInputApi() 34 self.mock_input_api.change.RepositoryRoot = lambda: os.path.join( 35 os.path.dirname(__file__), '..', '..') 36 self.mock_input_api.PresubmitLocalPath = lambda: os.path.dirname(__file__) 37 self.mock_output_api = MockOutputApi() 38 39 def testBasic(self): 40 """ 41 Checks that a glob can be expanded to build a file list and if it 42 matches the existing file list, we should see no error. 43 """ 44 results = presubmit_support.CheckBundleData(self.mock_input_api, 45 self.mock_output_api, 46 'test_data/basic', '.') 47 self.assertEqual([], results) 48 49 def testExclusion(self): 50 """ 51 Check that globs can be used to exclude files from file lists. 52 """ 53 results = presubmit_support.CheckBundleData(self.mock_input_api, 54 self.mock_output_api, 55 'test_data/exclusions', '.') 56 self.assertEqual([], results) 57 58 def testDifferentLocalPath(self): 59 """ 60 Checks the case where the presubmit directory is not the same as the 61 globroot, but it is still local (i.e., not relative to the repository 62 root) 63 """ 64 results = presubmit_support.CheckBundleData( 65 self.mock_input_api, self.mock_output_api, 66 'test_data/different_local_path', 'test_data') 67 self.assertEqual([], results) 68 69 def testRepositoryRelative(self): 70 """ 71 Checks the case where globs are relative to the repository root. 72 """ 73 results = presubmit_support.CheckBundleData( 74 self.mock_input_api, self.mock_output_api, 75 'test_data/repository_relative') 76 self.assertEqual([], results) 77 78 def testMissingFilesInFilelist(self): 79 """ 80 Checks that we do indeed return an error if the filelist is missing a 81 file. In this case, all of the test .filelist and .globlist files are 82 excluded. 83 """ 84 results = presubmit_support.CheckBundleData(self.mock_input_api, 85 self.mock_output_api, 86 'test_data/missing', '.') 87 self.assertEqual(1, len(results)) 88 89 def testExtraFilesInFilelist(self): 90 """ 91 Checks the case where extra files have been included in the file list. 92 """ 93 results = presubmit_support.CheckBundleData(self.mock_input_api, 94 self.mock_output_api, 95 'test_data/extra', '.') 96 self.assertEqual(1, len(results)) 97 98 def testOrderInsensitive(self): 99 """ 100 Checks that we do not trigger an error for cases where the file list is 101 correct, but in a different order than the globlist expansion. 102 """ 103 results = presubmit_support.CheckBundleData(self.mock_input_api, 104 self.mock_output_api, 105 'test_data/reorder', '.') 106 self.assertEqual([], results) 107 108 def testUnexpectedHeader(self): 109 """ 110 Checks an unexpected header in a file list causes an error. 111 """ 112 results = presubmit_support.CheckBundleData(self.mock_input_api, 113 self.mock_output_api, 114 'test_data/comment', '.') 115 self.assertEqual(1, len(results)) 116 117 def testUntrackedFiles(self): 118 """ 119 Checks that the untracked files are correctly ignored. 120 """ 121 with tempfile.TemporaryDirectory() as temp_dir: 122 with open(os.path.join(temp_dir, 'untracked.filelist'), 'w') as f: 123 f.write(_TEMP_FILELIST_CONTENTS) 124 with open(os.path.join(temp_dir, 'untracked.globlist'), 'w') as f: 125 f.write(_TEMP_GLOBLIST_CONTENTS) 126 with open(os.path.join(temp_dir, 'untracked.txt'), 'w') as f: 127 f.write('Hello, World!') 128 path = os.path.join(temp_dir, 'untracked') 129 self.mock_input_api.change.RepositoryRoot = lambda: temp_dir 130 self.mock_input_api.PresubmitLocalPath = lambda: temp_dir 131 results = presubmit_support.CheckBundleData(self.mock_input_api, 132 self.mock_output_api, 133 'untracked') 134 self.assertEqual([], results) 135 136 def testExcludeDuplicates(self): 137 """ 138 Checks that duplicate filenames are not added to a filelist. 139 """ 140 results = presubmit_support.CheckBundleData(self.mock_input_api, 141 self.mock_output_api, 142 'test_data/duplicates', '.') 143 self.assertEqual([], results) 144 145 def testCheckOutsideGloblistDir(self): 146 """ 147 Checks that including files outside the globlist directory is an error. 148 """ 149 results = presubmit_support.CheckBundleData( 150 self.mock_input_api, self.mock_output_api, 151 'test_data/outside_globlist_dir', '.') 152 self.assertEqual(1, len(results)) 153 154 def testCheckIgnoreOutsideGloblistDir(self): 155 """ 156 Checks that files outside the globlist directory can be ignored. 157 """ 158 results = presubmit_support.CheckBundleData( 159 self.mock_input_api, self.mock_output_api, 160 'test_data/ignore_outside_globlist_dir', '.') 161 self.assertEqual([], results) 162 163 164if __name__ == '__main__': 165 unittest.main() 166