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

Make MockBuilder support build_extensions option. #685

Merged
merged 6 commits into from
Aug 18, 2023
Merged

Make MockBuilder support build_extensions option. #685

merged 6 commits into from
Aug 18, 2023

Conversation

LuisDuarte1
Copy link
Contributor

@LuisDuarte1 LuisDuarte1 commented Aug 10, 2023

This PR changes the behaviour of the buildMocks factory, in order to support the build_extensions option.

This is useful in order to change the destination directory of the mocks, allowing for more customizability as in this article.

This change is a bit opinionated, because it forces the end-user to have a output filename pattern that ends with .mocks.dart in order to be consistent with the already generated files from mockito. It also forces the user, to have an input pattern ending with .dart.

Closes #545


  • I’ve reviewed the contributor guide and applied the relevant portions to this PR.
Contribution guidelines:

Note that many Dart repos have a weekly cadence for reviewing PRs - please allow for some latency before initial review feedback.

Copy link
Contributor

@yanok yanok left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, but we need:

  1. Some documentation update to mention this feature, maybe even a FAQ entry;
  2. Maybe add build_extensions to example/ project?

@yanok
Copy link
Contributor

yanok commented Aug 11, 2023

Corr: actually example/ is not a separate project, so maybe just change the top-level build.yaml file to show how it works?

@LuisDuarte1
Copy link
Contributor Author

LGTM, but we need:

  1. Some documentation update to mention this feature, maybe even a FAQ entry;
  2. Maybe add build_extensions to example/ project?

I agree so I've added them.

Also, I found some edge cases with my implementation so I took care of them, I'll list them:

  • Conflicting outputs, the user can supply multiple build_extensions and the builder will throw if it finds a conflicting output
  • If the user provided a build_extension that doesn't match all .dart files that can be processed, the non-matched files would be ignored
  • Relative includes in the .mocks.dart weren't being properly resolved if the output was in a different directory.

@LuisDuarte1 LuisDuarte1 requested a review from yanok August 11, 2023 17:12
lib/src/builder.dart Outdated Show resolved Hide resolved
@yanok yanok self-requested a review August 14, 2023 10:29
Copy link
Contributor

@yanok yanok left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Copy link
Contributor

@yanok yanok left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please accept my changes and squash all commits into one?

build.yaml Outdated Show resolved Hide resolved
build.yaml Outdated Show resolved Hide resolved
lib/src/builder.dart Outdated Show resolved Hide resolved
lib/src/builder.dart Outdated Show resolved Hide resolved
@natebosch
Copy link
Member

and squash all commits into one?

We should be able to squash for the internal commit - generally it's easier to avoid force-pushing over shared history in a PR.

This is useful to change the destination of the generated files.
i.e: instead of having them on the same folder,
you can specify a diferent folder for the mocks.

Closes #545
@LuisDuarte1 LuisDuarte1 requested a review from yanok August 15, 2023 20:22
@LuisDuarte1
Copy link
Contributor Author

and squash all commits into one?

We should be able to squash for the internal commit - generally it's easier to avoid force-pushing over shared history in a PR.

Well didn't read that in time, sorry ^^ should I revert it then?

@natebosch
Copy link
Member

should I revert it then?

No need - we can work with it either way. I was just hoping to inform on best practices since I frequently use shared PR history when reviewing in dart-lang reps 😄

Comment on lines 80 to 81
// While it can be acceptable that we get more than 2 allowedOutputs,
// because it's the general one and the user defined one. Having
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand this - user defined configuration for the .dart input will override the default. I would expect that the allowed outputs should always have a single value.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I see, the examples are using a ^test in the build extensions to substitute for a generate_for. cc @jakemac53 - did we predict this would happen when we enhanced the build extension syntax?

Maybe it would be better to only do a full override and not do a merge.

  @override
  final Map<String, List<String>> buildExtensions;

  MockBuilder({Map<String, List<String>>? buildExtensions})
      : buildExtensions = buildExtensions ??
            const {
              '.dart': ['.mocks.dart'],
            };

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having the commit history would be helpful now ahah, but that was my first implementation, but there's a reason I changed it into a merge. I've discovered this when making the example for this config, example/build_extensions, when doing the full override and only having an input pattern that only partially covers all .dart files, the unmatched .dart files wouldn't be processed, even though there's a generate_for for those files. So my fix was to merge the user rules with the generic one ( .dart=>.mocks.dart)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when doing the full override and only having an input pattern that only partially covers all .dart files, the unmatched .dart files wouldn't be processed, even though there's a generate_for for those files.

I think not processing them is preferable to silently falling back on potentially unexpected behavior. I think it is reasonable to expect the overridden config to cover all inputs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think not processing them is preferable to silently falling back on potentially unexpected behavior. I think it is reasonable to expect the overridden config to cover all inputs.

I can understand your perspective, in this case it's best to be consistent and not introduce any unexpected behavior, even though I think my approach is more ergonomic for developers. I'll try to change it then.

lib/src/builder.dart Outdated Show resolved Hide resolved
Co-authored-by: Nate Bosch <nbosch1@gmail.com>
@yanok
Copy link
Contributor

yanok commented Aug 16, 2023

We should be able to squash for the internal commit - generally it's easier to avoid force-pushing over shared history in a PR.

Well, actually Copybara already does the squash, so it's mostly about getting a nicer CL description, as now it will list all the commits. We should probably just fix our Copybara config to copy the PR description into CL.

@yanok
Copy link
Contributor

yanok commented Aug 16, 2023

We need to wait for the resolution of the discussion above. Also please convert the added double-quotes in the test file into single quotes: we have a lint complaining about that internally.

The previous behaviour was that if the user adds a build_extension,
the build would inject the generic build extension to cover all files.
This was useful to simplify the build.yaml file at the cost of
unexpected behaviour.

To avoid potencial unexpected behaviour,
if the user provides custom build_extensions we assume that the patterns
cover all files and therefore we do not merge the generic
build_extension.
lib/src/builder.dart Outdated Show resolved Hide resolved
Co-authored-by: Nate Bosch <nbosch1@gmail.com>
@yanok
Copy link
Contributor

yanok commented Aug 18, 2023

Now the formatter complains :) Could you please re-format it?

@LuisDuarte1
Copy link
Contributor Author

Now the formatter complains :) Could you please re-format it?

Done. It seems that Dart 3.0 has a different formatting than Dart 2.19, a quick setup in github codespaces allowed me to format the files in the CI version 😅.

@yanok
Copy link
Contributor

yanok commented Aug 18, 2023

Done. It seems that Dart 3.0 has a different formatting than Dart 2.19, a quick setup in github codespaces allowed me to format the files in the CI version 😅.

Arghh, I see. Yes, I think we did change the formatter some time ago. Damn, then we should have just bumped the required SDK version I guess... I wonder if now formatter will start complaining internally, since we use more or less HEAD version... But let's wait.

@copybara-service copybara-service bot merged commit e54a006 into dart-lang:master Aug 18, 2023
7 checks passed
@yanok
Copy link
Contributor

yanok commented Aug 18, 2023

Nope, it worked fine, thanks!

mosuem pushed a commit to dart-lang/test that referenced this pull request Oct 17, 2024
…ld-extensions

PiperOrigin-RevId: 558114686
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Change directory for generated mocks
3 participants