Skip to content

Commit

Permalink
Use CanonicalPackageLocation instead of PackageIdentity to correctly …
Browse files Browse the repository at this point in the history
…differentiate remote and local packages
  • Loading branch information
ikesyo committed Dec 13, 2024
1 parent db3ec7d commit f02498c
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 6 deletions.
6 changes: 3 additions & 3 deletions Sources/ScipioKit/Producer/Cache/CacheSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ extension PinsStore.PinState: @retroactive Hashable {
}

public struct SwiftPMCacheKey: CacheKey {
public var packageIdentity: PackageIdentity
public var canonicalPackageLocation: String
public var targetName: String
public var pin: PinsStore.PinState
var buildOptions: BuildOptions
Expand Down Expand Up @@ -231,7 +231,7 @@ struct CacheSystem: Sendable {
}

func calculateCacheKey(of target: CacheTarget) async throws -> SwiftPMCacheKey {
let packageIdentity = target.buildProduct.package.identity
let canonicalPackageLocation = target.buildProduct.package.manifest.canonicalPackageLocation
let targetName = target.buildProduct.target.name
let pin = try retrievePin(package: target.buildProduct.package)
let buildOptions = target.buildOptions
Expand All @@ -242,7 +242,7 @@ struct CacheSystem: Sendable {
throw Error.xcodeVersionNotDetected
}
return SwiftPMCacheKey(
packageIdentity: packageIdentity,
canonicalPackageLocation: canonicalPackageLocation.description,
targetName: targetName,
pin: pin.state,
buildOptions: buildOptions,
Expand Down
77 changes: 74 additions & 3 deletions Tests/ScipioKitTests/CacheSystemTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import Foundation
@testable import ScipioKit
import XCTest
import Basics
import PackageModel

private let fixturePath = URL(fileURLWithPath: #filePath)
.deletingLastPathComponent()
Expand All @@ -20,7 +19,7 @@ final class CacheSystemTests: XCTestCase {

func testEncodeCacheKey() throws {
let cacheKey = SwiftPMCacheKey(
packageIdentity: PackageIdentity.plain("MyPackage"),
canonicalPackageLocation: "/path/to/MyPackage",
targetName: "MyTarget",
pin: .revision("111111111"),
buildOptions: .init(
Expand Down Expand Up @@ -65,8 +64,8 @@ final class CacheSystemTests: XCTestCase {
"iOS"
]
},
"canonicalPackageLocation" : "\\/path\\/to\\/MyPackage",
"clangVersion" : "clang-1400.0.29.102",
"packageIdentity" : "MyPackage",
"pin" : {
"revision" : "111111111"
},
Expand All @@ -81,6 +80,78 @@ final class CacheSystemTests: XCTestCase {
XCTAssertEqual(rawString, expected)
}

func testCacheKeyForRemoteAndLocalPackageDifference() async throws {
let fileSystem = localFileSystem

let tempDir = try fileSystem.tempDirectory.appending(#function)
try fileSystem.removeFileTree(tempDir)
try fileSystem.createDirectory(tempDir)

defer { try? fileSystem.removeFileTree(tempDir) }

let tempCacheKeyTestsDir = tempDir.appending(component: "CacheKeyTests").scipioAbsolutePath
try fileSystem.copy(
from: fixturePath.appending(component: "CacheKeyTests").absolutePath,
to: tempCacheKeyTestsDir
)

// For local package consumption
let executor = ProcessExecutor()
_ = try await executor.execute([
"git",
"clone",
"https://github.com/giginet/scipio-testing",
tempDir.appending(component: "scipio-testing").pathString,
"-b",
"3.0.0",
"--depth",
"1",
])

func scipioTestingCacheKey(fixture: String) async throws -> SwiftPMCacheKey {
let descriptionPackage = try DescriptionPackage(
packageDirectory: tempCacheKeyTestsDir.appending(component: fixture),
mode: .createPackage,
onlyUseVersionsFromResolvedFile: false
)
let package = descriptionPackage
.graph
.packages
.first { $0.manifest.displayName == "scipio-testing" }!
let target = package.modules.first { $0.name == "ScipioTesting" }!
let cacheTarget = CacheSystem.CacheTarget(
buildProduct: BuildProduct(
package: package,
target: target
),
buildOptions: BuildOptions(
buildConfiguration: .release,
isDebugSymbolsEmbedded: false,
frameworkType: .dynamic,
sdks: [.iOS, .iOSSimulator],
extraFlags: nil,
extraBuildParameters: nil,
enableLibraryEvolution: false,
keepPublicHeadersStructure: false,
customFrameworkModuleMapContents: nil
)
)

let cacheSystem = CacheSystem(
pinsStore: try descriptionPackage.workspace.pinsStore.load(),
outputDirectory: FileManager.default.temporaryDirectory.appendingPathComponent("XCFrameworks")
)
return try await cacheSystem.calculateCacheKey(of: cacheTarget)
}

let scipioTestingRemote = try await scipioTestingCacheKey(fixture: "AsRemotePackage")
let scipioTestingLocal = try await scipioTestingCacheKey(fixture: "AsLocalPackage")

XCTAssertNotEqual(scipioTestingRemote.canonicalPackageLocation, scipioTestingLocal.canonicalPackageLocation)
XCTAssertEqual(scipioTestingRemote.targetName, scipioTestingLocal.targetName)
XCTAssertEqual(scipioTestingRemote.pin, scipioTestingLocal.pin)
}

func testCacheKeyCalculationForRootPackageTarget() async throws {
let fileSystem = localFileSystem
let testingPackagePath = fixturePath.appendingPathComponent("TestingPackage")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// swift-tools-version: 6.0
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "AsLocalPackage",
products: [
.library(
name: "Foo",
targets: ["Foo"]
),
],
dependencies: [
.package(path: "../../scipio-testing"),
],
targets: [
.target(
name: "Foo",
dependencies: [
.product(name: "ScipioTesting", package: "scipio-testing"),
]
),
]
)
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// swift-tools-version: 6.0
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "AsRemotePackage",
products: [
.library(
name: "Foo",
targets: ["Foo"]
),
],
dependencies: [
.package(url: "https://github.com/giginet/scipio-testing.git", exact: "3.0.0"),
],
targets: [
.target(
name: "Foo",
dependencies: [
.product(name: "ScipioTesting", package: "scipio-testing"),
]
),
]
)
Empty file.

0 comments on commit f02498c

Please sign in to comment.