-
Notifications
You must be signed in to change notification settings - Fork 166
Compare screenshots, generate report of differences. #8579
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| // Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file | ||
| // for details. All rights reserved. Use of this source code is governed by a | ||
| // BSD-style license that can be found in the LICENSE file. | ||
|
|
||
| import 'dart:io'; | ||
|
|
||
| import 'package:markdown/markdown.dart'; | ||
| import 'package:path/path.dart' as p; | ||
|
|
||
| /// Compares the screenshots from the previous and current test runs. | ||
| /// Uses imagemagick for image processing. | ||
| /// | ||
| /// dart `<script.dart>` `<before-dir>` `<after-dir>` `<report-dir>`. | ||
| Future<void> main(List<String> args) async { | ||
| final beforeFiles = await _list(args[0]); | ||
| final afterFiles = await _list(args[1]); | ||
|
|
||
| final reportDir = Directory(args[2]); | ||
| await reportDir.create(recursive: true); | ||
| await _CompareTool(beforeFiles, afterFiles, reportDir)._compare(); | ||
| } | ||
|
|
||
| class _CompareTool { | ||
| final Directory _reportDir; | ||
| final Map<String, File> _beforeFiles; | ||
| final Map<String, File> _afterFiles; | ||
| final _report = StringBuffer(); | ||
|
|
||
| _CompareTool( | ||
| this._beforeFiles, | ||
| this._afterFiles, | ||
| this._reportDir, | ||
| ); | ||
|
|
||
| Future<void> _compare() async { | ||
| _report.writeln( | ||
| 'Screenshot comparison report generated at ${DateTime.now().toIso8601String()}.'); | ||
|
|
||
| final newFiles = _afterFiles.keys | ||
| .where((key) => !_beforeFiles.containsKey(key)) | ||
| .toList(); | ||
| if (newFiles.isNotEmpty) { | ||
| _report.writeln([ | ||
| '', | ||
| '# New files', | ||
| newFiles.map((e) => '- `$e`').join('\n'), | ||
| ].join('\n\n')); | ||
| } | ||
|
|
||
| final missingFiles = _beforeFiles.keys | ||
| .where((key) => !_afterFiles.containsKey(key)) | ||
| .toList(); | ||
| if (missingFiles.isNotEmpty) { | ||
| _report.writeln([ | ||
| '', | ||
| '# Missing files', | ||
| missingFiles.map((e) => '- `$e`').join('\n'), | ||
| ].join('\n\n')); | ||
| } | ||
|
|
||
| for (final path in _afterFiles.keys) { | ||
| final after = _afterFiles[path]!; | ||
| if (!_beforeFiles.containsKey(path)) continue; | ||
| final before = _beforeFiles[path]!; | ||
|
|
||
| // quick byte-content check | ||
| final afterBytes = await after.readAsBytes(); | ||
| final beforeBytes = await before.readAsBytes(); | ||
| if (afterBytes.length == beforeBytes.length && | ||
| afterBytes.indexed.every((e) => beforeBytes[e.$1] == e.$2)) { | ||
| continue; | ||
| } | ||
|
|
||
| final relativeDir = p.dirname(path); | ||
| final basename = p.basenameWithoutExtension(path); | ||
| final diffPath = | ||
| p.join(_reportDir.path, relativeDir, '$basename-diff.png'); | ||
| await File(diffPath).parent.create(recursive: true); | ||
|
|
||
| final pr = await Process.run('compare', [ | ||
| before.path, | ||
| after.path, | ||
| diffPath, | ||
| ]); | ||
| if (pr.exitCode == 0) continue; | ||
|
|
||
| final beforeFile = | ||
| File(p.join(_reportDir.path, relativeDir, '$basename-before.png')); | ||
| await beforeFile.writeAsBytes(beforeBytes); | ||
| final afterFile = | ||
| File(p.join(_reportDir.path, relativeDir, '$basename-after.png')); | ||
| await afterFile.writeAsBytes(afterBytes); | ||
|
|
||
| _report.writeln('`$path`\n'); | ||
| _report.writeln( | ||
| '})\n'); | ||
| _report | ||
| .writeln('})\n'); | ||
| _report | ||
| .writeln('})\n'); | ||
| _report.writeln(); | ||
| } | ||
|
|
||
| await _writeIndexHtml(); | ||
| } | ||
|
|
||
| Future<void> _writeIndexHtml() async { | ||
| await File(p.join(_reportDir.path, 'index.html')).writeAsString([ | ||
| '<html><body>', | ||
| markdownToHtml(_report.toString()), | ||
| '</body></html>', | ||
| ].join('\n')); | ||
| } | ||
| } | ||
|
|
||
| Future<Map<String, File>> _list(String path) async { | ||
| final map = <String, File>{}; | ||
| await for (final file in Directory(path).list(recursive: true)) { | ||
| if (file is! File) continue; | ||
| final rp = p.relative(file.path, from: path); | ||
| map[rp] = file; | ||
| } | ||
| return Map.fromEntries( | ||
| map.entries.toList()..sort((a, b) => a.key.compareTo(b.key))); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.