Skip to content

Commit

Permalink
Eliminate all CMake stuff and SwiftSyntax dependencies.
Browse files Browse the repository at this point in the history
  • Loading branch information
dabrahams committed Mar 19, 2024
1 parent ecc248f commit f39531c
Show file tree
Hide file tree
Showing 8 changed files with 5 additions and 242 deletions.
24 changes: 0 additions & 24 deletions CMakeLists.txt

This file was deleted.

9 changes: 0 additions & 9 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,6 @@
"revision" : "c8ed701b513cf5177118a175d85fbbbcd707ab41",
"version" : "1.3.0"
}
},
{
"identity" : "swift-syntax",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-syntax.git",
"state" : {
"branch" : "main",
"revision" : "3ec17e06c7289217006e5232244ebccc477a5c4e"
}
}
],
"version" : 2
Expand Down
15 changes: 4 additions & 11 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ let package = Package(

dependencies: [
.package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.2.0"),
.package(url: "https://github.com/apple/swift-syntax.git", branch: "main"),
],
targets: [
// Targets are the basic building blocks of a package, defining a module or a test suite.
Expand All @@ -19,30 +18,24 @@ let package = Package(
name: "GenerateSwiftXCTestMain",
dependencies: [
.product(name: "ArgumentParser", package: "swift-argument-parser"),
.product(name: "SwiftSyntax", package: "swift-syntax"),
.product(name: "SwiftSyntaxBuilder", package: "swift-syntax"),
.product(name: "SwiftOperators", package: "swift-syntax"),
.product(name: "SwiftParser", package: "swift-syntax"),
.product(name: "SwiftParserDiagnostics", package: "swift-syntax"),
],
path: "Sources",
exclude: ["CMakeLists.txt"]),
path: "Sources"),

.target(name: "DummyTestee",
path: "Tests",
exclude: ["XCTestImporter.swift", "CMakeLists.txt", "Tests.swift"],
exclude: ["XCTestImporter.swift", "Tests.swift"],
sources: [ "Dummy.swift"]),

.target(name: "XCTestImporter",
path: "Tests",
exclude: ["Dummy.swift", "CMakeLists.txt", "Tests.swift"],
exclude: ["Dummy.swift", "Tests.swift"],
sources: [ "XCTestImporter.swift" ] ),

.testTarget(
name: "Tests",
dependencies: ["GenerateSwiftXCTestMain", "DummyTestee", "XCTestImporter"],
path: "Tests",
exclude: ["XCTestImporter.swift", "CMakeLists.txt", "Dummy.swift"],
exclude: ["XCTestImporter.swift", "Dummy.swift"],
sources: ["Tests.swift"])
]
)
16 changes: 0 additions & 16 deletions Sources/CMakeLists.txt

This file was deleted.

105 changes: 1 addition & 104 deletions Sources/GenerateSwiftXCTestMain.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import ArgumentParser
import Foundation
import SwiftDiagnostics
import SwiftOperators
import SwiftParser
import SwiftParserDiagnostics
import SwiftSyntax

/// Mapping from (presumed) XCTest type names to a list of test method
/// names.
Expand Down Expand Up @@ -45,10 +40,7 @@ struct GenerateSwiftXCTestMain: ParsableCommand {
///
/// - See also: `discoveredTests()` for more information.
func discoveredTests(in f: URL) throws -> TestCatalog {
let tree = try parseAndEmitDiagnostics(source: String(contentsOf: f))
let scraper = TestScraper()
scraper.walk(tree)
return scraper.result
return [:]
}

/// Returns the text of an extension to the type named `testCaseName` that adds a static
Expand Down Expand Up @@ -141,98 +133,3 @@ struct GenerateSwiftXCTestMain: ParsableCommand {
}

}

struct ParseFailure: Error {
let diagnostics: [Diagnostic]
}

/// Parses the given source code and returns a valid `SourceFileSyntax` node.
///
/// This helper function automatically folds sequence expressions using the given operator table,
/// ignoring errors so that formatting can do something reasonable in the presence of unrecognized
/// operators.
///
/// - Throws: If an unrecoverable error occurs when formatting the code.
func parseAndEmitDiagnostics(
source: String,
operatorTable: OperatorTable = .standardOperators
) throws -> SourceFileSyntax {
let sourceFile =
operatorTable.foldAll(Parser.parse(source: source)) { _ in }.as(SourceFileSyntax.self)!

let diagnostics = ParseDiagnosticsGenerator.diagnostics(for: sourceFile)
if !diagnostics.isEmpty {
throw ParseFailure(diagnostics: diagnostics)
}

return sourceFile
}

class TestScraper: SyntaxVisitor {

init() { super.init(viewMode: .all) }

var scope: [(name: String, possibleClass: Bool)] = []
var result: TestCatalog = [:]

private func enterScope(name: String, possibleClass: Bool, modifiers: DeclModifierListSyntax) -> SyntaxVisitorContinueKind {
scope.append((name, possibleClass))
return modifiers.contains(where: { $0.name.text == "private" }) ? .skipChildren
: .visitChildren
}

private func leaveScope() { scope.removeLast() }

override func visit(_ n: ClassDeclSyntax) -> SyntaxVisitorContinueKind {
enterScope(name: n.name.text, possibleClass: true, modifiers: n.modifiers)
}

override func visitPost(_: ClassDeclSyntax) {
leaveScope()
}

override func visit(_ n: StructDeclSyntax) -> SyntaxVisitorContinueKind {
enterScope(name: n.name.text, possibleClass: false, modifiers: n.modifiers)
}

override func visitPost(_: StructDeclSyntax) {
leaveScope()
}

override func visit(_ n: EnumDeclSyntax) -> SyntaxVisitorContinueKind {
enterScope(name: n.name.text, possibleClass: false, modifiers: n.modifiers)
}

override func visitPost(_: EnumDeclSyntax) {
scope.removeLast()
}

override func visit(_ n: ExtensionDeclSyntax) -> SyntaxVisitorContinueKind {
enterScope(
name: n.extendedType.trimmedDescription, possibleClass: true, modifiers: n.modifiers)
}

override func visitPost(_: ExtensionDeclSyntax) {
scope.removeLast()
}

override func visit(_ n: FunctionDeclSyntax) -> SyntaxVisitorContinueKind {
// Bail if we can't possibly be in a class, or if the method is static or private.
if scope.isEmpty || !scope.last!.possibleClass
|| n.modifiers.contains(where: { ["static", "private"].contains($0.name.text) })
{
return .skipChildren
}

let baseName = n.name.text
if baseName.starts(with: "test")
&& n.signature.parameterClause.parameters.isEmpty
&& n.genericParameterClause == nil
&& n.genericWhereClause == nil
{
result[scope.lazy.map(\.name).joined(separator: "."), default: []].append(baseName)
}

return .skipChildren
}
}
8 changes: 0 additions & 8 deletions Tests/CMakeLists.txt

This file was deleted.

5 changes: 0 additions & 5 deletions cmake/TopLevelDefaults.cmake

This file was deleted.

65 changes: 0 additions & 65 deletions cmake/modules/GenerateSwiftXCTestMain_FetchDependencies.cmake

This file was deleted.

0 comments on commit f39531c

Please sign in to comment.