How do I make a shared directory to share code in flutter between integration tests and widget tests? - flutter

How do i create a directory of shared code between Integration tests and Widget tests in flutter? There could be quite a bit of it since they use the same API now as of the new official
in Android native you can do the below in order to have code that is shared between local non-instrumented UI tests (Roboelectric under the hood) and instrumented Espresso tests with the Espresso API. And of course miscellaneous shared helpers, mocks, whatever.
How do i achieve a similar goal in Flutter?
// This allows us to share classes between androidTest & test directories.
android.sourceSets {
test {
java.srcDirs += "$projectDir/src/testShared"
}
androidTest {
java.srcDirs += "$projectDir/src/testShared"
}
}

This could be achieved using a shared package (namely test_base).
Create a package for your test codes (confusion avoidance: your code will be written in its lib folder).
run:
flutter create --template=package test_base
Add your main package as a dependency to test_base so you have access to your main package codes.
test_base/pubspec.yaml:
dependencies:
<your-package-name>:
path: ../
Add test_base to your main library as a dev-dependency.
./pubspec.yaml:
dev_dependencies:
test_base:
path: test_base/
Now you can import the test_base package in your widget/integration tests.
Note: This way within test_base you can import packages of transitive dependencies (dependencies of your main package) without a compilation error. But it is discouraged in dart to explicitly import packages of dependencies you don't explicitly define in test_base/pubspec.yaml and it's probable to face linter or compilation warnings in future versions of dart (currently it's 2.13.4 and no warnings). Which means then you need to explicitly define those dependencies in test_base/pubspec.yaml. Though I think this is the case it would be harmless to import transitive dependencies packages.

Related

dart reflectable in library

I'm making a library for persistence implementation. For that I want to use reflection to be able to restore an object from the storage.
I can't use dart:mirrors because this library will be use also in Flutter application. So I'm trying to use reflectable package. And here I'm getting a major issue with understanding of how it should work. The doc tells reflectable uses build package and that some files must be generated. But I couldn't find anywhere whether this generation should happen for each file in my library package or just those where reflectable is used.
My library project structure is standard:
/examples
/lib
/lib/src
/lib/my_package.dart
/test
/pubspec.yaml
When I run dart run build_runner or dart run build_runner lib from the project root I see for each file in /example and /tests a matching file is generated. But in /lib folder nothing is generated. Only file where I'm intending to use reflectable is /lib/src/persistence/persistence_model.dart. I've created a file /lib/build.yaml of following content:
targets:
$default:
builders:
reflectable:
generate_for:
- src/persistence/persistence_model.dart
options:
formatted: true
but it seems to have no effect.
So, what should be right approach?
And probably a side question or rather consideration: isn't using reflection in Dart and Flutter excessively complex comparing to other languages (Python, C#, Java)?

Can not declare dependency to bblanchon/ArduinoJson in my library.json (platform io)

I am creating a platform IO library for one of my projects, which depends on ArduinoJson from bblanchon.
I have declared the dependency in my library.json file (both with the name as well as with the git repository as reference - see below). But when I try to compile a project that is using my library, the ArduinoJson library is not added to the dependency tree and compiling fails as the header file ArduinoJson.h is not found.
If I add the ArduinoJson as a dependency to the platformio.ini it is added to the dependency tree and the software compiles without issues. However this is not a clean solution and I want to now how I should declare the dependency in my library so that I do not have to add ArduinoJson to every project I build with this library.
Do you have any ideas how I can make this work?
Thx Christian
Declaration of dependency as suggested by pio library finder:
"dependencies" :
{
...
"bblanchon/ArduinoJson" : "*"
}
As reference to the repository
"dependencies" :
{
...
"bblanchon/ArduinoJson" : "https://github.com/bblanchon/ArduinoJson.git"
}
This one had me going for a bit as well. Here are the steps I took to solve.
Add to platformio.ini:
lib_deps = Arduino-libraries/Arduino_JSON # 0.1.0
Verify that you have the folder /.pio/libdeps/Arduino_JSON
Use #include <Arduino_JSON> in you code.

How to make VS code able to lookup code in subproject directory for autocompletion?

I have project structure
-core_data
-core_domain
-core_ui
-core_launcher
The dependency of these 4 projects is
core_launcher -> core_ui -> core_domain -> core_data
4 projects are located in the same directory and I include one to another via pubspec.yaml file (for example core_launcher/pubspec.yaml):
dependencies:
flutter:
sdk: flutter
core_ui:
path: ../core_ui
The same thing I do with all projects to make dependency hierarchy.
The problem is that I can import all files from core_ui subproject when I'm currently editing some file in core_launcher but VSCode doesn't see any classes from his parents
(core_domain & core_data).
However, I can input import 'blah-blah-blah manually and VSCode see this class and import works well, but I can't do that with hit Alt+Enter that I do for fast-import.
So, I'm wondering why autocomplete is not working for inherited libraries.
Somebody had the same issue?
Code completion will only show classes from your direct dependencies. There are two possible reasons for this:
Relying on transitive dependencies is not a good idea, because it's possible that your dependencies will remove or change their dependencies and not consider that a breaking change.
If code completion listed all classes from all transitive dependencies the code completion list would be huge and include classes from packages you do not recognise (because they are just other packages dependencies). This would be a bad user experience and make it easy to accidentally rely on packages that are not listed in your pubspec.yaml.
The fix is to explicitly list core_domain and core_data in your pubspec.yaml too, because if your project is using classes from them, then they are dependencies.

Error saying "Module Not Found" when adding SPM which uses other SPMs as dependencies within itself

I have been creating a Swift Package Manager. It uses 2 other SPMs within itself. SPM compiles fine when compiled independently. As soon as the project is imported into an Xcode project I get a compiler error saying that:
No such module 'ModuleName'
Note: The ModuleName in the above error corresponds to the package imported within the package that is being imported to my project.
I have been stuck on this for a pretty while now and have tried the following:
Removed and readded the SPMs to dependencies to my SPM, and then tried importing my SPM to my project (I did this before and after each of the other steps too).
Checked to see where these packages where being added as dependencies. It shows up in the SPM main target Module -> Build Phases -> Link binary with libraries. I additionally added it to the Dependencies section to see if it changes anything.
Tried adding SPMs to ModulePackageDescription target to Dependencies section.
Added the dependencies in the Package.swift file as follows.
dependencies: [
// Dependencies declare other packages that this package depends on.
.package(url: "package1_url", .branch("master")),
.package(url: "package2_url", .branch("master"))
]
Adding this would import the other dependencies to my Xcode project. I don't exactly want this to happen because in case I try to use another version of the SPM that is being imported within my SPM, it would cause conflict between the two versions. But I'm willing to do this if it's the right way to go. But even adding dependencies in Package.swift didn't work for me. How would I resolve this issue? Let me know if anyone has faced the same issue.
Are the libraries public classes also need to contain constructors?
public struct NumbersA {
public init () {
}
}
also add them to the dependency Package.swft->dependencies: ["NumbersA"]),

Local swift package with local dependency

I have a project that I plan on developing in modules, the final application will be any number of the modules built together based on a configuration. I have a swift package that has all of my common code it it, we can call that the platform package. I then went to create my first feature, this went just fine however when I created the wrapper application to pull in each feature, I got this error from SPM in xcode11:
package 'Platform' is required using a revision-based requirement and it depends on local package
'Feature1', which is not supported.
Looking at the code base for SPM here (line 72)
https://github.com/apple/swift-package-manager/blob/master/Sources/PackageGraph/DependencyResolver.swift
It looks like this is something that is just not supported, the mixing of local and remote dependencies? Is this a limitation of SPM / should I be trying to use another tool for this type of app architecture?
In my case, I was trying to add a package, which I was developing, and its Package.swift contained dependencies of the form:
dependencies: [
.package(path: "../PackageName"),
// etc
Changing the references to specific repos solved the problem:
dependencies: [
.package(path: "http://github.com/..."),
// etc