-
Notifications
You must be signed in to change notification settings - Fork 2
Add Danger check for @UserDefaultsWrapper additions #18
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
Merged
+146
−8
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0b6c659
Add Danger check for @UserDefaultsWrapper additions
ayoy c3519ab
Fix a typo :)
ayoy 9be0041
Merge branch 'main' into dominik/user-defaults-wrapper
ayoy 5061c1c
Remove unneded code
ayoy fd7fdc7
KeyValueStoring -> KeyedStoring
ayoy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| jest.mock("danger", () => jest.fn()) | ||
| import danger from 'danger' | ||
| const dm = danger as any; | ||
|
|
||
| import { userDefaultsWrapper } from '../org/allPRs' | ||
|
|
||
| beforeEach(() => { | ||
| dm.addedLines = "" | ||
| dm.fail = jest.fn().mockReturnValue(true); | ||
|
|
||
| dm.danger = { | ||
| git: { | ||
| diffForFile: async (_filename) => { | ||
| return { added: dm.addedLines } | ||
| }, | ||
| modified_files: [ | ||
| "ModifiedFile.swift" | ||
| ], | ||
| created_files: [ | ||
| "CreatedFile.swift" | ||
| ] | ||
| } | ||
| } | ||
| }) | ||
|
|
||
| describe("@UserDefaultsWrapper checks", () => { | ||
| it("does not fail with no changes to Swift files", async () => { | ||
| dm.danger.git.modified_files = [] | ||
|
|
||
| await userDefaultsWrapper() | ||
| expect(dm.fail).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it("does not fail with no diff in Swift files", async () => { | ||
| dm.danger.git.diffForFile = async (_filename) => {} | ||
|
|
||
| await userDefaultsWrapper() | ||
| expect(dm.fail).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it("does not fail with no additions", async () => { | ||
| await userDefaultsWrapper() | ||
| expect(dm.fail).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it("does not fail with non-@UserDefaultsWrapper additions", async () => { | ||
| dm.addedLines = ` | ||
| + let subscription = PrivacyProSubscription(status: .inactive) | ||
| + let isSubscribed: Bool | ||
| + var billingPeriod: String? | ||
| + public let startedAt: Int? | ||
| + internal let expiresOrRenewsAt: Int? | ||
| + private var paymentPlatform: String? | ||
| + let status: String? | ||
| ` | ||
|
|
||
| await userDefaultsWrapper() | ||
| expect(dm.fail).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it("does not fail with UserDefaultsWrapper usage additions", async () => { | ||
| dm.addedLines = ` | ||
| + let userDefaults: UserDefaults = UserDefaultsWrapper<Any>.sharedDefaults | ||
| ` | ||
|
|
||
| await userDefaultsWrapper() | ||
| expect(dm.fail).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it("does not fail with commented out @UserDefaultsWrapper additions", async () => { | ||
| dm.addedLines = ` | ||
| + // @UserDefaultsWrapper(key: .homePageIsFavoriteVisible, defaultValue: true) | ||
| ` | ||
|
|
||
| await userDefaultsWrapper() | ||
| expect(dm.fail).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it("fails with @UserDefaultsWrapper additions", async () => { | ||
| dm.addedLines = ` | ||
| + @UserDefaultsWrapper(key: .homePageIsFavoriteVisible, defaultValue: true) | ||
| ` | ||
|
|
||
| await userDefaultsWrapper() | ||
| expect(dm.fail).toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it("fails with @UserDefaultsWrapper additions with open bracket only", async () => { | ||
| dm.addedLines = ` | ||
| + @UserDefaultsWrapper( | ||
| + key: .homePageIsFavoriteVisible, | ||
| + defaultValue: true | ||
| + ) | ||
| ` | ||
|
|
||
| await userDefaultsWrapper() | ||
| expect(dm.fail).toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it("fails with @UserDefaultsWrapper additions without parameters", async () => { | ||
| dm.addedLines = ` | ||
| + @UserDefaultsWrapper | ||
| + private var didCrashDuringCrashHandlersSetUp: Bool | ||
| ` | ||
|
|
||
| await userDefaultsWrapper() | ||
| expect(dm.fail).toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it("does not fail with @UserDefaultsWrapper deletions", async () => { | ||
| dm.addedLines = ` | ||
| - @UserDefaultsWrapper | ||
| - private var didCrashDuringCrashHandlersSetUp: Bool | ||
| - | ||
| - @UserDefaultsWrapper( | ||
| - key: .homePageIsFavoriteVisible, | ||
| - defaultValue: true | ||
| - ) | ||
| - | ||
| - @UserDefaultsWrapper(key: .homePageIsFavoriteVisible, defaultValue: true) | ||
| - var isFavoriteVisible: Bool | ||
| ` | ||
|
|
||
| await userDefaultsWrapper() | ||
| expect(dm.fail).not.toHaveBeenCalled() | ||
| }) | ||
| }) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Regex misses single-line property wrapper declarations
Medium Severity
The regex
/^\+(?!\s*\/\/)\s*@UserDefaultsWrapper(?:\((?:.*\))?)?$/requires@UserDefaultsWrapperto be at the end of the line due to the$anchor. This means valid Swift declarations where the wrapper and property are on the same line (e.g.,@UserDefaultsWrapper var myProperty: Boolor@UserDefaultsWrapper(key: .foo) var myProperty: Bool) won't be detected. Developers could bypass the check by using single-line syntax instead of placing the attribute on its own line.Please tell me if this was useful or not with a 👍 or 👎.
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.
This is enough for now, we only use UserDefaultsWrapper on a separate line.