-
-
Notifications
You must be signed in to change notification settings - Fork 27
Fix version sorting by using SemVer instead of publishedDate #85
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
Open
Moustachauve
wants to merge
5
commits into
main
Choose a base branch
from
hotfix/version-sorting
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b011a03
Fix version sorting by using SemVer instead of publishedDate
Moustachauve 0a11e15
Refactor: use init() for test setup
Moustachauve 6ea6cc2
Refactor: use init() + @Suite(.serialized) for test setup and isolation
Moustachauve dccd0d2
Address PR feedback: use SemanticVersion in getNewerReleaseTag, optim…
Moustachauve 767826d
Fix CI: use programmatic NSManagedObjectModel in tests, fix unused-va…
Moustachauve 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
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,187 @@ | ||
| import Testing | ||
| import CoreData | ||
| @testable import WLED | ||
|
|
||
| // .serialized prevents the two test suite instances Xcode runs concurrently from | ||
| // interfering with each other via the shared in-memory Core Data store. | ||
| @Suite(.serialized) | ||
| @MainActor | ||
| struct ReleaseServiceTests { | ||
|
|
||
| // Hold a strong reference to prevent the container (and its in-memory store) from | ||
| // being deallocated while a test is running. | ||
| let container: NSPersistentContainer | ||
| let context: NSManagedObjectContext | ||
| let service: ReleaseService | ||
|
|
||
| init() throws { | ||
| // Build a fresh in-memory store using NSInMemoryStoreType so that: | ||
| // 1. No on-disk store or migration is attempted (avoids CI environment issues). | ||
| // 2. Each test suite instance gets a completely isolated store. | ||
| let model = Self.makeModel() | ||
| let newContainer = NSPersistentContainer(name: UUID().uuidString, managedObjectModel: model) | ||
| let description = NSPersistentStoreDescription() | ||
| description.type = NSInMemoryStoreType | ||
| newContainer.persistentStoreDescriptions = [description] | ||
|
|
||
| var loadError: Error? | ||
| newContainer.loadPersistentStores { _, error in | ||
| loadError = error | ||
| } | ||
| precondition(loadError == nil, "Failed to load in-memory store: \(String(describing: loadError))") | ||
|
|
||
| container = newContainer | ||
| context = newContainer.viewContext | ||
| service = ReleaseService(context: context) | ||
| } | ||
|
|
||
| /// Builds a minimal NSManagedObjectModel containing only the Version entity, | ||
| /// which is all ReleaseService needs. This avoids loading from disk entirely. | ||
| private static func makeModel() -> NSManagedObjectModel { | ||
| let model = NSManagedObjectModel() | ||
|
|
||
| let versionEntity = NSEntityDescription() | ||
| versionEntity.name = "Version" | ||
| versionEntity.managedObjectClassName = NSStringFromClass(Version.self) | ||
|
|
||
| let tagNameAttr = NSAttributeDescription() | ||
| tagNameAttr.name = "tagName" | ||
| tagNameAttr.attributeType = .stringAttributeType | ||
|
|
||
| let nameAttr = NSAttributeDescription() | ||
| nameAttr.name = "name" | ||
| nameAttr.attributeType = .stringAttributeType | ||
| nameAttr.isOptional = true | ||
|
|
||
| let descAttr = NSAttributeDescription() | ||
| descAttr.name = "versionDescription" | ||
| descAttr.attributeType = .stringAttributeType | ||
| descAttr.isOptional = true | ||
|
|
||
| let isPrereleaseAttr = NSAttributeDescription() | ||
| isPrereleaseAttr.name = "isPrerelease" | ||
| isPrereleaseAttr.attributeType = .booleanAttributeType | ||
| isPrereleaseAttr.defaultValue = false | ||
|
|
||
| let publishedDateAttr = NSAttributeDescription() | ||
| publishedDateAttr.name = "publishedDate" | ||
| publishedDateAttr.attributeType = .dateAttributeType | ||
| publishedDateAttr.isOptional = true | ||
|
|
||
| let htmlUrlAttr = NSAttributeDescription() | ||
| htmlUrlAttr.name = "htmlUrl" | ||
| htmlUrlAttr.attributeType = .stringAttributeType | ||
| htmlUrlAttr.isOptional = true | ||
|
|
||
| versionEntity.properties = [tagNameAttr, nameAttr, descAttr, isPrereleaseAttr, publishedDateAttr, htmlUrlAttr] | ||
| model.entities = [versionEntity] | ||
|
|
||
| return model | ||
| } | ||
|
|
||
| /// Inserts a Version entity into the context. | ||
| @discardableResult | ||
| private func insertVersion( | ||
| tagName: String, | ||
| isPrerelease: Bool = false, | ||
| publishedDate: Date = Date() | ||
| ) -> Version { | ||
| let version = Version(context: context) | ||
| version.tagName = tagName | ||
| version.name = "v\(tagName)" | ||
| version.versionDescription = "" | ||
| version.isPrerelease = isPrerelease | ||
| version.publishedDate = publishedDate | ||
| return version | ||
| } | ||
|
|
||
| // MARK: - getLatestVersion tests | ||
|
|
||
| @Test func latestVersionUsesSemVerNotPublishedDate() throws { | ||
| // v0.15.5 has a MORE RECENT publishedDate, but v0.16.0 is a higher semver | ||
| insertVersion(tagName: "0.15.5", publishedDate: Date(timeIntervalSince1970: 2_000_000)) | ||
| insertVersion(tagName: "0.16.0", publishedDate: Date(timeIntervalSince1970: 1_000_000)) | ||
| try context.save() | ||
|
|
||
| let latest = service.getLatestVersion(branch: .beta) | ||
| #expect(latest?.tagName == "0.16.0") | ||
| } | ||
|
|
||
| @Test func latestStableVersionExcludesPrereleases() throws { | ||
| insertVersion(tagName: "0.15.0") | ||
| insertVersion(tagName: "0.16.0-b1", isPrerelease: true) | ||
| try context.save() | ||
|
|
||
| let latest = service.getLatestVersion(branch: .stable) | ||
| #expect(latest?.tagName == "0.15.0") | ||
| } | ||
|
|
||
| @Test func latestBetaVersionIncludesPrereleases() throws { | ||
| insertVersion(tagName: "0.15.0") | ||
| insertVersion(tagName: "0.16.0-b1", isPrerelease: true) | ||
| try context.save() | ||
|
|
||
| let latest = service.getLatestVersion(branch: .beta) | ||
| #expect(latest?.tagName == "0.16.0-b1") | ||
| } | ||
|
|
||
| @Test func latestVersionExcludesNightlyTag() throws { | ||
| insertVersion(tagName: "nightly", publishedDate: Date(timeIntervalSince1970: 9_999_999)) | ||
| insertVersion(tagName: "0.15.0") | ||
| try context.save() | ||
|
|
||
| let latest = service.getLatestVersion(branch: .beta) | ||
| #expect(latest?.tagName == "0.15.0") | ||
| } | ||
|
|
||
| @Test func latestVersionWithMultipleVersions() throws { | ||
| // Insert versions with intentionally misleading published dates | ||
| insertVersion(tagName: "0.14.0", publishedDate: Date(timeIntervalSince1970: 3_000_000)) | ||
| insertVersion(tagName: "0.15.5", publishedDate: Date(timeIntervalSince1970: 4_000_000)) | ||
| insertVersion(tagName: "0.16.0", publishedDate: Date(timeIntervalSince1970: 1_000_000)) | ||
| insertVersion(tagName: "0.14.1", publishedDate: Date(timeIntervalSince1970: 5_000_000)) | ||
| try context.save() | ||
|
|
||
| let latest = service.getLatestVersion(branch: .beta) | ||
| #expect(latest?.tagName == "0.16.0") | ||
| } | ||
|
|
||
| @Test func latestVersionReturnsNilWhenEmpty() throws { | ||
| let latest = service.getLatestVersion(branch: .beta) | ||
| #expect(latest == nil) | ||
| } | ||
|
|
||
| // MARK: - getNewerReleaseTag tests | ||
|
|
||
| @Test func newerReleaseTagReturnsLatestWhenNewer() throws { | ||
| insertVersion(tagName: "0.16.0") | ||
| try context.save() | ||
|
|
||
| let result = service.getNewerReleaseTag(versionName: "0.15.0", branch: .stable, ignoreVersion: "") | ||
| #expect(result == "0.16.0") | ||
| } | ||
|
|
||
| @Test func newerReleaseTagReturnsEmptyWhenAlreadyLatest() throws { | ||
| insertVersion(tagName: "0.15.0") | ||
| try context.save() | ||
|
|
||
| let result = service.getNewerReleaseTag(versionName: "0.15.0", branch: .stable, ignoreVersion: "") | ||
| #expect(result == "") | ||
| } | ||
|
|
||
| @Test func newerReleaseTagRespectsIgnoreVersion() throws { | ||
| insertVersion(tagName: "0.16.0") | ||
| try context.save() | ||
|
|
||
| let result = service.getNewerReleaseTag(versionName: "0.15.0", branch: .stable, ignoreVersion: "0.16.0") | ||
| #expect(result == "") | ||
| } | ||
|
|
||
| @Test func newerReleaseTagDetectsUpdateFromBetaToStable() throws { | ||
| insertVersion(tagName: "0.16.0") | ||
| try context.save() | ||
|
|
||
| let result = service.getNewerReleaseTag(versionName: "0.16.0-b1", branch: .beta, ignoreVersion: "") | ||
| #expect(result == "0.16.0") | ||
| } | ||
| } |
Oops, something went wrong.
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.
The
propertiesToFetchproperty is only effective when theresultTypeof theNSFetchRequestis set to.dictionaryResultType. Since this request uses the default.managedObjectResultType, this line is ignored by Core Data. For a small dataset like the one typically returned by the GitHub releases API, this optimization is unnecessary and should be removed to keep the code clean and avoid potential confusion.