Skip to content

Commit f2db564

Browse files
committed
more cleanup
1 parent 521392a commit f2db564

File tree

4 files changed

+140
-76
lines changed

4 files changed

+140
-76
lines changed

Sources/PackageGraph/PinsStore.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ fileprivate struct PinsStorage {
238238
struct Pin: Codable {
239239
let package: String?
240240
let repositoryURL: String
241-
let state: PinStateInfo
241+
let state: State
242242

243243
init(_ pin: PinsStore.Pin, mirrors: DependencyMirrors) throws {
244244
let location: String
@@ -258,7 +258,7 @@ fileprivate struct PinsStorage {
258258
}
259259
}
260260

261-
struct PinStateInfo: Codable {
261+
struct State: Codable {
262262
let revision: String
263263
let branch: String?
264264
let version: String?
@@ -302,7 +302,7 @@ fileprivate struct PinsStorage {
302302
let identity: PackageIdentity
303303
let kind: Kind
304304
let location: String
305-
let state: PinStateInfo
305+
let state: State
306306

307307
init(_ pin: PinsStore.Pin, mirrors: DependencyMirrors) throws {
308308
let kind: Kind
@@ -316,7 +316,7 @@ fileprivate struct PinsStorage {
316316
location = url.absoluteString
317317
case .registry:
318318
kind = .registry
319-
location = ""
319+
location = "" // FIXME: this is likely not correct
320320
default:
321321
throw StringError("invalid package type \(pin.packageRef.kind)")
322322
}
@@ -335,7 +335,7 @@ fileprivate struct PinsStorage {
335335
case registry
336336
}
337337

338-
struct PinStateInfo: Codable {
338+
struct State: Codable {
339339
let version: String?
340340
let branch: String?
341341
let revision: String?
@@ -384,7 +384,7 @@ extension PinsStore.Pin {
384384
}
385385

386386
extension PinsStore.PinState {
387-
fileprivate init(_ state: PinsStorage.V1.PinStateInfo) throws {
387+
fileprivate init(_ state: PinsStorage.V1.State) throws {
388388
let revision = state.revision
389389
if let version = state.version {
390390
self = try .version(Version(versionString: version), revision: revision)
@@ -421,7 +421,7 @@ extension PinsStore.Pin {
421421
}
422422

423423
extension PinsStore.PinState {
424-
fileprivate init(_ state: PinsStorage.V2.PinStateInfo) throws {
424+
fileprivate init(_ state: PinsStorage.V2.State) throws {
425425
if let version = state.version {
426426
self = try .version(Version(versionString: version), revision: state.revision)
427427
} else if let branch = state.branch, let revision = state.revision {

Sources/Workspace/Workspace.swift

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1252,7 +1252,7 @@ extension Workspace {
12521252
}
12531253

12541254
// Form the edit working repo path.
1255-
let path = self.location.editsSubdirectory(for: dependency)
1255+
let path = self.location.editSubdirectory(for: dependency)
12561256
// Check for uncommited and unpushed changes if force removal is off.
12571257
if !forceRemove {
12581258
let workingCopy = try repositoryManager.openWorkingCopy(at: path)
@@ -1557,7 +1557,7 @@ extension Workspace {
15571557
case .registry:
15581558
return self.location.registryDownloadSubdirectory(for: dependency)
15591559
case .edited(_, let path):
1560-
return path ?? self.location.editsSubdirectory(for: dependency)
1560+
return path ?? self.location.editSubdirectory(for: dependency)
15611561
case .fileSystem(let path):
15621562
return path
15631563
}
@@ -3580,3 +3580,20 @@ extension CheckoutState {
35803580
}
35813581
}
35823582
}
3583+
3584+
extension Workspace.Location {
3585+
/// Returns the path to the dependency's repository checkout directory.
3586+
fileprivate func repositoriesCheckoutSubdirectory(for dependency: Workspace.ManagedDependency) -> AbsolutePath {
3587+
self.repositoriesCheckoutsDirectory.appending(dependency.subpath)
3588+
}
3589+
3590+
/// Returns the path to the dependency's download directory.
3591+
fileprivate func registryDownloadSubdirectory(for dependency: Workspace.ManagedDependency) -> AbsolutePath {
3592+
self.registryDownloadDirectory.appending(dependency.subpath)
3593+
}
3594+
3595+
/// Returns the path to the dependency's edit directory.
3596+
fileprivate func editSubdirectory(for dependency: Workspace.ManagedDependency) -> AbsolutePath {
3597+
self.editsDirectory.appending(dependency.subpath)
3598+
}
3599+
}

Sources/Workspace/WorkspaceConfiguration.swift

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,31 +42,16 @@ extension Workspace {
4242
self.workingDirectory.appending(component: "repositories")
4343
}
4444

45-
/// Returns the path to the repository checkout directory for a package.
46-
public func editsSubdirectory(for dependency: ManagedDependency) -> AbsolutePath {
47-
self.editsDirectory.appending(dependency.subpath)
48-
}
49-
5045
/// Path to the repository checkouts.
5146
public var repositoriesCheckoutsDirectory: AbsolutePath {
5247
self.workingDirectory.appending(component: "checkouts")
5348
}
5449

55-
/// Returns the path to the repository checkout directory for a package.
56-
public func repositoriesCheckoutSubdirectory(for dependency: ManagedDependency) -> AbsolutePath {
57-
self.repositoriesCheckoutsDirectory.appending(dependency.subpath)
58-
}
59-
6050
/// Path to the registry downloads.
6151
public var registryDownloadDirectory: AbsolutePath {
6252
self.workingDirectory.appending(components: "registry", "downloads")
6353
}
6454

65-
/// Returns the path to the repository checkout directory for a package.
66-
public func registryDownloadSubdirectory(for dependency: ManagedDependency) -> AbsolutePath {
67-
self.registryDownloadDirectory.appending(dependency.subpath)
68-
}
69-
7055
/// Path to the downloaded binary artifacts.
7156
public var artifactsDirectory: AbsolutePath {
7257
self.workingDirectory.appending(component: "artifacts")

Tests/WorkspaceTests/PinsStoreTests.swift

Lines changed: 114 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -23,77 +23,133 @@ final class PinsStoreTests: XCTestCase {
2323
let v1: Version = "1.0.0"
2424

2525
func testBasics() throws {
26-
let fooPath = AbsolutePath("/foo")
27-
let barPath = AbsolutePath("/bar")
28-
let foo = PackageIdentity(path: fooPath)
29-
let bar = PackageIdentity(path: barPath)
30-
//let fooRepo = RepositorySpecifier(url: fooPath.pathString)
31-
//let barRepo = RepositorySpecifier(url: barPath.pathString)
32-
let revision = "81513c8fd220cf1ed1452b98060cd80d3725c5b7"
33-
let fooRef = PackageReference.localSourceControl(identity: foo, path: fooPath)
34-
let barRef = PackageReference.localSourceControl(identity: bar, path: barPath)
35-
36-
let state = PinsStore.PinState.version(v1, revision: revision)
37-
3826
let fs = InMemoryFileSystem()
3927
let pinsFile = AbsolutePath("/pinsfile.txt")
40-
var store = try PinsStore(pinsFile: pinsFile, workingDirectory: .root, fileSystem: fs, mirrors: .init())
41-
// Pins file should not be created right now.
42-
XCTAssert(!fs.exists(pinsFile))
43-
XCTAssert(store.pins.map{$0}.isEmpty)
4428

45-
store.pin(packageRef: fooRef, state: state)
46-
try store.saveState(toolsVersion: ToolsVersion.currentToolsVersion)
29+
do {
30+
let fooPath = AbsolutePath("/foo")
31+
let foo = PackageIdentity(path: fooPath)
32+
let fooRef = PackageReference.localSourceControl(identity: foo, path: fooPath)
33+
34+
let barPath = AbsolutePath("/bar")
35+
let bar = PackageIdentity(path: barPath)
36+
let barRef = PackageReference.localSourceControl(identity: bar, path: barPath)
37+
38+
var store = try PinsStore(pinsFile: pinsFile, workingDirectory: .root, fileSystem: fs, mirrors: .init())
39+
40+
// Pins file should not be created right now.
41+
XCTAssert(!fs.exists(pinsFile))
42+
XCTAssert(store.pins.map{$0}.isEmpty)
43+
44+
let revision = UUID().uuidString
45+
let state = PinsStore.PinState.version(v1, revision: revision)
46+
store.pin(packageRef: fooRef, state: state)
47+
try store.saveState(toolsVersion: ToolsVersion.currentToolsVersion)
4748

48-
XCTAssert(fs.exists(pinsFile))
49+
XCTAssert(fs.exists(pinsFile))
50+
51+
// Load the store again from disk.
52+
let store2 = try PinsStore(pinsFile: pinsFile, workingDirectory: .root, fileSystem: fs, mirrors: .init())
53+
// Test basics on the store.
54+
for s in [store, store2] {
55+
XCTAssert(s.pins.map{$0}.count == 1)
56+
XCTAssertEqual(s.pinsMap[bar], nil)
57+
let fooPin = s.pinsMap[foo]!
58+
XCTAssertEqual(fooPin.packageRef, fooRef)
59+
XCTAssertEqual(fooPin.state, .version(v1, revision: revision))
60+
XCTAssertEqual(fooPin.state.description, v1.description)
61+
}
62+
63+
// We should be able to pin again.
64+
store.pin(packageRef: fooRef, state: state)
65+
store.pin(
66+
packageRef: fooRef,
67+
state: .version("1.0.2", revision: revision)
68+
)
69+
store.pin(packageRef: barRef, state: state)
70+
try store.saveState(toolsVersion: ToolsVersion.currentToolsVersion)
71+
72+
store = try PinsStore(pinsFile: pinsFile, workingDirectory: .root, fileSystem: fs, mirrors: .init())
73+
XCTAssert(store.pins.map{$0}.count == 2)
4974

50-
// Load the store again from disk.
51-
let store2 = try PinsStore(pinsFile: pinsFile, workingDirectory: .root, fileSystem: fs, mirrors: .init())
52-
// Test basics on the store.
53-
for s in [store, store2] {
54-
XCTAssert(s.pins.map{$0}.count == 1)
55-
XCTAssertEqual(s.pinsMap[bar], nil)
56-
let fooPin = s.pinsMap[foo]!
57-
XCTAssertEqual(fooPin.packageRef, fooRef)
58-
XCTAssertEqual(fooPin.state, .version(v1, revision: revision))
59-
XCTAssertEqual(fooPin.state.description, v1.description)
6075
}
6176

62-
// We should be able to pin again.
63-
store.pin(packageRef: fooRef, state: state)
64-
store.pin(
65-
packageRef: fooRef,
66-
state: .version("1.0.2", revision: revision)
67-
)
68-
store.pin(packageRef: barRef, state: state)
69-
try store.saveState(toolsVersion: ToolsVersion.currentToolsVersion)
77+
// Test source control version pin.
78+
79+
do {
80+
let path = AbsolutePath("/foo")
81+
let identity = PackageIdentity(path: path)
82+
let revision = UUID().uuidString
83+
84+
var store = try PinsStore(pinsFile: pinsFile, workingDirectory: .root, fileSystem: fs, mirrors: .init())
85+
store.pin(
86+
packageRef: .localSourceControl(identity: identity, path: path),
87+
state: .version("1.2.3", revision: revision)
88+
)
89+
try store.saveState(toolsVersion: ToolsVersion.currentToolsVersion)
90+
store = try PinsStore(pinsFile: pinsFile, workingDirectory: .root, fileSystem: fs, mirrors: .init())
91+
92+
let pin = store.pinsMap[identity]!
93+
XCTAssertEqual(pin.state, .version("1.2.3", revision: revision))
94+
XCTAssertEqual(pin.state.description, "1.2.3")
95+
}
7096

71-
store = try PinsStore(pinsFile: pinsFile, workingDirectory: .root, fileSystem: fs, mirrors: .init())
72-
XCTAssert(store.pins.map{$0}.count == 2)
97+
// Test source control branch pin.
7398

74-
// Test branch pin.
7599
do {
100+
let path = AbsolutePath("/foo")
101+
let identity = PackageIdentity(path: path)
102+
let revision = UUID().uuidString
103+
104+
var store = try PinsStore(pinsFile: pinsFile, workingDirectory: .root, fileSystem: fs, mirrors: .init())
76105
store.pin(
77-
packageRef: barRef,
106+
packageRef: .localSourceControl(identity: identity, path: path),
78107
state: .branch(name: "develop", revision: revision)
79108
)
80109
try store.saveState(toolsVersion: ToolsVersion.currentToolsVersion)
81110
store = try PinsStore(pinsFile: pinsFile, workingDirectory: .root, fileSystem: fs, mirrors: .init())
82111

83-
let barPin = store.pinsMap[bar]!
84-
XCTAssertEqual(barPin.state, .branch(name: "develop", revision: revision))
85-
XCTAssertEqual(barPin.state.description, "develop")
112+
let pin = store.pinsMap[identity]!
113+
XCTAssertEqual(pin.state, .branch(name: "develop", revision: revision))
114+
XCTAssertEqual(pin.state.description, "develop")
115+
}
116+
117+
// Test source control revision pin.
118+
119+
do {
120+
let path = AbsolutePath("/foo")
121+
let identity = PackageIdentity(path: path)
122+
let revision = UUID().uuidString
123+
124+
var store = try PinsStore(pinsFile: pinsFile, workingDirectory: .root, fileSystem: fs, mirrors: .init())
125+
store.pin(
126+
packageRef: .localSourceControl(identity: identity, path: path),
127+
state: .revision(revision)
128+
)
129+
try store.saveState(toolsVersion: ToolsVersion.currentToolsVersion)
130+
store = try PinsStore(pinsFile: pinsFile, workingDirectory: .root, fileSystem: fs, mirrors: .init())
131+
132+
let pin = store.pinsMap[identity]!
133+
XCTAssertEqual(pin.state, .revision(revision))
134+
XCTAssertEqual(pin.state.description, revision)
86135
}
87136

88-
// Test revision pin.
137+
// Test registry pin.
138+
89139
do {
90-
store.pin(packageRef: barRef, state: .revision(revision))
140+
let identity = PackageIdentity.plain("baz.baz") // FIXME: use scope identifier
141+
142+
var store = try PinsStore(pinsFile: pinsFile, workingDirectory: .root, fileSystem: fs, mirrors: .init())
143+
store.pin(
144+
packageRef: .registry(identity: identity),
145+
state: .version("1.2.3", revision: .none)
146+
)
91147
try store.saveState(toolsVersion: ToolsVersion.currentToolsVersion)
92148
store = try PinsStore(pinsFile: pinsFile, workingDirectory: .root, fileSystem: fs, mirrors: .init())
93149

94-
let barPin = store.pinsMap[bar]!
95-
XCTAssertEqual(barPin.state, .revision(revision))
96-
XCTAssertEqual(barPin.state.description, revision)
150+
let pin = store.pinsMap[identity]!
151+
XCTAssertEqual(pin.state, .version("1.2.3", revision: .none))
152+
XCTAssertEqual(pin.state.description, "1.2.3")
97153
}
98154
}
99155

@@ -149,7 +205,6 @@ final class PinsStoreTests: XCTestCase {
149205
"kind": "remoteSourceControl",
150206
"location": "https://github.com/something/Clang_C.git",
151207
"state": {
152-
"branch": null,
153208
"revision": "90a9574276f0fd17f02f58979423c3fd4d73b59e",
154209
"version": "1.0.2",
155210
}
@@ -159,18 +214,25 @@ final class PinsStoreTests: XCTestCase {
159214
"kind": "remoteSourceControl",
160215
"location": "https://github.com/something/Commandant.git",
161216
"state": {
162-
"branch": null,
163217
"revision": "c281992c31c3f41c48b5036c5a38185eaec32626",
164218
"version": "0.12.0"
165219
}
220+
},
221+
{
222+
"identity": "scope.package",
223+
"kind": "registry",
224+
"location": "",
225+
"state": {
226+
"version": "0.12.0"
227+
}
166228
}
167229
]
168230
}
169231
"""
170232
)
171233

172234
let store = try PinsStore(pinsFile: pinsFile, workingDirectory: .root, fileSystem: fs, mirrors: .init())
173-
XCTAssertEqual(store.pinsMap.keys.map { $0.description }.sorted(), ["clang_c", "commandant"])
235+
XCTAssertEqual(store.pinsMap.keys.map { $0.description }.sorted(), ["clang_c", "commandant", "scope.package"])
174236
}
175237

176238
func testLoadingUnknownSchemaVersion() throws {

0 commit comments

Comments
 (0)