Skip to content

Commit

Permalink
. B fix getting path and split for fileName
Browse files Browse the repository at this point in the history
  • Loading branch information
yelmuratoff committed May 18, 2024
1 parent 3451a54 commit d0a9860
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 36 deletions.
12 changes: 4 additions & 8 deletions lib/src/approvals.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
}

Expand Down Expand Up @@ -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,
);
}
}
Expand Down
10 changes: 6 additions & 4 deletions lib/src/core/file_path_extractor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
3 changes: 2 additions & 1 deletion lib/src/core/utils/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
22 changes: 9 additions & 13 deletions lib/src/namer/namer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,39 +17,31 @@ 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
String get approvedFileName {
if (options != null) {
return options!.approvedFileName;
}
return addTestName
? '$_fileName.$currentTestName.$approvedExtension'
: '$_fileName.$approvedExtension';
return addTestName ? '$_fileName.$currentTestName.$approvedExtension' : '$_fileName.$approvedExtension';
}

@override
String get received {
if (options != null) {
return options!.received;
}
return addTestName
? '$filePath.$currentTestName.$receivedExtension'
: '$filePath.$receivedExtension';
return addTestName ? '$filePath.$currentTestName.$receivedExtension' : '$filePath.$receivedExtension';
}

@override
String get receivedFileName {
if (options != null) {
return options!.receivedFileName;
}
return addTestName
? '$_fileName.$currentTestName.$receivedExtension'
: '$_fileName.$receivedExtension';
return addTestName ? '$_fileName.$currentTestName.$receivedExtension' : '$_fileName.$receivedExtension';
}

@override
Expand All @@ -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';

Expand Down
3 changes: 2 additions & 1 deletion lib/src/reporters/diff_tool/diff_tool_reporter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion lib/src/reporters/diff_tool/diff_tools.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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',
);
Expand Down
23 changes: 16 additions & 7 deletions test/groups/minor_tests.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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',
Expand All @@ -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(
Expand Down Expand Up @@ -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);
Expand All @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion test/utils/helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit d0a9860

Please sign in to comment.