diff --git a/lib/src/approvals.dart b/lib/src/approvals.dart index 2d12f6a..18ef435 100644 --- a/lib/src/approvals.dart +++ b/lib/src/approvals.dart @@ -2,8 +2,7 @@ part of '../approval_tests.dart'; /// `Approvals` is a class that provides methods to verify the content of a response. class Approvals { - static const FilePathExtractor filePathExtractor = - FilePathExtractor(stackTraceFetcher: StackTraceFetcher()); + static const FilePathExtractor filePathExtractor = FilePathExtractor(stackTraceFetcher: StackTraceFetcher()); // Factory method to create an instance of ApprovalNamer with given file name static ApprovalNamer makeNamer(String filePath) => Namer(filePath: filePath); @@ -17,8 +16,7 @@ class Approvals { }) { try { // Get the file path without extension or use the provided file path - final completedPath = options.namer?.filePath ?? - filePathExtractor.filePath.split('.dart').first; + final completedPath = options.namer?.filePath ?? filePathExtractor.filePath.split('.dart').first; // Create namer object with given or computed file name final namer = options.namer ?? makeNamer(completedPath); @@ -31,8 +29,7 @@ class Approvals { // Write the content to a file whose path is specified in namer.received writer.writeToFile(namer.received); - if (options.approveResult || - !ApprovalUtils.isFileExists(namer.approved)) { + if (options.approveResult || !ApprovalUtils.isFileExists(namer.approved)) { writer.writeToFile(namer.approved); } @@ -74,8 +71,7 @@ class Approvals { ApprovalUtils.deleteFile(options.namer!.received); } else { ApprovalUtils.deleteFile( - Namer(filePath: filePathExtractor.filePath.split('.dart').first) - .received, + Namer(filePath: filePathExtractor.filePath.split('.dart').first).received, ); } } diff --git a/lib/src/core/file_path_extractor.dart b/lib/src/core/file_path_extractor.dart index 1f2261e..99e8109 100644 --- a/lib/src/core/file_path_extractor.dart +++ b/lib/src/core/file_path_extractor.dart @@ -10,13 +10,15 @@ class FilePathExtractor { String get filePath { try { final stackTraceString = _stackTraceFetcher.currentStackTrace; - final uriRegExp = RegExp(r'file:\/\/\/([^\s:]+)'); - + // ApprovalLogger.log('Stack trace: $stackTraceString'); + //final uriRegExp = RegExp(r'file:\/\/\/([^\s:]+)'); + final uriRegExp = RegExp(r'file://(/[a-zA-Z]:[^\s]*)'); final match = uriRegExp.firstMatch(stackTraceString); if (match != null) { - final filePath = Uri.tryParse('file:///${match.group(1)!}'); - return filePath!.toFilePath(); + final rawPath = match.group(1)!.replaceAll(RegExp(r':\d+:\d+\)$'), ''); + final filePath = Uri.parse('file://$rawPath'); + return filePath.toFilePath(windows: Platform.isWindows); } else { throw FileNotFoundException( message: 'File not found in stack trace', diff --git a/lib/src/core/utils/utils.dart b/lib/src/core/utils/utils.dart index 075ef41..c28932e 100644 --- a/lib/src/core/utils/utils.dart +++ b/lib/src/core/utils/utils.dart @@ -7,7 +7,8 @@ final class ApprovalUtils { final int green = int.parse(hex.substring(2, 4), radix: 16); final int blue = int.parse(hex.substring(4, 6), radix: 16); - final AnsiPen pen = AnsiPen()..rgb(r: red / 255, g: green / 255, b: blue / 255); + final AnsiPen pen = AnsiPen() + ..rgb(r: red / 255, g: green / 255, b: blue / 255); return pen; } diff --git a/lib/src/namer/namer.dart b/lib/src/namer/namer.dart index 4cbbc1e..2b23a7d 100644 --- a/lib/src/namer/namer.dart +++ b/lib/src/namer/namer.dart @@ -17,9 +17,7 @@ final class Namer implements ApprovalNamer { if (options != null) { return options!.approved; } - return addTestName - ? '$filePath.$currentTestName.$approvedExtension' - : '$filePath.$approvedExtension'; + return addTestName ? '$filePath.$currentTestName.$approvedExtension' : '$filePath.$approvedExtension'; } @override @@ -27,9 +25,7 @@ final class Namer implements ApprovalNamer { if (options != null) { return options!.approvedFileName; } - return addTestName - ? '$_fileName.$currentTestName.$approvedExtension' - : '$_fileName.$approvedExtension'; + return addTestName ? '$_fileName.$currentTestName.$approvedExtension' : '$_fileName.$approvedExtension'; } @override @@ -37,9 +33,7 @@ final class Namer implements ApprovalNamer { if (options != null) { return options!.received; } - return addTestName - ? '$filePath.$currentTestName.$receivedExtension' - : '$filePath.$receivedExtension'; + return addTestName ? '$filePath.$currentTestName.$receivedExtension' : '$filePath.$receivedExtension'; } @override @@ -47,9 +41,7 @@ final class Namer implements ApprovalNamer { if (options != null) { return options!.receivedFileName; } - return addTestName - ? '$_fileName.$currentTestName.$receivedExtension' - : '$_fileName.$receivedExtension'; + return addTestName ? '$_fileName.$currentTestName.$receivedExtension' : '$_fileName.$receivedExtension'; } @override @@ -58,7 +50,11 @@ final class Namer implements ApprovalNamer { return testName == null ? '' : testName.replaceAll(' ', '_'); } - String get _fileName => filePath!.split('/').last.split('.dart').first; + String get _fileName { + final path = filePath!; + final separator = Platform.isWindows ? '\\' : '/'; + return path.split(separator).last.split('.dart').first; + } static const String approvedExtension = 'approved.txt'; diff --git a/lib/src/reporters/diff_tool/diff_tool_reporter.dart b/lib/src/reporters/diff_tool/diff_tool_reporter.dart index efdb1ce..9f89413 100644 --- a/lib/src/reporters/diff_tool/diff_tool_reporter.dart +++ b/lib/src/reporters/diff_tool/diff_tool_reporter.dart @@ -32,7 +32,8 @@ class DiffReporter implements Reporter { rethrow; } if (e is ProcessException) { - final ProcessResult result = await Process.run(ApprovalUtils.commandWhere, [diffInfo.command]); + final ProcessResult result = + await Process.run(ApprovalUtils.commandWhere, [diffInfo.command]); ApprovalLogger.exception( 'Error during comparison via ${ide.name}. Please try check path of IDE. \n Current path: ${diffInfo.command} with arg: "${diffInfo.arg}" \n Path to IDE (${Platform.operatingSystem}): ${result.stdout} \n Please, add path to customDiffInfo.', stackTrace: st, diff --git a/lib/src/reporters/diff_tool/diff_tools.dart b/lib/src/reporters/diff_tool/diff_tools.dart index 136f90f..02489b7 100644 --- a/lib/src/reporters/diff_tool/diff_tools.dart +++ b/lib/src/reporters/diff_tool/diff_tools.dart @@ -3,7 +3,8 @@ part of '../../../../approval_tests.dart'; /// `MacDiffTools` contains diff tools available on macOS. final class MacDiffTools { static const DiffInfo visualStudioCode = DiffInfo( - command: '/Applications/Visual Studio Code.app/Contents/Resources/app/bin/code', + command: + '/Applications/Visual Studio Code.app/Contents/Resources/app/bin/code', arg: '-d', name: 'code', ); diff --git a/test/groups/minor_tests.dart b/test/groups/minor_tests.dart index 672ec58..08d0527 100644 --- a/test/groups/minor_tests.dart +++ b/test/groups/minor_tests.dart @@ -10,7 +10,9 @@ 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 @@ -31,7 +33,9 @@ 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 @@ -87,7 +91,8 @@ 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', @@ -97,8 +102,10 @@ 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( @@ -148,7 +155,8 @@ void minorTests({ 'file:///path/to/file.dart:10:11\nother stack trace lines...', ); - const filePathExtractor = FilePathExtractor(stackTraceFetcher: fakeStackTraceFetcher); + const filePathExtractor = + FilePathExtractor(stackTraceFetcher: fakeStackTraceFetcher); final filePath = filePathExtractor.filePath; expect(filePath, helper.testPath); @@ -162,7 +170,8 @@ 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 60d68fa..a6cd99d 100644 --- a/test/utils/helper.dart +++ b/test/utils/helper.dart @@ -55,7 +55,8 @@ 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,