-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Report major breaking change type in JSON report and support pub refs without version #189
Changes from all commits
6b1f594
b18821e
8454e71
b5e6fff
e36cec3
2d7f8f8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,181 @@ | ||||||||||||
import 'dart:convert'; | ||||||||||||
import 'dart:io'; | ||||||||||||
|
||||||||||||
import 'package:dart_apitool/api_tool_cli.dart'; | ||||||||||||
import 'package:dart_apitool/src/cli/commands/version_check.dart'; | ||||||||||||
import 'package:dart_apitool/src/diff/report/json_diff_reporter.dart'; | ||||||||||||
import 'package:pub_semver/pub_semver.dart'; | ||||||||||||
import 'package:test/test.dart'; | ||||||||||||
|
||||||||||||
import 'package:mocktail/mocktail.dart'; | ||||||||||||
|
||||||||||||
class MockFile extends Mock implements File {} | ||||||||||||
|
||||||||||||
void main() { | ||||||||||||
group('JsonDiffReporter', () { | ||||||||||||
late JsonDiffReporter reporter; | ||||||||||||
final anyVersionCheckResult = VersionCheckResult.success( | ||||||||||||
oldVersion: Version(1, 0, 0), | ||||||||||||
newVersion: Version(2, 0, 0), | ||||||||||||
explanation: '', | ||||||||||||
); | ||||||||||||
final oldPackageRef = PackageRef('pub://package/1.0.0'); | ||||||||||||
final newPackageRef = PackageRef('pub://package/2.0.0'); | ||||||||||||
late MockFile mockFile; | ||||||||||||
late StringBuffer collectedFileContent; | ||||||||||||
|
||||||||||||
setUp(() { | ||||||||||||
collectedFileContent = StringBuffer(); | ||||||||||||
mockFile = MockFile(); | ||||||||||||
reporter = JsonDiffReporter( | ||||||||||||
oldPackageRef: oldPackageRef, | ||||||||||||
newPackageRef: newPackageRef, | ||||||||||||
outputFile: mockFile, | ||||||||||||
); | ||||||||||||
when(() => mockFile.writeAsString(any())).thenAnswer((invocation) async { | ||||||||||||
collectedFileContent.write(invocation.positionalArguments.first); | ||||||||||||
return mockFile; | ||||||||||||
}); | ||||||||||||
when(() => mockFile.path).thenReturn('mocked_file_path.json'); | ||||||||||||
}); | ||||||||||||
|
||||||||||||
PackageApiDiffResult createEmptyDiffResult() { | ||||||||||||
return PackageApiDiffResult(); | ||||||||||||
} | ||||||||||||
|
||||||||||||
void addBreakingChange( | ||||||||||||
PackageApiDiffResult diffResult, { | ||||||||||||
ApiChangeCode changeCode = ApiChangeCode.ci01, | ||||||||||||
}) { | ||||||||||||
diffResult.addApiChange(ApiChange( | ||||||||||||
changeCode: changeCode, | ||||||||||||
changeDescription: 'Test breaking change: ${changeCode.name}', | ||||||||||||
contextTrace: [], | ||||||||||||
isExperimental: false, | ||||||||||||
type: ApiChangeType.remove, | ||||||||||||
)); | ||||||||||||
} | ||||||||||||
|
||||||||||||
void addNonBreakingChange( | ||||||||||||
PackageApiDiffResult diffResult, { | ||||||||||||
ApiChangeCode changeCode = ApiChangeCode.ci02, | ||||||||||||
ApiChangeType changeType = ApiChangeType.addCompatiblePatch, | ||||||||||||
}) { | ||||||||||||
diffResult.addApiChange(ApiChange( | ||||||||||||
changeCode: changeCode, | ||||||||||||
changeDescription: 'Test non-breaking change: ${changeCode.name}', | ||||||||||||
contextTrace: [], | ||||||||||||
isExperimental: false, | ||||||||||||
type: changeType, | ||||||||||||
)); | ||||||||||||
} | ||||||||||||
|
||||||||||||
test('Can be instantiated', () { | ||||||||||||
// the setup would have failed already | ||||||||||||
expect(reporter, isA<JsonDiffReporter>()); | ||||||||||||
}); | ||||||||||||
|
||||||||||||
test('Can handle empty diff report', () async { | ||||||||||||
final diffResult = createEmptyDiffResult(); | ||||||||||||
await reporter.generateReport(diffResult, anyVersionCheckResult); | ||||||||||||
final jsonReport = jsonDecode(collectedFileContent.toString()); | ||||||||||||
expect(jsonReport['report']['noChangesDetected'], isTrue); | ||||||||||||
}); | ||||||||||||
test('Can handle diff report with only one breaking change', () async { | ||||||||||||
Comment on lines
+83
to
+84
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
same for the other ones below if you agree with it |
||||||||||||
final diffResult = createEmptyDiffResult(); | ||||||||||||
addBreakingChange(diffResult, changeCode: ApiChangeCode.ci01); | ||||||||||||
await reporter.generateReport(diffResult, anyVersionCheckResult); | ||||||||||||
final jsonReport = jsonDecode(collectedFileContent.toString()); | ||||||||||||
expect(jsonReport['report']['breakingChanges'], isNotNull); | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I would create a method to return the breaking changes property and another one to get the 'children' of it. Otherwise, if we need to change the name of any of it, we would need to adapt a lot of tests |
||||||||||||
expect(jsonReport['report']['breakingChanges']['children'].length, 1); | ||||||||||||
expect( | ||||||||||||
jsonReport['report']['breakingChanges']['children'] | ||||||||||||
.single['changeCode'], | ||||||||||||
ApiChangeCode.ci01.code); | ||||||||||||
expect(jsonReport['report']['breakingChanges']['children'].single['type'], | ||||||||||||
'major'); | ||||||||||||
}); | ||||||||||||
test('Can handle diff report with multiple breaking changes', () async { | ||||||||||||
final diffResult = createEmptyDiffResult(); | ||||||||||||
addBreakingChange(diffResult, changeCode: ApiChangeCode.ci01); | ||||||||||||
addBreakingChange(diffResult, changeCode: ApiChangeCode.ci04); | ||||||||||||
await reporter.generateReport(diffResult, anyVersionCheckResult); | ||||||||||||
final jsonReport = jsonDecode(collectedFileContent.toString()); | ||||||||||||
expect(jsonReport['report']['breakingChanges'], isNotNull); | ||||||||||||
expect(jsonReport['report']['breakingChanges']['children'].length, 2); | ||||||||||||
expect( | ||||||||||||
jsonReport['report']['breakingChanges']['children'][0]['changeCode'], | ||||||||||||
ApiChangeCode.ci01.code); | ||||||||||||
expect( | ||||||||||||
jsonReport['report']['breakingChanges']['children'][1]['changeCode'], | ||||||||||||
ApiChangeCode.ci04.code); | ||||||||||||
}); | ||||||||||||
test('Can handle diff report with only one non-breaking change', () async { | ||||||||||||
final diffResult = createEmptyDiffResult(); | ||||||||||||
addNonBreakingChange(diffResult, changeCode: ApiChangeCode.ci02); | ||||||||||||
await reporter.generateReport(diffResult, anyVersionCheckResult); | ||||||||||||
final jsonReport = jsonDecode(collectedFileContent.toString()); | ||||||||||||
expect(jsonReport['report']['nonBreakingChanges'], isNotNull); | ||||||||||||
expect(jsonReport['report']['nonBreakingChanges']['children'].length, 1); | ||||||||||||
expect( | ||||||||||||
jsonReport['report']['nonBreakingChanges']['children'] | ||||||||||||
.single['changeCode'], | ||||||||||||
ApiChangeCode.ci02.code); | ||||||||||||
expect( | ||||||||||||
jsonReport['report']['nonBreakingChanges']['children'].single['type'], | ||||||||||||
'patch'); | ||||||||||||
}); | ||||||||||||
test('Can handle diff report with multiple non-breaking changes', () async { | ||||||||||||
final diffResult = createEmptyDiffResult(); | ||||||||||||
addNonBreakingChange(diffResult, changeCode: ApiChangeCode.ci02); | ||||||||||||
addNonBreakingChange(diffResult, | ||||||||||||
changeCode: ApiChangeCode.ci05, | ||||||||||||
changeType: ApiChangeType.addCompatibleMinor); | ||||||||||||
await reporter.generateReport(diffResult, anyVersionCheckResult); | ||||||||||||
final jsonReport = jsonDecode(collectedFileContent.toString()); | ||||||||||||
expect(jsonReport['report']['nonBreakingChanges'], isNotNull); | ||||||||||||
expect(jsonReport['report']['nonBreakingChanges']['children'].length, 2); | ||||||||||||
expect( | ||||||||||||
jsonReport['report']['nonBreakingChanges']['children'][0] | ||||||||||||
['changeCode'], | ||||||||||||
ApiChangeCode.ci02.code); | ||||||||||||
expect( | ||||||||||||
jsonReport['report']['nonBreakingChanges']['children'][1] | ||||||||||||
['changeCode'], | ||||||||||||
ApiChangeCode.ci05.code); | ||||||||||||
expect(jsonReport['report']['nonBreakingChanges']['children'][0]['type'], | ||||||||||||
'patch'); | ||||||||||||
expect(jsonReport['report']['nonBreakingChanges']['children'][1]['type'], | ||||||||||||
'minor'); | ||||||||||||
}); | ||||||||||||
test('Can handle diff report with breaking and non-breaking changes', | ||||||||||||
() async { | ||||||||||||
final diffResult = createEmptyDiffResult(); | ||||||||||||
addBreakingChange(diffResult, changeCode: ApiChangeCode.ci01); | ||||||||||||
addNonBreakingChange(diffResult, changeCode: ApiChangeCode.ci02); | ||||||||||||
addBreakingChange(diffResult, changeCode: ApiChangeCode.ci04); | ||||||||||||
addNonBreakingChange(diffResult, changeCode: ApiChangeCode.ci05); | ||||||||||||
|
||||||||||||
await reporter.generateReport(diffResult, anyVersionCheckResult); | ||||||||||||
final jsonReport = jsonDecode(collectedFileContent.toString()); | ||||||||||||
expect(jsonReport['report']['breakingChanges'], isNotNull); | ||||||||||||
expect(jsonReport['report']['breakingChanges']['children'].length, 2); | ||||||||||||
expect( | ||||||||||||
jsonReport['report']['breakingChanges']['children'][0]['changeCode'], | ||||||||||||
ApiChangeCode.ci01.code); | ||||||||||||
expect( | ||||||||||||
jsonReport['report']['breakingChanges']['children'][1]['changeCode'], | ||||||||||||
ApiChangeCode.ci04.code); | ||||||||||||
expect(jsonReport['report']['nonBreakingChanges'], isNotNull); | ||||||||||||
expect(jsonReport['report']['nonBreakingChanges']['children'].length, 2); | ||||||||||||
expect( | ||||||||||||
jsonReport['report']['nonBreakingChanges']['children'][0] | ||||||||||||
['changeCode'], | ||||||||||||
ApiChangeCode.ci02.code); | ||||||||||||
expect( | ||||||||||||
jsonReport['report']['nonBreakingChanges']['children'][1] | ||||||||||||
['changeCode'], | ||||||||||||
ApiChangeCode.ci05.code); | ||||||||||||
}); | ||||||||||||
}); | ||||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I saw that we are repeating ourselves with this comment of 'just some random package ...'. What do you think about adding this comment at the top of the test file instead of repeating it for every test? I know that it will (probably) be harder to understand where the package came from but I don't think that repeating it for all the tests is much better too :/