dart-lang / mockito

Mockito-inspired mock library for Dart
https://pub.dev/packages/mockito
Apache License 2.0
632 stars 162 forks source link

Generated Imports use Unescaped backslashes #711

Closed centmeteenvin closed 10 months ago

centmeteenvin commented 11 months ago

When generating a mock of a class that imports from the other class .

test.dart

import '../bin/webhook.dart';

@GenerateNiceMocks([MockSpec<WebHookService>()])
import "test.mocks.dart";

test.mocks.dart

// Mocks generated by Mockito 5.4.2 from annotations
// in cd/test/server_test.dart.
// Do not manually edit this file.

// ignore_for_file: no_leading_underscores_for_library_prefixes
import 'package:mockito/mockito.dart' as _i1;

import '..\bin\models\webhook_payload\webhook_payload.dart' as _i3;
import '..\bin\webhook.dart' as _i2;

The last 2 import statements use the backslash. This escapes the firstfollowing charachter making the import unusable.

srawlins commented 11 months ago

Great bug report! Can you mention what version of mockito you are running? Thanks!

centmeteenvin commented 11 months ago

The latest version 5.4.2. I did use a lot of other build_runner packages. What also might be useful is: Platform Windows. Dart SDK version 3.15 No flutter Coding in vscode but not using any of the build_runner extensions.

Sese-Schneider commented 10 months ago

I can reproduce this issue and am also impacted by this. cc @srawlins I'm unable to generate any mocks.

Details

Environment

Example

This simple file can always reproduce the issue.

import 'package:mockito/annotations.dart';

import '../u_folder_name/helper.dart';
import 'test.mocks.dart';

@GenerateMocks([Helper])
class Test {}

Stacktrace

I initially had this issue because my folder starts with u, and \u has a special treatment on windows.

[SEVERE] mockito:mockBuilder on test/test.dart (cached):

Could not format because the source could not be parsed:

line 10, column 11 of .: An escape sequence starting with '\u' must be followed by 4 hexadecimal digits.
   ╷
10 │ import '..\u_folder_name\helper.dart' as _i2;

Workarounds

See https://github.com/dart-lang/mockito/pull/721