Skip to content

Commit

Permalink
. r PathNotFoundException was removed because it is impossible
Browse files Browse the repository at this point in the history
  • Loading branch information
yelmuratoff committed May 16, 2024
1 parent 22ecb11 commit 364a1dd
Show file tree
Hide file tree
Showing 24 changed files with 207 additions and 150 deletions.
1 change: 1 addition & 0 deletions lib/approval_tests.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ part 'src/core/extensions/approval_string_extensions.dart';

part 'src/namer/namer.dart';
part 'src/core/approval_number.dart';
part 'src/namer/file_namer_options.dart';

part 'src/comparator/comparator.dart';
part 'src/comparator/command_line_comparator.dart';
Expand Down
14 changes: 7 additions & 7 deletions lib/src/approvals.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ part of '../approval_tests.dart';
/// `Approvals` is a class that provides methods to verify the content of a response.
class Approvals {
// Factory method to create an instance of ApprovalNamer with given file name
static ApprovalNamer makeNamer(String file) => Namer(file);
static ApprovalNamer makeNamer(String filePath) => Namer(filePath: filePath);

// ================== Verify methods ==================

Expand All @@ -14,10 +14,10 @@ class Approvals {
}) {
try {
// Get the file path without extension or use the provided file path
final completedPath = options.filesPath ?? ApprovalUtils.filePath.split('.dart').first;
final completedPath = options.namer?.filePath ?? ApprovalUtils.filePath.split('.dart').first;

// Create namer object with given or computed file name
final namer = options.namer ?? makeNamer(options.filesPath ?? completedPath);
final namer = options.namer ?? makeNamer(completedPath);

// Create writer object with scrubbed response and file extension retrieved from options
final writer = ApprovalTextWriter(
Expand All @@ -27,7 +27,7 @@ class Approvals {
// Write the content to a file whose path is specified in namer.received
writer.writeToFile(namer.received);

if (options.approveResult) {
if (options.approveResult || !ApprovalUtils.isFileExists(namer.approved)) {
writer.writeToFile(namer.approved);
}

Expand Down Expand Up @@ -65,11 +65,11 @@ class Approvals {
/// `_deleteFileAfterTest` method to delete the received file after the test.
static void _deleteFileAfterTest(Options options) {
if (options.deleteReceivedFile) {
if (options.filesPath != null) {
ApprovalUtils.deleteFile(Namer(options.filesPath!).received);
if (options.namer != null) {
ApprovalUtils.deleteFile(options.namer!.received);
} else {
ApprovalUtils.deleteFile(
Namer(ApprovalUtils.filePath.split('.dart').first).received,
Namer(filePath: ApprovalUtils.filePath.split('.dart').first).received,
);
}
}
Expand Down
4 changes: 0 additions & 4 deletions lib/src/core/options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ class Options {
/// A final bool variable `approveResult` used to determine if the result should be approved after the test.
final bool approveResult;

/// Path to the files: `approved` and `received`.
final String? filesPath;

/// A final bool variable `deleteReceivedFile` used to determine if the received file should be deleted after passed test.
final bool deleteReceivedFile;

Expand All @@ -31,7 +28,6 @@ class Options {
this.scrubber = const ScrubNothing(),
this.approveResult = false,
this.comparator = const CommandLineComparator(),
this.filesPath,
this.deleteReceivedFile = false,
this.namer,
this.logErrors = true,
Expand Down
17 changes: 8 additions & 9 deletions lib/src/core/utils/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ 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 All @@ -34,7 +33,6 @@ final class ApprovalUtils {

if (match != null) {
final filePath = Uri.tryParse(match.group(0)!);
ApprovalLogger.log('Running test: $filePath');
return filePath!.toFilePath();
} else {
throw Exception('Could not find file path');
Expand All @@ -48,18 +46,19 @@ final class ApprovalUtils {
return file.readAsStringSync().trim();
}

static bool isFileExists(String path) {
final File file = File(path);
return file.existsSync();
}

static String lines(int count) => List.filled(count, '=').join();

// Helper private method to check if contents of two files match
static bool filesMatch(String approvedPath, String receivedPath) {
try {
// Read contents of the approved and received files
final approved = ApprovalUtils.readFile(path: approvedPath)
.replaceAll('\r\n', '\n')
.trim();
final received = ApprovalUtils.readFile(path: receivedPath)
.replaceAll('\r\n', '\n')
.trim();
final approved = ApprovalUtils.readFile(path: approvedPath).replaceAll('\r\n', '\n').trim();
final received = ApprovalUtils.readFile(path: receivedPath).replaceAll('\r\n', '\n').trim();

// Return true if contents of both files match exactly
return approved.compareTo(received) == 0;
Expand Down
21 changes: 21 additions & 0 deletions lib/src/namer/file_namer_options.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
part of '../../approval_tests.dart';

final class FileNamerOptions {
final String folderPath;
final String fileName;
final String testName;

const FileNamerOptions({
required this.folderPath,
required this.fileName,
required this.testName,
});

String get approved => '$folderPath/$approvedFileName';

String get received => '$folderPath/$receivedFileName';

String get approvedFileName => '$fileName.$testName.approved.txt';

String get receivedFileName => '$fileName.$testName.received.txt';
}
44 changes: 38 additions & 6 deletions lib/src/namer/namer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,57 @@ part of '../../approval_tests.dart';

/// `Namer` class is used to generate the file names for the approved and received files.
final class Namer implements ApprovalNamer {
final String file;
final String? filePath;
final FileNamerOptions? options;
final bool addTestName;

const Namer(this.file);
const Namer({
this.filePath,
this.options,
this.addTestName = true,
});

@override
String get approved => '$file.$currentTestName.approved.txt';
String get approved {
if (options != null) {
return options!.approved;
}
return addTestName ? '$filePath.$currentTestName.$approvedExtension' : '$filePath.$approvedExtension';
}

@override
String get approvedFileName => '${file.split('/').last.split('.dart').first}.approved.txt';
String get approvedFileName {
if (options != null) {
return options!.approvedFileName;
}
return addTestName ? '$_fileName.$currentTestName.$approvedExtension' : '$_fileName.$approvedExtension';
}

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

@override
String get receivedFileName => '${file.split('/').last.split('.dart').first}.received.txt';
String get receivedFileName {
if (options != null) {
return options!.receivedFileName;
}
return addTestName ? '$_fileName.$currentTestName.$receivedExtension' : '$_fileName.$receivedExtension';
}

@override
String get currentTestName {
final testName = Invoker.current?.liveTest.individualName;
return testName == null ? '' : testName.replaceAll(' ', '_');
}

String get _fileName => filePath!.split('/').last.split('.dart').first;

static const String approvedExtension = 'approved.txt';

static const String receivedExtension = 'received.txt';
}
2 changes: 1 addition & 1 deletion lib/src/scrubbers/reg_exp_scrubber.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ScrubWithRegEx implements ApprovalScrubber {
String scrub(String input) => input
.replacingOccurrences(
matchingPattern: pattern,
replacementProvider: replacementFunction ?? (match) => '',
replacementProvider: replacementFunction ?? (match) => ' ',
)
.trim();

Expand Down
Loading

0 comments on commit 364a1dd

Please sign in to comment.