Skip to content

Commit 9d5c900

Browse files
TalelBrikiTalel BRIKI
andauthored
fix(windows): fix test_reporter running on Windows (#8)
* fix import in temp file * fix flutter test command --------- Co-authored-by: Talel BRIKI <talel.briki@value.com.tn>
1 parent 680d14c commit 9d5c900

File tree

2 files changed

+68
-9
lines changed

2 files changed

+68
-9
lines changed

packages/test_reporter/bin/test_reporter.dart

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ Future<(Isolate, SendPort)> createReporter(
6161
final Directory tempDir = Directory.systemTemp.createTempSync();
6262

6363
final (customImportLine, method, hasArgs) =
64-
await getCustomReporterImport(as: 'e1') ?? ('', '', false);
64+
await getCustomReporterImport(as: 'e1',path:tempDir.path) ?? ('', '', false);
6565
final importLine = customImportLine.isEmpty
6666
? switch (reporter) {
6767
String package when package.contains('/') =>
@@ -109,8 +109,10 @@ Future<void> main(List<String> args, SendPort sendPort) async {
109109
);
110110
}
111111
''';
112-
113-
final File tempFile = File(p.join(tempDir.path, '_reporter_temp_.dart'));
112+
String absoluteFilePath = File(p.join(tempDir.path, '_reporter_temp_.dart')).absolute.path;
113+
/// Ensure the parent directory exists
114+
Directory(p.dirname(absoluteFilePath)).createSync(recursive: true);
115+
final File tempFile = File(absoluteFilePath);
114116
await tempFile.writeAsString(code);
115117

116118
final ReceivePort receivePort = ReceivePort();
@@ -137,10 +139,13 @@ Future<void> main(List<String> args, SendPort sendPort) async {
137139
}
138140

139141
Future<(String, String, bool)?> getCustomReporterImport(
140-
{String as = ''}) async {
142+
{String as = '',String path=''}) async {
141143
final customReporter = File(p.join('test', 'reporter.dart'));
144+
String relativePath = _posixRelative(customReporter.absolute.path, from:path);
145+
/// Get the absolute path and replace '\' with '\\'
146+
final sanitizedPath = relativePath.replaceAll(r'\', r'\\');
142147
final customReporterImportLine =
143-
"import '${customReporter.absolute.path}'${as.isNotEmpty ? 'as $as' : ''};";
148+
"import '$sanitizedPath'${as.isNotEmpty ? 'as $as' : ''};";
144149

145150
if (!await customReporter.exists()) return null;
146151

@@ -159,3 +164,22 @@ Future<(String, String, bool)?> getCustomReporterImport(
159164

160165
return null;
161166
}
167+
/// This creates a relative path from `from` to `input`, the output being a
168+
/// posix path on all platforms.
169+
/// by talel briki 15/05/2025
170+
String _posixRelative(String input, {String from = ""}) {
171+
// Use the appropriate context for Windows
172+
final p.Context context = p.Context(style: p.Style.windows);
173+
174+
// Ensure the input and "from" paths are absolute
175+
final String absInputPath = File(input).absolute.path;
176+
final String absFromPath = Directory(from).absolute.path;
177+
178+
// Print the resolved absolute paths
179+
180+
// Compute the relative path
181+
final String relativePath = context.relative(absInputPath, from: absFromPath);
182+
183+
return relativePath;
184+
}
185+

packages/test_reporter/lib/src/runner/test_runner.dart

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,15 @@ Stream<TestEvent> flutterTest({
5858
Map<String, String>? environment,
5959
bool runInShell = false,
6060
StartProcess startProcess = Process.start,
61-
}) {
62-
return _runTestProcess(
63-
() => startProcess(
64-
'flutter',
61+
}) async*{
62+
///extract flutter path to run command
63+
final flutterPath = await getActiveFlutterPath();
64+
if (flutterPath == null) {
65+
throw Exception('Flutter executable not found in PATH.');
66+
}
67+
yield* _runTestProcess(
68+
() => startProcess(
69+
flutterPath,
6570
['test', ...?arguments, '--reporter=json'],
6671
environment: environment,
6772
workingDirectory: workingDirectory,
@@ -144,3 +149,33 @@ extension on Stream<List<int>> {
144149
}
145150
}
146151
}
152+
///get flutter path
153+
/// by talel briki 15/05/2025
154+
Future<String?> getActiveFlutterPath() async {
155+
try {
156+
final result = await Process.run(
157+
Platform.isWindows ? 'flutter.bat' : 'flutter',
158+
['--version', '--machine'],
159+
);
160+
161+
162+
if (result.exitCode == 0) {
163+
final output = result.stdout as String;
164+
final jsonMap = jsonDecode(output);
165+
166+
final flutterRoot = jsonMap['flutterRoot'];
167+
168+
if (flutterRoot != null) {
169+
final flutterPath = Platform.isWindows
170+
? '$flutterRoot\\bin\\flutter.bat'
171+
: '$flutterRoot/bin/flutter';
172+
return flutterPath;
173+
}
174+
}
175+
} catch (e) {
176+
print('Failed to detect Flutter path: $e');
177+
}
178+
179+
return null;
180+
}
181+

0 commit comments

Comments
 (0)