-
Notifications
You must be signed in to change notification settings - Fork 307
Implement a syntactic test discovery fallback for XCTests written in Swift #1148
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
ahoppen
merged 1 commit into
swiftlang:main
from
ahoppen:ahoppen/syntactic-test-discovery-fallback
Apr 2, 2024
Merged
Changes from all commits
Commits
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,101 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import Foundation | ||
import IndexStoreDB | ||
import LSPLogging | ||
|
||
/// Helper class to check if symbols from the index are up-to-date or if the source file has been modified after it was | ||
/// indexed. | ||
/// | ||
/// The checker caches mod dates of source files. It should thus not be long lived. Its intended lifespan is the | ||
/// evaluation of a single request. | ||
struct IndexOutOfDateChecker { | ||
/// The last modification time of a file. Can also represent the fact that the file does not exist. | ||
private enum ModificationTime { | ||
case fileDoesNotExist | ||
case date(Date) | ||
} | ||
|
||
private enum Error: Swift.Error, CustomStringConvertible { | ||
case fileAttributesDontHaveModificationDate | ||
|
||
var description: String { | ||
switch self { | ||
case .fileAttributesDontHaveModificationDate: | ||
return "File attributes don't contain a modification date" | ||
} | ||
} | ||
} | ||
|
||
/// File paths to modification times that have already been computed. | ||
private var modTimeCache: [String: ModificationTime] = [:] | ||
|
||
private func modificationDateUncached(of path: String) throws -> ModificationTime { | ||
do { | ||
let attributes = try FileManager.default.attributesOfItem(atPath: path) | ||
guard let modificationDate = attributes[FileAttributeKey.modificationDate] as? Date else { | ||
throw Error.fileAttributesDontHaveModificationDate | ||
} | ||
return .date(modificationDate) | ||
} catch let error as NSError where error.domain == NSCocoaErrorDomain && error.code == NSFileReadNoSuchFileError { | ||
return .fileDoesNotExist | ||
} | ||
} | ||
|
||
private mutating func modificationDate(of path: String) throws -> ModificationTime { | ||
if let cached = modTimeCache[path] { | ||
return cached | ||
} | ||
let modTime = try modificationDateUncached(of: path) | ||
modTimeCache[path] = modTime | ||
return modTime | ||
} | ||
|
||
/// Returns `true` if the source file for the given symbol location exists and has not been modified after it has been | ||
/// indexed. | ||
mutating func isUpToDate(_ symbolLocation: SymbolLocation) -> Bool { | ||
do { | ||
let sourceFileModificationDate = try modificationDate(of: symbolLocation.path) | ||
switch sourceFileModificationDate { | ||
case .fileDoesNotExist: | ||
return false | ||
case .date(let sourceFileModificationDate): | ||
return sourceFileModificationDate <= symbolLocation.timestamp | ||
} | ||
} catch { | ||
logger.fault("Unable to determine if SymbolLocation is up-to-date: \(error.forLogging)") | ||
return true | ||
} | ||
} | ||
|
||
/// Return `true` if a unit file has been indexed for the given file path after its last modification date. | ||
/// | ||
/// This means that at least a single build configuration of this file has been indexed since its last modification. | ||
mutating func indexHasUpToDateUnit(for filePath: String, index: IndexStoreDB) -> Bool { | ||
guard let lastUnitDate = index.dateOfLatestUnitFor(filePath: filePath) else { | ||
return false | ||
} | ||
do { | ||
let sourceModificationDate = try modificationDate(of: filePath) | ||
switch sourceModificationDate { | ||
case .fileDoesNotExist: | ||
return false | ||
case .date(let sourceModificationDate): | ||
return sourceModificationDate <= lastUnitDate | ||
} | ||
} catch { | ||
logger.fault("Unable to determine if source file has up-to-date unit: \(error.forLogging)") | ||
return true | ||
} | ||
} | ||
} |
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
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 only error at the moment is no mtime in attributes. Seems like we should just assume it isn't up to date in that case? I doubt it really matters though.
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.
My thinking was that if sourcekit-lsp is running on some platform that for some reason doesn’t have modification time or if Foundation doesn’t know how to extract it (not sure if those exist), it’s better to assume all files are up-to-date than to completely ignore index results. But as you said, I don’t think it really makes a difference.
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.
Fair enough, that's fine with me 👍