1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165

build / ios / presubmit_support_test.py [blame]

#!/usr/bin/env python3
# Copyright 2023 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import datetime
import os.path
import sys
import tempfile
import unittest

sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..'))

from PRESUBMIT_test_mocks import MockInputApi, MockOutputApi
from build.ios import presubmit_support

_TEMP_FILELIST_CONTENTS = """# Copyright %d The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
# NOTE: this file is generated by build/ios/update_bundle_filelist.py
#       If it requires updating, you should get a presubmit error with
#       instructions on how to regenerate. Otherwise, do not edit.
""" % (datetime.datetime.now().year)

_TEMP_GLOBLIST_CONTENTS = """**
-*.globlist
-*.filelist
"""


class BundleDataPresubmit(unittest.TestCase):
  def setUp(self):
    self.mock_input_api = MockInputApi()
    self.mock_input_api.change.RepositoryRoot = lambda: os.path.join(
        os.path.dirname(__file__), '..', '..')
    self.mock_input_api.PresubmitLocalPath = lambda: os.path.dirname(__file__)
    self.mock_output_api = MockOutputApi()

  def testBasic(self):
    """
        Checks that a glob can be expanded to build a file list and if it
        matches the existing file list, we should see no error.
        """
    results = presubmit_support.CheckBundleData(self.mock_input_api,
                                                self.mock_output_api,
                                                'test_data/basic', '.')
    self.assertEqual([], results)

  def testExclusion(self):
    """
        Check that globs can be used to exclude files from file lists.
        """
    results = presubmit_support.CheckBundleData(self.mock_input_api,
                                                self.mock_output_api,
                                                'test_data/exclusions', '.')
    self.assertEqual([], results)

  def testDifferentLocalPath(self):
    """
        Checks the case where the presubmit directory is not the same as the
        globroot, but it is still local (i.e., not relative to the repository
        root)
        """
    results = presubmit_support.CheckBundleData(
        self.mock_input_api, self.mock_output_api,
        'test_data/different_local_path', 'test_data')
    self.assertEqual([], results)

  def testRepositoryRelative(self):
    """
        Checks the case where globs are relative to the repository root.
        """
    results = presubmit_support.CheckBundleData(
        self.mock_input_api, self.mock_output_api,
        'test_data/repository_relative')
    self.assertEqual([], results)

  def testMissingFilesInFilelist(self):
    """
        Checks that we do indeed return an error if the filelist is missing a
        file. In this case, all of the test .filelist and .globlist files are
        excluded.
        """
    results = presubmit_support.CheckBundleData(self.mock_input_api,
                                                self.mock_output_api,
                                                'test_data/missing', '.')
    self.assertEqual(1, len(results))

  def testExtraFilesInFilelist(self):
    """
        Checks the case where extra files have been included in the file list.
        """
    results = presubmit_support.CheckBundleData(self.mock_input_api,
                                                self.mock_output_api,
                                                'test_data/extra', '.')
    self.assertEqual(1, len(results))

  def testOrderInsensitive(self):
    """
        Checks that we do not trigger an error for cases where the file list is
        correct, but in a different order than the globlist expansion.
        """
    results = presubmit_support.CheckBundleData(self.mock_input_api,
                                                self.mock_output_api,
                                                'test_data/reorder', '.')
    self.assertEqual([], results)

  def testUnexpectedHeader(self):
    """
        Checks an unexpected header in a file list causes an error.
        """
    results = presubmit_support.CheckBundleData(self.mock_input_api,
                                                self.mock_output_api,
                                                'test_data/comment', '.')
    self.assertEqual(1, len(results))

  def testUntrackedFiles(self):
    """
        Checks that the untracked files are correctly ignored.
        """
    with tempfile.TemporaryDirectory() as temp_dir:
      with open(os.path.join(temp_dir, 'untracked.filelist'), 'w') as f:
        f.write(_TEMP_FILELIST_CONTENTS)
      with open(os.path.join(temp_dir, 'untracked.globlist'), 'w') as f:
        f.write(_TEMP_GLOBLIST_CONTENTS)
      with open(os.path.join(temp_dir, 'untracked.txt'), 'w') as f:
        f.write('Hello, World!')
      path = os.path.join(temp_dir, 'untracked')
      self.mock_input_api.change.RepositoryRoot = lambda: temp_dir
      self.mock_input_api.PresubmitLocalPath = lambda: temp_dir
      results = presubmit_support.CheckBundleData(self.mock_input_api,
                                                  self.mock_output_api,
                                                  'untracked')
      self.assertEqual([], results)

  def testExcludeDuplicates(self):
    """
        Checks that duplicate filenames are not added to a filelist.
        """
    results = presubmit_support.CheckBundleData(self.mock_input_api,
                                                self.mock_output_api,
                                                'test_data/duplicates', '.')
    self.assertEqual([], results)

  def testCheckOutsideGloblistDir(self):
    """
        Checks that including files outside the globlist directory is an error.
        """
    results = presubmit_support.CheckBundleData(
        self.mock_input_api, self.mock_output_api,
        'test_data/outside_globlist_dir', '.')
    self.assertEqual(1, len(results))

  def testCheckIgnoreOutsideGloblistDir(self):
    """
        Checks that files outside the globlist directory can be ignored.
        """
    results = presubmit_support.CheckBundleData(
        self.mock_input_api, self.mock_output_api,
        'test_data/ignore_outside_globlist_dir', '.')
    self.assertEqual([], results)


if __name__ == '__main__':
  unittest.main()