Attempt to fix flaky tests caused by modifying the same test apps. (#9929)

* Attempt to fix flaky tests caused by modifying the same test apps.

* review comments

* formatting

* use temp directory with inspector service
diff --git a/packages/devtools_app/test/screens/inspector/inspector_integration_test.dart b/packages/devtools_app/test/screens/inspector/inspector_integration_test.dart
index 970108a..3f09b29 100644
--- a/packages/devtools_app/test/screens/inspector/inspector_integration_test.dart
+++ b/packages/devtools_app/test/screens/inspector/inspector_integration_test.dart
@@ -31,39 +31,19 @@
 // reduced to under 1 second without introducing flakes.
 const inspectorChangeSettleTime = Duration(seconds: 2);
 
-void _copyDirectorySync(Directory source, Directory destination) {
-  if (!destination.existsSync()) {
-    destination.createSync(recursive: true);
-  }
-  for (final entity in source.listSync()) {
-    final newPath = p.join(destination.path, p.basename(entity.path));
-    if (entity is Directory) {
-      _copyDirectorySync(entity, Directory(newPath));
-    } else if (entity is File) {
-      entity.copySync(newPath);
-    }
-  }
-}
-
 void main() {
   // We need to use real async in this test so we need to use this binding.
   initializeLiveTestWidgetsFlutterBindingWithAssets();
   const windowSize = Size(2600.0, 1200.0);
 
-  // We copy the fixture app to a temporary directory because the
-  // auto-refresh tests modify lib/main.dart in-place. If this used the shared
-  // 'inspector_app' fixture, it could cause flaky test failures in other tests
-  // (like inspector_service_test.dart) that run in parallel.
-  final tempAppDir =
-      'test/test_infra/fixtures/inspector_app_temp_${DateTime.now().millisecondsSinceEpoch}';
-  _copyDirectorySync(
-    Directory('test/test_infra/fixtures/inspector_app'),
-    Directory(tempAppDir),
-  );
-
+  // The auto-refresh tests modify lib/main.dart in-place.
+  // We use `useTempDirectory: true` to copy the fixture app to a temporary
+  // directory. This prevents flaky test failures in other tests (like
+  // inspector_service_test.dart) that run in parallel.
   final env = FlutterTestEnvironment(
     const FlutterRunConfiguration(withDebugger: true),
-    testAppDirectory: tempAppDir,
+    testAppDirectory: 'test/test_infra/fixtures/inspector_app',
+    useTempDirectory: true,
   );
 
   env.afterEverySetup = () async {
@@ -92,10 +72,6 @@
 
   tearDownAll(() {
     env.finalTeardown();
-    final dir = Directory(tempAppDir);
-    if (dir.existsSync()) {
-      dir.deleteSync(recursive: true);
-    }
   });
 
   group('screenshot tests', () {
diff --git a/packages/devtools_app/test/shared/diagnostics/inspector_service_test.dart b/packages/devtools_app/test/shared/diagnostics/inspector_service_test.dart
index 63657de..99b2929 100644
--- a/packages/devtools_app/test/shared/diagnostics/inspector_service_test.dart
+++ b/packages/devtools_app/test/shared/diagnostics/inspector_service_test.dart
@@ -26,6 +26,7 @@
   final env = FlutterTestEnvironment(
     const FlutterRunConfiguration(withDebugger: true),
     testAppDirectory: 'test/test_infra/fixtures/inspector_app',
+    useTempDirectory: true,
   );
 
   InspectorService? inspectorService;
diff --git a/packages/devtools_app/test/shared/eval_integration_test.dart b/packages/devtools_app/test/shared/eval_integration_test.dart
index f6f16e6..3dea886 100644
--- a/packages/devtools_app/test/shared/eval_integration_test.dart
+++ b/packages/devtools_app/test/shared/eval_integration_test.dart
@@ -14,6 +14,7 @@
 void main() {
   final env = FlutterTestEnvironment(
     const FlutterRunConfiguration(withDebugger: true),
+    useTempDirectory: true,
   );
 
   late Disposable isAlive;
diff --git a/packages/devtools_app/test/test_infra/flutter_test_environment.dart b/packages/devtools_app/test/test_infra/flutter_test_environment.dart
index bb81e3e..d85b328 100644
--- a/packages/devtools_app/test/test_infra/flutter_test_environment.dart
+++ b/packages/devtools_app/test/test_infra/flutter_test_environment.dart
@@ -34,9 +34,10 @@
        _flutterDriverFactory = flutterDriverFactory ?? defaultFlutterRunDriver,
        _flutterExe = _parseFlutterExeFromEnv() {
     if (useTempDirectory) {
-      final tempDirectory = Directory.systemTemp.createTempSync(
-        'flutter_test_temp',
-      );
+      final parentDir = p.dirname(testAppDirectory);
+      final tempDirectory = Directory(
+        parentDir,
+      ).createTempSync('flutter_test_temp_');
       _tempTestAppDirectory = tempDirectory.path;
       _copyToTempDirectory(testAppDirectory, tempDirectory);
     }