-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Feature flags #4582
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
Merged
Feature flags #4582
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| /// FeatureFlag exposes a series of features to be conditionally enabled on | ||
| /// different builds. | ||
| @objc | ||
| enum FeatureFlag: Int { | ||
| /// My Sites > Site > People | ||
| /// Development on hold while we focus on Me | ||
| case People | ||
| /// Me > My Profile | ||
| /// Needs better UI for loading/failed states | ||
| case MyProfile | ||
|
|
||
| /// Returns a boolean indicating if the feature is enabled | ||
| var enabled: Bool { | ||
| switch self { | ||
| case .People: | ||
| return build(.Debug) | ||
| case .MyProfile: | ||
| return build(.Debug, .Alpha, .Internal) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Objective-C bridge for FeatureFlag. | ||
| /// | ||
| /// Since we can't expose properties on Swift enums we use a class instead | ||
| class Feature: NSObject { | ||
| /// Returns a boolean indicating if the feature is enabled | ||
| static func enabled(feature: FeatureFlag) -> Bool { | ||
| return feature.enabled | ||
| } | ||
| } | ||
|
|
||
| /// Represents a build configuration. | ||
| enum Build: Int { | ||
| /// Development build, usually what you get when you run from Xcode | ||
| case Debug | ||
| /// Daily buiilds released internally for Automattic employees | ||
| case Alpha | ||
| /// Beta released internally for Automattic employees | ||
| case Internal | ||
| /// Production build released in the app store | ||
| case AppStore | ||
|
|
||
| /// Returns the current build type | ||
| static var current: Build { | ||
| if let override = _overrideCurrent { | ||
| return override | ||
| } | ||
|
|
||
| #if DEBUG | ||
| return .Debug | ||
| #elseif ALPHA_BUILD | ||
| return .Alpha | ||
| #elseif INTERNAL_BUILD | ||
| return .Internal | ||
| #else | ||
| return .AppStore | ||
| #endif | ||
| } | ||
|
|
||
| /// For testing purposes only | ||
| static var _overrideCurrent: Build? = nil | ||
| } | ||
|
|
||
| /// Returns true if any of the given build types matches the current build | ||
| /// | ||
| /// Example: | ||
| /// | ||
| /// let enableExperimentalStuff = build(.Debug, .Internal) | ||
| func build(any: Build...) -> Bool { | ||
| return any.reduce(false, combine: { previous, buildValue in | ||
| previous || Build.current == buildValue | ||
| }) | ||
| } | ||
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,40 @@ | ||
| import Nimble | ||
| import XCTest | ||
| @testable import WordPress | ||
|
|
||
| class FeatureFlagTest: XCTestCase { | ||
|
|
||
| func testBuild() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pretty looking tests ;) |
||
| Build.withCurrent(.Debug) { | ||
| expect(build(.Debug)).to(beTrue()) | ||
| expect(build(.Debug, .Alpha)).to(beTrue()) | ||
| expect(build(.Alpha)).to(beFalse()) | ||
| expect(build(.Internal)).to(beFalse()) | ||
| expect(build(.AppStore)).to(beFalse()) | ||
| } | ||
|
|
||
| Build.withCurrent(.AppStore) { | ||
| expect(build(.Debug)).to(beFalse()) | ||
| expect(build(.Alpha)).to(beFalse()) | ||
| expect(build(.Internal)).to(beFalse()) | ||
| expect(build(.AppStore)).to(beTrue()) | ||
| expect(build(.Internal,.AppStore)).to(beTrue()) | ||
| } | ||
| } | ||
|
|
||
| func testEnsureDisabledFeaturesInProduction() { | ||
| Build.withCurrent(.AppStore) { | ||
| expect(FeatureFlag.People.enabled).to(beFalse()) | ||
| expect(FeatureFlag.MyProfile.enabled).to(beFalse()) | ||
| } | ||
| } | ||
|
|
||
| } | ||
|
|
||
| extension Build { | ||
| static func withCurrent(value: Build, @noescape block: () -> Void) { | ||
| Build._overrideCurrent = value | ||
| block() | ||
| Build._overrideCurrent = nil | ||
| } | ||
| } | ||
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.
On the PR description you say that the checks pass from compile to run time, but this looks to be set on compile time?
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.
I think that bit wasn't clear. The build type is still defined by build parameters, but using this code does the check at runtime. For instance: