-
-
Notifications
You must be signed in to change notification settings - Fork 508
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
Add shared instance to the Printer #483
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for doing this work.
It might be good to consider what other refactorings we might want to do in follow up pull requests for similar objects.
e.g.
- FileHander
- System
- ResourceLocator
- ...
/// Mocks the shared context instance and returns the mock. | ||
/// | ||
/// - Returns: Mocked context. | ||
static func mockSharedContext() -> MockContext { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be good to set this up automatically using XCTestObservation
(see https://developer.apple.com/documentation/xctest/xctestobservation)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good idea 😛
var deprecator: Deprecating { get } | ||
} | ||
|
||
public final class Context: Contexting { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about just exposing public var printer: Printing
and public var deprecator: Deprecating
as top level variables. I was trying to understand what the benefit of grouping these in a single object is.
Context.shared.printer.print(deprecation: message)
printer.print(deprecation: message)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good point. I can remove one level of indirection there. Thanks for the suggestion @ollieatkinson
@ollieatkinson I got rid of the What I did instead was extending the |
@kwridan / @marciniwanicki could you have a look at this PR? |
SwiftLint found issuesWarnings
Generated by 🚫 Danger |
Codecov Report
@@ Coverage Diff @@
## master #483 +/- ##
==========================================
+ Coverage 92.4% 92.63% +0.22%
==========================================
Files 348 347 -1
Lines 17809 17752 -57
==========================================
- Hits 16457 16444 -13
+ Misses 1352 1308 -44
Continue to review full report at Codecov.
|
Codecov Report
@@ Coverage Diff @@
## master #483 +/- ##
==========================================
+ Coverage 92.4% 92.63% +0.22%
==========================================
Files 348 347 -1
Lines 17809 17752 -57
==========================================
- Hits 16457 16444 -13
+ Misses 1352 1308 -44
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lots of similar changes, and running locally still shows the correct output! 👍
Thanks for making the sweeping changes @pepibumur Personally, I quite liked the previous approach of having explicit dependencies. I agree that passing them around everywhere was quite cumbersome - for that @marciniwanicki's suggestions would help address that not only for those lower level components but for some of the other bigger ones too. I don't have a strong view on this, so let's give the changes here a go and revisit it when needed 🙂 The good thing here is we can still control this for testing and when using Tuist as a library 👍 |
Resolves #351
Short description 📝
Our current approach of using utilities like the
FileHandler
,System
, orPrinter
, is cumbersome and requires a lot of boilerplate code and passing instances around.Solution 📦
I added a
shared
static instance to thePrinter
.Any class in the project can access the shared instance of
Printer
easily.That removes the need of having to inject the dependencies through the constructor and define instance variables to hold references to them.
When it comes to testing, I added a convenience static method that can be used from a test case to replace all utilities with their mocks. Here's an example of how the tests look:
Moreover, I added an extension to
XCTestCase
with some XCT assertion functions that can be used with context: