Skip to content

Commit

Permalink
Merge pull request #168 from devmil/fix/dont_detect_changes_on_librar…
Browse files Browse the repository at this point in the history
…y_rename

fixes interface equality logic for the differ
  • Loading branch information
devmil authored Feb 9, 2024
2 parents fd6cfc5 + 1ecb423 commit 324e4e9
Show file tree
Hide file tree
Showing 17 changed files with 287 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Version 0.18.0
- add missing export to json output for `extract` command
- fixes interface equality logic in the differ functionality to be less strict on library renaming (if the entry points are still the same)
- fix change compatibility check for `dynamic` and `Object?` parameters
- fix compatibility checks for return types of executables

Expand Down
30 changes: 25 additions & 5 deletions lib/src/diff/package_api_differ.dart
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,31 @@ class PackageApiDiffer {
final interfaceListDiff = _diffIterables<InterfaceDeclaration>(
oldInterfaces,
newInterfaces,
(oldInterface, newInterface) =>
// for top level elements we have to consider the path as well as we might run into duplicate namings otherwise
oldInterface.name == newInterface.name &&
(context.isNotEmpty ||
oldInterface.relativePath == newInterface.relativePath),
(oldInterface, newInterface) {
// if the names are not the same, we already have a mismatch
if (oldInterface.name != newInterface.name) {
return false;
}

// we need to do additional checks as we might have naming conflicts otherwise

// if the entry points are the same, then we can assume that the interfaces are the same (independent of their relative path)
if (SetEquality<String>()
.equals(oldInterface.entryPoints, newInterface.entryPoints)) {
return true;
}

// here the name is equal but we have different entry points.
// to support detection of entry point changes we consider the interfaces equal if they have the same relative path
// (only works for top level elements)
if (context.isEmpty &&
oldInterface.relativePath == newInterface.relativePath) {
return true;
}

// if we are here then we only consider the interfaces equal (by name) if they are not top-level
return context.isNotEmpty;
},
);
final changes = <ApiChange>[];
for (final oldInterface in interfaceListDiff.matches.keys) {
Expand Down
45 changes: 45 additions & 0 deletions test/integration_tests/diff/test_package_library_rename_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import 'package:dart_apitool/api_tool.dart';
import 'package:test/test.dart';
import 'package:path/path.dart' as path;

void main() {
group('test_package with library rename but untouched entry point', () {
late PackageApiAnalyzer packageAWithOldLibraryName;
late PackageApiAnalyzer packageAWithNewLibraryName;

setUpAll(
() {
packageAWithOldLibraryName = PackageApiAnalyzer(
packagePath: path.join(
'test',
'test_packages',
'library_rename_without_entry_point_change',
'package_a_with_old_library_name',
));
packageAWithNewLibraryName = PackageApiAnalyzer(
packagePath: path.join(
'test',
'test_packages',
'library_rename_without_entry_point_change',
'package_a_with_new_library_name',
));
},
);
group('diff', () {
late PackageApiDiffResult diffResult;
setUpAll(() async {
diffResult = PackageApiDiffer().diff(
oldApi: await packageAWithOldLibraryName.analyze(),
newApi: await packageAWithNewLibraryName.analyze(),
);
});

test('doesn\'t count as any public API change', () {
expect(
diffResult.apiChanges.length,
0,
);
});
});
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# https://dart.dev/guides/libraries/private-files
# Created by `dart pub`
.dart_tool/

# Avoid committing pubspec.lock for library packages; see
# https://dart.dev/guides/libraries/private-files#pubspeclock.
pubspec.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 1.0.0

- Initial version.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<!--
This README describes the package. If you publish this package to pub.dev,
this README's contents appear on the landing page for your package.
For information about how to write a good package README, see the guide for
[writing package pages](https://dart.dev/guides/libraries/writing-package-pages).
For general information about developing packages, see the Dart guide for
[creating packages](https://dart.dev/guides/libraries/create-library-packages)
and the Flutter guide for
[developing packages and plugins](https://flutter.dev/developing-packages).
-->

TODO: Put a short description of the package here that helps potential users
know whether this package might be useful for them.

## Features

TODO: List what your package can do. Maybe include images, gifs, or videos.

## Getting started

TODO: List prerequisites and provide or point to information on how to
start using the package.

## Usage

TODO: Include short and useful examples for package users. Add longer examples
to `/example` folder.

```dart
const like = 'sample';
```

## Additional information

TODO: Tell users more about the package: where to find more information, how to
contribute to the package, how to file issues, what response they can expect
from the package authors, and more.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# This file configures the static analysis results for your project (errors,
# warnings, and lints).
#
# This enables the 'recommended' set of lints from `package:lints`.
# This set helps identify many issues that may lead to problems when running
# or consuming Dart code, and enforces writing Dart using a single, idiomatic
# style and format.
#
# If you want a smaller set of lints you can change this to specify
# 'package:lints/core.yaml'. These are just the most critical lints
# (the recommended set includes the core lints).
# The core lints are also what is used by pub.dev for scoring packages.

include: package:lints/recommended.yaml

# Uncomment the following section to specify additional rules.

# linter:
# rules:
# - camel_case_types

# analyzer:
# exclude:
# - path/to/excluded/files/**

# For more information about the core and recommended set of lints, see
# https://dart.dev/go/core-lints

# For additional information about configuring this file, see
# https://dart.dev/guides/language/analysis-options
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/// Support for doing something awesome.
///
/// More dartdocs go here.
library package_a;

export 'src/class_a_renamed.dart';
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class ClassA {
final String someValue;
const ClassA(this.someValue);

@override
String toString() {
return 'ClassA{someValue: $someValue}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: package_a
description: A starting point for Dart libraries or applications.
version: 1.0.0
# repository: https://github.com/my_org/my_repo

environment:
sdk: '>=2.18.4 <3.0.0'

# dependencies:
# path: ^1.8.0

dev_dependencies:
lints: ^2.0.0
test: ^1.21.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# https://dart.dev/guides/libraries/private-files
# Created by `dart pub`
.dart_tool/

# Avoid committing pubspec.lock for library packages; see
# https://dart.dev/guides/libraries/private-files#pubspeclock.
pubspec.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 1.0.0

- Initial version.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<!--
This README describes the package. If you publish this package to pub.dev,
this README's contents appear on the landing page for your package.
For information about how to write a good package README, see the guide for
[writing package pages](https://dart.dev/guides/libraries/writing-package-pages).
For general information about developing packages, see the Dart guide for
[creating packages](https://dart.dev/guides/libraries/create-library-packages)
and the Flutter guide for
[developing packages and plugins](https://flutter.dev/developing-packages).
-->

TODO: Put a short description of the package here that helps potential users
know whether this package might be useful for them.

## Features

TODO: List what your package can do. Maybe include images, gifs, or videos.

## Getting started

TODO: List prerequisites and provide or point to information on how to
start using the package.

## Usage

TODO: Include short and useful examples for package users. Add longer examples
to `/example` folder.

```dart
const like = 'sample';
```

## Additional information

TODO: Tell users more about the package: where to find more information, how to
contribute to the package, how to file issues, what response they can expect
from the package authors, and more.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# This file configures the static analysis results for your project (errors,
# warnings, and lints).
#
# This enables the 'recommended' set of lints from `package:lints`.
# This set helps identify many issues that may lead to problems when running
# or consuming Dart code, and enforces writing Dart using a single, idiomatic
# style and format.
#
# If you want a smaller set of lints you can change this to specify
# 'package:lints/core.yaml'. These are just the most critical lints
# (the recommended set includes the core lints).
# The core lints are also what is used by pub.dev for scoring packages.

include: package:lints/recommended.yaml

# Uncomment the following section to specify additional rules.

# linter:
# rules:
# - camel_case_types

# analyzer:
# exclude:
# - path/to/excluded/files/**

# For more information about the core and recommended set of lints, see
# https://dart.dev/go/core-lints

# For additional information about configuring this file, see
# https://dart.dev/guides/language/analysis-options
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/// Support for doing something awesome.
///
/// More dartdocs go here.
library package_a;

export 'src/class_a.dart';
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class ClassA {
final String someValue;
const ClassA(this.someValue);

@override
String toString() {
return 'ClassA{someValue: $someValue}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: package_a
description: A starting point for Dart libraries or applications.
version: 1.0.0
# repository: https://github.com/my_org/my_repo

environment:
sdk: '>=2.18.4 <3.0.0'

# dependencies:
# path: ^1.8.0

dev_dependencies:
lints: ^2.0.0
test: ^1.21.0

0 comments on commit 324e4e9

Please sign in to comment.