Co-locate flutter test files to source - flutter

Putting test files in the test directory involves mental gymnastics of remembering to move test file when sources move or to even find a corresponding test file.
Is there a way in flutter to place test files next the source?
flutter test -h doesn't have any path related options.

This can be achieved with
flutter test lib/**/*_test.dart
Found the hint from Dart test package readme at https://github.com/dart-lang/test/tree/master/pkgs/test#running-tests

Related

Flutter optional Assets

I'm building an app in Flutter, and I'm using the flutter-dotenv package so we can change things around on development time or testing time without changing the code.
I can see that for it to work I must include a .env asset in the pubspec YAML file, however, that .env file may or may not be there. Currently I'm just using an empty .env file, but I don't think this is ideal.
Is there any way to instruct the flutter build process to copy a file only if it exists?
Thanks!!

How to create/provide multiple example apps for a flutter plugin?

Flutter seems to have a very strict/rigid structure for plugins with example folder and all contents inside that folder.
I want to provide multiple examples with my plugin. Something like examples folder and then examples/demo1 and examples/demo2 as two different app examples.
I tried doing this but flutter run or pub get command breaks with this change. it's gets stuck with below error which wasn't thrown with exact same code in previous structure before change. Also my app actually follow embedding v2 code so this error is completely false too.
The plugin `<MY PLUGIN>` requires your app to be migrated to the Android embedding v2. Follow the steps on
https://flutter.dev/go/android-project-migration and re-run this command.
Somehow is it expecting that there should be only one example and that too with example folder only ?
Can someone help, and if possible point me to a plugin project where it's using multiple examples ?
After trying multiple ways to deal with the situation,
I ended up with a good enough solution.
I moved entire flutter repo inside an sdk directory, and then Introduced a samples folder at root level which can contain multiple sample applications.
sdk itself has a default sample app under example folder which I kept so sdk can ship with one example project.
Final structure look like below,
- Root
- sdk
- Flutter plugin project (like android/example/ios/lib directory etc.)
- samples
- sampleOne
- sampleTwo
I then mentioned relative path to sdk for sampleOne or sampleTwo inside their pubspec.yaml like path: ../../sdk/
When I want to open sampleOne in AndroidStudio, I import sampleOne directory and it works like charm. Make sure you don't import entire samples directory or sampleOne/Android.
For any regular Flutter commands for plugin, I run them inside sdk directory and everything works fine as expected. So I would run publish or pub get inside root/sdk directory

Flutter Package URI doesn't exist inside package's test

Inside the Flutter project directory, I created a package with the following command :
flutter create --template=package emoji_support
After it completes creating the package and completes flutter get.
But..
Files inisde package's /test directory can't find files inside package's /lib directory :
import 'package:flutter_test/flutter_test.dart'; is resolved ✅
But...
import 'package:emoji_support/emoji_support.dart'; is not resolved ❌
File structure
What error am I making here?
I created a package inside my project flutter_example_file_picker with flutter create --template=package emoji_support
Then in the pubspec.yaml of the project (not the one inside the plugin) flutter_example_file_picker > pubspec.yaml I added to the dependenceis the package
dependencies:
flutter:
sdk: flutter
emoji_support:
path: ./emoji_support
and the problem resolved, I could run the test with no problem, also tried with a relative path without adding it to the dependencies and worked too
UPDATE
I think I undestand how to fix it without adding it to the pubspec, when creating a plugin inside a project, the flutter plugin of AndroidStudio (or VS) is still working in the main route (check the terminal dir and it will be C:/.../Workspace_Android\hundreddaysofflutter> so it doesnt detect the inner plugin) and doesn't update to detect the new folder. Even if it's red and says it cannot detect the URI.
You can ignore it and try to run the test and see if it detects the inner dart_tool with package_config.json, then it will run succesfully and the problem will dissapear. The second option is to move to the folder where the plugin is (in the terminal cd my_plugin_name_folder or just file open and open the plugin).
Run flutter get pub (it does it automatically when creting the first time a project but sometimes when you create one inside another it doesnt do it) to run for the first time the package and create the package_config.json inside dart_tool (the dart_tool of the plugin), at the end of the file you can see the name and rootUri of the package, now you can use it in your test nad it should detect it correctly. Sometimes it's generated but it seems it doesn't update correctly and the IDE doesn't know even if it's there.
This is just a problem that occurs , when you are adding new sub_directories in vsCode.
just a simple exist and re-open will fix the issue.
tell me if that fix it.

How to develop Flutter app and related package at same time in VS Code

I have a Flutter app and a package folder loaded in VS code at the same time within a workspace. What entry do I need to make to my app's pubspec.yaml file to ensure that changes I've made to the package are compiled and included whenever I hot reload or restart the app? What would be an alternate strategy if this is not possible?
If your pubspec.yaml refers to your package with a path then I would expect this to happen automatically. If not, I would consider it a bug. Please file an issue at https://github.com/Dart-Code/Dart-Code and include a log file generated by running the Dart: Capture Logs command and as much info about your project layout as possible (a clonable repo to repro would be perfect).

How can I generate test coverage of untested files on my flutter tests?

I'm making Widget and Unit testing on my app, I make the tests normally, according to the basic guides, and to generate the coverage I use:
flutter test --coverage
However I just can see the coverage of the files directly tested, I'd like to see the other files (with 0% of coverage), then I could check the real coverage of my code.
Is there a way for doing that?
I have created a small helper script to help with the full coverage report generation. It scans your lib directory for *.dart files (excluding *.g.dart) and imports them into the generated test/coverage_test.dart file. Having this generated file coverage analyser will go through the whole project next time you run it. To use the script:
Clone it to any location
wget https://raw.githubusercontent.com/priezz/dart_full_coverage/master/dart-coverage-helper
Make it executable
chmod +x dart-coverage-helper
Ensure that the location of the script is in your PATH environment variable (or just put it into the root of your project).
Run from the root of your Dart/Flutter project
dart-coverage-helper
Then generate the coverage report as usual
flutter test --coverage # for Flutter project
# or
pub run test_coverage # for Dart project