summaryrefslogtreecommitdiff
path: root/LICENSES/MIT.txt
blob: 204b93da48d02900098ced21c54062ffbff36b9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
MIT License Copyright (c) <year> <copyright holders>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice (including the next
paragraph) shall be included in all copies or substantial portions of the
Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
> 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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
#!/usr/bin/env python3
# Copyright 2020 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import json
import os
import os.path
import shutil
import tempfile
import unittest

import check_stable_mojom_compatibility

from mojom.generate import module


class Change:
  """Helper to clearly define a mojom file delta to be analyzed."""

  def __init__(self, filename, old=None, new=None):
    """If old is None, this is a file addition. If new is None, this is a file
    deletion. Otherwise it's a file change."""
    self.filename = filename
    self.old = old
    self.new = new


class UnchangedFile(Change):
  def __init__(self, filename, contents):
    super().__init__(filename, old=contents, new=contents)


class CheckStableMojomCompatibilityTest(unittest.TestCase):
  """Tests covering the behavior of the compatibility checking tool. Note that
  details of different compatibility checks and relevant failure modes are NOT
  covered by these tests. Those are instead covered by unittests in
  version_compatibility_unittest.py. Additionally, the tests which ensure a
  given set of [Stable] mojom definitions are indeed plausibly stable (i.e. they
  have no unstable dependencies) are covered by stable_attribute_unittest.py.

  These tests cover higher-level concerns of the compatibility checking tool,
  like file or symbol, renames, changes spread over multiple files, etc."""

  def verifyBackwardCompatibility(self, changes):
    """Helper for implementing assertBackwardCompatible and
    assertNotBackwardCompatible"""

    temp_dir = tempfile.mkdtemp()
    for change in changes:
      if change.old:
        # Populate the old file on disk in our temporary fake source root
        file_path = os.path.join(temp_dir, change.filename)
        dir_path = os.path.dirname(file_path)
        if not os.path.exists(dir_path):
          os.makedirs(dir_path)
        with open(file_path, 'w') as f:
          f.write(change.old)

    delta = []
    for change in changes:
      if change.old != change.new:
        delta.append({
            'filename': change.filename,
            'old': change.old,
            'new': change.new
        })

    try:
      check_stable_mojom_compatibility.Run(['--src-root', temp_dir],
                                           delta=delta)
    finally:
      shutil.rmtree(temp_dir)

  def assertBackwardCompatible(self, changes):
    self.verifyBackwardCompatibility(changes)

  def assertNotBackwardCompatible(self, changes):
    try:
      self.verifyBackwardCompatibility(changes)
    except Exception:
      return

    raise Exception('Change unexpectedly passed a backward-compatibility check')

  def testBasicCompatibility(self):
    """Minimal smoke test to verify acceptance of a simple valid change."""
    self.assertBackwardCompatible([
        Change('foo/foo.mojom',
               old='[Stable] struct S {};',
               new='[Stable] struct S { [MinVersion=1] int32 x; };')
    ])

  def testBasicIncompatibility(self):
    """Minimal smoke test to verify rejection of a simple invalid change."""
    self.assertNotBackwardCompatible([
        Change('foo/foo.mojom',
               old='[Stable] struct S {};',
               new='[Stable] struct S { int32 x; };')
    ])

  def testIgnoreIfNotStable(self):
    """We don't care about types not marked [Stable]"""
    self.assertBackwardCompatible([
        Change('foo/foo.mojom',
               old='struct S {};',
               new='struct S { int32 x; };')
    ])

  def testRename(self):
    """We can do checks for renamed types."""
    self.assertBackwardCompatible([
        Change('foo/foo.mojom',
               old='[Stable] struct S {};',
               new='[Stable, RenamedFrom="S"] struct T {};')
    ])
    self.assertNotBackwardCompatible([
        Change('foo/foo.mojom',
               old='[Stable] struct S {};',
               new='[Stable, RenamedFrom="S"] struct T { int32 x; };')
    ])
    self.assertBackwardCompatible([
        Change('foo/foo.mojom',
               old='[Stable] struct S {};',
               new="""\
               [Stable, RenamedFrom="S"]
               struct T { [MinVersion=1] int32 x; };
               """)
    ])

  def testNewlyStable(self):
    """We don't care about types newly marked as [Stable]."""
    self.assertBackwardCompatible([
        Change('foo/foo.mojom',
               old='struct S {};',
               new='[Stable] struct S { int32 x; };')
    ])

  def testFileRename(self):
    """Make sure we can still do compatibility checks after a file rename."""
    self.assertBackwardCompatible([
        Change('foo/foo.mojom', old='[Stable] struct S {};', new=None),
        Change('bar/bar.mojom',
               old=None,
               new='[Stable] struct S { [MinVersion=1] int32 x; };')
    ])
    self.assertNotBackwardCompatible([
        Change('foo/foo.mojom', old='[Stable] struct S {};', new=None),
        Change('bar/bar.mojom', old=None, new='[Stable] struct S { int32 x; };')
    ])

  def testWithImport(self):
    """Ensure that cross-module dependencies do not break the compatibility
    checking tool."""
    self.assertBackwardCompatible([
        Change('foo/foo.mojom',
               old="""\
               module foo;
               [Stable] struct S {};
               """,
               new="""\
               module foo;
               [Stable] struct S { [MinVersion=2] int32 x; };
               """),
        Change('bar/bar.mojom',
               old="""\
               module bar;
               import "foo/foo.mojom";
               [Stable] struct T { foo.S s; };
               """,
               new="""\
               module bar;
               import "foo/foo.mojom";
               [Stable] struct T { foo.S s; [MinVersion=1] int32 y; };
               """)
    ])

  def testWithMovedDefinition(self):
    """If a definition moves from one file to another, we should still be able
    to check compatibility accurately."""
    self.assertBackwardCompatible([
        Change('foo/foo.mojom',
               old="""\
               module foo;
               [Stable] struct S {};
               """,
               new="""\
               module foo;
               """),
        Change('bar/bar.mojom',
               old="""\
               module bar;
               import "foo/foo.mojom";
               [Stable] struct T { foo.S s; };
               """,
               new="""\
               module bar;
               import "foo/foo.mojom";
               [Stable, RenamedFrom="foo.S"] struct S {
                 [MinVersion=2] int32 x;
               };
               [Stable] struct T { S s; [MinVersion=1] int32 y; };
               """)
    ])

    self.assertNotBackwardCompatible([
        Change('foo/foo.mojom',
               old="""\
               module foo;
               [Stable] struct S {};
               """,
               new="""\
               module foo;
               """),
        Change('bar/bar.mojom',
               old="""\
               module bar;
               import "foo/foo.mojom";
               [Stable] struct T { foo.S s; };
               """,
               new="""\
               module bar;
               import "foo/foo.mojom";
               [Stable, RenamedFrom="foo.S"] struct S { int32 x; };
               [Stable] struct T { S s; [MinVersion=1] int32 y; };
               """)
    ])

  def testWithUnmodifiedImport(self):
    """Unchanged files in the filesystem are still parsed by the compatibility
    checking tool if they're imported by a changed file."""
    self.assertBackwardCompatible([
        UnchangedFile('foo/foo.mojom', 'module foo; [Stable] struct S {};'),
        Change('bar/bar.mojom',
               old="""\
               module bar;
               import "foo/foo.mojom";
               [Stable] struct T { foo.S s; };
               """,
               new="""\
               module bar;
               import "foo/foo.mojom";
               [Stable] struct T { foo.S s; [MinVersion=1] int32 x; };
               """)
    ])

    self.assertNotBackwardCompatible([
        UnchangedFile('foo/foo.mojom', 'module foo; [Stable] struct S {};'),
        Change('bar/bar.mojom',
               old="""\
               module bar;
               import "foo/foo.mojom";
               [Stable] struct T { foo.S s; };
               """,
               new="""\
               module bar;
               import "foo/foo.mojom";
               [Stable] struct T { foo.S s; int32 x; };
               """)
    ])

  def testWithPartialImport(self):
    """The compatibility checking tool correctly parses imports with partial
    paths."""
    self.assertBackwardCompatible([
        UnchangedFile('foo/foo.mojom', 'module foo; [Stable] struct S {};'),
        Change('foo/bar.mojom',
               old="""\
               module bar;
               import "foo/foo.mojom";
               [Stable] struct T { foo.S s; };
               """,
               new="""\
               module bar;
               import "foo.mojom";
               [Stable] struct T { foo.S s; };
               """)
    ])

    self.assertBackwardCompatible([
        UnchangedFile('foo/foo.mojom', 'module foo; [Stable] struct S {};'),
        Change('foo/bar.mojom',
               old="""\
               module bar;
               import "foo.mojom";
               [Stable] struct T { foo.S s; };
               """,
               new="""\
               module bar;
               import "foo/foo.mojom";
               [Stable] struct T { foo.S s; };
               """)
    ])

    self.assertNotBackwardCompatible([
        UnchangedFile('foo/foo.mojom', 'module foo; [Stable] struct S {};'),
        Change('bar/bar.mojom',
               old="""\
               module bar;
               import "foo/foo.mojom";
               [Stable] struct T { foo.S s; };
               """,
               new="""\
               module bar;
               import "foo.mojom";
               [Stable] struct T { foo.S s; };
               """)
    ])

    self.assertNotBackwardCompatible([
        UnchangedFile('foo/foo.mojom', 'module foo; [Stable] struct S {};'),
        Change('bar/bar.mojom',
               old="""\
               module bar;
               import "foo.mojom";
               [Stable] struct T { foo.S s; };
               """,
               new="""\
               module bar;
               import "foo/foo.mojom";
               [Stable] struct T { foo.S s; };
               """)
    ])

  def testNewEnumDefault(self):
    # Should be backwards compatible since it does not affect the wire format.
    # This specific case also checks that the backwards compatibility checker
    # does not throw an error due to the older version of the enum not
    # specifying [Default].
    self.assertBackwardCompatible([
        Change('foo/foo.mojom',
               old='[Extensible] enum E { One };',
               new='[Extensible] enum E { [Default] One };')
    ])
    self.assertBackwardCompatible([
        Change('foo/foo.mojom',
               old='[Extensible] enum E { [Default] One, Two, };',
               new='[Extensible] enum E { One, [Default] Two, };')
    ])