Skip to content

Commit

Permalink
Test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
buijs-dev committed May 11, 2024
1 parent 24c5b58 commit bcde0e3
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
15 changes: 11 additions & 4 deletions lib/src/cli/task_project_create.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@ import "context.dart";
/// Task
class CreateProject extends Task {
/// Create new Task.
CreateProject({Executor? executor, GetFlutterSDK? getFlutterSDK})
: super(TaskName.create, {
CreateProject({Executor? executor,
GetFlutterSDK? getFlutterSDK,
AddLibrary? addLibrary,
ProjectInit? projectInit
}) : super(TaskName.create, {
TaskOption.name: const PluginNameOption(),
TaskOption.group: const GroupNameOption(),
TaskOption.flutter: FlutterVersionOption(),
Expand All @@ -40,10 +43,14 @@ class CreateProject extends Task {
}) {
_executor = executor ?? Executor();
_getFlutterSDK = getFlutterSDK ?? GetFlutterSDK();
_projectInit = projectInit ?? ProjectInit();
_addLibrary = addLibrary ?? AddLibrary();
}

late final Executor _executor;
late final GetFlutterSDK _getFlutterSDK;
late final ProjectInit _projectInit;
late final AddLibrary _addLibrary;

@override
Future<void> toBeExecuted(
Expand Down Expand Up @@ -124,7 +131,7 @@ class CreateProject extends Task {
TaskOption.flutter: flutterSDK,
});

final init = await ProjectInit().execute(initContext);
final init = await _projectInit.execute(initContext);
if (!init.isOk) {
throw KlutterException(init.message ?? "project init failed");
}
Expand All @@ -134,7 +141,7 @@ class CreateProject extends Task {
TaskOption.lib: name,
});

final addLibrary = await AddLibrary().execute(addContext);
final addLibrary = await _addLibrary.execute(addContext);
if (!addLibrary.isOk) {
throw KlutterException(
addLibrary.message ?? "adding library to example project failed");
Expand Down
30 changes: 30 additions & 0 deletions test/src/cli/task_project_create_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,22 @@ void main() {
expect(result.isOk, false);
expect(result.message, "BOOM!");
});

test("Verify an exception is thrown if project init fails", () async {
final projectInit = ExplodingInit();
final task = CreateProject(projectInit: projectInit);
final result = await task.execute(Context(Directory.systemTemp, {}));
expect(result.isOk, false);
expect(result.message, "BOOM!");
});

test("Verify an exception is thrown if add library fails", () async {
final addLibrary = ExplodingAddLibrary();
final task = CreateProject(addLibrary: addLibrary);
final result = await task.execute(Context(Directory.systemTemp, {}));
expect(result.isOk, false);
expect(result.message, "BOOM!");
});
}

class NoFlutterSDK extends GetFlutterSDK {
Expand All @@ -69,3 +85,17 @@ class NoFlutterSDK extends GetFlutterSDK {
return const TaskResult(isOk: false, message: "BOOM!");
}
}

class ExplodingInit extends ProjectInit {
@override
Future<TaskResult<void>> execute(Context context) async {
return const TaskResult(isOk: false, message: "BOOM!");
}
}

class ExplodingAddLibrary extends AddLibrary {
@override
Future<TaskResult<void>> execute(Context context) async {
return const TaskResult(isOk: false, message: "BOOM!");
}
}

0 comments on commit bcde0e3

Please sign in to comment.