This commit is contained in:
LZX committed 2026-09-16 20:43:39 +08:00
1 parent 5efc778165
commit 59125d185c
64 files changed
+7912 -1393

No files matched your search

+101 -248
View File
@@ -1,209 +1,76 @@
import 'dart:io';
import 'package:flutter_test/flutter_test.dart';
import 'package:path/path.dart' as p;
import 'package:code_editor/models/run_task.dart';
import 'package:code_editor/models/run_task_type.dart';
import 'package:code_editor/providers/run_provider.dart';
import 'package:code_editor/services/project_modules/gradle_project_module.dart';
import 'package:code_editor/services/distro_manager.dart';
import 'package:code_editor/services/project_modules/project_module.dart';
import 'package:code_editor/services/project_modules/project_probe.dart';
import 'package:code_editor/services/project_task_dispatcher.dart';
import 'package:code_editor/services/run_task_storage_service.dart';
/// 单测用假系统名:探测必须基于"真实可用系统",单测用假 rootfs 代替真容器
const String kTestSystemName = 'test_sys';
/// 固定返回一组任务的假模块(用于验证类型表写入 / RunProvider 集成)
class _StubGradleModule extends ProjectModule {
@override
String get id => 'gradle';
@override
String get displayName => 'Gradle';
@override
List<String> get triggerFiles => const ['build.gradle'];
@override
bool shouldActivate(Directory projectDir) =>
File(p.join(projectDir.path, 'build.gradle')).existsSync();
@override
Future<ModuleProbeResult> probe(ProjectProbe probe) async => ModuleProbeResult.ok(id, [
const RunTask(
id: 'gradle_assemble',
name: 'assemble',
command: 'sh ./gradlew assemble',
source: TaskSource.detected,
moduleId: 'gradle',
group: 'build',
),
]);
}
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
group('GradleProjectModule Parser Tests', () {
test('parseGradleTasksOutput parses single project tasks, groups, and descriptions', () {
const output = '''
------------------------------------------------------------
Tasks runnable from root project 'my-app'
------------------------------------------------------------
Application tasks
-----------------
run - Runs this project as a JVM application
Build tasks
-----------
assemble - Assembles the outputs of this project.
build - Assembles and tests this project.
clean - Deletes the build directory.
Verification tasks
------------------
check - Runs all checks.
test - Runs the test suite.
Other tasks
-----------
customTask - Executes a custom task
''';
final tasks = GradleProjectModule.parseGradleTasksOutput(output, 'sh ./gradlew');
expect(tasks, isNotEmpty);
// 验证任务名称与命令
final runTask = tasks.firstWhere((t) => t.name == 'run');
expect(runTask.command, 'sh ./gradlew run');
expect(runTask.group, 'application');
expect(runTask.moduleId, 'gradle');
expect(runTask.source, TaskSource.detected);
expect(runTask.description, 'Runs this project as a JVM application');
final assembleTask = tasks.firstWhere((t) => t.name == 'assemble');
expect(assembleTask.command, 'sh ./gradlew assemble');
expect(assembleTask.group, 'build');
expect(assembleTask.moduleId, 'gradle');
final buildTask = tasks.firstWhere((t) => t.name == 'build');
expect(buildTask.command, 'sh ./gradlew build');
expect(buildTask.group, 'build');
final testTask = tasks.firstWhere((t) => t.name == 'test');
expect(testTask.command, 'sh ./gradlew test');
expect(testTask.group, 'verification');
final customTask = tasks.firstWhere((t) => t.name == 'customTask');
expect(customTask.command, 'sh ./gradlew customTask');
expect(customTask.group, 'other');
expect(customTask.description, 'Executes a custom task');
// 优先级排序验证:application -> build -> verification -> other
final groupsInOrder = tasks.map((t) => t.group).toList();
final appIdx = groupsInOrder.indexOf('application');
final buildIdx = groupsInOrder.indexOf('build');
final verifIdx = groupsInOrder.indexOf('verification');
final otherIdx = groupsInOrder.indexOf('other');
expect(appIdx, lessThan(buildIdx));
expect(buildIdx, lessThan(verifIdx));
expect(verifIdx, lessThan(otherIdx));
});
test('parseGradleTasksOutput parses multi-module projects with prefixes (:app:xxx)', () {
const output = '''
------------------------------------------------------------
Tasks runnable from project ':app'
------------------------------------------------------------
Build tasks
-----------
assembleDebug - Assembles all Debug builds.
assembleRelease - Assembles all Release builds.
Tasks runnable from project ':core'
------------------------------------------------------------
Build tasks
-----------
compileJava - Compiles Java sources.
''';
final tasks = GradleProjectModule.parseGradleTasksOutput(output, 'sh ./gradlew');
expect(tasks.length, 3);
final assembleDebugTask = tasks.firstWhere((t) => t.name == ':app:assembleDebug');
expect(assembleDebugTask.command, 'sh ./gradlew :app:assembleDebug');
expect(assembleDebugTask.id, 'gradle__app_assembleDebug');
expect(assembleDebugTask.group, 'build');
expect(assembleDebugTask.moduleId, 'gradle');
final compileJavaTask = tasks.firstWhere((t) => t.name == ':core:compileJava');
expect(compileJavaTask.command, 'sh ./gradlew :core:compileJava');
expect(compileJavaTask.id, 'gradle__core_compileJava');
});
});
group('Gradle Module Cache & Storage Tests', () {
late Directory tempDir;
late RunTaskStorageService storageService;
setUp(() async {
tempDir = await Directory.systemTemp.createTemp('gradle_module_test_');
storageService = RunTaskStorageService();
});
tearDown(() async {
if (await tempDir.exists()) {
await tempDir.delete(recursive: true);
}
});
test('saveModuleTasks writes to .code_editor/gradle_tasks.json and loadModuleTasks reads it back', () async {
final initial = await storageService.loadModuleTasks(tempDir.path, 'gradle');
expect(initial.tasks, isEmpty);
expect(initial.hash, isNull);
const tasksToSave = [
RunTask(
id: 'gradle_assembleDebug',
name: 'assembleDebug',
command: 'sh ./gradlew assembleDebug',
source: TaskSource.detected,
group: 'build',
moduleId: 'gradle',
description: 'Build debug package',
),
RunTask(
id: 'gradle_run',
name: 'run',
command: 'sh ./gradlew run',
source: TaskSource.detected,
group: 'application',
moduleId: 'gradle',
description: 'Run project',
),
];
await storageService.saveModuleTasks(
projectRoot: tempDir.path,
moduleName: 'gradle',
tasks: tasksToSave,
hash: 'test_hash_12345',
);
// 验证独立持久化文件存在:.code_editor/gradle_tasks.json
final cacheFile = File('${tempDir.path}/.code_editor/gradle_tasks.json');
expect(cacheFile.existsSync(), isTrue);
// 读取并校验
final loaded = await storageService.loadModuleTasks(tempDir.path, 'gradle');
expect(loaded.hash, 'test_hash_12345');
expect(loaded.tasks.length, 2);
expect(loaded.tasks.first.name, 'assembleDebug');
expect(loaded.tasks.first.group, 'build');
expect(loaded.tasks.first.moduleId, 'gradle');
expect(loaded.tasks.last.name, 'run');
// 验证 run_tasks.json(用户任务)完全不受影响且相互隔离
final userConfig = await storageService.loadConfig(tempDir.path);
expect(userConfig.tasks, isEmpty);
});
test('computeBuildScriptHash changes when build.gradle is modified', () async {
final buildGradle = File('${tempDir.path}/build.gradle');
await buildGradle.writeAsString('plugins { id "java" }\n');
final hash1 = GradleProjectModule.computeBuildScriptHash(tempDir);
expect(hash1.isNotEmpty, isTrue);
// 稍微休眠并修改内容
await Future<void>.delayed(const Duration(milliseconds: 50));
await buildGradle.writeAsString('plugins { id "application" }\n');
final hash2 = GradleProjectModule.computeBuildScriptHash(tempDir);
expect(hash2, isNot(equals(hash1)));
});
});
group('ProjectTaskDispatcher & RunProvider Modular Tests', () {
late Directory tempDir;
late Directory distroBaseDir;
late DistroManager distroManager;
setUp(() async {
tempDir = await Directory.systemTemp.createTemp('dispatcher_test_');
// 假系统:rootfs/{etc,bin,usr/bin/java}(依赖检测走宿主 rootfs 快速判定)
distroBaseDir = await Directory.systemTemp.createTemp('dispatcher_distro_');
final rootfs = Directory(p.join(distroBaseDir.path, kTestSystemName, 'rootfs'));
Directory(p.join(rootfs.path, 'etc')).createSync(recursive: true);
Directory(p.join(rootfs.path, 'bin')).createSync(recursive: true);
final usrBin = Directory(p.join(rootfs.path, 'usr', 'bin'))..createSync(recursive: true);
File(p.join(usrBin.path, 'java')).writeAsStringSync('#!/bin/sh\n');
distroManager = DistroManager()..customBaseDir = distroBaseDir;
});
tearDown(() async {
DistroManager().customBaseDir = null;
if (await tempDir.exists()) {
await tempDir.delete(recursive: true);
}
if (await distroBaseDir.exists()) {
await distroBaseDir.delete(recursive: true);
}
});
test('ProjectTaskDispatcher activates modules correctly', () async {
@@ -226,80 +93,66 @@ compileJava - Compiles Java sources.
expect(active2.any((m) => m.id == 'gradle'), isTrue);
});
test('GradleProjectModule returns cached tasks when hash matches without executing command', () async {
final buildGradle = File('${tempDir.path}/build.gradle');
await buildGradle.writeAsString('plugins { id "java" }\n');
test('未安装指定系统时既不探测,也绝不创建任何系统目录', () async {
final emptyBase = await Directory.systemTemp.createTemp('dispatcher_no_sys_');
addTearDown(() async {
if (await emptyBase.exists()) await emptyBase.delete(recursive: true);
});
DistroManager().customBaseDir = emptyBase;
final storageService = RunTaskStorageService();
final currentHash = GradleProjectModule.computeBuildScriptHash(tempDir);
await File('${tempDir.path}/build.gradle').writeAsString('plugins { id "java" }\n');
const cachedTasks = [
RunTask(
id: 'gradle_cached_task',
name: 'cachedTask',
command: 'sh ./gradlew cachedTask',
source: TaskSource.detected,
group: 'build',
moduleId: 'gradle',
),
];
await storageService.saveModuleTasks(
final tasks = await ProjectTaskDispatcher().detect(
projectRoot: tempDir.path,
moduleName: 'gradle',
tasks: cachedTasks,
hash: currentHash,
systemName: 'ubuntu',
distroManager: DistroManager(),
);
final dispatcher = ProjectTaskDispatcher();
final tasks = await dispatcher.detect(
projectRoot: tempDir.path,
storageService: storageService,
);
expect(tasks.any((t) => t.id == 'gradle_cached_task'), isTrue);
expect(tasks.firstWhere((t) => t.id == 'gradle_cached_task').name, 'cachedTask');
expect(tasks, isEmpty);
// 绝不能凭空造出 distros/ubuntu(历史缺陷会在这里 recursive 创建 rootfs/etc)
expect(Directory(p.join(emptyBase.path, 'ubuntu')).existsSync(), isFalse);
});
test('RunProvider integrates with modular task system and maintains customTasks', () async {
final storageService = RunTaskStorageService();
final runProvider = RunProvider(storageService: storageService);
// 准备工程与一个已缓存的 Gradle 任务
test('未传入系统名时不做任何探测(连纯静态模块任务也不产出)', () async {
await File('${tempDir.path}/Makefile').writeAsString('all:\n\tgcc main.c\n');
await File('${tempDir.path}/build.gradle').writeAsString('plugins { id "java" }\n');
final currentHash = GradleProjectModule.computeBuildScriptHash(tempDir);
const initialGradleTask = RunTask(
id: 'gradle_assemble',
name: 'assemble',
command: 'sh ./gradlew assemble',
source: TaskSource.detected,
group: 'build',
moduleId: 'gradle',
);
await storageService.saveModuleTasks(
final tasks = await ProjectTaskDispatcher().detect(
projectRoot: tempDir.path,
moduleName: 'gradle',
tasks: [initialGradleTask],
hash: currentHash,
distroManager: distroManager,
);
// 打开工程
await runProvider.onProjectOpened(tempDir.path);
expect(tasks, isEmpty);
});
test('真实自省失败时返回空结果:不产出任何兜底任务', () async {
await File('${tempDir.path}/build.gradle').writeAsString('plugins { id "java" }\n');
final tasks = await ProjectTaskDispatcher().detect(
projectRoot: tempDir.path,
systemName: kTestSystemName,
distroManager: distroManager,
);
// 测试环境无法真正执行 gradle -> 该模块失败且不产出任务(也绝无 detected_gradle_* 兜底)
expect(tasks.where((t) => t.moduleId == 'gradle'), isEmpty);
expect(tasks.any((t) => t.id.startsWith('detected_gradle_')), isFalse);
});
test('RunProvider 集成:模块结果写入"类型 → 任务数组"的对应格子', () async {
await File('${tempDir.path}/build.gradle').writeAsString('plugins { id "java" }\n');
final dispatcher = ProjectTaskDispatcher(customModules: [_StubGradleModule()]);
final runProvider = RunProvider(
dispatcher: dispatcher,
distroManager: distroManager,
);
await runProvider.onProjectOpened(tempDir.path, systemName: kTestSystemName);
expect(runProvider.taskTable.tasksOf(RunTaskType.gradle).map((t) => t.id), ['gradle_assemble']);
expect(runProvider.detectedTasks.any((t) => t.id == 'gradle_assemble'), isTrue);
// 添加用户自定义任务
const customTask = RunTask(
id: 'user_custom_1',
name: 'My Custom Run',
command: 'echo "custom"',
source: TaskSource.custom,
);
await runProvider.addCustomTask(customTask);
expect(runProvider.customTasks.length, 1);
// allTasks 自定义置顶
expect(runProvider.allTasks.first.id, 'user_custom_1');
expect(runProvider.allTasks.any((t) => t.id == 'gradle_assemble'), isTrue);
expect(runProvider.taskTable.tasksOf(RunTaskType.user), isEmpty);
});
});
}