Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generated test bundle is broken on Patrol 2.0 on Windows #1428

Closed
GentiShtjefni opened this issue Jun 29, 2023 · 26 comments · Fixed by #1797
Closed

Generated test bundle is broken on Patrol 2.0 on Windows #1428

GentiShtjefni opened this issue Jun 29, 2023 · 26 comments · Fixed by #1797
Labels
bug Something isn't working package: patrol_cli Related to the patrol_cli package

Comments

@GentiShtjefni
Copy link

GentiShtjefni commented Jun 29, 2023

Steps to reproduce

Create a simple Flutter project and setup patrol native automation.
main_test.dart:

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:patrol/patrol.dart';

void main() {
  patrolTest(
    'counter state is the same after going to home and switching apps',
    nativeAutomation: true,
        ($) async {
      // Replace later with your app's main widget
      await $.pumpWidgetAndSettle(
        MaterialApp(
          home: Scaffold(
            appBar: AppBar(title: const Text('app')),
            backgroundColor: Colors.blue,
          ),
        ),
      );

      expect($('app'), findsOneWidget);
      await $.native.pressHome();
    },
  );
}

MainActivityTest.java:

package com.ecample.test; // replace "pl.leancode.patrol.example" with your app's package

import androidx.test.platform.app.InstrumentationRegistry;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import pl.leancode.patrol.PatrolJUnitRunner;

@RunWith(Parameterized.class)
public class MainActivityTest {
    @Parameters(name = "{0}")
    public static Object[] testCases() {
        PatrolJUnitRunner instrumentation = (PatrolJUnitRunner) InstrumentationRegistry.getInstrumentation();
        instrumentation.setUp(MainActivity.class);
        instrumentation.waitForPatrolAppService();
        return instrumentation.listDartTests();
    }

    public MainActivityTest(String dartTestName) {
        this.dartTestName = dartTestName;
    }

    private final String dartTestName;

    @Test
    public void runDartTest() {
        PatrolJUnitRunner instrumentation = (PatrolJUnitRunner) InstrumentationRegistry.getInstrumentation();
        instrumentation.runDartTest(dartTestName);
    }
}

build.gradle:

def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
    localPropertiesFile.withReader('UTF-8') { reader ->
        localProperties.load(reader)
    }
}

def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
    throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}

def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
    flutterVersionCode = '1'
}

def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
    flutterVersionName = '1.0'
}

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
    namespace "com.example.test.int_test"
    compileSdkVersion flutter.compileSdkVersion
    ndkVersion flutter.ndkVersion

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "com.example.test.int_test"
        // You can update the following values to match your application needs.
        // For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-gradle-build-configuration.
        minSdkVersion flutter.minSdkVersion
        targetSdkVersion flutter.targetSdkVersion
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
        testInstrumentationRunner "pl.leancode.patrol.PatrolJUnitRunner"
    }

    testOptions {
        execution "ANDROIDX_TEST_ORCHESTRATOR"
    }

    buildTypes {
        release {
            // TODO: Add your own signing config for the release build.
            // Signing with the debug keys for now, so `flutter run --release` works.
            signingConfig signingConfigs.debug
        }
    }
}

dependencies {
    androidTestUtil "androidx.test:orchestrator:1.4.2"
}

flutter {
    source '../..'
}

Actual results

This is the error i get
C:\Users\User\AndroidStudioProjects\int_test>patrol test --target integration_test/main_test.dart
You're using Patrol CLI 2.0, which has breaking changes.
Read the migration guide at https://patrol.leancode.co/v2.
Disable this warning by setting the PATROL_MIGRATED environment variable.

No device specified, using the first one (emulator-5554)
• Building apk with entrypoint test_bundle.dart...
integration_test/test_bundle.dart:12:8: Error: Error when reading '/main_test.dart': The system cannot find the file specified.

    import '/main_test.dart' as __main_test;
           ^
    integration_test/test_bundle.dart:65:35: Error: Undefined name 'main'.
      group('.main_test', __main_test.main);
                                      ^^^^
    Target kernel_snapshot failed: Exception


    FAILURE: Build failed with an exception.

    * Where:
    Script 'C:\Program Files\Flutter\flutter\packages\flutter_tools\gradle\flutter.gradle' line: 1201

    * What went wrong:
    Execution failed for task ':app:compileFlutterBuildDebug'.
    > Process 'command 'C:\Program Files\Flutter\flutter\bin\flutter.bat'' finished with non-zero exit value 1

    * Try:
    > Run with --stacktrace option to get the stack trace.
    > Run with --info or --debug option to get more log output.
    > Run with --scan to get full insights.

    * Get more help at https://help.gradle.org

    BUILD FAILED in 37s

✗ Failed to build apk with entrypoint test_bundle.dart (Gradle build failed with code 1) (38.4s)
Exception: Gradle build failed with code 1
See the logs above to learn what happened. Also consider running with --verbose. If the logs still aren't useful, then it's a bug - please report it.
FAIL test_bundle.dart on emulator-5554

Logs

Logs
<!-- Replace this line with your logs. Do not remove the backticks! -->

Patrol version

patrol: 2.0.0

Patrol Doctor output

Patrol Doctor output
C:\Users\User\AndroidStudioProjects\int_test>patrol doctor
You're using Patrol CLI 2.0, which has breaking changes.
Read the migration guide at https://patrol.leancode.co/v2.
Disable this warning by setting the PATROL_MIGRATED environment variable.

Patrol CLI version: 2.0.0
Program adb found in C:\Users\User\AppData\Local\Android\Sdk\platform-tools\adb.exe
Env var $ANDROID_HOME is not set

Flutter Doctor output

Flutter Doctor output
Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 3.10.5, on Microsoft Windows [Version 10.0.19044.1288], locale en-GB)
[√] Windows Version (Installed version of Windows is version 10 or higher)
[√] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
[√] Chrome - develop for the web
[X] Visual Studio - develop for Windows
    X Visual Studio not installed; this is necessary for Windows development.
      Download at https://visualstudio.microsoft.com/downloads/.
      Please install the "Desktop development with C++" workload, including all of its default components
[√] Android Studio (version 2020.3)
[√] Android Studio (version 2022.2)
[√] Connected device (4 available)
[√] Network resources

! Doctor found issues in 1 category.


@bartekpacia
Copy link
Contributor

Hi, thanks for reporting. I see you're on Windows - it'll slow down me reproducing this problem and helping you.

@bartekpacia bartekpacia changed the title main_test.dart is not imported correctly on patrol 2.0 Generated test bundle is broken on Patrol 2.0 on Windows Jun 29, 2023
@bartekpacia bartekpacia added bug Something isn't working package: patrol_cli Related to the patrol_cli package labels Jun 29, 2023
@GentiShtjefni
Copy link
Author

@bartekpacia maybe this helps. I tried changing a little bit the command and the issue dissapeared but another one came to life:
C:\Users\User\Documents\DashFuel\flutter>patrol test --target integration_test\main_test.dart
You're using Patrol CLI 2.0, which has breaking changes.
Read the migration guide at https://patrol.leancode.co/v2.
Disable this warning by setting the PATROL_MIGRATED environment variable.

No device specified, using the first one (emulator-5554)
• Building apk with entrypoint test_bundle.dart...
Invalid depfile: C:\Users\User\Documents\DashFuel\flutter.dart_tool\flutter_build\15be9d42c5bda99275f2fd12e979251a\kernel_snapshot.d
Invalid depfile: C:\Users\User\Documents\DashFuel\flutter.dart_tool\flutter_build\15be9d42c5bda99275f2fd12e979251a\kernel_snapshot.d
Unhandled exception:
FileSystemException(uri=c:/Users/User/Documents/DashFuel/flutter/integration_test/main_test.dart; message=StandardFileSystem only supports file:* and data:* URIs)
#0 StandardFileSystem.entityForUri (package:front_end/src/api_prototype/standard_file_system.dart:34:7)
#1 IncrementalCompiler._initializeExperimentalInvalidation (package:front_end/src/fasta/incremental_compiler.dart:1268:56)
#2 IncrementalCompiler.computeDelta. (package:front_end/src/fasta/incremental_compiler.dart:325:17)

#3 IncrementalCompiler.compile (package:vm/incremental_compiler.dart:75:50)

#4 FrontendCompiler.compile (package:frontend_server/frontend_server.dart:595:11)

#5 starter (package:frontend_server/starter.dart:99:12)

#6 main (file:///C:/b/s/w/ir/x/w/sdk/pkg/frontend_server/bin/frontend_server_starter.dart:13:14)

    Target kernel_snapshot failed: Exception


    FAILURE: Build failed with an exception.

    * Where:
    Script 'C:\Program Files\Flutter\flutter\packages\flutter_tools\gradle\flutter.gradle' line: 1201

    * What went wrong:
    Execution failed for task ':app:compileFlutterBuildDebug'.
    > Process 'command 'C:\Program Files\Flutter\flutter\bin\flutter.bat'' finished with non-zero exit value 1

    * Try:
    > Run with --stacktrace option to get the stack trace.
    > Run with --info or --debug option to get more log output.
    > Run with --scan to get full insights.

    * Get more help at https://help.gradle.org

    BUILD FAILED in 31s

✗ Failed to build apk with entrypoint test_bundle.dart (Gradle build failed with code 1) (32.7s)
Exception: Gradle build failed with code 1
See the logs above to learn what happened. Also consider running with --verbose. If the logs still aren't useful, then it's a bug - please report it.
FAIL test_bundle.dart on emulator-5554

@GentiShtjefni
Copy link
Author

@bartekpacia using this command "patrol test --target integration_test\main_test.dart" it works on this test project, but on my working project I get this issue: #1428 (comment)

@bartekpacia
Copy link
Contributor

This looks like a bug related to Windows path separator.

Try doing patrol test --target integration_test/main_test.dart instead of patrol test --target integration_test\main_test.dart.

@GentiShtjefni
Copy link
Author

GentiShtjefni commented Jun 29, 2023

If you check the Actual Result part on this issue, you will see that when I try this command "patrol test --target integration_test/main_test.dart" then test_bundle.dart is not generated correctly

@bartekpacia
Copy link
Contributor

Yup I see.

For the record, here's the code responsible for generating that file. The bug is probably lurking there.

@GentiShtjefni
Copy link
Author

Can you help me with this bug? #1428 (comment) This is the issue I am stuck.
p.s with patrol 1.1.4 everything was working correctly

@bartekpacia
Copy link
Contributor

Hi, unfortunately, that looks like some compiler crash. No idea why it happens.

@devkaio
Copy link

devkaio commented Jun 29, 2023

@GentiShtjefni have you tried this:

  1. flutter clean
  2. flutter pub get
  3. patrol test --target integration_test\main_test.dart
  4. patrol test --target integration_test\your_project_test.dart // if necessary --flavor your_flavor

Maybe it's old files getting in the way of your updated build process.

@GentiShtjefni
Copy link
Author

GentiShtjefni commented Jun 30, 2023

Flutter pub cache rapair seemed to work for me. Now the tests seems to install the apk but now the tests dont start at all. The emulator seems to get stuck at splash screen. I had to cancel the test after 1637 seconds of not starting:
`C:\Users\User\Documents\DashFuel\flutter>patrol test --target integration_test\main_test.dart
You're using Patrol CLI 2.0, which has breaking changes.
Read the migration guide at https://patrol.leancode.co/v2.
Disable this warning by setting the PATROL_MIGRATED environment variable.

No device specified, using the first one (emulator-5554)
• Building apk with entrypoint test_bundle.dart...
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: C:\Users\User\AppData\Local\Pub\Cache\hosted\pub.dev\firebase_messaging-14.6.4\android\src\main\java\io\flutter\plugins\firebase\messaging\FlutterFirebaseMessagingP
lugin.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
✓ Completed building apk with entrypoint test_bundle.dart (256.4s)
• Executing tests of apk with entrypoint test_bundle.dart on emulator-5554...
See the report at: file:///C:/Users/User/Documents/DashFuel/flutter/build/app/reports/androidTests/connected/index.html
✗ Failed to execute tests of apk with entrypoint test_bundle.dart on emulator-5554 (Gradle test execution failed with code 1) (1697.4s)
Exception: Gradle test execution failed with code 1
See the logs above to learn what happened. Also consider running with --verbose. If the logs still aren't useful, then it's a bug - please report it.
FAIL test_bundle.dart on emulator-5554`

@bartekpacia
Copy link
Contributor

bartekpacia commented Jun 30, 2023

The emulator seems to get stuck at splash screen.

See flutter logs (or adb logcat -v color), there are probably some errors in there that cause your test to not start.

@victoria24a
Copy link

I want to notice, that I have a similar problem on Windows. When I try tun my test, using command:
patrol test --target integration_test/patrol_test.dart
there is an incorrect import in the test_bundle.dart file:
// START: GENERATED TEST IMPORTS
import '/patrol_test.dart' as __patrol_test;
// END: GENERATED TEST IMPORTS
it's even worse when I'm using following command:
patrol test --target .\integration_test\patrol_test.dart
// START: GENERATED TEST IMPORTS
import 'D:/Workspace/fp_sk/./integration_test/patrol_test.dart' as D:__Workspace__fp_sk__.__integration_test__patrol_test;
// END: GENERATED TEST IMPORTS

@GentiShtjefni
Copy link
Author

@victoria24a try patrol test --target integration_test\patrol_test.dart

@victoria24a
Copy link

Better, but now it freezes the tests don't start on device:
PS D:\Workspace\fpnb_skywave\skywave> patrol test --target integration_test\patrol_test.dart
You're using Patrol CLI 2.0, which has breaking changes.
Read the migration guide at https://patrol.leancode.co/v2.
Disable this warning by setting the PATROL_MIGRATED environment variable.

No device specified, using the first one (RF8NB1E5QSH)
• Building apk with entrypoint test_bundle.dart...
✓ Completed building apk with entrypoint test_bundle.dart (8.5s)
• Executing tests of apk with entrypoint test_bundle.dart on device RF8NB1E5QSH...

@bartekpacia
Copy link
Contributor

I confirm running patrol test -t integration_test/example_test.dart on Windows results in garbled test_bundle.dart file.

// START: GENERATED TEST IMPORTS
import '/example_test.dart' as __example_test;
// END: GENERATED TEST IMPORTS

Future<void> main() async {
  // ...

  // START: GENERATED TEST GROUPS
  group('.example_test', __example_test.main);
  // END: GENERATED TEST GROUPS

  // ...
}

Running patrol test -t integration_test\example_test.dart on Windows works fine.

@GentiShtjefni
Copy link
Author

@bartekpacia running patrol test -t integration_test\example_test.dart doesn't start the tests. They are stuck like this: #1428 (comment)

@bartekpacia
Copy link
Contributor

bartekpacia commented Jul 3, 2023

@GentiShtjefni This is a different problem. Please open a new issue for it (and remember to attach output of adb logcat in a file - this helps a lot).

@victoria24a
Copy link

I have reported separate issue #1456.

@GentiShtjefni

This comment was marked as off-topic.

@bartekpacia

This comment was marked as off-topic.

@GentiShtjefni

This comment was marked as off-topic.

@bartekpacia

This comment was marked as off-topic.

@oberoivarun
Copy link

oberoivarun commented Aug 13, 2023

I confirm running patrol test -t integration_test/example_test.dart on Windows results in garbled test_bundle.dart file.

// START: GENERATED TEST IMPORTS
import '/example_test.dart' as __example_test;
// END: GENERATED TEST IMPORTS

Future<void> main() async {
  // ...

  // START: GENERATED TEST GROUPS
  group('.example_test', __example_test.main);
  // END: GENERATED TEST GROUPS

  // ...
}

Running patrol test -t integration_test\example_test.dart on Windows works fine.

This worked for me. I am on windows.

@bartekpacia
Copy link
Contributor

bartekpacia commented Aug 15, 2023

I'm not sure what the correct behavior should be.

Questions

Should the below invocation work on Windows? (I guess "yes"?)

patrol test -t integration_test/example_test.dart

Should the below invocation work on Unix? (I guess "no"?)

patrol test -t integration_test\example_test.dart

@devkaio
Copy link

devkaio commented Aug 16, 2023

@bartekpacia do you think replacing the first / would solve this by adding

if (relativeTestFilePath.startsWith('/')) relativeTestFilePath.replaceFirst('/', '');

after this line

bartekpacia pushed a commit that referenced this issue Oct 12, 2023
* Removing first slash, closes #1428

* Formatted packages\patrol_cli\lib\src\test_bundler.dart

* Update CHANGELOG.md
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar problem, please file a new issue. Make sure to follow the template and provide all the information necessary to reproduce the issue.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 17, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working package: patrol_cli Related to the patrol_cli package
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants