From 4f7346c16860317d55311ef2b7505535325bd6c5 Mon Sep 17 00:00:00 2001 From: Yelaman Date: Sun, 19 May 2024 04:27:00 +0600 Subject: [PATCH] . B file path extractor fix issue on Windows --- lib/src/core/file_path_extractor.dart | 18 +++++--------- lib/src/reporters/diff_tool/diff_tools.dart | 2 +- test/approval_test.dart | 1 + test/groups/diff_tools_tests.dart | 2 +- test/groups/minor_tests.dart | 27 +++++++-------------- test/utils/helper.dart | 13 +++++++--- 6 files changed, 28 insertions(+), 35 deletions(-) diff --git a/lib/src/core/file_path_extractor.dart b/lib/src/core/file_path_extractor.dart index 778787d..e9eb949 100644 --- a/lib/src/core/file_path_extractor.dart +++ b/lib/src/core/file_path_extractor.dart @@ -10,19 +10,14 @@ class FilePathExtractor { String get filePath { try { final stackTraceString = _stackTraceFetcher.currentStackTrace; - + ApprovalLogger.log(stackTraceString); final uriRegExp = RegExp(isWindows ? _windowsPattern : _linuxMacOSPattern); final match = uriRegExp.firstMatch(stackTraceString); if (match != null) { - if (isWindows) { - final rawPath = match.group(1)!.replaceAll(RegExp(r':\d+:\d+\)$'), ''); - final filePath = Uri.parse('file://$rawPath'); - return filePath.toFilePath(windows: isWindows); - } else { - final filePath = Uri.tryParse('file:///${match.group(1)!}'); - return filePath!.toFilePath(); - } + final rawPath = match.group(1)!; + final filePath = isWindows ? Uri.file(rawPath, windows: true).toFilePath(windows: true) : Uri.file(rawPath).toFilePath(); + return filePath; } else { throw FileNotFoundException( message: 'File not found in stack trace', @@ -36,7 +31,6 @@ class FilePathExtractor { static bool isWindows = Platform.isWindows; - static const String _windowsPattern = r'file://(/[a-zA-Z]:[^\s]*)'; - - static const String _linuxMacOSPattern = r'file:\/\/\/([^\s:]+)'; + static const String _windowsPattern = r'file:///([a-zA-Z]:/[^:\s]+)'; + static const String _linuxMacOSPattern = r'file:///([^:\s]+)'; } diff --git a/lib/src/reporters/diff_tool/diff_tools.dart b/lib/src/reporters/diff_tool/diff_tools.dart index b527904..a4685fd 100644 --- a/lib/src/reporters/diff_tool/diff_tools.dart +++ b/lib/src/reporters/diff_tool/diff_tools.dart @@ -18,7 +18,7 @@ final class MacDiffTools { /// `WindowsDiffTools` contains diff tools available on Windows. final class WindowsDiffTools { static const DiffInfo visualStudioCode = DiffInfo( - command: 'C:\\Program Files\\Microsoft VS Code\\bin\\code', + command: 'C:\\Program Files\\Microsoft VS Code\\bin\\code.cmd', arg: '-d', name: 'code', ); diff --git a/test/approval_test.dart b/test/approval_test.dart index 7025d16..882e59c 100644 --- a/test/approval_test.dart +++ b/test/approval_test.dart @@ -3,6 +3,7 @@ import 'dart:io'; import 'package:approval_tests/approval_tests.dart'; import 'package:test/test.dart'; +import 'package:path/path.dart' as p; // Import project-specific dependencies import 'models/item.dart'; diff --git a/test/groups/diff_tools_tests.dart b/test/groups/diff_tools_tests.dart index e52afe9..beb5b12 100644 --- a/test/groups/diff_tools_tests.dart +++ b/test/groups/diff_tools_tests.dart @@ -26,7 +26,7 @@ void diffToolsTests({ existentApprovedPath, existentReceivedPath, ), - throwsA(isA()), + Platform.isWindows ? returnsNormally : throwsA(isA()), ); ApprovalLogger.success( diff --git a/test/groups/minor_tests.dart b/test/groups/minor_tests.dart index 08d0527..ffde95f 100644 --- a/test/groups/minor_tests.dart +++ b/test/groups/minor_tests.dart @@ -10,9 +10,7 @@ void minorTests({ ApprovalLogger.log("$lines25 Group: Minor tests are starting $lines25"); }); - test( - 'Simulate file not found error during comparison. Must throw PathNotFoundException.', - () async { + test('Simulate file not found error during comparison. Must throw PathNotFoundException.', () async { const comparator = FileComparator(); // Setup: paths to non-existent files @@ -33,9 +31,7 @@ void minorTests({ ); }); - test( - 'Simulate file not found error during comparison. Must throw IDEComparatorException.', - () async { + test('Simulate file not found error during comparison. Must throw IDEComparatorException.', () async { const reporter = DiffReporter(); // Setup: paths to non-existent files @@ -91,8 +87,7 @@ void minorTests({ }); // if (Platform.isLinux) { - test('Verify string with DiffReporter. Must throw IDEComparatorException.', - () async { + test('Verify string with DiffReporter. Must throw IDEComparatorException.', () async { const reporter = DiffReporter( customDiffInfo: DiffInfo( command: '/usr/bin/code', @@ -102,10 +97,8 @@ void minorTests({ ); // Setup: paths to non-existent files - const existentApprovedPath = - 'test/approved_files/approval_test.verify.approved.txt'; - const existentReceivedPath = - 'test/approved_files/approval_test.verify.received.txt'; + const existentApprovedPath = 'test/approved_files/approval_test.verify.approved.txt'; + const existentReceivedPath = 'test/approved_files/approval_test.verify.received.txt'; // Expect an exception to be thrown expect( @@ -151,12 +144,11 @@ void minorTests({ }); test('returns correct file path', () { - const fakeStackTraceFetcher = FakeStackTraceFetcher( - 'file:///path/to/file.dart:10:11\nother stack trace lines...', + final fakeStackTraceFetcher = FakeStackTraceFetcher( + helper.fakeStackTracePath, ); - const filePathExtractor = - FilePathExtractor(stackTraceFetcher: fakeStackTraceFetcher); + final filePathExtractor = FilePathExtractor(stackTraceFetcher: fakeStackTraceFetcher); final filePath = filePathExtractor.filePath; expect(filePath, helper.testPath); @@ -170,8 +162,7 @@ void minorTests({ 'no file path in this stack trace\nother stack trace lines...', ); - const filePathExtractor = - FilePathExtractor(stackTraceFetcher: fakeStackTraceFetcher); + const filePathExtractor = FilePathExtractor(stackTraceFetcher: fakeStackTraceFetcher); expect( () => filePathExtractor.filePath, diff --git a/test/utils/helper.dart b/test/utils/helper.dart index a6cd99d..6777bc0 100644 --- a/test/utils/helper.dart +++ b/test/utils/helper.dart @@ -55,8 +55,7 @@ class ApprovalTestHelper { }) { Approvals.verifyAll( contents, - processor: (item) => item - .toString(), // Simple processor function that returns the item itself. + processor: (item) => item.toString(), // Simple processor function that returns the item itself. options: _getOptions( testName, expectException: expectException, @@ -167,9 +166,17 @@ class ApprovalTestHelper { scrubber: scrubber, ); + String get fakeStackTracePath { + if (Platform.isWindows) { + return 'file:///C:/path/to/file.dart:10:11\nother stack trace lines...'; + } else { + return 'file:///path/to/file.dart:10:11\nother stack trace lines...'; + } + } + String get testPath { if (Platform.isWindows) { - return '\\path\\to\\file.dart'; + return 'C:\\path\\to\\file.dart'; } else { return '/path/to/file.dart'; }