Skip to content

Commit

Permalink
change reporter
Browse files Browse the repository at this point in the history
  • Loading branch information
yume190 committed Sep 8, 2023
1 parent e01565f commit 6cbf40a
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 51 deletions.
20 changes: 0 additions & 20 deletions Sources/LeakDetect/Arguments+Ex.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,6 @@ import Foundation
import SKClient
import PathKit

extension Reporter: ExpressibleByArgument {
public init?(argument: String) {

switch argument {
case "xcode":
self = .xcode
case "vscode":
self = .vscode
default:
self = .custom { _, _ in }
}
}
public var defaultValueDescription: String { "vscode" }

public static var allValueStrings: [String] { ["xcode", "vscode", "custom"] }
public static var defaultCompletionKind: CompletionKind { .list(allValueStrings) }

static let all: String = "xcode|vscode|custom"
}

extension SDK: ExpressibleByArgument {}

public enum TargetType: String, CaseIterable, ExpressibleByArgument {
Expand Down
4 changes: 2 additions & 2 deletions Sources/LeakDetect/Command.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ struct Command: AsyncParsableCommand {
switch reporter {
case .custom where github:
let githubAction = GithubAtionReporter(base: base)
let reporter = Reporter.custom { [weak githubAction] location, _ in
githubAction?.add(location)
let reporter = Reporter.custom { [weak githubAction] result in
githubAction?.add(result)
}
return (reporter, githubAction)
case .xcode: fallthrough
Expand Down
28 changes: 16 additions & 12 deletions Sources/LeakDetect/GitAction.swift
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
//
// Env.swift
// GithubAtionReporter.swift
//
//
// Created by Yume on 2023/8/28.
//

import Foundation
import PathKit
import LeakDetectKit
import SKClient

/// [Context](https://docs.github.com/en/actions/learn-github-actions/contexts#github-context)
Expand Down Expand Up @@ -60,16 +61,16 @@ class GithubAtionReporter {
let issue: String

let auth: String
private(set) var codes: [CodeLocation] = []
func add(_ code: CodeLocation) {
if let line = code.location.line, let col = code.location.column {
let path = rPath(code)
let target = code.syntax?.withoutTrivia().description ?? "_"
private(set) var codes: [LeakResult] = []
func add(_ result: LeakResult) {
let location = result.location
if let line = location.location.line, let col = location.location.column {
let path = rPath(location)
print("""
::warning file=\(path),line=\(line),col=\(col)::\(target)"
::warning file=\(path),line=\(line),col=\(col)::\(result.reportReason)
""")
}
codes.append(code)
codes.append(result)
}

/// base: https://github.com/
Expand All @@ -78,7 +79,8 @@ class GithubAtionReporter {
/// sha: 9fb49184787fe2bfbd0802bf87xxxxx
/// file: Sources/LeakDetect/Command.swift
/// line: #L10-L20
private func path(_ code: CodeLocation) -> String {
private func path(_ result: LeakResult) -> String {
let code = result.location
func lines(_ code: CodeLocation) -> String {
guard let line = code.location.line else {
return ""
Expand All @@ -95,15 +97,17 @@ class GithubAtionReporter {
"""
}

private func comment(code: CodeLocation) -> String {
private func comment(_ result: LeakResult) -> String {
let code = result.location
return """
\(path(code))
\(path(result))
> [!WARNING]
> Line: \(code.location.line ?? -1)
> Column: \(code.location.column ?? -1)
> Target: \(code.syntax?.withoutTrivia().description ?? "")
> Target: \(result.targetName ?? "")
> Reason: \(result.reason)
"""
}

Expand Down
49 changes: 49 additions & 0 deletions Sources/LeakDetect/Reporter.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//
// File.swift
//
//
// Created by Yume on 2023/9/7.
//

import Foundation
import LeakDetectKit
import ArgumentParser

public enum Reporter {
public typealias R = (LeakResult) -> Void

case xcode
case vscode
case custom(R)

public func report(_ result: LeakResult) {
switch self {
case .vscode:
print("\(result.location.reportVSCode) \(result.reason)")
case .xcode:
print("\(result.location.reportXCode) \(result.reason)")
case let .custom(report):
report(result)
}
}
}

extension Reporter: ExpressibleByArgument {
public init?(argument: String) {

switch argument {
case "xcode":
self = .xcode
case "vscode":
self = .vscode
default:
self = .custom { _ in }
}
}
public var defaultValueDescription: String { "vscode" }

public static var allValueStrings: [String] { ["xcode", "vscode", "custom"] }
public static var defaultCompletionKind: CompletionKind { .list(allValueStrings) }

static let all: String = "xcode|vscode|custom"
}
16 changes: 12 additions & 4 deletions Sources/LeakDetectKit/LeakResult.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,22 @@ public struct LeakResult {
self.verbose = verbose
}

var syntax: String? {
public var targetName: String? {
location.syntax?.withoutTrivia().description
}

var reportReason: String {
"Target: `\(syntax ?? "")`, Reason: \(reason)"
public var reportReason: String {
"Target: `\(targetName ?? "")`, Reason: \(reason)"
}

public var testLocation: String? {
guard
let targetName,
let line = location.location.line,
let col = location.location.column
else {return nil}
return "\(targetName):\(line):\(col)"
}

}

public extension Reporter {
Expand Down
4 changes: 2 additions & 2 deletions Sources/LeakDetectKit/SourceKit/SDK+Target.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ extension SDK {

case .iphoneos:
/// arm64-apple-ios11.0
return ["-target", "arm64-apple-ios"]
return ["-target", "arm64-apple-ios11.0"]
case .iphonesimulator:
/// x86_64-apple-ios16.2-simulator
return ["-target", "x86_64-apple-ios-simulator"]
return ["-target", "x86_64-apple-ios11.0-simulator"]

// TODO:
case .watchos:
Expand Down
19 changes: 9 additions & 10 deletions Tests/LeakDetectTests/Assign/AssignClosureVisitorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,18 @@ import XCTest
final class AssignClosureVisitorTests: XCTestCase {
func testNormal() throws {
let path: String = resource(file: "AssignClosure.swift.data")
let client = try SKClient(path: path)
let visitor = AssignClosureVisitor(client: client)
visitor.walk(client.sourceFile)
let results = visitor.results.map(\.location)
let pipeline = try Pipeline(path, SDK.iphoneos.args + [path])
let results = try pipeline.detectAssign().map(\.testLocation)

let espect = [
CodeLocation(path: path, location: SourceLocation(offset: 171, converter: client.converter)),
CodeLocation(path: path, location: SourceLocation(offset: 191, converter: client.converter)),
CodeLocation(path: path, location: SourceLocation(offset: 216, converter: client.converter)),
CodeLocation(path: path, location: SourceLocation(offset: 231, converter: client.converter)),
"abc:11:27",
"abc:12:17",
"abc:14:21",
"abc:15:11",
"def:29:100",
]

XCTAssertEqual(results.count, espect.count)
XCTAssertEqual(results.count, 5)
XCTAssertEqual(results, espect)
}
}
5 changes: 4 additions & 1 deletion Tests/LeakDetectTests/Resource/AssignClosure.swift.data
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,13 @@ class A {

let tf = UITextField()
tf.addTarget(self, action: #selector(self.abc), for: .editingChanged)

NotificationCenter.default.addObserver(forName: .init(""), object: nil, queue: .main, using: def)
}

func def(_ block: Closure?) {}
func def(_ sel: Selector) {}
func def(a: A) {}
func def(any: Any?) {}
}
func def(_ noti: Notification) {}
}

0 comments on commit 6cbf40a

Please sign in to comment.