Skip to content

Commit

Permalink
SwiftEval: Load tests bundle if exists. (johnno1962#66)
Browse files Browse the repository at this point in the history
Previously, when running tests through injection, if the test would have
depended upon a symbol that's in the tests bundle, it would give an
undefined symbol error. This commit solves this by loading the tests
bundle if it exists, which allows for much better support when loading
new test files through injection.
  • Loading branch information
byohay authored and johnno1962 committed Nov 24, 2022
1 parent 58d5953 commit d326bc1
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion Sources/HotReloading/SwiftEval.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// Created by John Holdsworth on 02/11/2017.
// Copyright © 2017 John Holdsworth. All rights reserved.
//
// $Id: //depot/HotReloading/Sources/HotReloading/SwiftEval.swift#156 $
// $Id: //depot/HotReloading/Sources/HotReloading/SwiftEval.swift#157 $
//
// Basic implementation of a Swift "eval()" including the
// mechanics of recompiling a class and loading the new
Expand Down Expand Up @@ -912,9 +912,42 @@ public class SwiftEval: NSObject {
}
}()

lazy var loadTestsBundle: () = {
do {
guard let testsBundlePath = try testsBundlePath() else {
debug("Tests bundle wasn't found - did you run the tests target before running the application?")
return
}
guard let bundle = Bundle(path: testsBundlePath), bundle.load() else {
debug("Failed loading tests bundle")
return
}
} catch {
debug("Error while searching for the tests bundle: \(error)")
return
}
}()

func testsBundlePath() throws -> String? {
guard let pluginsDirectory = Bundle.main.path(forResource: "PlugIns", ofType: nil) else {
return nil
}

let bundlePaths: [String] = try FileManager.default.contentsOfDirectory(atPath: pluginsDirectory)
.filter { $0.hasSuffix(".xctest") }
.map { directoryName in
return "\(pluginsDirectory)/\(directoryName)"
}
if bundlePaths.count > 1 {
debug("Found more than one tests bundle, using the first one")
}
return bundlePaths.first
}

@objc func loadAndInject(tmpfile: String, oldClass: AnyClass? = nil)
throws -> [AnyClass] {
_ = loadXCTest
_ = loadTestsBundle

print("\(APP_PREFIX)Loading .dylib ...")
// load patched .dylib into process with new version of class
Expand Down

0 comments on commit d326bc1

Please sign in to comment.