Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 9 additions & 15 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@ let sharedSettings: [SwiftSetting] = [
.unsafeFlags(["-warnings-as-errors"])
]

let enablePlugin = false

var products: [PackageDescription.Product] = [
let products: [PackageDescription.Product] = [
.executable(
name: "xcode-selective-test",
targets: ["xcode-selective-test"]
),
.plugin(
name: "XcodeSelectiveTest",
targets: ["SelectiveTestingPlugin"]
)
]

var targets: [PackageDescription.Target] = [
let targets: [PackageDescription.Target] = [
.executableTarget(
name: "xcode-selective-test",
dependencies: ["SelectiveTestingCore",
Expand Down Expand Up @@ -54,15 +56,7 @@ var targets: [PackageDescription.Target] = [
name: "DependencyCalculatorTests",
dependencies: ["DependencyCalculator", "Workspace", "PathKit", "SelectiveTestingCore"],
resources: [.copy("ExamplePackages")]),
]

if enablePlugin {
products.append(.plugin(
name: "XcodeSelectiveTest",
targets: ["SelectiveTestingPlugin"]
))

targets.append(.plugin(
.plugin(
name: "SelectiveTestingPlugin",
capability: .command(
intent: .custom(
Expand All @@ -72,8 +66,8 @@ if enablePlugin {
.writeToPackageDirectory(reason: "Update test plan file")
]),
dependencies: ["xcode-selective-test"]
))
}
)
]

let package = Package(
name: "XcodeSelectiveTesting",
Expand Down
20 changes: 15 additions & 5 deletions Plugins/SelectiveTestingPlugin/SelectiveTestingPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ import PackagePlugin

@main
struct SelectiveTestingPlugin: CommandPlugin {
private func run(_ executable: String) throws {
private func run(_ executable: String, arguments: [String] = []) throws {
let executableURL = URL(fileURLWithPath: executable)

let process = Process()
process.executableURL = executableURL
process.arguments = [
]
process.arguments = arguments

try process.run()
process.waitUntilExit()
Expand All @@ -28,7 +27,7 @@ struct SelectiveTestingPlugin: CommandPlugin {
FileManager().changeCurrentDirectoryPath(context.package.directory.string)
let tool = try context.tool(named: "xcode-selective-test")

try run(tool.path.string)
try run(tool.path.string, arguments: arguments)
}
}

Expand All @@ -38,9 +37,20 @@ import XcodeProjectPlugin
extension SelectiveTestingPlugin: XcodeCommandPlugin {
func performCommand(context: XcodePluginContext, arguments: [String]) throws {
FileManager().changeCurrentDirectoryPath(context.xcodeProject.directory.string)

let tool = try context.tool(named: "xcode-selective-test")

try run(tool.path.string)
var toolArguments = arguments

if !toolArguments.contains(where: { $0 == "--test-plan" }),
let testPlan = context.xcodeProject.filePaths.first(where: {
$0.extension == "xctestplan"
}) {
print("Using \(testPlan.string) test plan")
toolArguments.append(contentsOf: ["--test-plan", testPlan.string])
}

try run(tool.path.string, arguments: toolArguments)
}
}
#endif
Expand Down
33 changes: 26 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,27 @@ This technique saves time when testing locally and on the CI.

## Installation

### Xcode

Add to Xcode as SPM dependency.

- Open your project or workspace in Xcode
- Select yout project in the file list in Xcode
- In the right menu select "Project", open tab "Package Dependencies"
- Select "+"
- In the new window, paste `git@github.com:mikeger/XcodeSelectiveTesting` in the search field
- Select project if necessary, put a checkbox on "XcodeSelectiveTesting" in the list
- Click "Add Package"

### Using Swift Package Manager

Add `.package(url: "git@github.com:mikeger/XcodeSelectiveTesting", .upToNextMajor(from: "0.7.0"))` to your `Package.swift`'s `dependencies` section.
Add `.package(url: "git@github.com:mikeger/XcodeSelectiveTesting", .upToNextMajor(from: "0.8.0"))` to your `Package.swift`'s `dependencies` section.

Use SPM to run the command: `swift run xcode-selective-test`.

### Using [Mint](https://github.com/yonaskolb/Mint)

`mint install mikeger/XcodeSelectiveTesting@0.7.0`
`mint install mikeger/XcodeSelectiveTesting@0.8.0`

### Manually

Expand All @@ -55,16 +67,23 @@ Run `swift test --filter "$(swift run xcode-selective-test . --json | jq -r ". |

NB: This command assumes you have [jq](https://jqlang.github.io/jq/) tool installed. You can install it with Homebrew via `brew install jq`.

### Use case: Xcode-based project, prepare test plan locally
### Use case: Xcode-based project, run tests locally

1. Install the tool (see [Installation: Xcode](#xcode))
2. Select your project in the Xcode's file list
3. Right-click on it and select `SelectiveTestingPlugin`
4. Wait for the tool to run
5. Run tests normally, SelectiveTesting would modify your test plan according to the local changes

Alternatively, you can use CLI to achieve the same result:

1. Install the tool
2. Run `mint run mikeger/XcodeSelectiveTesting@0.7.0 YourWorkspace.xcworkspace --test-plan YourTestPlan.xctestplan`
3. Run tests normally, SelectiveTesting would modify your test plan according to the local changes
1. Run `mint run mikeger/XcodeSelectiveTesting@0.8.0 YourWorkspace.xcworkspace --test-plan YourTestPlan.xctestplan`
2. Run tests normally, XcodeSelectiveTesting would modify your test plan according to the local changes

### Use case: Xcode-based project, execute tests on the CI

1. Add code to install the tool
2. Add a CI step before you execute your tests: `mint run mikeger/XcodeSelectiveTesting@0.7.0 YourWorkspace.xcworkspace --test-plan YourTestPlan.xctestplan --base-branch $PR_BASE_BRANCH`
2. Add a CI step before you execute your tests: `mint run mikeger/XcodeSelectiveTesting@0.8.0 YourWorkspace.xcworkspace --test-plan YourTestPlan.xctestplan --base-branch $PR_BASE_BRANCH`
3. Execute your tests

## How does this work?
Expand Down
3 changes: 2 additions & 1 deletion Sources/DependencyCalculator/PackageMetadata.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ struct PackageTargetMetadata {

// TODO: Split in several methods
static func parse(at path: Path) throws -> [PackageTargetMetadata] {
let manifest = try Shell.execOrFail("cd \(path) && swift package dump-package").trimmingCharacters(in: .newlines)
// NB: Flag `--disable-sandbox` is required to allow running SPM from an SPM plugin
let manifest = try Shell.execOrFail("cd \(path) && swift package dump-package --disable-sandbox").trimmingCharacters(in: .newlines)
guard let manifestData = manifest.data(using: .utf8),
let manifestJson = try JSONSerialization.jsonObject(with: manifestData, options: []) as? [String: Any],
let targets = manifestJson["targets"] as? [[String: Any]]
Expand Down