Skip to content

Commit df39470

Browse files
John ButeJohn Bute
John Bute
authored and
John Bute
committed
added tests, naming consistency
1 parent c49285a commit df39470

File tree

3 files changed

+59
-13
lines changed

3 files changed

+59
-13
lines changed

Sources/Basics/Concurrency/PID.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import Foundation
99

10-
public protocol pidFileManipulator {
10+
public protocol PIDFileHandler {
1111
var scratchDirectory: AbsolutePath {get set}
1212

1313
init(scratchDirectory: AbsolutePath)
@@ -20,7 +20,7 @@ public protocol pidFileManipulator {
2020

2121

2222

23-
public struct pidFile: pidFileManipulator {
23+
public struct PIDFile: PIDFileHandler {
2424

2525
public var scratchDirectory: AbsolutePath
2626

@@ -29,22 +29,22 @@ public struct pidFile: pidFileManipulator {
2929
}
3030

3131
/// Return the path of the PackageManager.lock.pid file where the PID is located
32-
private var pidFilePath: AbsolutePath {
32+
private var lockFilePath: AbsolutePath {
3333
return self.scratchDirectory.appending(component: "PackageManager.lock.pid")
3434
}
3535

3636
/// Read the pid file
3737
public func readPID() -> Int32? {
3838
// Check if the file exists
39-
let filePath = pidFilePath.pathString
39+
let filePath = lockFilePath.pathString
4040
guard FileManager.default.fileExists(atPath: filePath) else {
4141
print("File does not exist at path: \(filePath)")
4242
return nil
4343
}
4444

4545
do {
4646
// Read the contents of the file
47-
let pidString = try String(contentsOf: pidFilePath.asURL, encoding: .utf8).trimmingCharacters(in: .whitespacesAndNewlines)
47+
let pidString = try String(contentsOf: lockFilePath.asURL, encoding: .utf8).trimmingCharacters(in: .whitespacesAndNewlines)
4848

4949
// Check if the PID string can be converted to an Int32
5050
if let pid = Int32(pidString) {
@@ -65,12 +65,12 @@ public struct pidFile: pidFileManipulator {
6565

6666
/// Write .pid file containing PID of process currently using .build directory
6767
public func writePID(pid: pid_t) throws {
68-
try "\(pid)".write(to: pidFilePath.asURL, atomically: true, encoding: .utf8)
68+
try "\(pid)".write(to: lockFilePath.asURL, atomically: true, encoding: .utf8)
6969
}
7070

7171
/// Delete PID file at URL
7272
public func deletePIDFile() throws {
73-
try FileManager.default.removeItem(at: pidFilePath.asURL)
73+
try FileManager.default.removeItem(at: lockFilePath.asURL)
7474
}
7575

7676
}

Sources/CoreCommands/SwiftCommandState.swift

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ public final class SwiftCommandState {
288288

289289
private let hostTriple: Basics.Triple?
290290

291-
private let pidManipulator: pidFileManipulator
291+
private let pidManipulator: PIDFileHandler
292292
package var preferredBuildConfiguration = BuildConfiguration.debug
293293

294294
/// Create an instance of this tool.
@@ -327,7 +327,7 @@ public final class SwiftCommandState {
327327
hostTriple: Basics.Triple? = nil,
328328
fileSystem: any FileSystem = localFileSystem,
329329
environment: Environment = .current,
330-
pidManipulator: pidFileManipulator? = nil
330+
pidManipulator: PIDFileHandler? = nil
331331
) throws {
332332
self.hostTriple = hostTriple
333333
self.fileSystem = fileSystem
@@ -411,7 +411,7 @@ public final class SwiftCommandState {
411411
explicitDirectory: options.locations.swiftSDKsDirectory ?? options.locations.deprecatedSwiftSDKsDirectory
412412
)
413413

414-
self.pidManipulator = pidManipulator ?? pidFile(scratchDirectory: self.scratchDirectory)
414+
self.pidManipulator = pidManipulator ?? PIDFile(scratchDirectory: self.scratchDirectory)
415415

416416
// set global process logging handler
417417
AsyncProcess.loggingHandler = { self.observabilityScope.emit(debug: $0) }
@@ -1072,8 +1072,8 @@ public final class SwiftCommandState {
10721072
lockAcquired = true
10731073
} catch ProcessLockError.unableToAquireLock(let errno) {
10741074
if errno == EWOULDBLOCK {
1075-
let existingPID = self.pidManipulator.readPID()
1076-
let pidInfo = existingPID.map { "(PID: \($0)) " } ?? ""
1075+
let existingProcessPID = self.pidManipulator.readPID()
1076+
let pidInfo = existingProcessPID.map { "(PID: \($0)) " } ?? ""
10771077
if self.options.locations.ignoreLock {
10781078
self.outputStream
10791079
.write(
@@ -1100,7 +1100,11 @@ public final class SwiftCommandState {
11001100
self.workspaceLock = workspaceLock
11011101

11021102
if lockAcquired || self.options.locations.ignoreLock {
1103-
try self.pidManipulator.writePID(pid: self.pidManipulator.getCurrentPID())
1103+
do {
1104+
try self.pidManipulator.writePID(pid: self.pidManipulator.getCurrentPID())
1105+
} catch {
1106+
self.observabilityScope.emit(warning: "Failed to write to PID file: \(error)")
1107+
}
11041108
}
11051109
}
11061110

Tests/CommandsTests/SwiftCommandStateTests.swift

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,48 @@ final class SwiftCommandStateTests: CommandsTestCase {
533533
}
534534
}
535535
}
536+
537+
func testPIDFileHandlerLifecycle() async throws {
538+
try withTemporaryDirectory { tmpDir in
539+
let scratchPath = tmpDir.appending(component: "scratch")
540+
try localFileSystem.createDirectory(scratchPath)
541+
542+
var pidHandler: PIDFile = PIDFile(scratchDirectory: scratchPath)
543+
544+
545+
546+
// Ensure no PID file exists initially
547+
XCTAssertNil(pidHandler.readPID(), "No PID should exist initially")
548+
549+
// Write current PID
550+
let currentPID = pidHandler.getCurrentPID()
551+
try pidHandler.writePID(pid: currentPID)
552+
553+
554+
// Read PID back
555+
let readPID = pidHandler.readPID()
556+
XCTAssertEqual(readPID, currentPID, "PID read should match written PID")
557+
558+
// Delete the file
559+
try pidHandler.deletePIDFile()
560+
561+
// Ensure file is gone
562+
XCTAssertNil(pidHandler.readPID(), "PID should be nil after deletion")
563+
}
564+
}
565+
566+
func testMalformedPIDFile() async throws {
567+
try withTemporaryDirectory { tmpDir in
568+
let scratchPath = tmpDir.appending(component: "scratch")
569+
try localFileSystem.createDirectory(scratchPath)
570+
571+
let pidPath = scratchPath.appending(component: "PackageManager.lock.pid")
572+
try localFileSystem.writeFileContents(pidPath, bytes: "notanumber")
573+
574+
let pidHandler = PIDFile(scratchDirectory: scratchPath)
575+
XCTAssertNil(pidHandler.readPID(), "Malformed PID file should result in nil")
576+
}
577+
}
536578
}
537579

538580
extension SwiftCommandState {

0 commit comments

Comments
 (0)