Skip to content

Commit

Permalink
. B file path extractor fix issue on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
yelmuratoff committed May 18, 2024
1 parent 14511d7 commit 4f7346c
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 35 deletions.
18 changes: 6 additions & 12 deletions lib/src/core/file_path_extractor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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]+)';
}
2 changes: 1 addition & 1 deletion lib/src/reporters/diff_tool/diff_tools.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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',
);
Expand Down
1 change: 1 addition & 0 deletions test/approval_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
2 changes: 1 addition & 1 deletion test/groups/diff_tools_tests.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ void diffToolsTests({
existentApprovedPath,
existentReceivedPath,
),
throwsA(isA<ProcessException>()),
Platform.isWindows ? returnsNormally : throwsA(isA<ProcessException>()),
);

ApprovalLogger.success(
Expand Down
27 changes: 9 additions & 18 deletions test/groups/minor_tests.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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',
Expand All @@ -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(
Expand Down Expand Up @@ -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);
Expand All @@ -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,
Expand Down
13 changes: 10 additions & 3 deletions test/utils/helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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';
}
Expand Down

0 comments on commit 4f7346c

Please sign in to comment.