Skip to content

Commit d0a9860

Browse files
committed
. B fix getting path and split for fileName
1 parent 3451a54 commit d0a9860

File tree

8 files changed

+43
-36
lines changed

8 files changed

+43
-36
lines changed

lib/src/approvals.dart

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ part of '../approval_tests.dart';
22

33
/// `Approvals` is a class that provides methods to verify the content of a response.
44
class Approvals {
5-
static const FilePathExtractor filePathExtractor =
6-
FilePathExtractor(stackTraceFetcher: StackTraceFetcher());
5+
static const FilePathExtractor filePathExtractor = FilePathExtractor(stackTraceFetcher: StackTraceFetcher());
76

87
// Factory method to create an instance of ApprovalNamer with given file name
98
static ApprovalNamer makeNamer(String filePath) => Namer(filePath: filePath);
@@ -17,8 +16,7 @@ class Approvals {
1716
}) {
1817
try {
1918
// Get the file path without extension or use the provided file path
20-
final completedPath = options.namer?.filePath ??
21-
filePathExtractor.filePath.split('.dart').first;
19+
final completedPath = options.namer?.filePath ?? filePathExtractor.filePath.split('.dart').first;
2220

2321
// Create namer object with given or computed file name
2422
final namer = options.namer ?? makeNamer(completedPath);
@@ -31,8 +29,7 @@ class Approvals {
3129
// Write the content to a file whose path is specified in namer.received
3230
writer.writeToFile(namer.received);
3331

34-
if (options.approveResult ||
35-
!ApprovalUtils.isFileExists(namer.approved)) {
32+
if (options.approveResult || !ApprovalUtils.isFileExists(namer.approved)) {
3633
writer.writeToFile(namer.approved);
3734
}
3835

@@ -74,8 +71,7 @@ class Approvals {
7471
ApprovalUtils.deleteFile(options.namer!.received);
7572
} else {
7673
ApprovalUtils.deleteFile(
77-
Namer(filePath: filePathExtractor.filePath.split('.dart').first)
78-
.received,
74+
Namer(filePath: filePathExtractor.filePath.split('.dart').first).received,
7975
);
8076
}
8177
}

lib/src/core/file_path_extractor.dart

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ class FilePathExtractor {
1010
String get filePath {
1111
try {
1212
final stackTraceString = _stackTraceFetcher.currentStackTrace;
13-
final uriRegExp = RegExp(r'file:\/\/\/([^\s:]+)');
14-
13+
// ApprovalLogger.log('Stack trace: $stackTraceString');
14+
//final uriRegExp = RegExp(r'file:\/\/\/([^\s:]+)');
15+
final uriRegExp = RegExp(r'file://(/[a-zA-Z]:[^\s]*)');
1516
final match = uriRegExp.firstMatch(stackTraceString);
1617

1718
if (match != null) {
18-
final filePath = Uri.tryParse('file:///${match.group(1)!}');
19-
return filePath!.toFilePath();
19+
final rawPath = match.group(1)!.replaceAll(RegExp(r':\d+:\d+\)$'), '');
20+
final filePath = Uri.parse('file://$rawPath');
21+
return filePath.toFilePath(windows: Platform.isWindows);
2022
} else {
2123
throw FileNotFoundException(
2224
message: 'File not found in stack trace',

lib/src/core/utils/utils.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ final class ApprovalUtils {
77
final int green = int.parse(hex.substring(2, 4), radix: 16);
88
final int blue = int.parse(hex.substring(4, 6), radix: 16);
99

10-
final AnsiPen pen = AnsiPen()..rgb(r: red / 255, g: green / 255, b: blue / 255);
10+
final AnsiPen pen = AnsiPen()
11+
..rgb(r: red / 255, g: green / 255, b: blue / 255);
1112
return pen;
1213
}
1314

lib/src/namer/namer.dart

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,39 +17,31 @@ final class Namer implements ApprovalNamer {
1717
if (options != null) {
1818
return options!.approved;
1919
}
20-
return addTestName
21-
? '$filePath.$currentTestName.$approvedExtension'
22-
: '$filePath.$approvedExtension';
20+
return addTestName ? '$filePath.$currentTestName.$approvedExtension' : '$filePath.$approvedExtension';
2321
}
2422

2523
@override
2624
String get approvedFileName {
2725
if (options != null) {
2826
return options!.approvedFileName;
2927
}
30-
return addTestName
31-
? '$_fileName.$currentTestName.$approvedExtension'
32-
: '$_fileName.$approvedExtension';
28+
return addTestName ? '$_fileName.$currentTestName.$approvedExtension' : '$_fileName.$approvedExtension';
3329
}
3430

3531
@override
3632
String get received {
3733
if (options != null) {
3834
return options!.received;
3935
}
40-
return addTestName
41-
? '$filePath.$currentTestName.$receivedExtension'
42-
: '$filePath.$receivedExtension';
36+
return addTestName ? '$filePath.$currentTestName.$receivedExtension' : '$filePath.$receivedExtension';
4337
}
4438

4539
@override
4640
String get receivedFileName {
4741
if (options != null) {
4842
return options!.receivedFileName;
4943
}
50-
return addTestName
51-
? '$_fileName.$currentTestName.$receivedExtension'
52-
: '$_fileName.$receivedExtension';
44+
return addTestName ? '$_fileName.$currentTestName.$receivedExtension' : '$_fileName.$receivedExtension';
5345
}
5446

5547
@override
@@ -58,7 +50,11 @@ final class Namer implements ApprovalNamer {
5850
return testName == null ? '' : testName.replaceAll(' ', '_');
5951
}
6052

61-
String get _fileName => filePath!.split('/').last.split('.dart').first;
53+
String get _fileName {
54+
final path = filePath!;
55+
final separator = Platform.isWindows ? '\\' : '/';
56+
return path.split(separator).last.split('.dart').first;
57+
}
6258

6359
static const String approvedExtension = 'approved.txt';
6460

lib/src/reporters/diff_tool/diff_tool_reporter.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ class DiffReporter implements Reporter {
3232
rethrow;
3333
}
3434
if (e is ProcessException) {
35-
final ProcessResult result = await Process.run(ApprovalUtils.commandWhere, [diffInfo.command]);
35+
final ProcessResult result =
36+
await Process.run(ApprovalUtils.commandWhere, [diffInfo.command]);
3637
ApprovalLogger.exception(
3738
'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.',
3839
stackTrace: st,

lib/src/reporters/diff_tool/diff_tools.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ part of '../../../../approval_tests.dart';
33
/// `MacDiffTools` contains diff tools available on macOS.
44
final class MacDiffTools {
55
static const DiffInfo visualStudioCode = DiffInfo(
6-
command: '/Applications/Visual Studio Code.app/Contents/Resources/app/bin/code',
6+
command:
7+
'/Applications/Visual Studio Code.app/Contents/Resources/app/bin/code',
78
arg: '-d',
89
name: 'code',
910
);

test/groups/minor_tests.dart

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ void minorTests({
1010
ApprovalLogger.log("$lines25 Group: Minor tests are starting $lines25");
1111
});
1212

13-
test('Simulate file not found error during comparison. Must throw PathNotFoundException.', () async {
13+
test(
14+
'Simulate file not found error during comparison. Must throw PathNotFoundException.',
15+
() async {
1416
const comparator = FileComparator();
1517

1618
// Setup: paths to non-existent files
@@ -31,7 +33,9 @@ void minorTests({
3133
);
3234
});
3335

34-
test('Simulate file not found error during comparison. Must throw IDEComparatorException.', () async {
36+
test(
37+
'Simulate file not found error during comparison. Must throw IDEComparatorException.',
38+
() async {
3539
const reporter = DiffReporter();
3640

3741
// Setup: paths to non-existent files
@@ -87,7 +91,8 @@ void minorTests({
8791
});
8892

8993
// if (Platform.isLinux) {
90-
test('Verify string with DiffReporter. Must throw IDEComparatorException.', () async {
94+
test('Verify string with DiffReporter. Must throw IDEComparatorException.',
95+
() async {
9196
const reporter = DiffReporter(
9297
customDiffInfo: DiffInfo(
9398
command: '/usr/bin/code',
@@ -97,8 +102,10 @@ void minorTests({
97102
);
98103

99104
// Setup: paths to non-existent files
100-
const existentApprovedPath = 'test/approved_files/approval_test.verify.approved.txt';
101-
const existentReceivedPath = 'test/approved_files/approval_test.verify.received.txt';
105+
const existentApprovedPath =
106+
'test/approved_files/approval_test.verify.approved.txt';
107+
const existentReceivedPath =
108+
'test/approved_files/approval_test.verify.received.txt';
102109

103110
// Expect an exception to be thrown
104111
expect(
@@ -148,7 +155,8 @@ void minorTests({
148155
'file:///path/to/file.dart:10:11\nother stack trace lines...',
149156
);
150157

151-
const filePathExtractor = FilePathExtractor(stackTraceFetcher: fakeStackTraceFetcher);
158+
const filePathExtractor =
159+
FilePathExtractor(stackTraceFetcher: fakeStackTraceFetcher);
152160
final filePath = filePathExtractor.filePath;
153161

154162
expect(filePath, helper.testPath);
@@ -162,7 +170,8 @@ void minorTests({
162170
'no file path in this stack trace\nother stack trace lines...',
163171
);
164172

165-
const filePathExtractor = FilePathExtractor(stackTraceFetcher: fakeStackTraceFetcher);
173+
const filePathExtractor =
174+
FilePathExtractor(stackTraceFetcher: fakeStackTraceFetcher);
166175

167176
expect(
168177
() => filePathExtractor.filePath,

test/utils/helper.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ class ApprovalTestHelper {
5555
}) {
5656
Approvals.verifyAll(
5757
contents,
58-
processor: (item) => item.toString(), // Simple processor function that returns the item itself.
58+
processor: (item) => item
59+
.toString(), // Simple processor function that returns the item itself.
5960
options: _getOptions(
6061
testName,
6162
expectException: expectException,

0 commit comments

Comments
 (0)