Skip to content

Commit

Permalink
WIP release 3.0.0:
Browse files Browse the repository at this point in the history
- Code formatting
- Add debugging for test failing in ci
  • Loading branch information
buijs-dev committed May 4, 2024
1 parent 4bc04b5 commit ea07864
Show file tree
Hide file tree
Showing 18 changed files with 216 additions and 184 deletions.
52 changes: 37 additions & 15 deletions test/src/cli/context_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,18 @@ void main() {
});

test("Verify parsing get flutter context", () {
final context = toContextOrNull(Directory.current, ["get", "flutter=3.10.6.macos.x64"]);
final context =
toContextOrNull(Directory.current, ["get", "flutter=3.10.6.macos.x64"]);

expect(context != null, true);
});

test("Verify parsing producer init context", () {
final context = toContextOrNull(Directory.current, ["init", "bom=2023.1.1.beta", "flutter=3.10.6.macos.arm64",]);
final context = toContextOrNull(Directory.current, [
"init",
"bom=2023.1.1.beta",
"flutter=3.10.6.macos.arm64",
]);

expect(context != null, true);
expect(context!.taskName, TaskName.init);
Expand All @@ -49,33 +54,48 @@ void main() {
});

test("When more than 2 arguments are supplied then context is null", () {
final context = toContextOrNull(Directory.current, ["init", "bom=2023.1.1.beta", "flutter=3.10.6.macos.arm64", "x=y"]);
final context = toContextOrNull(Directory.current,
["init", "bom=2023.1.1.beta", "flutter=3.10.6.macos.arm64", "x=y"]);

expect(context == null, true, reason: "context should be null because too many arguments");
expect(context == null, true,
reason: "context should be null because too many arguments");
});

test("When an argument is not a valid option value then context is null", () {
final context = toContextOrNull(Directory.current, ["init", "woot=2023.1.1.beta", "flutter=3.10.6.macos.arm64",]);

expect(context == null, true, reason: "context should be null because argument is invalid");
final context = toContextOrNull(Directory.current, [
"init",
"woot=2023.1.1.beta",
"flutter=3.10.6.macos.arm64",
]);

expect(context == null, true,
reason: "context should be null because argument is invalid");
});

test("When get context has more than 1 argument, context is null", () {
final context = toContextOrNull(Directory.current, ["get", "this", "and" ]);
final context = toContextOrNull(Directory.current, ["get", "this", "and"]);

expect(context == null, true, reason: "context should be null because get can only have one argument");
expect(context == null, true,
reason:
"context should be null because get can only have one argument");
});

for (final value in ["3.10=6=10", "'3.10=6=10'", '"3.10=6=10"', " 3.10 "]) {
for (final value in [
"3.10=6=10",
"'3.10=6=10'",
'"3.10=6=10"',
" 3.10 "
]) {
test("Verify parsing options", () {
final options = toTaskOptionsOrNull(["flutter=$value"]);
expect(options != null, true, reason: "options should be parsed successfully");
expect(options != null, true,
reason: "options should be parsed successfully");
final flutter = options![TaskOption.flutter];
expect(flutter, value.trim(), reason: "value should be stored");
});
}

for (final value in ["", " ","=", "= ", " = "]) {
for (final value in ["", " ", "=", "= ", " = "]) {
test("Verify parsing options fails on missing value", () {
expect(toTaskOptionsOrNull(["flutter$value"]), null);
});
Expand Down Expand Up @@ -104,14 +124,16 @@ void main() {

test("Verify copyWith merges options maps", () {
final map1 = {TaskOption.bom: "da-bom", TaskOption.name: "name"};
final map2 = {TaskOption.bom: "not-da-bom", TaskOption.klutterui:"union"};
final context = Context(workingDirectory: Directory(""), taskName: TaskName.build, taskOptions: map1);
final map2 = {TaskOption.bom: "not-da-bom", TaskOption.klutterui: "union"};
final context = Context(
workingDirectory: Directory(""),
taskName: TaskName.build,
taskOptions: map1);
final copied = context.copyWith(taskOptions: map2);
final copiedMap = copied.taskOptions;
expect(copiedMap.length, 3);
expect(copiedMap[TaskOption.bom], map2[TaskOption.bom]);
expect(copiedMap[TaskOption.name], map1[TaskOption.name]);
expect(copiedMap[TaskOption.klutterui], map2[TaskOption.klutterui]);
});

}
6 changes: 3 additions & 3 deletions test/src/cli/task_add_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ import "package:test/test.dart";

void main() {
test("ConsumerAdd fails when option is not set", () async {
final result = await AddLibrary()
.execute(Context(
final result = await AddLibrary().execute(Context(
taskName: TaskName.add,
workingDirectory: Directory.current,
taskOptions: <TaskOption, String>{}));
expect(result.isOk, false);
expect(result.message, "unable to run task add because: [missing value for option: lib]");
expect(result.message,
"unable to run task add because: [missing value for option: lib]");
});
}
36 changes: 16 additions & 20 deletions test/src/cli/task_get_flutter_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,43 +25,39 @@ import "package:klutter/src/cli/context.dart";
import "package:test/test.dart";

void main() {

test("GetFlutterSDK downloads latest compatible if version is not set", () async {
test("GetFlutterSDK downloads latest compatible if version is not set",
() async {
final task = GetFlutterSDK();
final result = await task.execute(context({
TaskOption.dryRun: "true"
}));
final result = await task.execute(context({TaskOption.dryRun: "true"}));
expect(result.isOk, true);
});

test("GetFlutterSDK fails when Flutter SDK is incorrect", () async {
final result = await GetFlutterSDK().execute(context({
TaskOption.flutter : "0.0.0",
TaskOption.dryRun : "true"
}));
final result = await GetFlutterSDK().execute(
context({TaskOption.flutter: "0.0.0", TaskOption.dryRun: "true"}));

expect(result.isOk, false);
expect(result.message, "unable to run task get because: [invalid value for option flutter: invalid flutter version (supported versions are: (3.0.5, 3.3.10, 3.7.12, 3.10.6)): 0.0.0]");
expect(result.message,
"unable to run task get because: [invalid value for option flutter: invalid flutter version (supported versions are: (3.0.5, 3.3.10, 3.7.12, 3.10.6)): 0.0.0]");
});

test("GetFlutterSDK uses OS from version if present in version String", () async {
test("GetFlutterSDK uses OS from version if present in version String",
() async {
final task = GetFlutterSDK();
final root = Directory.systemTemp;
root.resolveFile("kradle.env")
..createSync()
..writeAsStringSync("cache=${root.absolutePath}");
final result = await task.execute(context({
TaskOption.flutter : "3.3.10.linux.x64",
TaskOption.dryRun : "true",
TaskOption.root : root.absolutePath,
TaskOption.flutter: "3.3.10.linux.x64",
TaskOption.dryRun: "true",
TaskOption.root: root.absolutePath,
}));
expect(result.isOk, true, reason: result.message ?? "");
});

}

Context context(Map<TaskOption, String> options) =>
Context(
taskName: TaskName.get,
workingDirectory: Directory.current,
taskOptions: options);
Context context(Map<TaskOption, String> options) => Context(
taskName: TaskName.get,
workingDirectory: Directory.current,
taskOptions: options);
14 changes: 7 additions & 7 deletions test/src/cli/task_project_build_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ import "package:test/test.dart";
import "../common/executor_test.dart";

void main() {


test("Verify build command executes gradlew clean build", () {
final pathToRoot =
Directory("${Directory.systemTemp.absolute.path}/build_test".normalize)
Expand All @@ -38,11 +36,13 @@ void main() {
expectedPathToWorkingDirectory: pathToRoot.absolutePath,
expectedCommand: "gradlew clean build -p platform");

BuildProject(executor: executor).toBeExecuted(Context(
workingDirectory: pathToRoot,
taskName: TaskName.build,
taskOptions: { },
), {});
BuildProject(executor: executor).toBeExecuted(
Context(
workingDirectory: pathToRoot,
taskName: TaskName.build,
taskOptions: {},
),
{});

expect(executor.run().stdout, "Test OK!");
});
Expand Down
40 changes: 17 additions & 23 deletions test/src/cli/task_project_init_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import "package:klutter/src/cli/context.dart";
import "package:test/test.dart";

void main() {

test("Verify init in consumer project skips producer init", () async {
final pathToRoot =
Directory("${Directory.systemTemp.absolute.path}/build_test".normalize)
Expand All @@ -39,30 +38,25 @@ void main() {
group: "my.org",
);

final pathToConsumer = project.resolveFolder("example");
final task = ProjectInit();
final context = Context(workingDirectory: project.resolveFolder("example"),
taskName: TaskName.init,
taskOptions: {});
final context = Context(
workingDirectory: pathToConsumer,
taskName: TaskName.init,
taskOptions: {},
);

final result = await task.execute(context);
expect(result.isOk, true);

final mainDartFile = File("${project.absolutePath}/example/lib/main.dart"
.normalize);
expect(mainDartFile.existsSync(),
true,
reason: "example/lib/main.dart file should exist");

expect(mainDartFile
.readAsStringSync()
.contains('String _greeting = "There shall be no greeting for now!";'),
true,
reason: "main.dart content is overwritten");

final registry =
File("${project.absolutePath}/example/.klutter-plugins".normalize);

expect(registry.existsSync(), true,
reason: "klutter-plugins file should be created");

final file = pathToConsumer.resolveFile("/lib/main.dart");
var reason = "example/lib/main.dart file should exist";
expect(file.existsSync(), true, reason: reason);
const content = 'String _greeting = "There shall be no greeting for now!";';
final containsContent = file.readAsStringSync().contains(content);
reason = "main.dart content is overwritten:\n${file.readAsStringSync()}";
expect(containsContent, true, reason: reason);
final registry = pathToConsumer.resolveFile(".klutter-plugins");
reason = "klutter-plugins file should be created";
expect(registry.existsSync(), true, reason: reason);
});
}
7 changes: 3 additions & 4 deletions test/src/cli/task_service_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,16 @@ void main() {

expect(tasks.length, 6);

expect(tasks.getTask(TaskName.add).toString(),
"add\n lib (Required) name of the library to add.\n"
expect(
tasks.getTask(TaskName.add).toString(),
"add\n lib (Required) name of the library to add.\n"
" root (Optional) the klutter project root directory. Defaults to \'current working directory\'.\n",
);

expect(
tasks.getTask(TaskName.init).toString(),
"init\n bom (Optional) the klutter gradle version. Defaults to '2024.1.1.beta'.\n flutter (Optional) the flutter sdk version in format major.minor.patch. Defaults to '3.10.6'.\n root (Optional) the klutter project root directory. Defaults to \'current working directory\'.\n",
);

});

test("Verify exception is thrown if duplicate tasks are found", () {
Expand Down Expand Up @@ -106,7 +106,6 @@ void main() {
tearDownAll(() => pathToRoot.deleteSync(recursive: true));
}


extension on Set<Task> {
Task getTask(TaskName taskName) => firstWhere((task) {
return task.taskName == taskName;
Expand Down
10 changes: 5 additions & 5 deletions test/src/cli/task_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ import "package:test/test.dart";

void main() {
test("When a task fails with a KlutterException, it is caught", () async {
final result = await _ExplodingTask().execute(
Context(workingDirectory: Directory.current, taskName: TaskName.build, taskOptions: {})
);
final result = await _ExplodingTask().execute(Context(
workingDirectory: Directory.current,
taskName: TaskName.build,
taskOptions: {}));
expect(result.isOk, false);
expect(result.message, "BOOM!");
});
Expand All @@ -41,5 +42,4 @@ class _ExplodingTask extends Task {
Future<void> toBeExecuted(Context context, Map<TaskOption, dynamic> options) {
throw KlutterException("BOOM!");
}

}
}
30 changes: 18 additions & 12 deletions test/src/common/config_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,31 @@ import "package:test/test.dart";

void main() {
test("Valid Flutter versions are returned as VerifiedFlutterVersion", () {
for(final version in supportedFlutterVersions.keys) {
expect(version.verifyFlutterVersion != null, true, reason: "Version should be valid: $version");
expect("$version.windows.x64".verifyFlutterVersion != null, true, reason: "Version should be valid: $version.windows.x64");
expect("$version.macos.x64".verifyFlutterVersion != null, true, reason: "Version should be valid: $version.macos.x64");
expect("$version.linux.x64".verifyFlutterVersion != null, true, reason: "Version should be valid: $version.linux.x64");
expect("$version.windows.arm64".verifyFlutterVersion != null, true, reason: "Version should be valid: $version.windows.arm64");
expect("$version.macos.arm64".verifyFlutterVersion != null, true, reason: "Version should be valid: $version.macos.arm64");
expect("$version.linux.arm64".verifyFlutterVersion != null, true, reason: "Version should be valid: $version.linux.arm64");
for (final version in supportedFlutterVersions.keys) {
expect(version.verifyFlutterVersion != null, true,
reason: "Version should be valid: $version");
expect("$version.windows.x64".verifyFlutterVersion != null, true,
reason: "Version should be valid: $version.windows.x64");
expect("$version.macos.x64".verifyFlutterVersion != null, true,
reason: "Version should be valid: $version.macos.x64");
expect("$version.linux.x64".verifyFlutterVersion != null, true,
reason: "Version should be valid: $version.linux.x64");
expect("$version.windows.arm64".verifyFlutterVersion != null, true,
reason: "Version should be valid: $version.windows.arm64");
expect("$version.macos.arm64".verifyFlutterVersion != null, true,
reason: "Version should be valid: $version.macos.arm64");
expect("$version.linux.arm64".verifyFlutterVersion != null, true,
reason: "Version should be valid: $version.linux.arm64");
}
});

test("Invalid Flutter versions are returned as null", () {
expect("thisIsNotAFlutterVersion".verifyFlutterVersion == null, true);
expect("thisIsNotAFlutterVersion".verifyFlutterVersion == null, true);
});

group("Verify version sort order is descending by default", () {
void testSorting(String a, String b, List<String> expected) {
test("$a > $b",() {
test("$a > $b", () {
final sortedList = [a.toVersion, b.toVersion]..sort();
expect(sortedList.map((e) => e.prettyPrint).toList(), expected);
});
Expand All @@ -51,7 +58,6 @@ void main() {
testSorting("1.0.0", "1.0.1", ["1.0.1", "1.0.0"]);
testSorting("0.1.0", "0.0.1", ["0.1.0", "0.0.1"]);
});

}

extension on String {
Expand All @@ -63,4 +69,4 @@ extension on String {
patch: int.parse(splitted[2]),
);
}
}
}
Loading

0 comments on commit ea07864

Please sign in to comment.