Skip to content

Commit 76cb47f

Browse files
hartbitaciidgh
authored andcommitted
Fix InMemoryGitRepository by expecting its contents to present at root
`InMemoryGitRepository` is initialized with a path and a in-memory file system where it belongs. It then creates seperate in-memory file systems for each of its commits. Previously, the contents of the repository in the commit file systems was at the same location as in the working directory file system, which caused issues when cloning the repository to a different location on the file system (in installHead). Now, the contents of the repository in the commit file systems is located at root.
1 parent bbdbed3 commit 76cb47f

File tree

3 files changed

+26
-24
lines changed

3 files changed

+26
-24
lines changed

Sources/SPMTestSupport/TestWorkspace.swift

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,24 +94,25 @@ public final class TestWorkspace {
9494
func create(package: TestPackage, basePath: AbsolutePath, packageKind: PackageReference.Kind) throws {
9595
let packagePath = basePath.appending(RelativePath(package.path ?? package.name))
9696

97-
let sourcesDir = packagePath.appending(component: "Sources")
9897
let url = (packageKind == .root ? packagePath : packagesDir.appending(RelativePath(package.path ?? package.name))).pathString
9998
let specifier = RepositorySpecifier(url: url)
100-
let manifestPath = packagePath.appending(component: Manifest.filename)
10199

102100
// Create targets on disk.
103101
let repo = repoProvider.specifierMap[specifier] ?? InMemoryGitRepository(path: packagePath, fs: fs as! InMemoryFileSystem)
102+
let repoSourcesDir = AbsolutePath("/Sources")
104103
for target in package.targets {
105-
let targetDir = sourcesDir.appending(component: target.name)
106-
try repo.createDirectory(targetDir, recursive: true)
107-
try repo.writeFileContents(targetDir.appending(component: "file.swift"), bytes: "")
104+
let repoTargetDir = repoSourcesDir.appending(component: target.name)
105+
try repo.createDirectory(repoTargetDir, recursive: true)
106+
try repo.writeFileContents(repoTargetDir.appending(component: "file.swift"), bytes: "")
108107
}
109108
let toolsVersion = package.toolsVersion ?? .currentToolsVersion
110-
try repo.writeFileContents(manifestPath, bytes: "")
111-
try writeToolsVersion(at: packagePath, version: toolsVersion, fs: repo)
109+
let repoManifestPath = AbsolutePath.root.appending(component: Manifest.filename)
110+
try repo.writeFileContents(repoManifestPath, bytes: "")
111+
try writeToolsVersion(at: .root, version: toolsVersion, fs: repo)
112112
repo.commit()
113113

114114
let versions: [String?] = packageKind == .remote ? package.versions : [nil]
115+
let manifestPath = packagePath.appending(component: Manifest.filename)
115116
for version in versions {
116117
let v = version.flatMap(Version.init(string:))
117118
manifests[.init(url: url, version: v)] = Manifest(

Sources/SourceControl/InMemoryGitRepository.swift

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -145,23 +145,25 @@ public final class InMemoryGitRepository {
145145
let headFs = head.fileSystem
146146

147147
/// Recursively copies the content at HEAD to fs.
148-
func install(at path: AbsolutePath) throws {
149-
guard headFs.isDirectory(path) else { return }
150-
for entry in try headFs.getDirectoryContents(path) {
148+
func install(from sourcePath: AbsolutePath, to destinationPath: AbsolutePath) throws {
149+
assert(headFs.isDirectory(sourcePath))
150+
for entry in try headFs.getDirectoryContents(sourcePath) {
151151
// The full path of the entry.
152-
let entryPath = path.appending(component: entry)
153-
if headFs.isFile(entryPath) {
152+
let sourceEntryPath = sourcePath.appending(component: entry)
153+
let destinationEntryPath = destinationPath.appending(component: entry)
154+
if headFs.isFile(sourceEntryPath) {
154155
// If we have a file just write the file.
155-
try fs.writeFileContents(entryPath, bytes: try headFs.readFileContents(entryPath))
156-
} else if headFs.isDirectory(entryPath) {
156+
let bytes = try headFs.readFileContents(sourceEntryPath)
157+
try fs.writeFileContents(destinationEntryPath, bytes: bytes)
158+
} else if headFs.isDirectory(sourceEntryPath) {
157159
// If we have a directory, create that directory and copy its contents.
158-
try fs.createDirectory(entryPath, recursive: false)
159-
try install(at: entryPath)
160+
try fs.createDirectory(destinationEntryPath, recursive: false)
161+
try install(from: sourceEntryPath, to: destinationEntryPath)
160162
}
161163
}
162164
}
163165
// Install at the repository path.
164-
try install(at: path)
166+
try install(from: .root, to: path)
165167
}
166168

167169
/// Tag the current HEAD with the given name.
@@ -263,8 +265,7 @@ extension InMemoryGitRepository: Repository {
263265
}
264266

265267
public func openFileView(revision: Revision) throws -> FileSystem {
266-
let fs: FileSystem = history[revision.identifier]!.fileSystem
267-
return RerootedFileSystemView(fs, rootedAt: path)
268+
return history[revision.identifier]!.fileSystem
268269
}
269270
}
270271

Tests/PackageGraphTests/RepositoryPackageContainerProviderTests.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ class RepositoryPackageContainerProviderTests: XCTestCase {
161161
func testVprefixVersions() throws {
162162
let fs = InMemoryFileSystem()
163163

164-
let repoPath = AbsolutePath.root.appending(component: "some-repo")
164+
let repoPath = AbsolutePath.root
165165
let filePath = repoPath.appending(component: "Package.swift")
166166

167167
let specifier = RepositorySpecifier(url: repoPath.pathString)
@@ -198,7 +198,7 @@ class RepositoryPackageContainerProviderTests: XCTestCase {
198198
func testVersions() throws {
199199
let fs = InMemoryFileSystem()
200200

201-
let repoPath = AbsolutePath.root.appending(component: "some-repo")
201+
let repoPath = AbsolutePath.root
202202
let filePath = repoPath.appending(component: "Package.swift")
203203

204204
let specifier = RepositorySpecifier(url: repoPath.pathString)
@@ -276,15 +276,15 @@ class RepositoryPackageContainerProviderTests: XCTestCase {
276276
_ = try container.getDependencies(at: revision.identifier)
277277
} catch let error as RepositoryPackageContainer.GetDependenciesErrorWrapper {
278278
let error = error.underlyingError as! UnsupportedToolsVersion
279-
XCTAssertMatch(error.description, .and(.prefix("package at '/some-repo' @"), .suffix("is using Swift tools version 3.1.0 which is no longer supported; consider using '// swift-tools-version:4.0' to specify the current tools version")))
279+
XCTAssertMatch(error.description, .and(.prefix("package at '/' @"), .suffix("is using Swift tools version 3.1.0 which is no longer supported; consider using '// swift-tools-version:4.0' to specify the current tools version")))
280280
}
281281
}
282282
}
283283

284284
func testPrereleaseVersions() throws {
285285
let fs = InMemoryFileSystem()
286286

287-
let repoPath = AbsolutePath.root.appending(component: "some-repo")
287+
let repoPath = AbsolutePath.root
288288
let filePath = repoPath.appending(component: "Package.swift")
289289

290290
let specifier = RepositorySpecifier(url: repoPath.pathString)
@@ -323,7 +323,7 @@ class RepositoryPackageContainerProviderTests: XCTestCase {
323323
func testSimultaneousVersions() throws {
324324
let fs = InMemoryFileSystem()
325325

326-
let repoPath = AbsolutePath.root.appending(component: "some-repo")
326+
let repoPath = AbsolutePath.root
327327
let filePath = repoPath.appending(component: "Package.swift")
328328

329329
let specifier = RepositorySpecifier(url: repoPath.pathString)

0 commit comments

Comments
 (0)