Skip to content

Commit

Permalink
! F added new field to namer: useSubfolder to generate files in /appr…
Browse files Browse the repository at this point in the history
…ovals
  • Loading branch information
actions-user committed Feb 22, 2025
1 parent 934b935 commit 08263ba
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 60 deletions.
5 changes: 4 additions & 1 deletion example/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ void main() {
test("verify combinations", () {
Approvals.verifyAll(
[3, 5, 15],
options: const Options(
options: Options(
namer: Namer(
useSubfolder: true,
),
reporter: DiffReporter(),
),
processor: (items) => fizzBuzz(items).toString(),
Expand Down
26 changes: 12 additions & 14 deletions lib/src/approvals.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,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(
Expand All @@ -43,16 +42,18 @@ class Approvals {
Options options = const Options(),
}) {
// 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 = makeNamer(
completedPath,
description: options.namer?.description,
options: options.namer?.options,
addTestName: options.namer?.addTestName,
final namer = options.namer.copyWith(
filePath: completedPath,
);
// final namer = makeNamer(
// completedPath,
// description: options.namer?.description,
// options: options.namer?.options,
// addTestName: options.namer?.addTestName,
// );

try {
// Create writer object with scrubbed response and file extension retrieved from options
Expand All @@ -63,8 +64,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 @@ -109,9 +109,7 @@ class Approvals {
FileType.received: (ApprovalNamer n) => n.received,
};

final filePath = (namer == null)
? Namer(filePath: filePathExtractor.filePath.split('.dart').first)
: namer;
final filePath = (namer == null) ? Namer(filePath: filePathExtractor.filePath.split('.dart').first) : namer;

ApprovalUtils.deleteFile(fileToNamerMap[fileType]!(filePath));
}
Expand Down
7 changes: 3 additions & 4 deletions lib/src/core/options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Options {
final bool deleteReceivedFile;

/// A final variable `namer` of type `Namer` used to set the name and path of the file.
final Namer? namer;
final Namer namer;

/// A final bool variable `logErrors` used to determine if the errors should be logged.
final bool logErrors;
Expand All @@ -52,7 +52,7 @@ class Options {
this.comparator = const FileComparator(),
this.reporter = const CommandLineReporter(),
this.deleteReceivedFile = true,
this.namer,
this.namer = const Namer(),
this.logErrors = true,
this.logResults = true,
this.includeClassNameDuringSerialization = true,
Expand All @@ -79,7 +79,6 @@ class Options {
logErrors: logErrors ?? this.logErrors,
logResults: logResults ?? this.logResults,
includeClassNameDuringSerialization:
includeClassNameDuringSerialization ??
this.includeClassNameDuringSerialization,
includeClassNameDuringSerialization ?? this.includeClassNameDuringSerialization,
);
}
86 changes: 52 additions & 34 deletions lib/src/namer/namer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,79 +17,68 @@
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? filePath;
final FileNamerOptions? options;
final bool addTestName;
final String? description;
final bool useSubfolder;

const Namer({
this.filePath,
this.options,
this.addTestName = true,
this.description,
this.useSubfolder = false,
});

@override
String get approved {
if (options != null) {
return options!.approved;
}
if (options != null) return options!.approved;

if (description != null) {
return addTestName
? '$filePath.$currentTestName.$_updatedDescription.$approvedExtension'
: '$filePath.$_updatedDescription.$approvedExtension';
? '${_basePath}.$currentTestName.$_updatedDescription.$approvedExtension'
: '${_basePath}.$_updatedDescription.$approvedExtension';
}
return addTestName
? '$filePath.$currentTestName.$approvedExtension'
: '$filePath.$approvedExtension';
return addTestName ? '${_basePath}.$currentTestName.$approvedExtension' : '${_basePath}.$approvedExtension';
}

@override
String get approvedFileName {
if (options != null) {
return options!.approvedFileName;
}
if (options != null) return options!.approvedFileName;

if (description != null) {
return addTestName
? '$_fileName.$currentTestName.$_updatedDescription.$approvedExtension'
: '$_fileName.$_updatedDescription.$approvedExtension';
}
return addTestName
? '$_fileName.$currentTestName.$approvedExtension'
: '$_fileName.$approvedExtension';
return addTestName ? '$_fileName.$currentTestName.$approvedExtension' : '$_fileName.$approvedExtension';
}

@override
String get received {
if (options != null) {
return options!.received;
}
if (options != null) return options!.received;

if (description != null) {
return addTestName
? '$filePath.$currentTestName.$_updatedDescription.$receivedExtension'
: '$filePath.$_updatedDescription.$receivedExtension';
? '${_basePath}.$currentTestName.$_updatedDescription.$receivedExtension'
: '${_basePath}.$_updatedDescription.$receivedExtension';
}
return addTestName
? '$filePath.$currentTestName.$receivedExtension'
: '$filePath.$receivedExtension';
return addTestName ? '${_basePath}.$currentTestName.$receivedExtension' : '${_basePath}.$receivedExtension';
}

@override
String get receivedFileName {
if (options != null) {
return options!.receivedFileName;
}
if (options != null) return options!.receivedFileName;

if (description != null) {
return addTestName
? '$_fileName.$currentTestName.$_updatedDescription.$receivedExtension'
: '$_fileName.$_updatedDescription.$receivedExtension';
}
return addTestName
? '$_fileName.$currentTestName.$receivedExtension'
: '$_fileName.$receivedExtension';
return addTestName ? '$_fileName.$currentTestName.$receivedExtension' : '$_fileName.$receivedExtension';
}

@override
Expand All @@ -98,17 +87,46 @@ final class Namer implements ApprovalNamer {
return testName == null ? '' : testName.replaceAll(' ', '_').toLowerCase();
}

String get _updatedDescription => description == null
? ''
: description!.replaceAll(' ', '_').toLowerCase();
String get _updatedDescription => description == null ? '' : description!.replaceAll(' ', '_').toLowerCase();

/// Returns the path without extension.
/// If [useSubfolder] is `true`, appends `approval_tests` to the folder path.
String get _basePath {
final separator = Platform.isWindows ? '\\' : '/';
final idx = filePath!.lastIndexOf(separator);
final dir = idx < 0 ? '' : filePath!.substring(0, idx);
final name = filePath!.split(separator).last.split('.dart').first;
final baseDir = useSubfolder ? '$dir${separator}approvals' : dir;
return '$baseDir$separator$name';
}

/// Returns the file name part used in `approvedFileName` and `receivedFileName`.
String get _fileName {
final path = filePath!;
final separator = Platform.isWindows ? '\\' : '/';
return path.split(separator).last.split('.dart').first;
final name = filePath!.split(separator).last.split('.dart').first;
final dirIdx = filePath!.lastIndexOf(separator);
if (dirIdx < 0) return name;
final dir = filePath!.substring(0, dirIdx);
final baseDir = useSubfolder ? '$dir${separator}approvals' : dir;
return '$baseDir$separator$name';
}

static const String approvedExtension = 'approved.txt';

static const String receivedExtension = 'received.txt';

Namer copyWith({
String? filePath,
FileNamerOptions? options,
bool? addTestName,
String? description,
bool? useSubfolder,
}) {
return Namer(
filePath: filePath ?? this.filePath,
options: options ?? this.options,
addTestName: addTestName ?? this.addTestName,
description: description ?? this.description,
useSubfolder: useSubfolder ?? this.useSubfolder,
);
}
}
11 changes: 4 additions & 7 deletions test/utils/helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,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 All @@ -85,8 +84,7 @@ class ApprovalTestHelper {
expectException: expectException,
approveResult: approveResult,
deleteReceivedFile: deleteReceivedFile,
includeClassNameDuringSerialization:
includeClassNameDuringSerialization,
includeClassNameDuringSerialization: includeClassNameDuringSerialization,
),
);
}
Expand Down Expand Up @@ -167,14 +165,13 @@ class ApprovalTestHelper {
description: description,
),
)
: null,
: Namer(),
deleteReceivedFile: deleteReceivedFile,
approveResult: approveResult,
logErrors: !expectException,
reporter: reporter,
scrubber: scrubber,
includeClassNameDuringSerialization:
includeClassNameDuringSerialization,
includeClassNameDuringSerialization: includeClassNameDuringSerialization,
);

String get fakeStackTracePath {
Expand Down

0 comments on commit 08263ba

Please sign in to comment.